]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Reduce POFF_UNITS from 40 to 4, following Andy Ross's suggestion (to
[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;
169 static fntTexFont *led_font;
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(0, -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   // restore some original state
440   glPopAttrib();
441   glPolygonOffset(0, 0);
442   glDisable(GL_POLYGON_OFFSET_FILL);
443 }
444
445 /**
446  * Set the panel's visibility.
447  */
448 void
449 FGPanel::setVisibility (bool visibility)
450 {
451   _visibility = visibility;
452 }
453
454
455 /**
456  * Return true if the panel is visible.
457  */
458 bool
459 FGPanel::getVisibility () const
460 {
461   return _visibility;
462 }
463
464
465 /**
466  * Set the panel's background texture.
467  */
468 void
469 FGPanel::setBackground (ssgTexture * texture)
470 {
471   _bg = texture;
472 }
473
474 /**
475  * Set the panel's multiple background textures.
476  */
477 void
478 FGPanel::setMultiBackground (ssgTexture * texture, int idx)
479 {
480   _bg = 0;
481   _mbg[idx] = texture;
482 }
483
484 /**
485  * Set the panel's x-offset.
486  */
487 void
488 FGPanel::setXOffset (int offset)
489 {
490   if (offset <= 0 && offset >= -_width + WIN_W)
491     _x_offset = offset;
492 }
493
494
495 /**
496  * Set the panel's y-offset.
497  */
498 void
499 FGPanel::setYOffset (int offset)
500 {
501   if (offset <= 0 && offset >= -_height)
502     _y_offset = offset;
503 }
504
505 /**
506  * Handle a mouse action in panel-local (not screen) coordinates.
507  * Used by the 3D panel code in Model/panelnode.cxx, in situations
508  * where the panel doesn't control its own screen location.
509  */
510 bool
511 FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
512 {
513   // Note a released button and return
514   if (updown == 1) {
515     _mouseDown = false;
516     _mouseInstrument = 0;
517     return false;
518   }
519
520   // Search for a matching instrument.
521   for (int i = 0; i < (int)_instruments.size(); i++) {
522     FGPanelInstrument *inst = _instruments[i];
523     int ix = inst->getXPos();
524     int iy = inst->getYPos();
525     int iw = inst->getWidth() / 2;
526     int ih = inst->getHeight() / 2;
527     if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
528       _mouseDown = true;
529       _mouseDelay = 20;
530       _mouseInstrument = inst;
531       _mouseButton = button;
532       _mouseX = x - ix;
533       _mouseY = y - iy;
534       // Always do the action once.
535       return _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
536     }
537   }
538   return false;
539 }
540
541 /**
542  * Perform a mouse action.
543  */
544 bool
545 FGPanel::doMouseAction (int button, int updown, int x, int y)
546 {
547                                 // FIXME: this same code appears in update()
548   int xsize = _xsize_node->getIntValue();
549   int ysize = _ysize_node->getIntValue();
550   float aspect_adjust = get_aspect_adjust(xsize, ysize);
551
552                                 // Scale for the real window size.
553   if (aspect_adjust < 1.0) {
554     x = int(((float)x / xsize) * WIN_W * aspect_adjust);
555     y = int(WIN_H - ((float(y) / ysize) * WIN_H));
556   } else {
557     x = int(((float)x / xsize) * WIN_W);
558     y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
559   }
560
561                                 // Adjust for offsets.
562   x -= _x_offset;
563   y -= _y_offset;
564
565   // Having fixed up the coordinates, fall through to the local
566   // coordinate handler.
567   doLocalMouseAction(button, updown, x, y);
568
569
570
571 \f
572 ////////////////////////////////////////////////////////////////////////.
573 // Implementation of FGPanelAction.
574 ////////////////////////////////////////////////////////////////////////
575
576 FGPanelAction::FGPanelAction ()
577 {
578 }
579
580 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
581   : _button(button), _x(x), _y(y), _w(w), _h(h)
582 {
583   for (unsigned int i = 0; i < _bindings.size(); i++)
584     delete _bindings[i];
585 }
586
587 FGPanelAction::~FGPanelAction ()
588 {
589 }
590
591 void
592 FGPanelAction::addBinding (FGBinding * binding)
593 {
594   _bindings.push_back(binding);
595 }
596
597 void
598 FGPanelAction::doAction ()
599 {
600   if (test()) {
601     int nBindings = _bindings.size();
602     for (int i = 0; i < nBindings; i++) {
603       _bindings[i]->fire();
604     }
605   }
606 }
607
608
609 \f
610 ////////////////////////////////////////////////////////////////////////
611 // Implementation of FGPanelTransformation.
612 ////////////////////////////////////////////////////////////////////////
613
614 FGPanelTransformation::FGPanelTransformation ()
615   : table(0)
616 {
617 }
618
619 FGPanelTransformation::~FGPanelTransformation ()
620 {
621   delete table;
622 }
623
624
625 \f
626 ////////////////////////////////////////////////////////////////////////
627 // Implementation of FGPanelInstrument.
628 ////////////////////////////////////////////////////////////////////////
629
630
631 FGPanelInstrument::FGPanelInstrument ()
632 {
633   setPosition(0, 0);
634   setSize(0, 0);
635 }
636
637 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
638 {
639   setPosition(x, y);
640   setSize(w, h);
641 }
642
643 FGPanelInstrument::~FGPanelInstrument ()
644 {
645   for (action_list_type::iterator it = _actions.begin();
646        it != _actions.end();
647        it++) {
648     delete *it;
649     *it = 0;
650   }
651 }
652
653 void
654 FGPanelInstrument::setPosition (int x, int y)
655 {
656   _x = x;
657   _y = y;
658 }
659
660 void
661 FGPanelInstrument::setSize (int w, int h)
662 {
663   _w = w;
664   _h = h;
665 }
666
667 int
668 FGPanelInstrument::getXPos () const
669 {
670   return _x;
671 }
672
673 int
674 FGPanelInstrument::getYPos () const
675 {
676   return _y;
677 }
678
679 int
680 FGPanelInstrument::getWidth () const
681 {
682   return _w;
683 }
684
685 int
686 FGPanelInstrument::getHeight () const
687 {
688   return _h;
689 }
690
691 void
692 FGPanelInstrument::addAction (FGPanelAction * action)
693 {
694   _actions.push_back(action);
695 }
696
697                                 // Coordinates relative to centre.
698 bool
699 FGPanelInstrument::doMouseAction (int button, int x, int y)
700 {
701   if (test()) {
702     action_list_type::iterator it = _actions.begin();
703     action_list_type::iterator last = _actions.end();
704     for ( ; it != last; it++) {
705       if ((*it)->inArea(button, x, y)) {
706         (*it)->doAction();
707         return true;
708       }
709     }
710   }
711   return false;
712 }
713
714
715 \f
716 ////////////////////////////////////////////////////////////////////////
717 // Implementation of FGLayeredInstrument.
718 ////////////////////////////////////////////////////////////////////////
719
720 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
721   : FGPanelInstrument(x, y, w, h)
722 {
723 }
724
725 FGLayeredInstrument::~FGLayeredInstrument ()
726 {
727   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
728     delete *it;
729     *it = 0;
730   }
731 }
732
733 void
734 FGLayeredInstrument::draw ()
735 {
736   if (!test())
737     return;
738   
739   for (int i = 0; i < (int)_layers.size(); i++) {
740     glPushMatrix();
741     glPolygonOffset(-1, -POFF_UNITS*(i+2));
742     _layers[i]->draw();
743     glPopMatrix();
744   }
745 }
746
747 int
748 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
749 {
750   int n = _layers.size();
751   if (layer->getWidth() == -1) {
752     layer->setWidth(getWidth());
753   }
754   if (layer->getHeight() == -1) {
755     layer->setHeight(getHeight());
756   }
757   _layers.push_back(layer);
758   return n;
759 }
760
761 int
762 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
763                                int w, int h)
764 {
765   return addLayer(new FGTexturedLayer(texture, w, h));
766 }
767
768 void
769 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
770 {
771   int layer = _layers.size() - 1;
772   _layers[layer]->addTransformation(transformation);
773 }
774
775
776 \f
777 ////////////////////////////////////////////////////////////////////////
778 // Implementation of FGInstrumentLayer.
779 ////////////////////////////////////////////////////////////////////////
780
781 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
782   : _w(w),
783     _h(h)
784 {
785 }
786
787 FGInstrumentLayer::~FGInstrumentLayer ()
788 {
789   for (transformation_list::iterator it = _transformations.begin();
790        it != _transformations.end();
791        it++) {
792     delete *it;
793     *it = 0;
794   }
795 }
796
797 void
798 FGInstrumentLayer::transform () const
799 {
800   transformation_list::const_iterator it = _transformations.begin();
801   transformation_list::const_iterator last = _transformations.end();
802   while (it != last) {
803     FGPanelTransformation *t = *it;
804     if (t->test()) {
805       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
806       if (val < t->min) {
807         val = t->min;
808       } else if (val > t->max) {
809         val = t->max;
810       }
811       if(t->table==0) {
812         val = val * t->factor + t->offset;
813       } else {
814         val = t->table->interpolate(val) * t->factor + t->offset;
815       }
816       
817       switch (t->type) {
818       case FGPanelTransformation::XSHIFT:
819         glTranslatef(val, 0.0, 0.0);
820         break;
821       case FGPanelTransformation::YSHIFT:
822         glTranslatef(0.0, val, 0.0);
823         break;
824       case FGPanelTransformation::ROTATION:
825         glRotatef(-val, 0.0, 0.0, 1.0);
826         break;
827       }
828     }
829     it++;
830   }
831 }
832
833 void
834 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
835 {
836   _transformations.push_back(transformation);
837 }
838
839
840 \f
841 ////////////////////////////////////////////////////////////////////////
842 // Implementation of FGGroupLayer.
843 ////////////////////////////////////////////////////////////////////////
844
845 FGGroupLayer::FGGroupLayer ()
846 {
847 }
848
849 FGGroupLayer::~FGGroupLayer ()
850 {
851   for (unsigned int i = 0; i < _layers.size(); i++)
852     delete _layers[i];
853 }
854
855 void
856 FGGroupLayer::draw ()
857 {
858   if (test()) {
859     int nLayers = _layers.size();
860     for (int i = 0; i < nLayers; i++)
861       _layers[i]->draw();
862   }
863 }
864
865 void
866 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
867 {
868   _layers.push_back(layer);
869 }
870
871
872 \f
873 ////////////////////////////////////////////////////////////////////////
874 // Implementation of FGTexturedLayer.
875 ////////////////////////////////////////////////////////////////////////
876
877
878 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
879   : FGInstrumentLayer(w, h)
880 {
881   setTexture(texture);
882 }
883
884
885 FGTexturedLayer::~FGTexturedLayer ()
886 {
887 }
888
889
890 void
891 FGTexturedLayer::draw ()
892 {
893   if (test()) {
894     int w2 = _w / 2;
895     int h2 = _h / 2;
896     
897     transform();
898     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
899     glBegin(GL_POLYGON);
900     
901                                 // From Curt: turn on the panel
902                                 // lights after sundown.
903     sgVec4 panel_color;
904     sgCopyVec4( panel_color, cur_light_params.scene_diffuse );
905     if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
906         if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
907         if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
908         if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
909     }
910     glColor4fv( panel_color );
911
912     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
913     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
914     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
915     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
916     glEnd();
917   }
918 }
919
920
921 \f
922 ////////////////////////////////////////////////////////////////////////
923 // Implementation of FGTextLayer.
924 ////////////////////////////////////////////////////////////////////////
925
926 FGTextLayer::FGTextLayer (int w, int h)
927   : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("default")
928 {
929   _then.stamp();
930   _color[0] = _color[1] = _color[2] = 0.0;
931   _color[3] = 1.0;
932 }
933
934 FGTextLayer::~FGTextLayer ()
935 {
936   chunk_list::iterator it = _chunks.begin();
937   chunk_list::iterator last = _chunks.end();
938   for ( ; it != last; it++) {
939     delete *it;
940   }
941 }
942
943 void
944 FGTextLayer::draw ()
945 {
946   if (test()) {
947     glColor4fv(_color);
948     transform();
949     if ( _font_name == "led" ) {
950         text_renderer.setFont(led_font);
951     } else {
952         text_renderer.setFont(guiFntHandle);
953     }
954     text_renderer.setPointSize(_pointSize);
955     text_renderer.begin();
956     text_renderer.start3f(0, 0, 0);
957
958     _now.stamp();
959     long diff = _now - _then;
960
961     if (diff > 100000 || diff < 0 ) {
962       // ( diff < 0 ) is a sanity check and indicates our time stamp
963       // difference math probably overflowed.  We can handle a max
964       // difference of 35.8 minutes since the returned value is in
965       // usec.  So if the panel is left off longer than that we can
966       // over flow the math with it is turned back on.  This (diff <
967       // 0) catches that situation, get's us out of trouble, and
968       // back on track.
969       recalc_value();
970       _then = _now;
971     }
972
973     // Something is goofy.  The code in this file renders only CCW
974     // polygons, and I have verified that the font code in plib
975     // renders only CCW trianbles.  Yet they come out backwards.
976     // Something around here or in plib is either changing the winding
977     // order or (more likely) pushing a left-handed matrix onto the
978     // stack.  But I can't find it; get out the chainsaw...
979     glFrontFace(GL_CW);
980     text_renderer.puts((char *)(_value.c_str()));
981     glFrontFace(GL_CCW);
982
983     text_renderer.end();
984     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
985   }
986 }
987
988 void
989 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
990 {
991   _chunks.push_back(chunk);
992 }
993
994 void
995 FGTextLayer::setColor (float r, float g, float b)
996 {
997   _color[0] = r;
998   _color[1] = g;
999   _color[2] = b;
1000   _color[3] = 1.0;
1001 }
1002
1003 void
1004 FGTextLayer::setPointSize (float size)
1005 {
1006   _pointSize = size;
1007 }
1008
1009 void
1010 FGTextLayer::setFontName(const string &name)
1011 {
1012   _font_name = name;
1013 }
1014
1015
1016 void
1017 FGTextLayer::setFont(fntFont * font)
1018 {
1019   text_renderer.setFont(font);
1020 }
1021
1022
1023 void
1024 FGTextLayer::recalc_value () const
1025 {
1026   _value = "";
1027   chunk_list::const_iterator it = _chunks.begin();
1028   chunk_list::const_iterator last = _chunks.end();
1029   for ( ; it != last; it++) {
1030     _value += (*it)->getValue();
1031   }
1032 }
1033
1034
1035 \f
1036 ////////////////////////////////////////////////////////////////////////
1037 // Implementation of FGTextLayer::Chunk.
1038 ////////////////////////////////////////////////////////////////////////
1039
1040 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1041   : _type(FGTextLayer::TEXT), _fmt(fmt)
1042 {
1043   _text = text;
1044   if (_fmt.empty()) 
1045     _fmt = "%s";
1046 }
1047
1048 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1049                            const string &fmt, float mult)
1050   : _type(type), _fmt(fmt), _mult(mult)
1051 {
1052   if (_fmt.empty()) {
1053     if (type == TEXT_VALUE)
1054       _fmt = "%s";
1055     else
1056       _fmt = "%.2f";
1057   }
1058   _node = node;
1059 }
1060
1061 const char *
1062 FGTextLayer::Chunk::getValue () const
1063 {
1064   if (test()) {
1065     _buf[0] = '\0';
1066     switch (_type) {
1067     case TEXT:
1068       sprintf(_buf, _fmt.c_str(), _text.c_str());
1069       return _buf;
1070     case TEXT_VALUE:
1071       sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1072       break;
1073     case DOUBLE_VALUE:
1074       sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
1075       break;
1076     }
1077     return _buf;
1078   } else {
1079     return "";
1080   }
1081 }
1082
1083
1084 \f
1085 ////////////////////////////////////////////////////////////////////////
1086 // Implementation of FGSwitchLayer.
1087 ////////////////////////////////////////////////////////////////////////
1088
1089 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
1090                               FGInstrumentLayer * layer1,
1091                               FGInstrumentLayer * layer2)
1092   : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
1093 {
1094 }
1095
1096 FGSwitchLayer::~FGSwitchLayer ()
1097 {
1098   delete _layer1;
1099   delete _layer2;
1100 }
1101
1102 void
1103 FGSwitchLayer::draw ()
1104 {
1105   if (test()) {
1106     transform();
1107     if (_node->getBoolValue()) {
1108       _layer1->draw();
1109     } else {
1110       _layer2->draw();
1111     }
1112   }
1113 }
1114
1115 \f
1116 // end of panel.cxx
1117
1118
1119