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