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