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