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