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