]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Updates from David Megginson relating to the property manager.
[flightgear.git] / src / Cockpit / panel.cxx
1 //  panel.cxx - default, 2D single-engine prop instrument panel
2 //
3 //  Written by David Megginson, started January 2000.
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License as
7 //  published by the Free Software Foundation; either version 2 of the
8 //  License, or (at your option) any later version.
9 // 
10 //  This program is distributed in the hope that it will be useful, but
11 //  WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //  General Public License for more details.
14 // 
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //
19 //  $Id$
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #ifdef HAVE_WINDOWS_H          
26 #  include <windows.h>
27 #endif
28
29 #include <string.h>
30
31 #include <plib/ssg.h>
32 #include <plib/fnt.h>
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/fgpath.hxx>
36 #include <Main/options.hxx>
37 #include <Main/views.hxx>
38 #include <Objects/texload.h>
39
40 #include "hud.hxx"
41 #include "panel.hxx"
42
43
44 \f
45 ////////////////////////////////////////////////////////////////////////
46 // Implementation of FGTextureManager.
47 ////////////////////////////////////////////////////////////////////////
48
49 map<string,ssgTexture *> FGTextureManager::_textureMap;
50
51 ssgTexture *
52 FGTextureManager::createTexture (const string &relativePath)
53 {
54   ssgTexture * texture = _textureMap[relativePath];
55   if (texture == 0) {
56     cerr << "Texture " << relativePath << " does not yet exist" << endl;
57     FGPath tpath(current_options.get_fg_root());
58     tpath.append(relativePath);
59     texture = new ssgTexture((char *)tpath.c_str(), false, false);
60     _textureMap[relativePath] = texture;
61     if (_textureMap[relativePath] == 0) 
62       cerr << "Texture *still* doesn't exist" << endl;
63     cerr << "Created texture " << relativePath
64          << " handle=" << texture->getHandle() << endl;
65   }
66
67   return texture;
68 }
69
70
71
72 \f
73 ////////////////////////////////////////////////////////////////////////
74 // Implementation of FGCropped Texture.
75 ////////////////////////////////////////////////////////////////////////
76
77
78 FGCroppedTexture::FGCroppedTexture ()
79   : _path(""), _texture(0),
80     _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
81 {
82 }
83
84
85 FGCroppedTexture::FGCroppedTexture (const string &path,
86                                     float minX, float minY,
87                                     float maxX, float maxY)
88   : _path(path), _texture(0),
89     _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
90 {
91 }
92
93
94 FGCroppedTexture::~FGCroppedTexture ()
95 {
96 }
97
98
99 ssgTexture *
100 FGCroppedTexture::getTexture ()
101 {
102   if (_texture == 0) {
103     _texture = FGTextureManager::createTexture(_path);
104   }
105   return _texture;
106 }
107
108
109 \f
110 ////////////////////////////////////////////////////////////////////////
111 // Implementation of FGPanel.
112 ////////////////////////////////////////////////////////////////////////
113
114 FGPanel * current_panel = NULL;
115 static fntRenderer text_renderer;
116
117
118 FGPanel::FGPanel (int x, int y, int w, int h)
119   : _mouseDown(false),
120     _mouseInstrument(0),
121     _x(x), _y(y), _w(w), _h(h)
122 {
123   setVisibility(current_options.get_panel_status());
124   _panel_h = (int)(h * 0.5768 + 1);
125 }
126
127 FGPanel::~FGPanel ()
128 {
129   for (instrument_list_type::iterator it = _instruments.begin();
130        it != _instruments.end();
131        it++) {
132     delete *it;
133     *it = 0;
134   }
135 }
136
137 void
138 FGPanel::addInstrument (FGPanelInstrument * instrument)
139 {
140   _instruments.push_back(instrument);
141 }
142
143 void
144 FGPanel::update () const
145 {
146                                 // Do nothing if the panel isn't visible.
147   if (!_visibility)
148     return;
149
150                                 // If the mouse is down, do something
151   if (_mouseDown) {
152     _mouseDelay--;
153     if (_mouseDelay < 0) {
154       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
155       _mouseDelay = 2;
156     }
157   }
158
159                                 // Now, draw the panel
160   glMatrixMode(GL_PROJECTION);
161   glPushMatrix();
162   glLoadIdentity();
163   gluOrtho2D(_x, _x + _w, _y, _y + _h);
164
165   glMatrixMode(GL_MODELVIEW);
166   glPushMatrix();
167   glLoadIdentity();
168
169                                 // Draw the background
170   glEnable(GL_TEXTURE_2D);
171   glDisable(GL_LIGHTING);
172   glEnable(GL_BLEND);
173   glEnable(GL_ALPHA_TEST);
174   glEnable(GL_COLOR_MATERIAL);
175   // glColor4f(1.0, 1.0, 1.0, 1.0);
176   if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
177       glColor4fv( cur_light_params.scene_diffuse );
178   } else {
179       glColor4f(0.7, 0.2, 0.2, 1.0);
180   }
181   glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
182   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
183   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
184   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
185   glBegin(GL_POLYGON);
186   glTexCoord2f(0.0, 0.0); glVertex3f(_x, _y, 0);
187   glTexCoord2f(10.0, 0.0); glVertex3f(_x + _w, _y, 0);
188   glTexCoord2f(10.0, 5.0); glVertex3f(_x + _w, _y + _panel_h, 0);
189   glTexCoord2f(0.0, 5.0); glVertex3f(_x, _y + _panel_h, 0);
190   glEnd();
191
192                                 // Draw the instruments.
193   instrument_list_type::const_iterator current = _instruments.begin();
194   instrument_list_type::const_iterator end = _instruments.end();
195
196   for ( ; current != end; current++) {
197     FGPanelInstrument * instr = *current;
198     glLoadIdentity();
199     glTranslated(instr->getXPos(), instr->getYPos(), 0);
200     instr->draw();
201   }
202
203   glMatrixMode(GL_PROJECTION);
204   glPopMatrix();
205   glMatrixMode(GL_MODELVIEW);
206   glPopMatrix();
207   ssgForceBasicState();
208   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
209 }
210
211 void
212 FGPanel::setVisibility (bool visibility)
213 {
214   _visibility = visibility;
215 }
216
217 bool
218 FGPanel::getVisibility () const
219 {
220   return _visibility;
221 }
222
223 void
224 FGPanel::setBackground (ssgTexture * texture)
225 {
226   _bg = texture;
227 }
228
229 bool
230 FGPanel::doMouseAction (int button, int updown, int x, int y)
231 {
232                                 // Note a released button and return
233   // cerr << "Doing mouse action\n";
234   if (updown == 1) {
235     _mouseDown = false;
236     _mouseInstrument = 0;
237     return true;
238   }
239
240   x = (int)(((float)x / current_view.get_winWidth()) * _w);
241   y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
242
243   for (int i = 0; i < (int)_instruments.size(); i++) {
244     FGPanelInstrument *inst = _instruments[i];
245     int ix = inst->getXPos();
246     int iy = inst->getYPos();
247     int iw = inst->getWidth() / 2;
248     int ih = inst->getHeight() / 2;
249     if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
250       _mouseDown = true;
251       _mouseDelay = 20;
252       _mouseInstrument = inst;
253       _mouseButton = button;
254       _mouseX = x - ix;
255       _mouseY = y - iy;
256                                 // Always do the action once.
257       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
258       return true;
259     }
260   }
261   return false;
262 }
263
264
265 \f
266 ////////////////////////////////////////////////////////////////////////.
267 // Implementation of FGPanelAction.
268 ////////////////////////////////////////////////////////////////////////
269
270 FGPanelAction::FGPanelAction ()
271 {
272 }
273
274 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
275   : _button(button), _x(x), _y(y), _w(w), _h(h)
276 {
277 }
278
279 FGPanelAction::~FGPanelAction ()
280 {
281 }
282
283
284 \f
285 ////////////////////////////////////////////////////////////////////////
286 // Implementation of FGAdjustAction.
287 ////////////////////////////////////////////////////////////////////////
288
289 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
290                                 SGValue * value, float increment, 
291                                 float min, float max, bool wrap)
292   : FGPanelAction(button, x, y, w, h),
293     _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
294 {
295 }
296
297 FGAdjustAction::~FGAdjustAction ()
298 {
299 }
300
301 void
302 FGAdjustAction::doAction ()
303 {
304   float val = _value->getFloatValue();
305   val += _increment;
306   if (val < _min) {
307     val = (_wrap ? _max : _min);
308   } else if (val > _max) {
309     val = (_wrap ? _min : _max);
310   }
311   _value->setDoubleValue(val);
312 }
313
314
315 \f
316 ////////////////////////////////////////////////////////////////////////
317 // Implementation of FGSwapAction.
318 ////////////////////////////////////////////////////////////////////////
319
320 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
321                             SGValue * value1, SGValue * value2)
322   : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
323 {
324 }
325
326 FGSwapAction::~FGSwapAction ()
327 {
328 }
329
330 void
331 FGSwapAction::doAction ()
332 {
333   float val = _value1->getFloatValue();
334   _value1->setDoubleValue(_value2->getFloatValue());
335   _value2->setDoubleValue(val);
336 }
337
338
339 \f
340 ////////////////////////////////////////////////////////////////////////
341 // Implementation of FGToggleAction.
342 ////////////////////////////////////////////////////////////////////////
343
344 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
345                                 SGValue * value)
346   : FGPanelAction(button, x, y, w, h), _value(value)
347 {
348 }
349
350 FGToggleAction::~FGToggleAction ()
351 {
352 }
353
354 void
355 FGToggleAction::doAction ()
356 {
357   _value->setBoolValue(!(_value->getBoolValue()));
358 }
359
360
361 \f
362 ////////////////////////////////////////////////////////////////////////
363 // Implementation of FGPanelTransformation.
364 ////////////////////////////////////////////////////////////////////////
365
366 FGPanelTransformation::FGPanelTransformation ()
367 {
368 }
369
370 FGPanelTransformation::~FGPanelTransformation ()
371 {
372 }
373
374
375 \f
376 ////////////////////////////////////////////////////////////////////////
377 // Implementation of FGPanelInstrument.
378 ////////////////////////////////////////////////////////////////////////
379
380
381 FGPanelInstrument::FGPanelInstrument ()
382 {
383   setPosition(0, 0);
384   setSize(0, 0);
385 }
386
387 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
388 {
389   setPosition(x, y);
390   setSize(w, h);
391 }
392
393 FGPanelInstrument::~FGPanelInstrument ()
394 {
395   for (action_list_type::iterator it = _actions.begin();
396        it != _actions.end();
397        it++) {
398     delete *it;
399     *it = 0;
400   }
401 }
402
403 void
404 FGPanelInstrument::setPosition (int x, int y)
405 {
406   _x = x;
407   _y = y;
408 }
409
410 void
411 FGPanelInstrument::setSize (int w, int h)
412 {
413   _w = w;
414   _h = h;
415 }
416
417 int
418 FGPanelInstrument::getXPos () const
419 {
420   return _x;
421 }
422
423 int
424 FGPanelInstrument::getYPos () const
425 {
426   return _y;
427 }
428
429 int
430 FGPanelInstrument::getWidth () const
431 {
432   return _w;
433 }
434
435 int
436 FGPanelInstrument::getHeight () const
437 {
438   return _h;
439 }
440
441 void
442 FGPanelInstrument::addAction (FGPanelAction * action)
443 {
444   _actions.push_back(action);
445 }
446
447                                 // Coordinates relative to centre.
448 bool
449 FGPanelInstrument::doMouseAction (int button, int x, int y)
450 {
451   action_list_type::iterator it = _actions.begin();
452   action_list_type::iterator last = _actions.end();
453   for ( ; it != last; it++) {
454     if ((*it)->inArea(button, x, y)) {
455       (*it)->doAction();
456       return true;
457     }
458   }
459   return false;
460 }
461
462
463 \f
464 ////////////////////////////////////////////////////////////////////////
465 // Implementation of FGLayeredInstrument.
466 ////////////////////////////////////////////////////////////////////////
467
468 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
469   : FGPanelInstrument(x, y, w, h)
470 {
471 }
472
473 FGLayeredInstrument::~FGLayeredInstrument ()
474 {
475   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
476     delete *it;
477     *it = 0;
478   }
479 }
480
481 void
482 FGLayeredInstrument::draw ()
483 {
484   for (int i = 0; i < (int)_layers.size(); i++) {
485     glPushMatrix();
486     glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
487     _layers[i]->draw();
488     glPopMatrix();
489   }
490 }
491
492 int
493 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
494 {
495   int n = _layers.size();
496   if (layer->getWidth() == -1) {
497     layer->setWidth(getWidth());
498   }
499   if (layer->getHeight() == -1) {
500     layer->setHeight(getHeight());
501   }
502   _layers.push_back(layer);
503   return n;
504 }
505
506 int
507 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
508                                int w, int h)
509 {
510   return addLayer(new FGTexturedLayer(texture, w, h));
511 }
512
513 void
514 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
515 {
516   int layer = _layers.size() - 1;
517   _layers[layer]->addTransformation(transformation);
518 }
519
520
521 \f
522 ////////////////////////////////////////////////////////////////////////
523 // Implementation of FGInstrumentLayer.
524 ////////////////////////////////////////////////////////////////////////
525
526 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
527   : _w(w),
528     _h(h)
529 {
530 }
531
532 FGInstrumentLayer::~FGInstrumentLayer ()
533 {
534   for (transformation_list::iterator it = _transformations.begin();
535        it != _transformations.end();
536        it++) {
537     delete *it;
538     *it = 0;
539   }
540 }
541
542 void
543 FGInstrumentLayer::transform () const
544 {
545   transformation_list::const_iterator it = _transformations.begin();
546   transformation_list::const_iterator last = _transformations.end();
547   while (it != last) {
548     FGPanelTransformation *t = *it;
549     float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
550     if (val < t->min) {
551       val = t->min;
552     } else if (val > t->max) {
553       val = t->max;
554     }
555     val = val * t->factor + t->offset;
556
557     switch (t->type) {
558     case FGPanelTransformation::XSHIFT:
559       glTranslatef(val, 0.0, 0.0);
560       break;
561     case FGPanelTransformation::YSHIFT:
562       glTranslatef(0.0, val, 0.0);
563       break;
564     case FGPanelTransformation::ROTATION:
565       glRotatef(-val, 0.0, 0.0, 1.0);
566       break;
567     }
568     it++;
569   }
570 }
571
572 void
573 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
574 {
575   _transformations.push_back(transformation);
576 }
577
578
579 \f
580 ////////////////////////////////////////////////////////////////////////
581 // Implementation of FGTexturedLayer.
582 ////////////////////////////////////////////////////////////////////////
583
584
585 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
586   : FGInstrumentLayer(w, h)
587 {
588   setTexture(texture);
589 }
590
591
592 FGTexturedLayer::~FGTexturedLayer ()
593 {
594 }
595
596
597 void
598 FGTexturedLayer::draw ()
599 {
600   int w2 = _w / 2;
601   int h2 = _h / 2;
602
603   transform();
604   glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
605   glBegin(GL_POLYGON);
606
607                                 // From Curt: turn on the panel
608                                 // lights after sundown.
609   if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
610       glColor4fv( cur_light_params.scene_diffuse );
611   } else {
612       glColor4f(0.7, 0.2, 0.2, 1.0);
613   }
614
615
616   glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
617   glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
618   glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
619   glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
620   glEnd();
621 }
622
623
624 \f
625 ////////////////////////////////////////////////////////////////////////
626 // Implementation of FGTextLayer.
627 ////////////////////////////////////////////////////////////////////////
628
629 FGTextLayer::FGTextLayer (int w, int h)
630   : FGInstrumentLayer(w, h), _pointSize(14.0)
631 {
632   _then.stamp();
633   _color[0] = _color[1] = _color[2] = 0.0;
634   _color[3] = 1.0;
635 }
636
637 FGTextLayer::~FGTextLayer ()
638 {
639   chunk_list::iterator it = _chunks.begin();
640   chunk_list::iterator last = _chunks.end();
641   for ( ; it != last; it++) {
642     delete *it;
643   }
644 }
645
646 void
647 FGTextLayer::draw ()
648 {
649   glPushMatrix();
650   glColor4fv(_color);
651   transform();
652   text_renderer.setFont(guiFntHandle);
653   text_renderer.setPointSize(_pointSize);
654   text_renderer.begin();
655   text_renderer.start3f(0, 0, 0);
656
657   _now.stamp();
658   if (_now - _then > 100000) {
659     recalc_value();
660     _then = _now;
661   }
662   text_renderer.puts((char *)(_value.c_str()));
663
664   text_renderer.end();
665   glColor4f(1.0, 1.0, 1.0, 1.0);        // FIXME
666   glPopMatrix();
667 }
668
669 void
670 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
671 {
672   _chunks.push_back(chunk);
673 }
674
675 void
676 FGTextLayer::setColor (float r, float g, float b)
677 {
678   _color[0] = r;
679   _color[1] = g;
680   _color[2] = b;
681   _color[3] = 1.0;
682 }
683
684 void
685 FGTextLayer::setPointSize (float size)
686 {
687   _pointSize = size;
688 }
689
690 void
691 FGTextLayer::setFont(fntFont * font)
692 {
693   text_renderer.setFont(font);
694 }
695
696
697 void
698 FGTextLayer::recalc_value () const
699 {
700   _value = "";
701   chunk_list::const_iterator it = _chunks.begin();
702   chunk_list::const_iterator last = _chunks.end();
703   for ( ; it != last; it++) {
704     _value += (*it)->getValue();
705   }
706 }
707
708
709 \f
710 ////////////////////////////////////////////////////////////////////////
711 // Implementation of FGTextLayer::Chunk.
712 ////////////////////////////////////////////////////////////////////////
713
714 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
715   : _type(FGTextLayer::TEXT), _fmt(fmt)
716 {
717   _text = text;
718   if (_fmt == "") 
719     _fmt = "%s";
720 }
721
722 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
723                            const string &fmt, float mult)
724   : _type(type), _fmt(fmt), _mult(mult)
725 {
726   if (_fmt == "") {
727     if (type == TEXT_VALUE)
728       _fmt = "%s";
729     else
730       _fmt = "%.2f";
731   }
732   _value = value;
733 }
734
735 const char *
736 FGTextLayer::Chunk::getValue () const
737 {
738   switch (_type) {
739   case TEXT:
740     sprintf(_buf, _fmt.c_str(), _text.c_str());
741     return _buf;
742   case TEXT_VALUE:
743     sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
744     break;
745   case DOUBLE_VALUE:
746     sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
747     break;
748   }
749   return _buf;
750 }
751
752
753 \f
754 ////////////////////////////////////////////////////////////////////////
755 // Implementation of FGSwitchLayer.
756 ////////////////////////////////////////////////////////////////////////
757
758 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
759                               FGInstrumentLayer * layer1,
760                               FGInstrumentLayer * layer2)
761   : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
762 {
763 }
764
765 FGSwitchLayer::~FGSwitchLayer ()
766 {
767   delete _layer1;
768   delete _layer2;
769 }
770
771 void
772 FGSwitchLayer::draw ()
773 {
774   transform();
775   if (_value->getBoolValue()) {
776     _layer1->draw();
777   } else {
778     _layer2->draw();
779   }
780 }
781
782 \f
783 // end of panel.cxx