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