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