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