]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Cosmetic changes.
[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 #if 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
263                                 // If the mouse is down, do something
264     if (_mouseDown) {
265         _mouseDelay--;
266         if (_mouseDelay < 0) {
267             _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
268             _mouseDelay = 2;
269         }
270     }
271
272                                 // Now, draw the panel
273     float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
274                                             _ysize_node->getIntValue());
275     if (aspect_adjust <1.0)
276         update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
277     else
278         update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
279 }
280
281
282 void
283 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
284 {
285                                 // Calculate accelerations
286                                 // and jiggle the panel accordingly
287                                 // The factors and bounds are just
288                                 // initial guesses; using sqrt smooths
289                                 // out the spikes.
290   double x_offset = _x_offset;
291   double y_offset = _y_offset;
292
293   if (_jitter != 0.0) {
294     double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
295     double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
296     double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
297
298     double a_zx_pilot = a_z_pilot - a_x_pilot;
299     
300     int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
301                    (a_y_pilot < 0 ? -1 : 1);
302     int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
303                    (a_zx_pilot < 0 ? -1 : 1);
304
305                                 // adjustments in screen coordinates
306     x_offset += x_adjust;
307     y_offset += y_adjust;
308   }
309
310   glMatrixMode(GL_PROJECTION);
311   glPushMatrix();
312   glLoadIdentity();
313   gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
314   // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
315
316   glMatrixMode(GL_MODELVIEW);
317   glPushMatrix();
318   glLoadIdentity();
319
320   glTranslated(x_offset, y_offset, 0);
321
322                                 // Draw the background
323   glEnable(GL_TEXTURE_2D);
324   glDisable(GL_LIGHTING);
325   glEnable(GL_BLEND);
326   glEnable(GL_ALPHA_TEST);
327   glEnable(GL_COLOR_MATERIAL);
328   // glColor4f(1.0, 1.0, 1.0, 1.0);
329   if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
330       glColor4fv( cur_light_params.scene_diffuse );
331   } else {
332       glColor4f(0.7, 0.2, 0.2, 1.0);
333   }
334   glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
335   // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
336   // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
337   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
338   glBegin(GL_POLYGON);
339   glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
340   glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
341   glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
342   glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
343   glEnd();
344
345                                 // Draw the instruments.
346   instrument_list_type::const_iterator current = _instruments.begin();
347   instrument_list_type::const_iterator end = _instruments.end();
348
349   for ( ; current != end; current++) {
350     FGPanelInstrument * instr = *current;
351     glLoadIdentity();
352     glTranslated(x_offset, y_offset, 0);
353     glTranslated(instr->getXPos(), instr->getYPos(), 0);
354     instr->draw();
355   }
356
357   glMatrixMode(GL_PROJECTION);
358   glPopMatrix();
359   glMatrixMode(GL_MODELVIEW);
360   glPopMatrix();
361   ssgForceBasicState();
362   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
363 }
364
365
366 /**
367  * Set the panel's visibility.
368  */
369 void
370 FGPanel::setVisibility (bool visibility)
371 {
372   _visibility = visibility;
373 }
374
375
376 /**
377  * Return true if the panel is visible.
378  */
379 bool
380 FGPanel::getVisibility () const
381 {
382   return _visibility;
383 }
384
385
386 /**
387  * Set the panel's background texture.
388  */
389 void
390 FGPanel::setBackground (ssgTexture * texture)
391 {
392   _bg = texture;
393 }
394
395
396 /**
397  * Set the panel's x-offset.
398  */
399 void
400 FGPanel::setXOffset (int offset)
401 {
402   if (offset <= 0 && offset >= -_width + WIN_W)
403     _x_offset = offset;
404 }
405
406
407 /**
408  * Set the panel's y-offset.
409  */
410 void
411 FGPanel::setYOffset (int offset)
412 {
413   if (offset <= 0 && offset >= -_height)
414     _y_offset = offset;
415 }
416
417
418 /**
419  * Perform a mouse action.
420  */
421 bool
422 FGPanel::doMouseAction (int button, int updown, int x, int y)
423 {
424                                 // FIXME: this same code appears in update()
425   int xsize = _xsize_node->getIntValue();
426   int ysize = _ysize_node->getIntValue();
427   float aspect_adjust = get_aspect_adjust(xsize, ysize);
428
429                                 // Note a released button and return
430   // cerr << "Doing mouse action\n";
431   if (updown == 1) {
432     _mouseDown = false;
433     _mouseInstrument = 0;
434     return true;
435   }
436
437                                 // Scale for the real window size.
438   if (aspect_adjust < 1.0) {
439     x = int(((float)x / xsize) * WIN_W * aspect_adjust);
440     y = int(WIN_H - ((float(y) / ysize) * WIN_H));
441   } else {
442     x = int(((float)x / xsize) * WIN_W);
443     y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
444   }
445
446                                 // Adjust for offsets.
447   x -= _x_offset;
448   y -= _y_offset;
449
450                                 // Search for a matching instrument.
451   for (int i = 0; i < (int)_instruments.size(); i++) {
452     FGPanelInstrument *inst = _instruments[i];
453     int ix = inst->getXPos();
454     int iy = inst->getYPos();
455     int iw = inst->getWidth() / 2;
456     int ih = inst->getHeight() / 2;
457     if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
458       _mouseDown = true;
459       _mouseDelay = 20;
460       _mouseInstrument = inst;
461       _mouseButton = button;
462       _mouseX = x - ix;
463       _mouseY = y - iy;
464                                 // Always do the action once.
465       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
466       return true;
467     }
468   }
469   return false;
470 }
471
472
473 \f
474 ////////////////////////////////////////////////////////////////////////.
475 // Implementation of FGPanelAction.
476 ////////////////////////////////////////////////////////////////////////
477
478 FGPanelAction::FGPanelAction ()
479 {
480 }
481
482 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
483   : _button(button), _x(x), _y(y), _w(w), _h(h)
484 {
485   for (unsigned int i = 0; i < _bindings.size(); i++)
486     delete _bindings[i];
487 }
488
489 FGPanelAction::~FGPanelAction ()
490 {
491 }
492
493 void
494 FGPanelAction::addBinding (FGBinding * binding)
495 {
496   _bindings.push_back(binding);
497 }
498
499 void
500 FGPanelAction::doAction ()
501 {
502   if (test()) {
503     int nBindings = _bindings.size();
504     for (int i = 0; i < nBindings; i++) {
505       _bindings[i]->fire();
506     }
507   }
508 }
509
510
511 \f
512 ////////////////////////////////////////////////////////////////////////
513 // Implementation of FGPanelTransformation.
514 ////////////////////////////////////////////////////////////////////////
515
516 FGPanelTransformation::FGPanelTransformation ()
517   : table(0)
518 {
519 }
520
521 FGPanelTransformation::~FGPanelTransformation ()
522 {
523 }
524
525
526 \f
527 ////////////////////////////////////////////////////////////////////////
528 // Implementation of FGPanelInstrument.
529 ////////////////////////////////////////////////////////////////////////
530
531
532 FGPanelInstrument::FGPanelInstrument ()
533 {
534   setPosition(0, 0);
535   setSize(0, 0);
536 }
537
538 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
539 {
540   setPosition(x, y);
541   setSize(w, h);
542 }
543
544 FGPanelInstrument::~FGPanelInstrument ()
545 {
546   for (action_list_type::iterator it = _actions.begin();
547        it != _actions.end();
548        it++) {
549     delete *it;
550     *it = 0;
551   }
552 }
553
554 void
555 FGPanelInstrument::setPosition (int x, int y)
556 {
557   _x = x;
558   _y = y;
559 }
560
561 void
562 FGPanelInstrument::setSize (int w, int h)
563 {
564   _w = w;
565   _h = h;
566 }
567
568 int
569 FGPanelInstrument::getXPos () const
570 {
571   return _x;
572 }
573
574 int
575 FGPanelInstrument::getYPos () const
576 {
577   return _y;
578 }
579
580 int
581 FGPanelInstrument::getWidth () const
582 {
583   return _w;
584 }
585
586 int
587 FGPanelInstrument::getHeight () const
588 {
589   return _h;
590 }
591
592 void
593 FGPanelInstrument::addAction (FGPanelAction * action)
594 {
595   _actions.push_back(action);
596 }
597
598                                 // Coordinates relative to centre.
599 bool
600 FGPanelInstrument::doMouseAction (int button, int x, int y)
601 {
602   if (test()) {
603     action_list_type::iterator it = _actions.begin();
604     action_list_type::iterator last = _actions.end();
605     for ( ; it != last; it++) {
606       if ((*it)->inArea(button, x, y)) {
607         (*it)->doAction();
608         return true;
609       }
610     }
611   }
612   return false;
613 }
614
615
616 \f
617 ////////////////////////////////////////////////////////////////////////
618 // Implementation of FGLayeredInstrument.
619 ////////////////////////////////////////////////////////////////////////
620
621 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
622   : FGPanelInstrument(x, y, w, h)
623 {
624 }
625
626 FGLayeredInstrument::~FGLayeredInstrument ()
627 {
628   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
629     delete *it;
630     *it = 0;
631   }
632 }
633
634 void
635 FGLayeredInstrument::draw ()
636 {
637   if (test()) {
638     for (int i = 0; i < (int)_layers.size(); i++) {
639       glPushMatrix();
640       glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
641       _layers[i]->draw();
642       glPopMatrix();
643     }
644   }
645 }
646
647 int
648 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
649 {
650   int n = _layers.size();
651   if (layer->getWidth() == -1) {
652     layer->setWidth(getWidth());
653   }
654   if (layer->getHeight() == -1) {
655     layer->setHeight(getHeight());
656   }
657   _layers.push_back(layer);
658   return n;
659 }
660
661 int
662 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
663                                int w, int h)
664 {
665   return addLayer(new FGTexturedLayer(texture, w, h));
666 }
667
668 void
669 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
670 {
671   int layer = _layers.size() - 1;
672   _layers[layer]->addTransformation(transformation);
673 }
674
675
676 \f
677 ////////////////////////////////////////////////////////////////////////
678 // Implementation of FGInstrumentLayer.
679 ////////////////////////////////////////////////////////////////////////
680
681 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
682   : _w(w),
683     _h(h)
684 {
685 }
686
687 FGInstrumentLayer::~FGInstrumentLayer ()
688 {
689   for (transformation_list::iterator it = _transformations.begin();
690        it != _transformations.end();
691        it++) {
692     delete *it;
693     *it = 0;
694   }
695 }
696
697 void
698 FGInstrumentLayer::transform () const
699 {
700   transformation_list::const_iterator it = _transformations.begin();
701   transformation_list::const_iterator last = _transformations.end();
702   while (it != last) {
703     FGPanelTransformation *t = *it;
704     if (t->test()) {
705       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
706       if (val < t->min) {
707         val = t->min;
708       } else if (val > t->max) {
709         val = t->max;
710       }
711       if(t->table==0) {
712         val = val * t->factor + t->offset;
713       } else {
714         val = t->table->interpolate(val) * t->factor + t->offset;
715       }
716       
717       switch (t->type) {
718       case FGPanelTransformation::XSHIFT:
719         glTranslatef(val, 0.0, 0.0);
720         break;
721       case FGPanelTransformation::YSHIFT:
722         glTranslatef(0.0, val, 0.0);
723         break;
724       case FGPanelTransformation::ROTATION:
725         glRotatef(-val, 0.0, 0.0, 1.0);
726         break;
727       }
728     }
729     it++;
730   }
731 }
732
733 void
734 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
735 {
736   _transformations.push_back(transformation);
737 }
738
739
740 \f
741 ////////////////////////////////////////////////////////////////////////
742 // Implementation of FGGroupLayer.
743 ////////////////////////////////////////////////////////////////////////
744
745 FGGroupLayer::FGGroupLayer ()
746 {
747 }
748
749 FGGroupLayer::~FGGroupLayer ()
750 {
751   for (unsigned int i = 0; i < _layers.size(); i++)
752     delete _layers[i];
753 }
754
755 void
756 FGGroupLayer::draw ()
757 {
758   if (test()) {
759     int nLayers = _layers.size();
760     for (int i = 0; i < nLayers; i++)
761       _layers[i]->draw();
762   }
763 }
764
765 void
766 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
767 {
768   _layers.push_back(layer);
769 }
770
771
772 \f
773 ////////////////////////////////////////////////////////////////////////
774 // Implementation of FGTexturedLayer.
775 ////////////////////////////////////////////////////////////////////////
776
777
778 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
779   : FGInstrumentLayer(w, h)
780 {
781   setTexture(texture);
782 }
783
784
785 FGTexturedLayer::~FGTexturedLayer ()
786 {
787 }
788
789
790 void
791 FGTexturedLayer::draw ()
792 {
793   if (test()) {
794     int w2 = _w / 2;
795     int h2 = _h / 2;
796     
797     transform();
798     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
799     glBegin(GL_POLYGON);
800     
801                                 // From Curt: turn on the panel
802                                 // lights after sundown.
803     if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
804       glColor4fv( cur_light_params.scene_diffuse );
805     } else {
806       glColor4f(0.7, 0.2, 0.2, 1.0);
807     }
808
809
810     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
811     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
812     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
813     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
814     glEnd();
815   }
816 }
817
818
819 \f
820 ////////////////////////////////////////////////////////////////////////
821 // Implementation of FGTextLayer.
822 ////////////////////////////////////////////////////////////////////////
823
824 FGTextLayer::FGTextLayer (int w, int h)
825   : FGInstrumentLayer(w, h), _pointSize(14.0)
826 {
827   _then.stamp();
828   _color[0] = _color[1] = _color[2] = 0.0;
829   _color[3] = 1.0;
830 }
831
832 FGTextLayer::~FGTextLayer ()
833 {
834   chunk_list::iterator it = _chunks.begin();
835   chunk_list::iterator last = _chunks.end();
836   for ( ; it != last; it++) {
837     delete *it;
838   }
839 }
840
841 void
842 FGTextLayer::draw ()
843 {
844   if (test()) {
845     glPushMatrix();
846     glColor4fv(_color);
847     transform();
848     text_renderer.setFont(guiFntHandle);
849     text_renderer.setPointSize(_pointSize);
850     text_renderer.begin();
851     text_renderer.start3f(0, 0, 0);
852
853     _now.stamp();
854     if (_now - _then > 100000) {
855       recalc_value();
856       _then = _now;
857     }
858     text_renderer.puts((char *)(_value.c_str()));
859
860     text_renderer.end();
861     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
862     glPopMatrix();
863   }
864 }
865
866 void
867 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
868 {
869   _chunks.push_back(chunk);
870 }
871
872 void
873 FGTextLayer::setColor (float r, float g, float b)
874 {
875   _color[0] = r;
876   _color[1] = g;
877   _color[2] = b;
878   _color[3] = 1.0;
879 }
880
881 void
882 FGTextLayer::setPointSize (float size)
883 {
884   _pointSize = size;
885 }
886
887 void
888 FGTextLayer::setFont(fntFont * font)
889 {
890   text_renderer.setFont(font);
891 }
892
893
894 void
895 FGTextLayer::recalc_value () const
896 {
897   _value = "";
898   chunk_list::const_iterator it = _chunks.begin();
899   chunk_list::const_iterator last = _chunks.end();
900   for ( ; it != last; it++) {
901     _value += (*it)->getValue();
902   }
903 }
904
905
906 \f
907 ////////////////////////////////////////////////////////////////////////
908 // Implementation of FGTextLayer::Chunk.
909 ////////////////////////////////////////////////////////////////////////
910
911 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
912   : _type(FGTextLayer::TEXT), _fmt(fmt)
913 {
914   _text = text;
915   if (_fmt == "") 
916     _fmt = "%s";
917 }
918
919 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
920                            const string &fmt, float mult)
921   : _type(type), _fmt(fmt), _mult(mult)
922 {
923   if (_fmt == "") {
924     if (type == TEXT_VALUE)
925       _fmt = "%s";
926     else
927       _fmt = "%.2f";
928   }
929   _node = node;
930 }
931
932 const char *
933 FGTextLayer::Chunk::getValue () const
934 {
935   if (test()) {
936     switch (_type) {
937     case TEXT:
938       sprintf(_buf, _fmt.c_str(), _text.c_str());
939       return _buf;
940     case TEXT_VALUE:
941       sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
942       break;
943     case DOUBLE_VALUE:
944       sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
945       break;
946     }
947     return _buf;
948   } else {
949     return "";
950   }
951 }
952
953
954 \f
955 ////////////////////////////////////////////////////////////////////////
956 // Implementation of FGSwitchLayer.
957 ////////////////////////////////////////////////////////////////////////
958
959 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
960                               FGInstrumentLayer * layer1,
961                               FGInstrumentLayer * layer2)
962   : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
963 {
964 }
965
966 FGSwitchLayer::~FGSwitchLayer ()
967 {
968   delete _layer1;
969   delete _layer2;
970 }
971
972 void
973 FGSwitchLayer::draw ()
974 {
975   if (test()) {
976     transform();
977     if (_node->getBoolValue()) {
978       _layer1->draw();
979     } else {
980       _layer2->draw();
981     }
982   }
983 }
984
985 \f
986 // end of panel.cxx