]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Oops. A change to an upstream header seems to have remove glu.h, which
[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 <GL/glu.h>
33
34 #include <plib/ssg.h>
35 #include <plib/fnt.h>
36
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/misc/sg_path.hxx>
39
40 #include <Main/globals.hxx>
41 #include <Main/fg_props.hxx>
42 #include <Main/viewmgr.hxx>
43 #include <Time/light.hxx>
44
45 #include "hud.hxx"
46 #include "panel.hxx"
47
48 #define WIN_X 0
49 #define WIN_Y 0
50 #define WIN_W 1024
51 #define WIN_H 768
52
53 // The number of polygon-offset "units" to place between layers.  In
54 // principle, one is supposed to be enough.  In practice, I find that
55 // my hardware/driver requires many more.
56 #define POFF_UNITS 4
57
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(globals->get_current_panel() == 0)
84         return false;
85      if(globals->get_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 static fntRenderer text_renderer;
167 static fntTexFont *default_font = 0;
168 static fntTexFont *led_font = 0;
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     _view_height(int(WIN_H * 0.4232)),
178     _visibility(fgGetNode("/sim/panel/visibility", true)),
179     _x_offset(fgGetNode("/sim/panel/x-offset", true)),
180     _y_offset(fgGetNode("/sim/panel/y-offset", true)),
181     _jitter(fgGetNode("/sim/panel/jitter", true)),
182     _flipx(fgGetNode("/sim/panel/flip-x", true)),
183     _xsize_node(fgGetNode("/sim/startup/xsize", true)),
184     _ysize_node(fgGetNode("/sim/startup/ysize", true))
185 {
186 }
187
188
189 /**
190  * Destructor.
191  */
192 FGPanel::~FGPanel ()
193 {
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   fgSetArchivable("/sim/panel/visibility");
251   fgSetArchivable("/sim/panel/x-offset");
252   fgSetArchivable("/sim/panel/y-offset");
253   fgSetArchivable("/sim/panel/jitter");
254 }
255
256
257 /**
258  * Unbind panel properties.
259  */
260 void
261 FGPanel::unbind ()
262 {
263 }
264
265
266 /**
267  * Update the panel.
268  */
269 void
270 FGPanel::update (double dt)
271 {
272                                 // Do nothing if the panel isn't visible.
273     if ( !fgPanelVisible() ) {
274         return;
275     }
276
277     updateMouseDelay();
278
279                                 // Now, draw the panel
280     float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
281                                             _ysize_node->getIntValue());
282     if (aspect_adjust <1.0)
283         update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
284     else
285         update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
286 }
287
288 /**
289  * Handle repeatable mouse events.  Called from update() and from
290  * fgUpdate3DPanels().  This functionality needs to move into the
291  * input subsystem.  Counting a tick every two frames is clumsy...
292  */
293 void FGPanel::updateMouseDelay()
294 {
295     if (_mouseDown) {
296         _mouseDelay--;
297         if (_mouseDelay < 0) {
298             _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
299             _mouseDelay = 2;
300         }
301     }
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->getIntValue();
314   double y_offset = _y_offset->getIntValue();
315
316 #if 0
317   if (_jitter->getFloatValue() != 0.0) {
318     double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
319     double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
320     double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
321
322     double a_zx_pilot = a_z_pilot - a_x_pilot;
323     
324     int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter->getFloatValue())) *
325                    (a_y_pilot < 0 ? -1 : 1);
326     int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter->getFloatValue())) *
327                    (a_zx_pilot < 0 ? -1 : 1);
328
329                                 // adjustments in screen coordinates
330     x_offset += x_adjust;
331     y_offset += y_adjust;
332   }
333 #endif
334
335   glMatrixMode(GL_PROJECTION);
336   glPushMatrix();
337   glLoadIdentity();
338   if ( _flipx->getBoolValue() ) {
339     gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
340   } else {
341     gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
342   }
343   
344   glMatrixMode(GL_MODELVIEW);
345   glPushMatrix();
346   glLoadIdentity();
347   
348   glTranslated(x_offset, y_offset, 0);
349   
350   draw();
351
352   glMatrixMode(GL_PROJECTION);
353   glPopMatrix();
354   glMatrixMode(GL_MODELVIEW);
355   glPopMatrix();
356
357   ssgForceBasicState();
358   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
359 }
360
361 void
362 FGPanel::draw()
363 {
364   // In 3D mode, it's possible that we are being drawn exactly on top
365   // of an existing polygon.  Use an offset to prevent z-fighting.  In
366   // 2D mode, this is a no-op.
367   glEnable(GL_POLYGON_OFFSET_FILL);
368   glPolygonOffset(-1, -POFF_UNITS);
369
370   // save some state
371   glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
372                 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE 
373                 | GL_DEPTH_BUFFER_BIT );
374
375   // Draw the background
376   glEnable(GL_TEXTURE_2D);
377   glDisable(GL_LIGHTING);
378   glEnable(GL_BLEND);
379   glEnable(GL_ALPHA_TEST);
380   glEnable(GL_COLOR_MATERIAL);
381   glEnable(GL_CULL_FACE);
382   glCullFace(GL_BACK);
383   glDisable(GL_DEPTH_TEST);
384   sgVec4 panel_color;
385
386   FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
387   sgCopyVec4( panel_color, l->scene_diffuse());
388   if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
389       if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
390       if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
391       if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
392   }
393   glColor4fv( panel_color );
394   if (_bg != 0) {
395     glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
396     // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
397     // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
398     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
399     glBegin(GL_POLYGON);
400     glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
401     glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
402     glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
403     glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
404     glEnd();
405   } else {
406     for (int i = 0; i < 4; i ++) {
407       // top row of textures...(1,3,5,7)
408       glBindTexture(GL_TEXTURE_2D, _mbg[i*2]->getHandle());
409       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
410       glBegin(GL_POLYGON);
411       glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
412       glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
413       glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
414       glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
415       glEnd();
416       // bottom row of textures...(2,4,6,8)
417       glBindTexture(GL_TEXTURE_2D, _mbg[(i*2)+1]->getHandle());
418       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
419       glBegin(GL_POLYGON);
420       glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
421       glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
422       glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
423       glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
424       glEnd();
425     }
426   }
427
428   // Draw the instruments.
429   instrument_list_type::const_iterator current = _instruments.begin();
430   instrument_list_type::const_iterator end = _instruments.end();
431
432   for ( ; current != end; current++) {
433     FGPanelInstrument * instr = *current;
434     glPushMatrix();
435     glTranslated(instr->getXPos(), instr->getYPos(), 0);
436     instr->draw();
437     glPopMatrix();
438   }
439
440   // Draw yellow "hotspots" if directed to.  This is a panel authoring
441   // feature; not intended to be high performance or to look good.
442   if ( fgGetBool("/sim/panel-hotspots") ) {
443     glDisable(GL_TEXTURE_2D);
444     glColor3f(1, 1, 0);
445     
446     for ( unsigned int i = 0; i < _instruments.size(); i++ )
447       _instruments[i]->drawHotspots();
448   }
449
450
451   // restore some original state
452   glPopAttrib();
453   glPolygonOffset(0, 0);
454   glDisable(GL_POLYGON_OFFSET_FILL);
455 }
456
457 /**
458  * Set the panel's visibility.
459  */
460 void
461 FGPanel::setVisibility (bool visibility)
462 {
463   _visibility->setBoolValue( visibility );
464 }
465
466
467 /**
468  * Return true if the panel is visible.
469  */
470 bool
471 FGPanel::getVisibility () const
472 {
473   return _visibility->getBoolValue();
474 }
475
476
477 /**
478  * Set the panel's background texture.
479  */
480 void
481 FGPanel::setBackground (ssgTexture * texture)
482 {
483   _bg = texture;
484 }
485
486 /**
487  * Set the panel's multiple background textures.
488  */
489 void
490 FGPanel::setMultiBackground (ssgTexture * texture, int idx)
491 {
492   _bg = 0;
493   _mbg[idx] = texture;
494 }
495
496 /**
497  * Set the panel's x-offset.
498  */
499 void
500 FGPanel::setXOffset (int offset)
501 {
502   if (offset <= 0 && offset >= -_width + WIN_W)
503     _x_offset->setIntValue( offset );
504 }
505
506
507 /**
508  * Set the panel's y-offset.
509  */
510 void
511 FGPanel::setYOffset (int offset)
512 {
513   if (offset <= 0 && offset >= -_height)
514     _y_offset->setIntValue( offset );
515 }
516
517 /**
518  * Handle a mouse action in panel-local (not screen) coordinates.
519  * Used by the 3D panel code in Model/panelnode.cxx, in situations
520  * where the panel doesn't control its own screen location.
521  */
522 bool
523 FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
524 {
525   // Note a released button and return
526   if (updown == 1) {
527     if (_mouseInstrument != 0)
528         _mouseInstrument->doMouseAction(_mouseButton, 1, _mouseX, _mouseY);
529     _mouseDown = false;
530     _mouseInstrument = 0;
531     return false;
532   }
533
534   // Search for a matching instrument.
535   for (int i = 0; i < (int)_instruments.size(); i++) {
536     FGPanelInstrument *inst = _instruments[i];
537     int ix = inst->getXPos();
538     int iy = inst->getYPos();
539     int iw = inst->getWidth() / 2;
540     int ih = inst->getHeight() / 2;
541     if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
542       _mouseDown = true;
543       _mouseDelay = 20;
544       _mouseInstrument = inst;
545       _mouseButton = button;
546       _mouseX = x - ix;
547       _mouseY = y - iy;
548       // Always do the action once.
549       return _mouseInstrument->doMouseAction(_mouseButton, 0,
550                                              _mouseX, _mouseY);
551     }
552   }
553   return false;
554 }
555
556 /**
557  * Perform a mouse action.
558  */
559 bool
560 FGPanel::doMouseAction (int button, int updown, int x, int y)
561 {
562                                 // FIXME: this same code appears in update()
563   int xsize = _xsize_node->getIntValue();
564   int ysize = _ysize_node->getIntValue();
565   float aspect_adjust = get_aspect_adjust(xsize, ysize);
566
567                                 // Scale for the real window size.
568   if (aspect_adjust < 1.0) {
569     x = int(((float)x / xsize) * WIN_W * aspect_adjust);
570     y = int(WIN_H - ((float(y) / ysize) * WIN_H));
571   } else {
572     x = int(((float)x / xsize) * WIN_W);
573     y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
574   }
575
576                                 // Adjust for offsets.
577   x -= _x_offset->getIntValue();
578   y -= _y_offset->getIntValue();
579
580   // Having fixed up the coordinates, fall through to the local
581   // coordinate handler.
582   return doLocalMouseAction(button, updown, x, y);
583
584
585
586 \f
587 ////////////////////////////////////////////////////////////////////////.
588 // Implementation of FGPanelAction.
589 ////////////////////////////////////////////////////////////////////////
590
591 FGPanelAction::FGPanelAction ()
592 {
593 }
594
595 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h,
596                               bool repeatable)
597     : _button(button), _x(x), _y(y), _w(w), _h(h), _repeatable(repeatable)
598 {
599   for (unsigned int i = 0; i < 2; i++) {
600       for (unsigned int j = 0; j < _bindings[i].size(); j++)
601           delete _bindings[i][j];
602   }
603 }
604
605 FGPanelAction::~FGPanelAction ()
606 {
607 }
608
609 void
610 FGPanelAction::addBinding (FGBinding * binding, int updown)
611 {
612   _bindings[updown].push_back(binding);
613 }
614
615 bool
616 FGPanelAction::doAction (int updown)
617 {
618   if (test()) {
619     if ((updown != _last_state) || (updown == 0 && _repeatable)) {
620         int nBindings = _bindings[updown].size();
621         for (int i = 0; i < nBindings; i++)
622             _bindings[updown][i]->fire();
623     }
624     _last_state = updown;
625     return true;
626   } else {
627     return false;
628   }
629 }
630
631
632 \f
633 ////////////////////////////////////////////////////////////////////////
634 // Implementation of FGPanelTransformation.
635 ////////////////////////////////////////////////////////////////////////
636
637 FGPanelTransformation::FGPanelTransformation ()
638   : table(0)
639 {
640 }
641
642 FGPanelTransformation::~FGPanelTransformation ()
643 {
644   delete table;
645 }
646
647
648 \f
649 ////////////////////////////////////////////////////////////////////////
650 // Implementation of FGPanelInstrument.
651 ////////////////////////////////////////////////////////////////////////
652
653
654 FGPanelInstrument::FGPanelInstrument ()
655 {
656   setPosition(0, 0);
657   setSize(0, 0);
658 }
659
660 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
661 {
662   setPosition(x, y);
663   setSize(w, h);
664 }
665
666 FGPanelInstrument::~FGPanelInstrument ()
667 {
668   for (action_list_type::iterator it = _actions.begin();
669        it != _actions.end();
670        it++) {
671     delete *it;
672     *it = 0;
673   }
674 }
675
676 void
677 FGPanelInstrument::drawHotspots()
678 {
679   for ( unsigned int i = 0; i < _actions.size(); i++ ) {
680     FGPanelAction* a = _actions[i];
681     float x1 = getXPos() + a->getX();
682     float x2 = x1 + a->getWidth();
683     float y1 = getYPos() + a->getY();
684     float y2 = y1 + a->getHeight();
685
686     glBegin(GL_LINE_LOOP);
687     glVertex2f(x1, y1);
688     glVertex2f(x1, y2);
689     glVertex2f(x2, y2);
690     glVertex2f(x2, y1);
691     glEnd();
692   }
693 }
694
695 void
696 FGPanelInstrument::setPosition (int x, int y)
697 {
698   _x = x;
699   _y = y;
700 }
701
702 void
703 FGPanelInstrument::setSize (int w, int h)
704 {
705   _w = w;
706   _h = h;
707 }
708
709 int
710 FGPanelInstrument::getXPos () const
711 {
712   return _x;
713 }
714
715 int
716 FGPanelInstrument::getYPos () const
717 {
718   return _y;
719 }
720
721 int
722 FGPanelInstrument::getWidth () const
723 {
724   return _w;
725 }
726
727 int
728 FGPanelInstrument::getHeight () const
729 {
730   return _h;
731 }
732
733 void
734 FGPanelInstrument::addAction (FGPanelAction * action)
735 {
736   _actions.push_back(action);
737 }
738
739                                 // Coordinates relative to centre.
740 bool
741 FGPanelInstrument::doMouseAction (int button, int updown, int x, int y)
742 {
743   if (test()) {
744     action_list_type::iterator it = _actions.begin();
745     action_list_type::iterator last = _actions.end();
746     for ( ; it != last; it++) {
747       if ((*it)->inArea(button, x, y) &&
748           (*it)->doAction(updown))
749         return true;
750     }
751   }
752   return false;
753 }
754
755
756 \f
757 ////////////////////////////////////////////////////////////////////////
758 // Implementation of FGLayeredInstrument.
759 ////////////////////////////////////////////////////////////////////////
760
761 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
762   : FGPanelInstrument(x, y, w, h)
763 {
764 }
765
766 FGLayeredInstrument::~FGLayeredInstrument ()
767 {
768   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
769     delete *it;
770     *it = 0;
771   }
772 }
773
774 void
775 FGLayeredInstrument::draw ()
776 {
777   if (!test())
778     return;
779   
780   for (int i = 0; i < (int)_layers.size(); i++) {
781     glPushMatrix();
782     _layers[i]->draw();
783     glPopMatrix();
784   }
785 }
786
787 int
788 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
789 {
790   int n = _layers.size();
791   if (layer->getWidth() == -1) {
792     layer->setWidth(getWidth());
793   }
794   if (layer->getHeight() == -1) {
795     layer->setHeight(getHeight());
796   }
797   _layers.push_back(layer);
798   return n;
799 }
800
801 int
802 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
803                                int w, int h)
804 {
805   return addLayer(new FGTexturedLayer(texture, w, h));
806 }
807
808 void
809 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
810 {
811   int layer = _layers.size() - 1;
812   _layers[layer]->addTransformation(transformation);
813 }
814
815
816 \f
817 ////////////////////////////////////////////////////////////////////////
818 // Implementation of FGInstrumentLayer.
819 ////////////////////////////////////////////////////////////////////////
820
821 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
822   : _w(w),
823     _h(h)
824 {
825 }
826
827 FGInstrumentLayer::~FGInstrumentLayer ()
828 {
829   for (transformation_list::iterator it = _transformations.begin();
830        it != _transformations.end();
831        it++) {
832     delete *it;
833     *it = 0;
834   }
835 }
836
837 void
838 FGInstrumentLayer::transform () const
839 {
840   transformation_list::const_iterator it = _transformations.begin();
841   transformation_list::const_iterator last = _transformations.end();
842   while (it != last) {
843     FGPanelTransformation *t = *it;
844     if (t->test()) {
845       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
846
847       if (t->has_mod)
848           val = fmod(val, t->mod);
849       if (val < t->min) {
850         val = t->min;
851       } else if (val > t->max) {
852         val = t->max;
853       }
854
855       if(t->table==0) {
856         val = val * t->factor + t->offset;
857       } else {
858         val = t->table->interpolate(val) * t->factor + t->offset;
859       }
860       
861       switch (t->type) {
862       case FGPanelTransformation::XSHIFT:
863         glTranslatef(val, 0.0, 0.0);
864         break;
865       case FGPanelTransformation::YSHIFT:
866         glTranslatef(0.0, val, 0.0);
867         break;
868       case FGPanelTransformation::ROTATION:
869         glRotatef(-val, 0.0, 0.0, 1.0);
870         break;
871       }
872     }
873     it++;
874   }
875 }
876
877 void
878 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
879 {
880   _transformations.push_back(transformation);
881 }
882
883
884 \f
885 ////////////////////////////////////////////////////////////////////////
886 // Implementation of FGGroupLayer.
887 ////////////////////////////////////////////////////////////////////////
888
889 FGGroupLayer::FGGroupLayer ()
890 {
891 }
892
893 FGGroupLayer::~FGGroupLayer ()
894 {
895   for (unsigned int i = 0; i < _layers.size(); i++)
896     delete _layers[i];
897 }
898
899 void
900 FGGroupLayer::draw ()
901 {
902   if (test()) {
903     transform();
904     int nLayers = _layers.size();
905     for (int i = 0; i < nLayers; i++)
906       _layers[i]->draw();
907   }
908 }
909
910 void
911 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
912 {
913   _layers.push_back(layer);
914 }
915
916
917 \f
918 ////////////////////////////////////////////////////////////////////////
919 // Implementation of FGTexturedLayer.
920 ////////////////////////////////////////////////////////////////////////
921
922
923 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
924   : FGInstrumentLayer(w, h)
925 {
926   setTexture(texture);
927 }
928
929
930 FGTexturedLayer::~FGTexturedLayer ()
931 {
932 }
933
934
935 void
936 FGTexturedLayer::draw ()
937 {
938   if (test()) {
939     int w2 = _w / 2;
940     int h2 = _h / 2;
941     
942     transform();
943     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
944     glBegin(GL_POLYGON);
945     
946                                 // From Curt: turn on the panel
947                                 // lights after sundown.
948     sgVec4 panel_color;
949
950     FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
951     sgCopyVec4( panel_color, l->scene_diffuse());
952     if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
953         if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
954         if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
955         if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
956     }
957     glColor4fv( panel_color );
958
959     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
960     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
961     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
962     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
963     glEnd();
964   }
965 }
966
967
968 \f
969 ////////////////////////////////////////////////////////////////////////
970 // Implementation of FGTextLayer.
971 ////////////////////////////////////////////////////////////////////////
972
973 FGTextLayer::FGTextLayer (int w, int h)
974   : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("default")
975 {
976   _then.stamp();
977   _color[0] = _color[1] = _color[2] = 0.0;
978   _color[3] = 1.0;
979 }
980
981 FGTextLayer::~FGTextLayer ()
982 {
983   chunk_list::iterator it = _chunks.begin();
984   chunk_list::iterator last = _chunks.end();
985   for ( ; it != last; it++) {
986     delete *it;
987   }
988 }
989
990 void
991 FGTextLayer::draw ()
992 {
993   if (test()) {
994     glColor4fv(_color);
995     transform();
996     if ( _font_name == "led" && led_font != 0) {
997         text_renderer.setFont(led_font);
998     } else {
999         text_renderer.setFont(guiFntHandle);
1000     }
1001     text_renderer.setPointSize(_pointSize);
1002     text_renderer.begin();
1003     text_renderer.start3f(0, 0, 0);
1004
1005     _now.stamp();
1006     long diff = _now - _then;
1007
1008     if (diff > 100000 || diff < 0 ) {
1009       // ( diff < 0 ) is a sanity check and indicates our time stamp
1010       // difference math probably overflowed.  We can handle a max
1011       // difference of 35.8 minutes since the returned value is in
1012       // usec.  So if the panel is left off longer than that we can
1013       // over flow the math with it is turned back on.  This (diff <
1014       // 0) catches that situation, get's us out of trouble, and
1015       // back on track.
1016       recalc_value();
1017       _then = _now;
1018     }
1019
1020     // Something is goofy.  The code in this file renders only CCW
1021     // polygons, and I have verified that the font code in plib
1022     // renders only CCW trianbles.  Yet they come out backwards.
1023     // Something around here or in plib is either changing the winding
1024     // order or (more likely) pushing a left-handed matrix onto the
1025     // stack.  But I can't find it; get out the chainsaw...
1026     glFrontFace(GL_CW);
1027     text_renderer.puts((char *)(_value.c_str()));
1028     glFrontFace(GL_CCW);
1029
1030     text_renderer.end();
1031     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
1032   }
1033 }
1034
1035 void
1036 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1037 {
1038   _chunks.push_back(chunk);
1039 }
1040
1041 void
1042 FGTextLayer::setColor (float r, float g, float b)
1043 {
1044   _color[0] = r;
1045   _color[1] = g;
1046   _color[2] = b;
1047   _color[3] = 1.0;
1048 }
1049
1050 void
1051 FGTextLayer::setPointSize (float size)
1052 {
1053   _pointSize = size;
1054 }
1055
1056 void
1057 FGTextLayer::setFontName(const string &name)
1058 {
1059   _font_name = name;
1060 }
1061
1062
1063 void
1064 FGTextLayer::setFont(fntFont * font)
1065 {
1066   text_renderer.setFont(font);
1067 }
1068
1069
1070 void
1071 FGTextLayer::recalc_value () const
1072 {
1073   _value = "";
1074   chunk_list::const_iterator it = _chunks.begin();
1075   chunk_list::const_iterator last = _chunks.end();
1076   for ( ; it != last; it++) {
1077     _value += (*it)->getValue();
1078   }
1079 }
1080
1081
1082 \f
1083 ////////////////////////////////////////////////////////////////////////
1084 // Implementation of FGTextLayer::Chunk.
1085 ////////////////////////////////////////////////////////////////////////
1086
1087 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1088   : _type(FGTextLayer::TEXT), _fmt(fmt)
1089 {
1090   _text = text;
1091   if (_fmt.empty()) 
1092     _fmt = "%s";
1093 }
1094
1095 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1096                            const string &fmt, float mult, float offs,
1097                            bool truncation)
1098   : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
1099 {
1100   if (_fmt.empty()) {
1101     if (type == TEXT_VALUE)
1102       _fmt = "%s";
1103     else
1104       _fmt = "%.2f";
1105   }
1106   _node = node;
1107 }
1108
1109 const char *
1110 FGTextLayer::Chunk::getValue () const
1111 {
1112   if (test()) {
1113     _buf[0] = '\0';
1114     switch (_type) {
1115     case TEXT:
1116       sprintf(_buf, _fmt.c_str(), _text.c_str());
1117       return _buf;
1118     case TEXT_VALUE:
1119       sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1120       break;
1121     case DOUBLE_VALUE:
1122       double d = _offs + _node->getFloatValue() * _mult;
1123       if (_trunc)  d = (d < 0) ? -floor(-d) : floor(d);
1124       sprintf(_buf, _fmt.c_str(), d);
1125       break;
1126     }
1127     return _buf;
1128   } else {
1129     return "";
1130   }
1131 }
1132
1133
1134 \f
1135 ////////////////////////////////////////////////////////////////////////
1136 // Implementation of FGSwitchLayer.
1137 ////////////////////////////////////////////////////////////////////////
1138
1139 FGSwitchLayer::FGSwitchLayer ()
1140   : FGGroupLayer()
1141 {
1142 }
1143
1144 void
1145 FGSwitchLayer::draw ()
1146 {
1147   if (test()) {
1148     transform();
1149     int nLayers = _layers.size();
1150     for (int i = 0; i < nLayers; i++) {
1151       if (_layers[i]->test()) {
1152           _layers[i]->draw();
1153           return;
1154       }
1155     }
1156   }
1157 }
1158
1159 \f
1160 // end of panel.cxx
1161
1162
1163