]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
14810b4019d7108232441ec82b9228469cbf38a8
[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/sg_path.hxx>
36
37 #include <Main/globals.hxx>
38 #include <Main/fg_props.hxx>
39 #include <Main/viewmgr.hxx>
40 #include <Objects/texload.h>
41 #include <Time/light.hxx>
42
43 #include "hud.hxx"
44 #include "panel.hxx"
45
46 #define WIN_X 0
47 #define WIN_Y 0
48 #define WIN_W 1024
49 #define WIN_H 768
50
51 #ifdef defined( NONE ) && defined( _MSC_VER )
52 #  pragma message( "A sloppy coder has defined NONE as a macro!!!" )
53 #  undef NONE
54 #elif defined( NONE )
55 #  pragma warn A sloppy coder has defined NONE as a macro!!!
56 #  undef NONE
57 #endif
58
59
60 \f
61 ////////////////////////////////////////////////////////////////////////
62 // Local functions.
63 ////////////////////////////////////////////////////////////////////////
64
65
66 /**
67  * Calculate the aspect adjustment for the panel.
68  */
69 static float
70 get_aspect_adjust (int xsize, int ysize)
71 {
72   float ideal_aspect = float(WIN_W) / float(WIN_H);
73   float real_aspect = float(xsize) / float(ysize);
74   return (real_aspect / ideal_aspect);
75 }
76
77
78 \f
79 ////////////////////////////////////////////////////////////////////////
80 // Global functions.
81 ////////////////////////////////////////////////////////////////////////
82
83 bool
84 fgPanelVisible ()
85 {
86   return ((current_panel != 0) &&
87           (current_panel->getVisibility()) &&
88           (globals->get_viewmgr()->get_current() == 0) &&
89           (globals->get_current_view()->get_view_offset() == 0.0));
90 }
91
92
93 \f
94 ////////////////////////////////////////////////////////////////////////
95 // Implementation of FGTextureManager.
96 ////////////////////////////////////////////////////////////////////////
97
98 map<string,ssgTexture *> FGTextureManager::_textureMap;
99
100 ssgTexture *
101 FGTextureManager::createTexture (const string &relativePath)
102 {
103   ssgTexture * texture = _textureMap[relativePath];
104   if (texture == 0) {
105     SG_LOG( SG_COCKPIT, SG_DEBUG,
106             "Texture " << relativePath << " does not yet exist" );
107     SGPath tpath(globals->get_fg_root());
108     tpath.append(relativePath);
109     texture = new ssgTexture((char *)tpath.c_str(), false, false);
110     _textureMap[relativePath] = texture;
111     if (_textureMap[relativePath] == 0) 
112       SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
113     SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
114             << " handle=" << texture->getHandle() );
115   }
116
117   return texture;
118 }
119
120
121
122 \f
123 ////////////////////////////////////////////////////////////////////////
124 // Implementation of FGCropped Texture.
125 ////////////////////////////////////////////////////////////////////////
126
127
128 FGCroppedTexture::FGCroppedTexture ()
129   : _path(""), _texture(0),
130     _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
131 {
132 }
133
134
135 FGCroppedTexture::FGCroppedTexture (const string &path,
136                                     float minX, float minY,
137                                     float maxX, float maxY)
138   : _path(path), _texture(0),
139     _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
140 {
141 }
142
143
144 FGCroppedTexture::~FGCroppedTexture ()
145 {
146 }
147
148
149 ssgTexture *
150 FGCroppedTexture::getTexture ()
151 {
152   if (_texture == 0) {
153     _texture = FGTextureManager::createTexture(_path);
154   }
155   return _texture;
156 }
157
158
159 \f
160 ////////////////////////////////////////////////////////////////////////
161 // Implementation of FGPanel.
162 ////////////////////////////////////////////////////////////////////////
163
164 FGPanel * current_panel = NULL;
165 static fntRenderer text_renderer;
166
167
168 /**
169  * Constructor.
170  */
171 FGPanel::FGPanel ()
172   : _mouseDown(false),
173     _mouseInstrument(0),
174     _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
175     _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
176     _bound(false),
177     _jitter(0.0),
178     _xsize_node(fgGetNode("/sim/startup/xsize", true)),
179     _ysize_node(fgGetNode("/sim/startup/ysize", true))
180 {
181   setVisibility(fgPanelVisible());
182 }
183
184
185 /**
186  * Destructor.
187  */
188 FGPanel::~FGPanel ()
189 {
190   if (_bound)
191     unbind();
192   for (instrument_list_type::iterator it = _instruments.begin();
193        it != _instruments.end();
194        it++) {
195     delete *it;
196     *it = 0;
197   }
198 }
199
200
201 /**
202  * Add an instrument to the panel.
203  */
204 void
205 FGPanel::addInstrument (FGPanelInstrument * instrument)
206 {
207   _instruments.push_back(instrument);
208 }
209
210
211 /**
212  * Initialize the panel.
213  */
214 void
215 FGPanel::init ()
216 {
217   // NO-OP
218 }
219
220
221 /**
222  * Bind panel properties.
223  */
224 void
225 FGPanel::bind ()
226 {
227   fgTie("/sim/panel/visibility", &_visibility);
228   fgSetArchivable("/sim/panel/visibility");
229   fgTie("/sim/panel/x-offset", &_x_offset);
230   fgSetArchivable("/sim/panel/x-offset");
231   fgTie("/sim/panel/y-offset", &_y_offset);
232   fgSetArchivable("/sim/panel/y-offset");
233   fgTie("/sim/panel/jitter", &_jitter);
234   fgSetArchivable("/sim/panel/jitter");
235   _bound = true;
236 }
237
238
239 /**
240  * Unbind panel properties.
241  */
242 void
243 FGPanel::unbind ()
244 {
245   fgUntie("/sim/panel/visibility");
246   fgUntie("/sim/panel/x-offset");
247   fgUntie("/sim/panel/y-offset");
248   _bound = false;
249 }
250
251
252 /**
253  * Update the panel.
254  */
255 void
256 FGPanel::update ()
257 {
258                                 // Do nothing if the panel isn't visible.
259     if (!fgPanelVisible())
260         return;
261
262                                 // If the mouse is down, do something
263     if (_mouseDown) {
264         _mouseDelay--;
265         if (_mouseDelay < 0) {
266             _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
267             _mouseDelay = 2;
268         }
269     }
270
271                                 // Now, draw the panel
272     float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
273                                             _ysize_node->getIntValue());
274     if (aspect_adjust <1.0)
275         update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
276     else
277         update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
278 }
279
280
281 void
282 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
283 {
284                                 // Calculate accelerations
285                                 // and jiggle the panel accordingly
286                                 // The factors and bounds are just
287                                 // initial guesses; using sqrt smooths
288                                 // out the spikes.
289   double x_offset = _x_offset;
290   double y_offset = _y_offset;
291
292   if (_jitter != 0.0) {
293     double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
294     double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
295     double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
296
297     double a_zx_pilot = a_z_pilot - a_x_pilot;
298     
299     int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
300                    (a_y_pilot < 0 ? -1 : 1);
301     int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
302                    (a_zx_pilot < 0 ? -1 : 1);
303
304                                 // adjustments in screen coordinates
305     x_offset += x_adjust;
306     y_offset += y_adjust;
307   }
308
309   glMatrixMode(GL_PROJECTION);
310   glPushMatrix();
311   glLoadIdentity();
312   gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
313   // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
314
315   glMatrixMode(GL_MODELVIEW);
316   glPushMatrix();
317   glLoadIdentity();
318
319   glTranslated(x_offset, y_offset, 0);
320
321                                 // Draw the background
322   glEnable(GL_TEXTURE_2D);
323   glDisable(GL_LIGHTING);
324   glEnable(GL_BLEND);
325   glEnable(GL_ALPHA_TEST);
326   glEnable(GL_COLOR_MATERIAL);
327   // glColor4f(1.0, 1.0, 1.0, 1.0);
328   if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
329       glColor4fv( cur_light_params.scene_diffuse );
330   } else {
331       glColor4f(0.7, 0.2, 0.2, 1.0);
332   }
333   glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
334   // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
335   // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
336   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
337   glBegin(GL_POLYGON);
338   glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
339   glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
340   glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
341   glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
342   glEnd();
343
344                                 // Draw the instruments.
345   instrument_list_type::const_iterator current = _instruments.begin();
346   instrument_list_type::const_iterator end = _instruments.end();
347
348   for ( ; current != end; current++) {
349     FGPanelInstrument * instr = *current;
350     glLoadIdentity();
351     glTranslated(x_offset, y_offset, 0);
352     glTranslated(instr->getXPos(), instr->getYPos(), 0);
353     instr->draw();
354   }
355
356   glMatrixMode(GL_PROJECTION);
357   glPopMatrix();
358   glMatrixMode(GL_MODELVIEW);
359   glPopMatrix();
360   ssgForceBasicState();
361   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
362 }
363
364
365 /**
366  * Set the panel's visibility.
367  */
368 void
369 FGPanel::setVisibility (bool visibility)
370 {
371   _visibility = visibility;
372 }
373
374
375 /**
376  * Return true if the panel is visible.
377  */
378 bool
379 FGPanel::getVisibility () const
380 {
381   return _visibility;
382 }
383
384
385 /**
386  * Set the panel's background texture.
387  */
388 void
389 FGPanel::setBackground (ssgTexture * texture)
390 {
391   _bg = texture;
392 }
393
394
395 /**
396  * Set the panel's x-offset.
397  */
398 void
399 FGPanel::setXOffset (int offset)
400 {
401   if (offset <= 0 && offset >= -_width + WIN_W)
402     _x_offset = offset;
403 }
404
405
406 /**
407  * Set the panel's y-offset.
408  */
409 void
410 FGPanel::setYOffset (int offset)
411 {
412   if (offset <= 0 && offset >= -_height)
413     _y_offset = offset;
414 }
415
416
417 /**
418  * Perform a mouse action.
419  */
420 bool
421 FGPanel::doMouseAction (int button, int updown, int x, int y)
422 {
423                                 // FIXME: this same code appears in update()
424   int xsize = _xsize_node->getIntValue();
425   int ysize = _ysize_node->getIntValue();
426   float aspect_adjust = get_aspect_adjust(xsize, ysize);
427
428                                 // Note a released button and return
429   // cerr << "Doing mouse action\n";
430   if (updown == 1) {
431     _mouseDown = false;
432     _mouseInstrument = 0;
433     return true;
434   }
435
436                                 // Scale for the real window size.
437   if (aspect_adjust < 1.0) {
438     x = int(((float)x / xsize) * WIN_W * aspect_adjust);
439     y = int(WIN_H - ((float(y) / ysize) * WIN_H));
440   } else {
441     x = int(((float)x / xsize) * WIN_W);
442     y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
443   }
444
445                                 // Adjust for offsets.
446   x -= _x_offset;
447   y -= _y_offset;
448
449                                 // Search for a matching instrument.
450   for (int i = 0; i < (int)_instruments.size(); i++) {
451     FGPanelInstrument *inst = _instruments[i];
452     int ix = inst->getXPos();
453     int iy = inst->getYPos();
454     int iw = inst->getWidth() / 2;
455     int ih = inst->getHeight() / 2;
456     if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
457       _mouseDown = true;
458       _mouseDelay = 20;
459       _mouseInstrument = inst;
460       _mouseButton = button;
461       _mouseX = x - ix;
462       _mouseY = y - iy;
463                                 // Always do the action once.
464       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
465       return true;
466     }
467   }
468   return false;
469 }
470
471
472 \f
473 ////////////////////////////////////////////////////////////////////////.
474 // Implementation of FGPanelAction.
475 ////////////////////////////////////////////////////////////////////////
476
477 FGPanelAction::FGPanelAction ()
478 {
479 }
480
481 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
482   : _button(button), _x(x), _y(y), _w(w), _h(h)
483 {
484   for (unsigned int i = 0; i < _bindings.size(); i++)
485     delete _bindings[i];
486 }
487
488 FGPanelAction::~FGPanelAction ()
489 {
490 }
491
492 void
493 FGPanelAction::addBinding (FGBinding * binding)
494 {
495   _bindings.push_back(binding);
496 }
497
498 void
499 FGPanelAction::doAction ()
500 {
501   if (test()) {
502     int nBindings = _bindings.size();
503     for (int i = 0; i < nBindings; i++) {
504       _bindings[i]->fire();
505     }
506   }
507 }
508
509
510 \f
511 ////////////////////////////////////////////////////////////////////////
512 // Implementation of FGPanelTransformation.
513 ////////////////////////////////////////////////////////////////////////
514
515 FGPanelTransformation::FGPanelTransformation ()
516   : table(0)
517 {
518 }
519
520 FGPanelTransformation::~FGPanelTransformation ()
521 {
522 }
523
524
525 \f
526 ////////////////////////////////////////////////////////////////////////
527 // Implementation of FGPanelInstrument.
528 ////////////////////////////////////////////////////////////////////////
529
530
531 FGPanelInstrument::FGPanelInstrument ()
532 {
533   setPosition(0, 0);
534   setSize(0, 0);
535 }
536
537 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
538 {
539   setPosition(x, y);
540   setSize(w, h);
541 }
542
543 FGPanelInstrument::~FGPanelInstrument ()
544 {
545   for (action_list_type::iterator it = _actions.begin();
546        it != _actions.end();
547        it++) {
548     delete *it;
549     *it = 0;
550   }
551 }
552
553 void
554 FGPanelInstrument::setPosition (int x, int y)
555 {
556   _x = x;
557   _y = y;
558 }
559
560 void
561 FGPanelInstrument::setSize (int w, int h)
562 {
563   _w = w;
564   _h = h;
565 }
566
567 int
568 FGPanelInstrument::getXPos () const
569 {
570   return _x;
571 }
572
573 int
574 FGPanelInstrument::getYPos () const
575 {
576   return _y;
577 }
578
579 int
580 FGPanelInstrument::getWidth () const
581 {
582   return _w;
583 }
584
585 int
586 FGPanelInstrument::getHeight () const
587 {
588   return _h;
589 }
590
591 void
592 FGPanelInstrument::addAction (FGPanelAction * action)
593 {
594   _actions.push_back(action);
595 }
596
597                                 // Coordinates relative to centre.
598 bool
599 FGPanelInstrument::doMouseAction (int button, int x, int y)
600 {
601   if (test()) {
602     action_list_type::iterator it = _actions.begin();
603     action_list_type::iterator last = _actions.end();
604     for ( ; it != last; it++) {
605       if ((*it)->inArea(button, x, y)) {
606         (*it)->doAction();
607         return true;
608       }
609     }
610   }
611   return false;
612 }
613
614
615 \f
616 ////////////////////////////////////////////////////////////////////////
617 // Implementation of FGLayeredInstrument.
618 ////////////////////////////////////////////////////////////////////////
619
620 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
621   : FGPanelInstrument(x, y, w, h)
622 {
623 }
624
625 FGLayeredInstrument::~FGLayeredInstrument ()
626 {
627   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
628     delete *it;
629     *it = 0;
630   }
631 }
632
633 void
634 FGLayeredInstrument::draw ()
635 {
636   if (test()) {
637     for (int i = 0; i < (int)_layers.size(); i++) {
638       glPushMatrix();
639       glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
640       _layers[i]->draw();
641       glPopMatrix();
642     }
643   }
644 }
645
646 int
647 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
648 {
649   int n = _layers.size();
650   if (layer->getWidth() == -1) {
651     layer->setWidth(getWidth());
652   }
653   if (layer->getHeight() == -1) {
654     layer->setHeight(getHeight());
655   }
656   _layers.push_back(layer);
657   return n;
658 }
659
660 int
661 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
662                                int w, int h)
663 {
664   return addLayer(new FGTexturedLayer(texture, w, h));
665 }
666
667 void
668 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
669 {
670   int layer = _layers.size() - 1;
671   _layers[layer]->addTransformation(transformation);
672 }
673
674
675 \f
676 ////////////////////////////////////////////////////////////////////////
677 // Implementation of FGInstrumentLayer.
678 ////////////////////////////////////////////////////////////////////////
679
680 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
681   : _w(w),
682     _h(h)
683 {
684 }
685
686 FGInstrumentLayer::~FGInstrumentLayer ()
687 {
688   for (transformation_list::iterator it = _transformations.begin();
689        it != _transformations.end();
690        it++) {
691     delete *it;
692     *it = 0;
693   }
694 }
695
696 void
697 FGInstrumentLayer::transform () const
698 {
699   transformation_list::const_iterator it = _transformations.begin();
700   transformation_list::const_iterator last = _transformations.end();
701   while (it != last) {
702     FGPanelTransformation *t = *it;
703     if (t->test()) {
704       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
705       if (val < t->min) {
706         val = t->min;
707       } else if (val > t->max) {
708         val = t->max;
709       }
710       if(t->table==0) {
711         val = val * t->factor + t->offset;
712       } else {
713         val = t->table->interpolate(val) * t->factor + t->offset;
714       }
715       
716       switch (t->type) {
717       case FGPanelTransformation::XSHIFT:
718         glTranslatef(val, 0.0, 0.0);
719         break;
720       case FGPanelTransformation::YSHIFT:
721         glTranslatef(0.0, val, 0.0);
722         break;
723       case FGPanelTransformation::ROTATION:
724         glRotatef(-val, 0.0, 0.0, 1.0);
725         break;
726       }
727     }
728     it++;
729   }
730 }
731
732 void
733 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
734 {
735   _transformations.push_back(transformation);
736 }
737
738
739 \f
740 ////////////////////////////////////////////////////////////////////////
741 // Implementation of FGGroupLayer.
742 ////////////////////////////////////////////////////////////////////////
743
744 FGGroupLayer::FGGroupLayer ()
745 {
746 }
747
748 FGGroupLayer::~FGGroupLayer ()
749 {
750   for (unsigned int i = 0; i < _layers.size(); i++)
751     delete _layers[i];
752 }
753
754 void
755 FGGroupLayer::draw ()
756 {
757   if (test()) {
758     int nLayers = _layers.size();
759     for (int i = 0; i < nLayers; i++)
760       _layers[i]->draw();
761   }
762 }
763
764 void
765 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
766 {
767   _layers.push_back(layer);
768 }
769
770
771 \f
772 ////////////////////////////////////////////////////////////////////////
773 // Implementation of FGTexturedLayer.
774 ////////////////////////////////////////////////////////////////////////
775
776
777 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
778   : FGInstrumentLayer(w, h)
779 {
780   setTexture(texture);
781 }
782
783
784 FGTexturedLayer::~FGTexturedLayer ()
785 {
786 }
787
788
789 void
790 FGTexturedLayer::draw ()
791 {
792   if (test()) {
793     int w2 = _w / 2;
794     int h2 = _h / 2;
795     
796     transform();
797     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
798     glBegin(GL_POLYGON);
799     
800                                 // From Curt: turn on the panel
801                                 // lights after sundown.
802     if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
803       glColor4fv( cur_light_params.scene_diffuse );
804     } else {
805       glColor4f(0.7, 0.2, 0.2, 1.0);
806     }
807
808
809     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
810     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
811     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
812     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
813     glEnd();
814   }
815 }
816
817
818 \f
819 ////////////////////////////////////////////////////////////////////////
820 // Implementation of FGTextLayer.
821 ////////////////////////////////////////////////////////////////////////
822
823 FGTextLayer::FGTextLayer (int w, int h)
824   : FGInstrumentLayer(w, h), _pointSize(14.0)
825 {
826   _then.stamp();
827   _color[0] = _color[1] = _color[2] = 0.0;
828   _color[3] = 1.0;
829 }
830
831 FGTextLayer::~FGTextLayer ()
832 {
833   chunk_list::iterator it = _chunks.begin();
834   chunk_list::iterator last = _chunks.end();
835   for ( ; it != last; it++) {
836     delete *it;
837   }
838 }
839
840 void
841 FGTextLayer::draw ()
842 {
843   if (test()) {
844     glPushMatrix();
845     glColor4fv(_color);
846     transform();
847     text_renderer.setFont(guiFntHandle);
848     text_renderer.setPointSize(_pointSize);
849     text_renderer.begin();
850     text_renderer.start3f(0, 0, 0);
851
852     _now.stamp();
853     if (_now - _then > 100000) {
854       recalc_value();
855       _then = _now;
856     }
857     text_renderer.puts((char *)(_value.c_str()));
858
859     text_renderer.end();
860     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
861     glPopMatrix();
862   }
863 }
864
865 void
866 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
867 {
868   _chunks.push_back(chunk);
869 }
870
871 void
872 FGTextLayer::setColor (float r, float g, float b)
873 {
874   _color[0] = r;
875   _color[1] = g;
876   _color[2] = b;
877   _color[3] = 1.0;
878 }
879
880 void
881 FGTextLayer::setPointSize (float size)
882 {
883   _pointSize = size;
884 }
885
886 void
887 FGTextLayer::setFont(fntFont * font)
888 {
889   text_renderer.setFont(font);
890 }
891
892
893 void
894 FGTextLayer::recalc_value () const
895 {
896   _value = "";
897   chunk_list::const_iterator it = _chunks.begin();
898   chunk_list::const_iterator last = _chunks.end();
899   for ( ; it != last; it++) {
900     _value += (*it)->getValue();
901   }
902 }
903
904
905 \f
906 ////////////////////////////////////////////////////////////////////////
907 // Implementation of FGTextLayer::Chunk.
908 ////////////////////////////////////////////////////////////////////////
909
910 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
911   : _type(FGTextLayer::TEXT), _fmt(fmt)
912 {
913   _text = text;
914   if (_fmt == "") 
915     _fmt = "%s";
916 }
917
918 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
919                            const string &fmt, float mult)
920   : _type(type), _fmt(fmt), _mult(mult)
921 {
922   if (_fmt == "") {
923     if (type == TEXT_VALUE)
924       _fmt = "%s";
925     else
926       _fmt = "%.2f";
927   }
928   _node = node;
929 }
930
931 const char *
932 FGTextLayer::Chunk::getValue () const
933 {
934   if (test()) {
935     switch (_type) {
936     case TEXT:
937       sprintf(_buf, _fmt.c_str(), _text.c_str());
938       return _buf;
939     case TEXT_VALUE:
940       sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
941       break;
942     case DOUBLE_VALUE:
943       sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
944       break;
945     }
946     return _buf;
947   } else {
948     return "";
949   }
950 }
951
952
953 \f
954 ////////////////////////////////////////////////////////////////////////
955 // Implementation of FGSwitchLayer.
956 ////////////////////////////////////////////////////////////////////////
957
958 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
959                               FGInstrumentLayer * layer1,
960                               FGInstrumentLayer * layer2)
961   : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
962 {
963 }
964
965 FGSwitchLayer::~FGSwitchLayer ()
966 {
967   delete _layer1;
968   delete _layer2;
969 }
970
971 void
972 FGSwitchLayer::draw ()
973 {
974   if (test()) {
975     transform();
976     if (_node->getBoolValue()) {
977       _layer1->draw();
978     } else {
979       _layer2->draw();
980     }
981   }
982 }
983
984 \f
985 // end of panel.cxx