]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
- added support for conditions
[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 }
454
455 FGPanelAction::~FGPanelAction ()
456 {
457 }
458
459 void
460 FGPanelAction::addBinding (const FGBinding &binding)
461 {
462   _bindings.push_back(binding);
463 }
464
465 void
466 FGPanelAction::doAction ()
467 {
468   if (test()) {
469     int nBindings = _bindings.size();
470     for (int i = 0; i < nBindings; i++) {
471       _bindings[i].fire();
472     }
473   }
474 }
475
476
477 \f
478 ////////////////////////////////////////////////////////////////////////
479 // Implementation of FGPanelTransformation.
480 ////////////////////////////////////////////////////////////////////////
481
482 FGPanelTransformation::FGPanelTransformation ()
483   : table(0)
484 {
485 }
486
487 FGPanelTransformation::~FGPanelTransformation ()
488 {
489 }
490
491
492 \f
493 ////////////////////////////////////////////////////////////////////////
494 // Implementation of FGPanelInstrument.
495 ////////////////////////////////////////////////////////////////////////
496
497
498 FGPanelInstrument::FGPanelInstrument ()
499 {
500   setPosition(0, 0);
501   setSize(0, 0);
502 }
503
504 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
505 {
506   setPosition(x, y);
507   setSize(w, h);
508 }
509
510 FGPanelInstrument::~FGPanelInstrument ()
511 {
512   for (action_list_type::iterator it = _actions.begin();
513        it != _actions.end();
514        it++) {
515     delete *it;
516     *it = 0;
517   }
518 }
519
520 void
521 FGPanelInstrument::setPosition (int x, int y)
522 {
523   _x = x;
524   _y = y;
525 }
526
527 void
528 FGPanelInstrument::setSize (int w, int h)
529 {
530   _w = w;
531   _h = h;
532 }
533
534 int
535 FGPanelInstrument::getXPos () const
536 {
537   return _x;
538 }
539
540 int
541 FGPanelInstrument::getYPos () const
542 {
543   return _y;
544 }
545
546 int
547 FGPanelInstrument::getWidth () const
548 {
549   return _w;
550 }
551
552 int
553 FGPanelInstrument::getHeight () const
554 {
555   return _h;
556 }
557
558 void
559 FGPanelInstrument::addAction (FGPanelAction * action)
560 {
561   _actions.push_back(action);
562 }
563
564                                 // Coordinates relative to centre.
565 bool
566 FGPanelInstrument::doMouseAction (int button, int x, int y)
567 {
568   if (test()) {
569     action_list_type::iterator it = _actions.begin();
570     action_list_type::iterator last = _actions.end();
571     for ( ; it != last; it++) {
572       if ((*it)->inArea(button, x, y)) {
573         (*it)->doAction();
574         return true;
575       }
576     }
577   }
578   return false;
579 }
580
581
582 \f
583 ////////////////////////////////////////////////////////////////////////
584 // Implementation of FGLayeredInstrument.
585 ////////////////////////////////////////////////////////////////////////
586
587 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
588   : FGPanelInstrument(x, y, w, h)
589 {
590 }
591
592 FGLayeredInstrument::~FGLayeredInstrument ()
593 {
594   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
595     delete *it;
596     *it = 0;
597   }
598 }
599
600 void
601 FGLayeredInstrument::draw ()
602 {
603   if (test()) {
604     for (int i = 0; i < (int)_layers.size(); i++) {
605       glPushMatrix();
606       glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
607       _layers[i]->draw();
608       glPopMatrix();
609     }
610   }
611 }
612
613 int
614 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
615 {
616   int n = _layers.size();
617   if (layer->getWidth() == -1) {
618     layer->setWidth(getWidth());
619   }
620   if (layer->getHeight() == -1) {
621     layer->setHeight(getHeight());
622   }
623   _layers.push_back(layer);
624   return n;
625 }
626
627 int
628 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
629                                int w, int h)
630 {
631   return addLayer(new FGTexturedLayer(texture, w, h));
632 }
633
634 void
635 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
636 {
637   int layer = _layers.size() - 1;
638   _layers[layer]->addTransformation(transformation);
639 }
640
641
642 \f
643 ////////////////////////////////////////////////////////////////////////
644 // Implementation of FGInstrumentLayer.
645 ////////////////////////////////////////////////////////////////////////
646
647 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
648   : _w(w),
649     _h(h)
650 {
651 }
652
653 FGInstrumentLayer::~FGInstrumentLayer ()
654 {
655   for (transformation_list::iterator it = _transformations.begin();
656        it != _transformations.end();
657        it++) {
658     delete *it;
659     *it = 0;
660   }
661 }
662
663 void
664 FGInstrumentLayer::transform () const
665 {
666   transformation_list::const_iterator it = _transformations.begin();
667   transformation_list::const_iterator last = _transformations.end();
668   while (it != last) {
669     FGPanelTransformation *t = *it;
670     if (t->test()) {
671       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
672       if (val < t->min) {
673         val = t->min;
674       } else if (val > t->max) {
675         val = t->max;
676       }
677       if(t->table==0) {
678         val = val * t->factor + t->offset;
679       } else {
680         val = t->table->interpolate(val) * t->factor + t->offset;
681       }
682       
683       switch (t->type) {
684       case FGPanelTransformation::XSHIFT:
685         glTranslatef(val, 0.0, 0.0);
686         break;
687       case FGPanelTransformation::YSHIFT:
688         glTranslatef(0.0, val, 0.0);
689         break;
690       case FGPanelTransformation::ROTATION:
691         glRotatef(-val, 0.0, 0.0, 1.0);
692         break;
693       }
694     }
695     it++;
696   }
697 }
698
699 void
700 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
701 {
702   _transformations.push_back(transformation);
703 }
704
705
706 \f
707 ////////////////////////////////////////////////////////////////////////
708 // Implementation of FGGroupLayer.
709 ////////////////////////////////////////////////////////////////////////
710
711 FGGroupLayer::FGGroupLayer ()
712 {
713 }
714
715 FGGroupLayer::~FGGroupLayer ()
716 {
717   for (int i = 0; i < _layers.size(); i++)
718     delete _layers[i];
719 }
720
721 void
722 FGGroupLayer::draw ()
723 {
724   if (test()) {
725     int nLayers = _layers.size();
726     for (int i = 0; i < nLayers; i++)
727       _layers[i]->draw();
728   }
729 }
730
731 void
732 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
733 {
734   _layers.push_back(layer);
735 }
736
737
738 \f
739 ////////////////////////////////////////////////////////////////////////
740 // Implementation of FGTexturedLayer.
741 ////////////////////////////////////////////////////////////////////////
742
743
744 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
745   : FGInstrumentLayer(w, h)
746 {
747   setTexture(texture);
748 }
749
750
751 FGTexturedLayer::~FGTexturedLayer ()
752 {
753 }
754
755
756 void
757 FGTexturedLayer::draw ()
758 {
759   if (test()) {
760     int w2 = _w / 2;
761     int h2 = _h / 2;
762     
763     transform();
764     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
765     glBegin(GL_POLYGON);
766     
767                                 // From Curt: turn on the panel
768                                 // lights after sundown.
769     if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
770       glColor4fv( cur_light_params.scene_diffuse );
771     } else {
772       glColor4f(0.7, 0.2, 0.2, 1.0);
773     }
774
775
776     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
777     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
778     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
779     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
780     glEnd();
781   }
782 }
783
784
785 \f
786 ////////////////////////////////////////////////////////////////////////
787 // Implementation of FGTextLayer.
788 ////////////////////////////////////////////////////////////////////////
789
790 FGTextLayer::FGTextLayer (int w, int h)
791   : FGInstrumentLayer(w, h), _pointSize(14.0)
792 {
793   _then.stamp();
794   _color[0] = _color[1] = _color[2] = 0.0;
795   _color[3] = 1.0;
796 }
797
798 FGTextLayer::~FGTextLayer ()
799 {
800   chunk_list::iterator it = _chunks.begin();
801   chunk_list::iterator last = _chunks.end();
802   for ( ; it != last; it++) {
803     delete *it;
804   }
805 }
806
807 void
808 FGTextLayer::draw ()
809 {
810   if (test()) {
811     glPushMatrix();
812     glColor4fv(_color);
813     transform();
814     text_renderer.setFont(guiFntHandle);
815     text_renderer.setPointSize(_pointSize);
816     text_renderer.begin();
817     text_renderer.start3f(0, 0, 0);
818
819     _now.stamp();
820     if (_now - _then > 100000) {
821       recalc_value();
822       _then = _now;
823     }
824     text_renderer.puts((char *)(_value.c_str()));
825
826     text_renderer.end();
827     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
828     glPopMatrix();
829   }
830 }
831
832 void
833 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
834 {
835   _chunks.push_back(chunk);
836 }
837
838 void
839 FGTextLayer::setColor (float r, float g, float b)
840 {
841   _color[0] = r;
842   _color[1] = g;
843   _color[2] = b;
844   _color[3] = 1.0;
845 }
846
847 void
848 FGTextLayer::setPointSize (float size)
849 {
850   _pointSize = size;
851 }
852
853 void
854 FGTextLayer::setFont(fntFont * font)
855 {
856   text_renderer.setFont(font);
857 }
858
859
860 void
861 FGTextLayer::recalc_value () const
862 {
863   _value = "";
864   chunk_list::const_iterator it = _chunks.begin();
865   chunk_list::const_iterator last = _chunks.end();
866   for ( ; it != last; it++) {
867     _value += (*it)->getValue();
868   }
869 }
870
871
872 \f
873 ////////////////////////////////////////////////////////////////////////
874 // Implementation of FGTextLayer::Chunk.
875 ////////////////////////////////////////////////////////////////////////
876
877 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
878   : _type(FGTextLayer::TEXT), _fmt(fmt)
879 {
880   _text = text;
881   if (_fmt == "") 
882     _fmt = "%s";
883 }
884
885 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
886                            const string &fmt, float mult)
887   : _type(type), _fmt(fmt), _mult(mult)
888 {
889   if (_fmt == "") {
890     if (type == TEXT_VALUE)
891       _fmt = "%s";
892     else
893       _fmt = "%.2f";
894   }
895   _node = node;
896 }
897
898 const char *
899 FGTextLayer::Chunk::getValue () const
900 {
901   if (test()) {
902     switch (_type) {
903     case TEXT:
904       sprintf(_buf, _fmt.c_str(), _text.c_str());
905       return _buf;
906     case TEXT_VALUE:
907       sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
908       break;
909     case DOUBLE_VALUE:
910       sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
911       break;
912     }
913     return _buf;
914   } else {
915     return "";
916   }
917 }
918
919
920 \f
921 ////////////////////////////////////////////////////////////////////////
922 // Implementation of FGSwitchLayer.
923 ////////////////////////////////////////////////////////////////////////
924
925 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
926                               FGInstrumentLayer * layer1,
927                               FGInstrumentLayer * layer2)
928   : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
929 {
930 }
931
932 FGSwitchLayer::~FGSwitchLayer ()
933 {
934   delete _layer1;
935   delete _layer2;
936 }
937
938 void
939 FGSwitchLayer::draw ()
940 {
941   if (test()) {
942     transform();
943     if (_node->getBoolValue()) {
944       _layer1->draw();
945     } else {
946       _layer2->draw();
947     }
948   }
949 }
950
951 \f
952 // end of panel.cxx