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