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