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