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