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