]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
52319efd88912dd33d23969feec54a51bf6d5355
[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 <Objects/texload.h>
42 #include <Time/light.hxx>
43
44 #include "hud.hxx"
45 #include "panel.hxx"
46
47 #define WIN_X 0
48 #define WIN_Y 0
49 #define WIN_W 1024
50 #define WIN_H 768
51
52 // The number of polygon-offset "units" to place between layers.  In
53 // principle, one is supposed to be enough.  In practice, I find that
54 // my hardware/driver requires many more.
55 #define POFF_UNITS 4
56
57 \f
58 ////////////////////////////////////////////////////////////////////////
59 // Local functions.
60 ////////////////////////////////////////////////////////////////////////
61
62
63 /**
64  * Calculate the aspect adjustment for the panel.
65  */
66 static float
67 get_aspect_adjust (int xsize, int ysize)
68 {
69   float ideal_aspect = float(WIN_W) / float(WIN_H);
70   float real_aspect = float(xsize) / float(ysize);
71   return (real_aspect / ideal_aspect);
72 }
73
74
75 \f
76 ////////////////////////////////////////////////////////////////////////
77 // Global functions.
78 ////////////////////////////////////////////////////////////////////////
79
80 bool
81 fgPanelVisible ()
82 {
83      if(globals->get_current_panel() == 0)
84         return false;
85      if(globals->get_current_panel()->getVisibility() == 0)
86         return false;
87      if(globals->get_viewmgr()->get_current() != 0)
88         return false;
89      if(globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS != 0)
90         return false;
91      return true;
92 }
93
94
95 \f
96 ////////////////////////////////////////////////////////////////////////
97 // Implementation of FGTextureManager.
98 ////////////////////////////////////////////////////////////////////////
99
100 map<string,ssgTexture *> FGTextureManager::_textureMap;
101
102 ssgTexture *
103 FGTextureManager::createTexture (const string &relativePath)
104 {
105   ssgTexture * texture = _textureMap[relativePath];
106   if (texture == 0) {
107     SG_LOG( SG_COCKPIT, SG_DEBUG,
108             "Texture " << relativePath << " does not yet exist" );
109     SGPath tpath(globals->get_fg_root());
110     tpath.append(relativePath);
111     texture = new ssgTexture((char *)tpath.c_str(), false, false);
112     _textureMap[relativePath] = texture;
113     if (_textureMap[relativePath] == 0) 
114       SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
115     SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
116             << " handle=" << texture->getHandle() );
117   }
118
119   return texture;
120 }
121
122
123
124 \f
125 ////////////////////////////////////////////////////////////////////////
126 // Implementation of FGCropped Texture.
127 ////////////////////////////////////////////////////////////////////////
128
129
130 FGCroppedTexture::FGCroppedTexture ()
131   : _path(""), _texture(0),
132     _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
133 {
134 }
135
136
137 FGCroppedTexture::FGCroppedTexture (const string &path,
138                                     float minX, float minY,
139                                     float maxX, float maxY)
140   : _path(path), _texture(0),
141     _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
142 {
143 }
144
145
146 FGCroppedTexture::~FGCroppedTexture ()
147 {
148 }
149
150
151 ssgTexture *
152 FGCroppedTexture::getTexture ()
153 {
154   if (_texture == 0) {
155     _texture = FGTextureManager::createTexture(_path);
156   }
157   return _texture;
158 }
159
160
161 \f
162 ////////////////////////////////////////////////////////////////////////
163 // Implementation of FGPanel.
164 ////////////////////////////////////////////////////////////////////////
165
166 static fntRenderer text_renderer;
167 static fntTexFont *default_font = 0;
168 static fntTexFont *led_font = 0;
169
170 /**
171  * Constructor.
172  */
173 FGPanel::FGPanel ()
174   : _mouseDown(false),
175     _mouseInstrument(0),
176     _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
177     _view_height(int(WIN_H * 0.4232)),
178     _xsize_node(fgGetNode("/sim/startup/xsize", true)),
179     _ysize_node(fgGetNode("/sim/startup/ysize", true)),
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 {
186 }
187
188
189 /**
190  * Destructor.
191  */
192 FGPanel::~FGPanel ()
193 {
194   for (instrument_list_type::iterator it = _instruments.begin();
195        it != _instruments.end();
196        it++) {
197     delete *it;
198     *it = 0;
199   }
200 }
201
202
203 /**
204  * Add an instrument to the panel.
205  */
206 void
207 FGPanel::addInstrument (FGPanelInstrument * instrument)
208 {
209   _instruments.push_back(instrument);
210 }
211
212
213 /**
214  * Initialize the panel.
215  */
216 void
217 FGPanel::init ()
218 {
219     SGPath base_path;
220     char* envp = ::getenv( "FG_FONTS" );
221     if ( envp != NULL ) {
222         base_path.set( envp );
223     } else {
224         base_path.set( globals->get_fg_root() );
225         base_path.append( "Fonts" );
226     }
227
228     SGPath fntpath;
229
230     // Install the default font
231     fntpath = base_path;
232     fntpath.append( "typewriter.txf" );
233     default_font = new fntTexFont ;
234     default_font -> load ( (char *)fntpath.c_str() ) ;
235
236     // Install the LED font
237     fntpath = base_path;
238     fntpath.append( "led.txf" );
239     led_font = new fntTexFont ;
240     led_font -> load ( (char *)fntpath.c_str() ) ;
241 }
242
243
244 /**
245  * Bind panel properties.
246  */
247 void
248 FGPanel::bind ()
249 {
250   fgSetArchivable("/sim/panel/visibility");
251   fgSetArchivable("/sim/panel/x-offset");
252   fgSetArchivable("/sim/panel/y-offset");
253   fgSetArchivable("/sim/panel/jitter");
254 }
255
256
257 /**
258  * Unbind panel properties.
259  */
260 void
261 FGPanel::unbind ()
262 {
263 }
264
265
266 /**
267  * Update the panel.
268  */
269 void
270 FGPanel::update (double dt)
271 {
272                                 // Do nothing if the panel isn't visible.
273     if ( !fgPanelVisible() ) {
274         return;
275     }
276
277     updateMouseDelay();
278
279                                 // Now, draw the panel
280     float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
281                                             _ysize_node->getIntValue());
282     if (aspect_adjust <1.0)
283         update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
284     else
285         update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
286 }
287
288 /**
289  * Handle repeatable mouse events.  Called from update() and from
290  * fgUpdate3DPanels().  This functionality needs to move into the
291  * input subsystem.  Counting a tick every two frames is clumsy...
292  */
293 void FGPanel::updateMouseDelay()
294 {
295     if (_mouseDown) {
296         _mouseDelay--;
297         if (_mouseDelay < 0) {
298             _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
299             _mouseDelay = 2;
300         }
301     }
302 }
303
304
305 void
306 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
307 {
308                                 // Calculate accelerations
309                                 // and jiggle the panel accordingly
310                                 // The factors and bounds are just
311                                 // initial guesses; using sqrt smooths
312                                 // out the spikes.
313   double x_offset = _x_offset->getIntValue();
314   double y_offset = _y_offset->getIntValue();
315
316 #if 0
317   if (_jitter->getFloatValue() != 0.0) {
318     double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
319     double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
320     double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
321
322     double a_zx_pilot = a_z_pilot - a_x_pilot;
323     
324     int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter->getFloatValue())) *
325                    (a_y_pilot < 0 ? -1 : 1);
326     int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter->getFloatValue())) *
327                    (a_zx_pilot < 0 ? -1 : 1);
328
329                                 // adjustments in screen coordinates
330     x_offset += x_adjust;
331     y_offset += y_adjust;
332   }
333 #endif
334
335   glMatrixMode(GL_PROJECTION);
336   glPushMatrix();
337   glLoadIdentity();
338   if ( _flipx->getBoolValue() ) {
339     gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
340   } else {
341     gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
342   }
343   
344   glMatrixMode(GL_MODELVIEW);
345   glPushMatrix();
346   glLoadIdentity();
347   
348   glTranslated(x_offset, y_offset, 0);
349   
350   draw();
351
352   glMatrixMode(GL_PROJECTION);
353   glPopMatrix();
354   glMatrixMode(GL_MODELVIEW);
355   glPopMatrix();
356
357   ssgForceBasicState();
358   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
359 }
360
361 void
362 FGPanel::draw()
363 {
364   // In 3D mode, it's possible that we are being drawn exactly on top
365   // of an existing polygon.  Use an offset to prevent z-fighting.  In
366   // 2D mode, this is a no-op.
367   glEnable(GL_POLYGON_OFFSET_FILL);
368   glPolygonOffset(-1, -POFF_UNITS);
369
370   // save some state
371   glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
372                 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT );
373
374   // Draw the background
375   glEnable(GL_TEXTURE_2D);
376   glDisable(GL_LIGHTING);
377   glEnable(GL_BLEND);
378   glEnable(GL_ALPHA_TEST);
379   glEnable(GL_COLOR_MATERIAL);
380   sgVec4 panel_color;
381   sgCopyVec4( panel_color, cur_light_params.scene_diffuse );
382   if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
383       if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
384       if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
385       if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
386   }
387   glColor4fv( panel_color );
388   if (_bg != 0) {
389     glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
390     // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
391     // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
392     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
393     glBegin(GL_POLYGON);
394     glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
395     glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
396     glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
397     glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
398     glEnd();
399   } else {
400     for (int i = 0; i < 4; i ++) {
401       // top row of textures...(1,3,5,7)
402       glBindTexture(GL_TEXTURE_2D, _mbg[i*2]->getHandle());
403       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
404       glBegin(GL_POLYGON);
405       glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
406       glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
407       glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
408       glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
409       glEnd();
410       // bottom row of textures...(2,4,6,8)
411       glBindTexture(GL_TEXTURE_2D, _mbg[(i*2)+1]->getHandle());
412       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
413       glBegin(GL_POLYGON);
414       glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
415       glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
416       glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
417       glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
418       glEnd();
419     }
420   }
421
422   // Draw the instruments.
423   instrument_list_type::const_iterator current = _instruments.begin();
424   instrument_list_type::const_iterator end = _instruments.end();
425
426   for ( ; current != end; current++) {
427     FGPanelInstrument * instr = *current;
428     glPushMatrix();
429     glTranslated(instr->getXPos(), instr->getYPos(), 0);
430     instr->draw();
431     glPopMatrix();
432   }
433
434   // Draw yellow "hotspots" if directed to.  This is a panel authoring
435   // feature; not intended to be high performance or to look good.
436   if(fgGetBool("/sim/panel-hotspots")) {
437     glPushAttrib(GL_ALL_ATTRIB_BITS);
438     glDisable(GL_DEPTH_TEST);
439     glDisable(GL_TEXTURE_2D);
440     glColor3f(1, 1, 0);
441     
442     for(int i=0; i<_instruments.size(); i++)
443       _instruments[i]->drawHotspots();
444
445     glPopAttrib();
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(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     glPolygonOffset(-1, -POFF_UNITS*(i+2));
781     _layers[i]->draw();
782     glPopMatrix();
783   }
784 }
785
786 int
787 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
788 {
789   int n = _layers.size();
790   if (layer->getWidth() == -1) {
791     layer->setWidth(getWidth());
792   }
793   if (layer->getHeight() == -1) {
794     layer->setHeight(getHeight());
795   }
796   _layers.push_back(layer);
797   return n;
798 }
799
800 int
801 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
802                                int w, int h)
803 {
804   return addLayer(new FGTexturedLayer(texture, w, h));
805 }
806
807 void
808 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
809 {
810   int layer = _layers.size() - 1;
811   _layers[layer]->addTransformation(transformation);
812 }
813
814
815 \f
816 ////////////////////////////////////////////////////////////////////////
817 // Implementation of FGInstrumentLayer.
818 ////////////////////////////////////////////////////////////////////////
819
820 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
821   : _w(w),
822     _h(h)
823 {
824 }
825
826 FGInstrumentLayer::~FGInstrumentLayer ()
827 {
828   for (transformation_list::iterator it = _transformations.begin();
829        it != _transformations.end();
830        it++) {
831     delete *it;
832     *it = 0;
833   }
834 }
835
836 void
837 FGInstrumentLayer::transform () const
838 {
839   transformation_list::const_iterator it = _transformations.begin();
840   transformation_list::const_iterator last = _transformations.end();
841   while (it != last) {
842     FGPanelTransformation *t = *it;
843     if (t->test()) {
844       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
845
846       if (t->has_mod)
847           val = fmod(val, t->mod);
848       if (val < t->min) {
849         val = t->min;
850       } else if (val > t->max) {
851         val = t->max;
852       }
853
854       if(t->table==0) {
855         val = val * t->factor + t->offset;
856       } else {
857         val = t->table->interpolate(val) * t->factor + t->offset;
858       }
859       
860       switch (t->type) {
861       case FGPanelTransformation::XSHIFT:
862         glTranslatef(val, 0.0, 0.0);
863         break;
864       case FGPanelTransformation::YSHIFT:
865         glTranslatef(0.0, val, 0.0);
866         break;
867       case FGPanelTransformation::ROTATION:
868         glRotatef(-val, 0.0, 0.0, 1.0);
869         break;
870       }
871     }
872     it++;
873   }
874 }
875
876 void
877 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
878 {
879   _transformations.push_back(transformation);
880 }
881
882
883 \f
884 ////////////////////////////////////////////////////////////////////////
885 // Implementation of FGGroupLayer.
886 ////////////////////////////////////////////////////////////////////////
887
888 FGGroupLayer::FGGroupLayer ()
889 {
890 }
891
892 FGGroupLayer::~FGGroupLayer ()
893 {
894   for (unsigned int i = 0; i < _layers.size(); i++)
895     delete _layers[i];
896 }
897
898 void
899 FGGroupLayer::draw ()
900 {
901   if (test()) {
902     transform();
903     int nLayers = _layers.size();
904     for (int i = 0; i < nLayers; i++)
905       _layers[i]->draw();
906   }
907 }
908
909 void
910 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
911 {
912   _layers.push_back(layer);
913 }
914
915
916 \f
917 ////////////////////////////////////////////////////////////////////////
918 // Implementation of FGTexturedLayer.
919 ////////////////////////////////////////////////////////////////////////
920
921
922 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
923   : FGInstrumentLayer(w, h)
924 {
925   setTexture(texture);
926 }
927
928
929 FGTexturedLayer::~FGTexturedLayer ()
930 {
931 }
932
933
934 void
935 FGTexturedLayer::draw ()
936 {
937   if (test()) {
938     int w2 = _w / 2;
939     int h2 = _h / 2;
940     
941     transform();
942     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
943     glBegin(GL_POLYGON);
944     
945                                 // From Curt: turn on the panel
946                                 // lights after sundown.
947     sgVec4 panel_color;
948     sgCopyVec4( panel_color, cur_light_params.scene_diffuse );
949     if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
950         if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
951         if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
952         if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
953     }
954     glColor4fv( panel_color );
955
956     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
957     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
958     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
959     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
960     glEnd();
961   }
962 }
963
964
965 \f
966 ////////////////////////////////////////////////////////////////////////
967 // Implementation of FGTextLayer.
968 ////////////////////////////////////////////////////////////////////////
969
970 FGTextLayer::FGTextLayer (int w, int h)
971   : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("default")
972 {
973   _then.stamp();
974   _color[0] = _color[1] = _color[2] = 0.0;
975   _color[3] = 1.0;
976 }
977
978 FGTextLayer::~FGTextLayer ()
979 {
980   chunk_list::iterator it = _chunks.begin();
981   chunk_list::iterator last = _chunks.end();
982   for ( ; it != last; it++) {
983     delete *it;
984   }
985 }
986
987 void
988 FGTextLayer::draw ()
989 {
990   if (test()) {
991     glColor4fv(_color);
992     transform();
993     if ( _font_name == "led" && led_font != 0) {
994         text_renderer.setFont(led_font);
995     } else {
996         text_renderer.setFont(guiFntHandle);
997     }
998     text_renderer.setPointSize(_pointSize);
999     text_renderer.begin();
1000     text_renderer.start3f(0, 0, 0);
1001
1002     _now.stamp();
1003     long diff = _now - _then;
1004
1005     if (diff > 100000 || diff < 0 ) {
1006       // ( diff < 0 ) is a sanity check and indicates our time stamp
1007       // difference math probably overflowed.  We can handle a max
1008       // difference of 35.8 minutes since the returned value is in
1009       // usec.  So if the panel is left off longer than that we can
1010       // over flow the math with it is turned back on.  This (diff <
1011       // 0) catches that situation, get's us out of trouble, and
1012       // back on track.
1013       recalc_value();
1014       _then = _now;
1015     }
1016
1017     // Something is goofy.  The code in this file renders only CCW
1018     // polygons, and I have verified that the font code in plib
1019     // renders only CCW trianbles.  Yet they come out backwards.
1020     // Something around here or in plib is either changing the winding
1021     // order or (more likely) pushing a left-handed matrix onto the
1022     // stack.  But I can't find it; get out the chainsaw...
1023     glFrontFace(GL_CW);
1024     text_renderer.puts((char *)(_value.c_str()));
1025     glFrontFace(GL_CCW);
1026
1027     text_renderer.end();
1028     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
1029   }
1030 }
1031
1032 void
1033 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1034 {
1035   _chunks.push_back(chunk);
1036 }
1037
1038 void
1039 FGTextLayer::setColor (float r, float g, float b)
1040 {
1041   _color[0] = r;
1042   _color[1] = g;
1043   _color[2] = b;
1044   _color[3] = 1.0;
1045 }
1046
1047 void
1048 FGTextLayer::setPointSize (float size)
1049 {
1050   _pointSize = size;
1051 }
1052
1053 void
1054 FGTextLayer::setFontName(const string &name)
1055 {
1056   _font_name = name;
1057 }
1058
1059
1060 void
1061 FGTextLayer::setFont(fntFont * font)
1062 {
1063   text_renderer.setFont(font);
1064 }
1065
1066
1067 void
1068 FGTextLayer::recalc_value () const
1069 {
1070   _value = "";
1071   chunk_list::const_iterator it = _chunks.begin();
1072   chunk_list::const_iterator last = _chunks.end();
1073   for ( ; it != last; it++) {
1074     _value += (*it)->getValue();
1075   }
1076 }
1077
1078
1079 \f
1080 ////////////////////////////////////////////////////////////////////////
1081 // Implementation of FGTextLayer::Chunk.
1082 ////////////////////////////////////////////////////////////////////////
1083
1084 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1085   : _type(FGTextLayer::TEXT), _fmt(fmt)
1086 {
1087   _text = text;
1088   if (_fmt.empty()) 
1089     _fmt = "%s";
1090 }
1091
1092 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1093                            const string &fmt, float mult)
1094   : _type(type), _fmt(fmt), _mult(mult)
1095 {
1096   if (_fmt.empty()) {
1097     if (type == TEXT_VALUE)
1098       _fmt = "%s";
1099     else
1100       _fmt = "%.2f";
1101   }
1102   _node = node;
1103 }
1104
1105 const char *
1106 FGTextLayer::Chunk::getValue () const
1107 {
1108   if (test()) {
1109     _buf[0] = '\0';
1110     switch (_type) {
1111     case TEXT:
1112       sprintf(_buf, _fmt.c_str(), _text.c_str());
1113       return _buf;
1114     case TEXT_VALUE:
1115       sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1116       break;
1117     case DOUBLE_VALUE:
1118       sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
1119       break;
1120     }
1121     return _buf;
1122   } else {
1123     return "";
1124   }
1125 }
1126
1127
1128 \f
1129 ////////////////////////////////////////////////////////////////////////
1130 // Implementation of FGSwitchLayer.
1131 ////////////////////////////////////////////////////////////////////////
1132
1133 FGSwitchLayer::FGSwitchLayer ()
1134   : FGGroupLayer()
1135 {
1136 }
1137
1138 void
1139 FGSwitchLayer::draw ()
1140 {
1141   if (test()) {
1142     transform();
1143     int nLayers = _layers.size();
1144     for (int i = 0; i < nLayers; i++) {
1145       if (_layers[i]->test()) {
1146           _layers[i]->draw();
1147           return;
1148       }
1149     }
1150   }
1151 }
1152
1153 \f
1154 // end of panel.cxx
1155
1156
1157