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