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