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