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