]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Save state before drawing '3d' panel, and then restore it after.
[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 // The number of polygon-offset "units" to place between layers.  In
53 // principle, one is supposed to be enough.  In practice, I find that
54 // my hardware/driver requires many more.
55 #define POFF_UNITS 40
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      if(current_panel == 0)
84         return false;
85      if(current_panel->getVisibility() == 0)
86         return false;
87      if(globals->get_viewmgr()->get_current() != 0)
88         return false;
89      if(globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS != 0)
90         return false;
91      return true;
92 }
93
94
95 \f
96 ////////////////////////////////////////////////////////////////////////
97 // Implementation of FGTextureManager.
98 ////////////////////////////////////////////////////////////////////////
99
100 map<string,ssgTexture *> FGTextureManager::_textureMap;
101
102 ssgTexture *
103 FGTextureManager::createTexture (const string &relativePath)
104 {
105   ssgTexture * texture = _textureMap[relativePath];
106   if (texture == 0) {
107     SG_LOG( SG_COCKPIT, SG_DEBUG,
108             "Texture " << relativePath << " does not yet exist" );
109     SGPath tpath(globals->get_fg_root());
110     tpath.append(relativePath);
111     texture = new ssgTexture((char *)tpath.c_str(), false, false);
112     _textureMap[relativePath] = texture;
113     if (_textureMap[relativePath] == 0) 
114       SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
115     SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
116             << " handle=" << texture->getHandle() );
117   }
118
119   return texture;
120 }
121
122
123
124 \f
125 ////////////////////////////////////////////////////////////////////////
126 // Implementation of FGCropped Texture.
127 ////////////////////////////////////////////////////////////////////////
128
129
130 FGCroppedTexture::FGCroppedTexture ()
131   : _path(""), _texture(0),
132     _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
133 {
134 }
135
136
137 FGCroppedTexture::FGCroppedTexture (const string &path,
138                                     float minX, float minY,
139                                     float maxX, float maxY)
140   : _path(path), _texture(0),
141     _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
142 {
143 }
144
145
146 FGCroppedTexture::~FGCroppedTexture ()
147 {
148 }
149
150
151 ssgTexture *
152 FGCroppedTexture::getTexture ()
153 {
154   if (_texture == 0) {
155     _texture = FGTextureManager::createTexture(_path);
156   }
157   return _texture;
158 }
159
160
161 \f
162 ////////////////////////////////////////////////////////////////////////
163 // Implementation of FGPanel.
164 ////////////////////////////////////////////////////////////////////////
165
166 FGPanel * current_panel = NULL;
167 static fntRenderer text_renderer;
168 static fntTexFont *default_font;
169 static fntTexFont *led_font;
170
171 /**
172  * Constructor.
173  */
174 FGPanel::FGPanel ()
175   : _mouseDown(false),
176     _mouseInstrument(0),
177     _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
178     _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
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   for (instrument_list_type::iterator it = _instruments.begin();
193        it != _instruments.end();
194        it++) {
195     delete *it;
196     *it = 0;
197   }
198 }
199
200
201 /**
202  * Add an instrument to the panel.
203  */
204 void
205 FGPanel::addInstrument (FGPanelInstrument * instrument)
206 {
207   _instruments.push_back(instrument);
208 }
209
210
211 /**
212  * Initialize the panel.
213  */
214 void
215 FGPanel::init ()
216 {
217     SGPath base_path;
218     char* envp = ::getenv( "FG_FONTS" );
219     if ( envp != NULL ) {
220         base_path.set( envp );
221     } else {
222         base_path.set( globals->get_fg_root() );
223         base_path.append( "Fonts" );
224     }
225
226     SGPath fntpath;
227
228     // Install the default font
229     fntpath = base_path;
230     fntpath.append( "typewriter.txf" );
231     default_font = new fntTexFont ;
232     default_font -> load ( (char *)fntpath.c_str() ) ;
233
234     // Install the LED font
235     fntpath = base_path;
236     fntpath.append( "led.txf" );
237     led_font = new fntTexFont ;
238     led_font -> load ( (char *)fntpath.c_str() ) ;
239 }
240
241
242 /**
243  * Bind panel properties.
244  */
245 void
246 FGPanel::bind ()
247 {
248   fgSetArchivable("/sim/panel/visibility");
249   fgSetArchivable("/sim/panel/x-offset");
250   fgSetArchivable("/sim/panel/y-offset");
251   fgSetArchivable("/sim/panel/jitter");
252 }
253
254
255 /**
256  * Unbind panel properties.
257  */
258 void
259 FGPanel::unbind ()
260 {
261 }
262
263
264 /**
265  * Update the panel.
266  */
267 void
268 FGPanel::update (double dt)
269 {
270                                 // TODO: cache the nodes
271     _visibility = fgGetBool("/sim/panel/visibility");
272     _x_offset = fgGetInt("/sim/panel/x-offset");
273     _y_offset = fgGetInt("/sim/panel/y-offset");
274     _jitter = fgGetFloat("/sim/panel/jitter");
275
276                                 // Do nothing if the panel isn't visible.
277     if ( !fgPanelVisible() ) {
278         return;
279     }
280
281                                 // If the mouse is down, do something
282     if (_mouseDown) {
283         _mouseDelay--;
284         if (_mouseDelay < 0) {
285             _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
286             _mouseDelay = 2;
287         }
288     }
289
290                                 // Now, draw the panel
291     float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
292                                             _ysize_node->getIntValue());
293     if (aspect_adjust <1.0)
294         update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
295     else
296         update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
297 }
298
299
300 void
301 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
302 {
303                                 // Calculate accelerations
304                                 // and jiggle the panel accordingly
305                                 // The factors and bounds are just
306                                 // initial guesses; using sqrt smooths
307                                 // out the spikes.
308   double x_offset = _x_offset;
309   double y_offset = _y_offset;
310
311 #if 0
312   if (_jitter != 0.0) {
313     double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
314     double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
315     double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
316
317     double a_zx_pilot = a_z_pilot - a_x_pilot;
318     
319     int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
320                    (a_y_pilot < 0 ? -1 : 1);
321     int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
322                    (a_zx_pilot < 0 ? -1 : 1);
323
324                                 // adjustments in screen coordinates
325     x_offset += x_adjust;
326     y_offset += y_adjust;
327   }
328 #endif
329
330   glMatrixMode(GL_PROJECTION);
331   glPushMatrix();
332   glLoadIdentity();
333   gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
334   // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
335   
336   glMatrixMode(GL_MODELVIEW);
337   glPushMatrix();
338   glLoadIdentity();
339   
340   glTranslated(x_offset, y_offset, 0);
341   
342   draw();
343
344   glMatrixMode(GL_PROJECTION);
345   glPopMatrix();
346   glMatrixMode(GL_MODELVIEW);
347   glPopMatrix();
348
349   ssgForceBasicState();
350   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
351 }
352
353 void
354 FGPanel::draw()
355 {
356   // In 3D mode, it's possible that we are being drawn exactly on top
357   // of an existing polygon.  Use an offset to prevent z-fighting.  In
358   // 2D mode, this is a no-op.
359   glEnable(GL_POLYGON_OFFSET_FILL);
360   glPolygonOffset(0, -POFF_UNITS);
361
362   // save some state
363   glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
364                 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT );
365
366   // Draw the background
367   glEnable(GL_TEXTURE_2D);
368   glDisable(GL_LIGHTING);
369   glEnable(GL_BLEND);
370   glEnable(GL_ALPHA_TEST);
371   glEnable(GL_COLOR_MATERIAL);
372   // glColor4f(1.0, 1.0, 1.0, 1.0);
373   if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
374       glColor4fv( cur_light_params.scene_diffuse );
375   } else {
376       glColor4f(0.7, 0.2, 0.2, 1.0);
377   }
378   if (_bg != 0) {
379     glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
380     // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
381     // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
382     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
383     glBegin(GL_POLYGON);
384     glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
385     glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
386     glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
387     glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
388     glEnd();
389   } else {
390     for (int i = 0; i < 4; i ++) {
391       // top row of textures...(1,3,5,7)
392       glBindTexture(GL_TEXTURE_2D, _mbg[i*2]->getHandle());
393       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
394       glBegin(GL_POLYGON);
395       glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
396       glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
397       glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
398       glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
399       glEnd();
400       // bottom row of textures...(2,4,6,8)
401       glBindTexture(GL_TEXTURE_2D, _mbg[(i*2)+1]->getHandle());
402       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
403       glBegin(GL_POLYGON);
404       glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
405       glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
406       glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
407       glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
408       glEnd();
409     }
410   }
411
412   // Draw the instruments.
413   instrument_list_type::const_iterator current = _instruments.begin();
414   instrument_list_type::const_iterator end = _instruments.end();
415
416   for ( ; current != end; current++) {
417     FGPanelInstrument * instr = *current;
418     glPushMatrix();
419     glTranslated(instr->getXPos(), instr->getYPos(), 0);
420     instr->draw();
421     glPopMatrix();
422   }
423
424   // restore some original state
425   glPopAttrib();
426   glDisable(GL_POLYGON_OFFSET_FILL);
427 }
428
429 /**
430  * Set the panel's visibility.
431  */
432 void
433 FGPanel::setVisibility (bool visibility)
434 {
435   _visibility = visibility;
436 }
437
438
439 /**
440  * Return true if the panel is visible.
441  */
442 bool
443 FGPanel::getVisibility () const
444 {
445   return _visibility;
446 }
447
448
449 /**
450  * Set the panel's background texture.
451  */
452 void
453 FGPanel::setBackground (ssgTexture * texture)
454 {
455   _bg = texture;
456 }
457
458 /**
459  * Set the panel's multiple background textures.
460  */
461 void
462 FGPanel::setMultiBackground (ssgTexture * texture, int idx)
463 {
464   _bg = 0;
465   _mbg[idx] = texture;
466 }
467
468 /**
469  * Set the panel's x-offset.
470  */
471 void
472 FGPanel::setXOffset (int offset)
473 {
474   if (offset <= 0 && offset >= -_width + WIN_W)
475     _x_offset = offset;
476 }
477
478
479 /**
480  * Set the panel's y-offset.
481  */
482 void
483 FGPanel::setYOffset (int offset)
484 {
485   if (offset <= 0 && offset >= -_height)
486     _y_offset = offset;
487 }
488
489 /**
490  * Perform a mouse action.
491  */
492 bool
493 FGPanel::doMouseAction (int button, int updown, int x, int y)
494 {
495                                 // FIXME: this same code appears in update()
496   int xsize = _xsize_node->getIntValue();
497   int ysize = _ysize_node->getIntValue();
498   float aspect_adjust = get_aspect_adjust(xsize, ysize);
499
500                                 // Note a released button and return
501   // cerr << "Doing mouse action\n";
502   if (updown == 1) {
503     _mouseDown = false;
504     _mouseInstrument = 0;
505     return false;
506   }
507
508                                 // Scale for the real window size.
509   if (aspect_adjust < 1.0) {
510     x = int(((float)x / xsize) * WIN_W * aspect_adjust);
511     y = int(WIN_H - ((float(y) / ysize) * WIN_H));
512   } else {
513     x = int(((float)x / xsize) * WIN_W);
514     y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
515   }
516
517                                 // Adjust for offsets.
518   x -= _x_offset;
519   y -= _y_offset;
520
521                                 // Search for a matching instrument.
522   for (int i = 0; i < (int)_instruments.size(); i++) {
523     FGPanelInstrument *inst = _instruments[i];
524     int ix = inst->getXPos();
525     int iy = inst->getYPos();
526     int iw = inst->getWidth() / 2;
527     int ih = inst->getHeight() / 2;
528     if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
529       _mouseDown = true;
530       _mouseDelay = 20;
531       _mouseInstrument = inst;
532       _mouseButton = button;
533       _mouseX = x - ix;
534       _mouseY = y - iy;
535                                 // Always do the action once.
536       return _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
537     }
538   }
539   return false;
540 }
541
542
543 \f
544 ////////////////////////////////////////////////////////////////////////.
545 // Implementation of FGPanelAction.
546 ////////////////////////////////////////////////////////////////////////
547
548 FGPanelAction::FGPanelAction ()
549 {
550 }
551
552 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
553   : _button(button), _x(x), _y(y), _w(w), _h(h)
554 {
555   for (unsigned int i = 0; i < _bindings.size(); i++)
556     delete _bindings[i];
557 }
558
559 FGPanelAction::~FGPanelAction ()
560 {
561 }
562
563 void
564 FGPanelAction::addBinding (FGBinding * binding)
565 {
566   _bindings.push_back(binding);
567 }
568
569 void
570 FGPanelAction::doAction ()
571 {
572   if (test()) {
573     int nBindings = _bindings.size();
574     for (int i = 0; i < nBindings; i++) {
575       _bindings[i]->fire();
576     }
577   }
578 }
579
580
581 \f
582 ////////////////////////////////////////////////////////////////////////
583 // Implementation of FGPanelTransformation.
584 ////////////////////////////////////////////////////////////////////////
585
586 FGPanelTransformation::FGPanelTransformation ()
587   : table(0)
588 {
589 }
590
591 FGPanelTransformation::~FGPanelTransformation ()
592 {
593   delete table;
594 }
595
596
597 \f
598 ////////////////////////////////////////////////////////////////////////
599 // Implementation of FGPanelInstrument.
600 ////////////////////////////////////////////////////////////////////////
601
602
603 FGPanelInstrument::FGPanelInstrument ()
604 {
605   setPosition(0, 0);
606   setSize(0, 0);
607 }
608
609 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
610 {
611   setPosition(x, y);
612   setSize(w, h);
613 }
614
615 FGPanelInstrument::~FGPanelInstrument ()
616 {
617   for (action_list_type::iterator it = _actions.begin();
618        it != _actions.end();
619        it++) {
620     delete *it;
621     *it = 0;
622   }
623 }
624
625 void
626 FGPanelInstrument::setPosition (int x, int y)
627 {
628   _x = x;
629   _y = y;
630 }
631
632 void
633 FGPanelInstrument::setSize (int w, int h)
634 {
635   _w = w;
636   _h = h;
637 }
638
639 int
640 FGPanelInstrument::getXPos () const
641 {
642   return _x;
643 }
644
645 int
646 FGPanelInstrument::getYPos () const
647 {
648   return _y;
649 }
650
651 int
652 FGPanelInstrument::getWidth () const
653 {
654   return _w;
655 }
656
657 int
658 FGPanelInstrument::getHeight () const
659 {
660   return _h;
661 }
662
663 void
664 FGPanelInstrument::addAction (FGPanelAction * action)
665 {
666   _actions.push_back(action);
667 }
668
669                                 // Coordinates relative to centre.
670 bool
671 FGPanelInstrument::doMouseAction (int button, int x, int y)
672 {
673   if (test()) {
674     action_list_type::iterator it = _actions.begin();
675     action_list_type::iterator last = _actions.end();
676     for ( ; it != last; it++) {
677       if ((*it)->inArea(button, x, y)) {
678         (*it)->doAction();
679         return true;
680       }
681     }
682   }
683   return false;
684 }
685
686
687 \f
688 ////////////////////////////////////////////////////////////////////////
689 // Implementation of FGLayeredInstrument.
690 ////////////////////////////////////////////////////////////////////////
691
692 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
693   : FGPanelInstrument(x, y, w, h)
694 {
695 }
696
697 FGLayeredInstrument::~FGLayeredInstrument ()
698 {
699   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
700     delete *it;
701     *it = 0;
702   }
703 }
704
705 void
706 FGLayeredInstrument::draw ()
707 {
708   if (!test())
709     return;
710   
711   for (int i = 0; i < (int)_layers.size(); i++) {
712     glPushMatrix();
713     glPolygonOffset(-1, -POFF_UNITS*(i+2));
714     _layers[i]->draw();
715     glPopMatrix();
716   }
717 }
718
719 int
720 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
721 {
722   int n = _layers.size();
723   if (layer->getWidth() == -1) {
724     layer->setWidth(getWidth());
725   }
726   if (layer->getHeight() == -1) {
727     layer->setHeight(getHeight());
728   }
729   _layers.push_back(layer);
730   return n;
731 }
732
733 int
734 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
735                                int w, int h)
736 {
737   return addLayer(new FGTexturedLayer(texture, w, h));
738 }
739
740 void
741 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
742 {
743   int layer = _layers.size() - 1;
744   _layers[layer]->addTransformation(transformation);
745 }
746
747
748 \f
749 ////////////////////////////////////////////////////////////////////////
750 // Implementation of FGInstrumentLayer.
751 ////////////////////////////////////////////////////////////////////////
752
753 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
754   : _w(w),
755     _h(h)
756 {
757 }
758
759 FGInstrumentLayer::~FGInstrumentLayer ()
760 {
761   for (transformation_list::iterator it = _transformations.begin();
762        it != _transformations.end();
763        it++) {
764     delete *it;
765     *it = 0;
766   }
767 }
768
769 void
770 FGInstrumentLayer::transform () const
771 {
772   transformation_list::const_iterator it = _transformations.begin();
773   transformation_list::const_iterator last = _transformations.end();
774   while (it != last) {
775     FGPanelTransformation *t = *it;
776     if (t->test()) {
777       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
778       if (val < t->min) {
779         val = t->min;
780       } else if (val > t->max) {
781         val = t->max;
782       }
783       if(t->table==0) {
784         val = val * t->factor + t->offset;
785       } else {
786         val = t->table->interpolate(val) * t->factor + t->offset;
787       }
788       
789       switch (t->type) {
790       case FGPanelTransformation::XSHIFT:
791         glTranslatef(val, 0.0, 0.0);
792         break;
793       case FGPanelTransformation::YSHIFT:
794         glTranslatef(0.0, val, 0.0);
795         break;
796       case FGPanelTransformation::ROTATION:
797         glRotatef(-val, 0.0, 0.0, 1.0);
798         break;
799       }
800     }
801     it++;
802   }
803 }
804
805 void
806 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
807 {
808   _transformations.push_back(transformation);
809 }
810
811
812 \f
813 ////////////////////////////////////////////////////////////////////////
814 // Implementation of FGGroupLayer.
815 ////////////////////////////////////////////////////////////////////////
816
817 FGGroupLayer::FGGroupLayer ()
818 {
819 }
820
821 FGGroupLayer::~FGGroupLayer ()
822 {
823   for (unsigned int i = 0; i < _layers.size(); i++)
824     delete _layers[i];
825 }
826
827 void
828 FGGroupLayer::draw ()
829 {
830   if (test()) {
831     int nLayers = _layers.size();
832     for (int i = 0; i < nLayers; i++)
833       _layers[i]->draw();
834   }
835 }
836
837 void
838 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
839 {
840   _layers.push_back(layer);
841 }
842
843
844 \f
845 ////////////////////////////////////////////////////////////////////////
846 // Implementation of FGTexturedLayer.
847 ////////////////////////////////////////////////////////////////////////
848
849
850 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
851   : FGInstrumentLayer(w, h)
852 {
853   setTexture(texture);
854 }
855
856
857 FGTexturedLayer::~FGTexturedLayer ()
858 {
859 }
860
861
862 void
863 FGTexturedLayer::draw ()
864 {
865   if (test()) {
866     int w2 = _w / 2;
867     int h2 = _h / 2;
868     
869     transform();
870     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
871     glBegin(GL_POLYGON);
872     
873                                 // From Curt: turn on the panel
874                                 // lights after sundown.
875     if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
876       glColor4fv( cur_light_params.scene_diffuse );
877     } else {
878       glColor4f(0.7, 0.2, 0.2, 1.0);
879     }
880
881
882     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
883     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
884     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
885     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
886     glEnd();
887   }
888 }
889
890
891 \f
892 ////////////////////////////////////////////////////////////////////////
893 // Implementation of FGTextLayer.
894 ////////////////////////////////////////////////////////////////////////
895
896 FGTextLayer::FGTextLayer (int w, int h)
897   : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("default")
898 {
899   _then.stamp();
900   _color[0] = _color[1] = _color[2] = 0.0;
901   _color[3] = 1.0;
902 }
903
904 FGTextLayer::~FGTextLayer ()
905 {
906   chunk_list::iterator it = _chunks.begin();
907   chunk_list::iterator last = _chunks.end();
908   for ( ; it != last; it++) {
909     delete *it;
910   }
911 }
912
913 void
914 FGTextLayer::draw ()
915 {
916   if (test()) {
917     glColor4fv(_color);
918     transform();
919     if ( _font_name == "led" ) {
920         text_renderer.setFont(led_font);
921     } else {
922         text_renderer.setFont(guiFntHandle);
923     }
924     text_renderer.setPointSize(_pointSize);
925     text_renderer.begin();
926     text_renderer.start3f(0, 0, 0);
927
928     _now.stamp();
929     long diff = _now - _then;
930
931     if (diff > 100000 || diff < 0 ) {
932       // ( diff < 0 ) is a sanity check and indicates our time stamp
933       // difference math probably overflowed.  We can handle a max
934       // difference of 35.8 minutes since the returned value is in
935       // usec.  So if the panel is left off longer than that we can
936       // over flow the math with it is turned back on.  This (diff <
937       // 0) catches that situation, get's us out of trouble, and
938       // back on track.
939       recalc_value();
940       _then = _now;
941     }
942
943     // Something is goofy.  The code in this file renders only CCW
944     // polygons, and I have verified that the font code in plib
945     // renders only CCW trianbles.  Yet they come out backwards.
946     // Something around here or in plib is either changing the winding
947     // order or (more likely) pushing a left-handed matrix onto the
948     // stack.  But I can't find it; get out the chainsaw...
949     glFrontFace(GL_CW);
950     text_renderer.puts((char *)(_value.c_str()));
951     glFrontFace(GL_CCW);
952
953     text_renderer.end();
954     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
955   }
956 }
957
958 void
959 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
960 {
961   _chunks.push_back(chunk);
962 }
963
964 void
965 FGTextLayer::setColor (float r, float g, float b)
966 {
967   _color[0] = r;
968   _color[1] = g;
969   _color[2] = b;
970   _color[3] = 1.0;
971 }
972
973 void
974 FGTextLayer::setPointSize (float size)
975 {
976   _pointSize = size;
977 }
978
979 void
980 FGTextLayer::setFontName(const string &name)
981 {
982   _font_name = name;
983 }
984
985
986 void
987 FGTextLayer::setFont(fntFont * font)
988 {
989   text_renderer.setFont(font);
990 }
991
992
993 void
994 FGTextLayer::recalc_value () const
995 {
996   _value = "";
997   chunk_list::const_iterator it = _chunks.begin();
998   chunk_list::const_iterator last = _chunks.end();
999   for ( ; it != last; it++) {
1000     _value += (*it)->getValue();
1001   }
1002 }
1003
1004
1005 \f
1006 ////////////////////////////////////////////////////////////////////////
1007 // Implementation of FGTextLayer::Chunk.
1008 ////////////////////////////////////////////////////////////////////////
1009
1010 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1011   : _type(FGTextLayer::TEXT), _fmt(fmt)
1012 {
1013   _text = text;
1014   if (_fmt.empty()) 
1015     _fmt = "%s";
1016 }
1017
1018 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1019                            const string &fmt, float mult)
1020   : _type(type), _fmt(fmt), _mult(mult)
1021 {
1022   if (_fmt.empty()) {
1023     if (type == TEXT_VALUE)
1024       _fmt = "%s";
1025     else
1026       _fmt = "%.2f";
1027   }
1028   _node = node;
1029 }
1030
1031 const char *
1032 FGTextLayer::Chunk::getValue () const
1033 {
1034   if (test()) {
1035     _buf[0] = '\0';
1036     switch (_type) {
1037     case TEXT:
1038       sprintf(_buf, _fmt.c_str(), _text.c_str());
1039       return _buf;
1040     case TEXT_VALUE:
1041       sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1042       break;
1043     case DOUBLE_VALUE:
1044       sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
1045       break;
1046     }
1047     return _buf;
1048   } else {
1049     return "";
1050   }
1051 }
1052
1053
1054 \f
1055 ////////////////////////////////////////////////////////////////////////
1056 // Implementation of FGSwitchLayer.
1057 ////////////////////////////////////////////////////////////////////////
1058
1059 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
1060                               FGInstrumentLayer * layer1,
1061                               FGInstrumentLayer * layer2)
1062   : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
1063 {
1064 }
1065
1066 FGSwitchLayer::~FGSwitchLayer ()
1067 {
1068   delete _layer1;
1069   delete _layer2;
1070 }
1071
1072 void
1073 FGSwitchLayer::draw ()
1074 {
1075   if (test()) {
1076     transform();
1077     if (_node->getBoolValue()) {
1078       _layer1->draw();
1079     } else {
1080       _layer2->draw();
1081     }
1082   }
1083 }
1084
1085 \f
1086 // end of panel.cxx
1087
1088
1089