1 // panel.cxx - default, 2D single-engine prop instrument panel
3 // Written by David Megginson, started January 2000.
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.
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.
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.
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
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.
36 #include <stdio.h> // sprintf
41 #include <osg/CullFace>
43 #include <osg/Material>
44 #include <osg/Matrixf>
46 #include <osg/PolygonOffset>
48 #include <simgear/compiler.h>
52 #include <boost/foreach.hpp>
53 #include <simgear/debug/logstream.hxx>
54 #include <simgear/misc/sg_path.hxx>
55 #include <simgear/misc/strutils.hxx>
56 #include <simgear/scene/model/model.hxx>
59 #include <Main/globals.hxx>
60 #include <Main/fg_props.hxx>
61 #include <Viewer/viewmgr.hxx>
62 #include <Viewer/viewer.hxx>
63 #include <Time/light.hxx>
64 #include <GUI/FGFontCache.hxx>
65 #include <Instrumentation/dclgps.hxx>
72 // The number of polygon-offset "units" to place between layers. In
73 // principle, one is supposed to be enough. In practice, I find that
74 // my hardware/driver requires many more.
77 const double MOUSE_ACTION_REPEAT_DELAY = 0.5; // 500msec initial delay
78 const double MOUSE_ACTION_REPEAT_INTERVAL = 0.1; // 10Hz repeat rate
82 ////////////////////////////////////////////////////////////////////////
84 ////////////////////////////////////////////////////////////////////////
88 * Calculate the aspect adjustment for the panel.
91 get_aspect_adjust (int xsize, int ysize)
93 float ideal_aspect = float(WIN_W) / float(WIN_H);
94 float real_aspect = float(xsize) / float(ysize);
95 return (real_aspect / ideal_aspect);
98 ////////////////////////////////////////////////////////////////////////
99 // Implementation of FGTextureManager.
100 ////////////////////////////////////////////////////////////////////////
102 map<std::string,osg::ref_ptr<osg::Texture2D> > FGTextureManager::_textureMap;
105 FGTextureManager::createTexture (const std::string &relativePath, bool staticTexture)
107 osg::Texture2D* texture = _textureMap[relativePath].get();
109 SGPath tpath = globals->resolve_aircraft_path(relativePath);
110 texture = SGLoadTexture2D(staticTexture, tpath);
112 _textureMap[relativePath] = texture;
113 if (!_textureMap[relativePath].valid())
114 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
115 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath );
122 void FGTextureManager::addTexture(const std::string &relativePath,
123 osg::Texture2D* texture)
125 _textureMap[relativePath] = texture;
128 ////////////////////////////////////////////////////////////////////////
129 // Implementation of FGCropped Texture.
130 ////////////////////////////////////////////////////////////////////////
133 FGCroppedTexture::FGCroppedTexture ()
134 : _path(""), _texture(0),
135 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
140 FGCroppedTexture::FGCroppedTexture (const std::string &path,
141 float minX, float minY,
142 float maxX, float maxY)
143 : _path(path), _texture(0),
144 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
149 FGCroppedTexture::~FGCroppedTexture ()
155 FGCroppedTexture::getTexture ()
158 _texture = new osg::StateSet;
159 _texture->setTextureAttribute(0, FGTextureManager::createTexture(_path));
160 _texture->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
161 _texture->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
163 return _texture.get();
168 ////////////////////////////////////////////////////////////////////////
169 // Implementation of FGPanel.
170 ////////////////////////////////////////////////////////////////////////
172 static fntRenderer text_renderer;
173 static sgVec4 panel_color;
174 static sgVec4 emissive_panel_color = {1,1,1,1};
182 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
183 _x_offset(fgGetNode("/sim/panel/x-offset", true)),
184 _y_offset(fgGetNode("/sim/panel/y-offset", true)),
185 _jitter(fgGetNode("/sim/panel/jitter", true)),
186 _flipx(fgGetNode("/sim/panel/flip-x", true)),
187 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
188 _ysize_node(fgGetNode("/sim/startup/ysize", true)),
189 _enable_depth_test(false),
191 _drawPanelHotspots("/sim/panel-hotspots")
201 for (instrument_list_type::iterator it = _instruments.begin();
202 it != _instruments.end();
211 * Add an instrument to the panel.
214 FGPanel::addInstrument (FGPanelInstrument * instrument)
216 _instruments.push_back(instrument);
220 FGPanel::getAspectScale() const
222 // set corner-coordinates correctly
224 int xsize = _xsize_node->getIntValue();
225 int ysize = _ysize_node->getIntValue();
226 float aspect_adjust = get_aspect_adjust(xsize, ysize);
228 if (aspect_adjust < 1.0)
229 return ysize / (double) WIN_H;
231 return xsize /(double) WIN_W;
235 * Handle repeatable mouse events. Called from update() and from
236 * fgUpdate3DPanels(). This functionality needs to move into the
237 * input subsystem. Counting a tick every two frames is clumsy...
239 void FGPanel::updateMouseDelay(double dt)
245 _mouseActionRepeat -= dt;
246 while (_mouseActionRepeat < 0.0) {
247 _mouseActionRepeat += MOUSE_ACTION_REPEAT_INTERVAL;
248 _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
254 FGPanel::draw(osg::State& state)
257 // In 3D mode, it's possible that we are being drawn exactly on top
258 // of an existing polygon. Use an offset to prevent z-fighting. In
259 // 2D mode, this is a no-op.
260 static osg::ref_ptr<osg::StateSet> panelStateSet;
261 if (!panelStateSet.valid()) {
262 panelStateSet = new osg::StateSet;
263 panelStateSet->setAttributeAndModes(new osg::PolygonOffset(-1, -POFF_UNITS));
264 panelStateSet->setTextureAttribute(0, new osg::TexEnv);
266 // Draw the background
267 panelStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
268 panelStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
269 panelStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
270 panelStateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
272 osg::Material* material = new osg::Material;
273 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
274 material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
275 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
276 material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
277 material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
278 panelStateSet->setAttribute(material);
280 panelStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
281 panelStateSet->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK));
282 panelStateSet->setAttributeAndModes(new osg::Depth(osg::Depth::LEQUAL));
284 if ( _enable_depth_test )
285 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
287 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
288 state.pushStateSet(panelStateSet.get());
290 state.setActiveTextureUnit(0);
291 state.setClientActiveTextureUnit(0);
293 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
294 sgCopyVec4( panel_color, l->scene_diffuse().data());
295 if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
296 if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
297 if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
298 if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
300 glColor4fv( panel_color );
302 state.pushStateSet(_bg.get());
304 state.setActiveTextureUnit(0);
305 state.setClientActiveTextureUnit(0);
308 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
309 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
310 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
311 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
315 state.setActiveTextureUnit(0);
316 state.setClientActiveTextureUnit(0);
319 for (int i = 0; i < 4; i ++) {
320 // top row of textures...(1,3,5,7)
321 state.pushStateSet(_mbg[i*2].get());
323 state.setActiveTextureUnit(0);
324 state.setClientActiveTextureUnit(0);
327 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
328 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
329 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
330 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
334 state.setActiveTextureUnit(0);
335 state.setClientActiveTextureUnit(0);
337 // bottom row of textures...(2,4,6,8)
338 state.pushStateSet(_mbg[i*2+1].get());
340 state.setActiveTextureUnit(0);
341 state.setClientActiveTextureUnit(0);
344 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
345 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
346 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
347 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
351 state.setActiveTextureUnit(0);
352 state.setClientActiveTextureUnit(0);
357 // Draw the instruments.
358 // Syd Adams: added instrument clipping
359 instrument_list_type::const_iterator current = _instruments.begin();
360 instrument_list_type::const_iterator end = _instruments.end();
362 GLdouble blx[4]={1.0,0.0,0.0,0.0};
363 GLdouble bly[4]={0.0,1.0,0.0,0.0};
364 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
365 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
367 for ( ; current != end; current++) {
368 FGPanelInstrument * instr = *current;
370 glTranslated(instr->getXPos(), instr->getYPos(), 0);
372 int ix= instr->getWidth();
373 int iy= instr->getHeight();
375 glTranslated(-ix/2,-iy/2,0);
376 glClipPlane(GL_CLIP_PLANE0,blx);
377 glClipPlane(GL_CLIP_PLANE1,bly);
378 glEnable(GL_CLIP_PLANE0);
379 glEnable(GL_CLIP_PLANE1);
381 glTranslated(ix,iy,0);
382 glClipPlane(GL_CLIP_PLANE2,urx);
383 glClipPlane(GL_CLIP_PLANE3,ury);
384 glEnable(GL_CLIP_PLANE2);
385 glEnable(GL_CLIP_PLANE3);
392 glDisable(GL_CLIP_PLANE0);
393 glDisable(GL_CLIP_PLANE1);
394 glDisable(GL_CLIP_PLANE2);
395 glDisable(GL_CLIP_PLANE3);
399 state.setActiveTextureUnit(0);
400 state.setClientActiveTextureUnit(0);
403 // Draw yellow "hotspots" if directed to. This is a panel authoring
404 // feature; not intended to be high performance or to look good.
405 if ( _drawPanelHotspots ) {
406 static osg::ref_ptr<osg::StateSet> hotspotStateSet;
407 if (!hotspotStateSet.valid()) {
408 hotspotStateSet = new osg::StateSet;
409 hotspotStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
410 hotspotStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
413 state.pushStateSet(hotspotStateSet.get());
415 state.setActiveTextureUnit(0);
416 state.setClientActiveTextureUnit(0);
419 glPushAttrib(GL_ENABLE_BIT);
420 glDisable(GL_COLOR_MATERIAL);
423 for ( unsigned int i = 0; i < _instruments.size(); i++ )
424 _instruments[i]->drawHotspots(state);
426 // disable drawing of panel extents for the 2.8 release, since it
427 // confused use of hot-spot drawing in tutorials.
428 #ifdef DRAW_PANEL_EXTENTS
432 getLogicalExtent(x0, y0, x1, y1);
434 glBegin(GL_LINE_LOOP);
446 state.setActiveTextureUnit(0);
447 state.setClientActiveTextureUnit(0);
453 * Set the panel's background texture.
456 FGPanel::setBackground (osg::Texture2D* texture)
458 osg::StateSet* stateSet = new osg::StateSet;
459 stateSet->setTextureAttribute(0, texture);
460 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
461 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
466 * Set the panel's multiple background textures.
469 FGPanel::setMultiBackground (osg::Texture2D* texture, int idx)
473 osg::StateSet* stateSet = new osg::StateSet;
474 stateSet->setTextureAttribute(0, texture);
475 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
476 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
477 _mbg[idx] = stateSet;
481 * Set the panel's x-offset.
484 FGPanel::setXOffset (int offset)
486 if (offset <= 0 && offset >= -_width + WIN_W)
487 _x_offset->setIntValue( offset );
492 * Set the panel's y-offset.
495 FGPanel::setYOffset (int offset)
497 if (offset <= 0 && offset >= -_height)
498 _y_offset->setIntValue( offset );
502 * Handle a mouse action in panel-local (not screen) coordinates.
503 * Used by the 3D panel code in Model/panelnode.cxx, in situations
504 * where the panel doesn't control its own screen location.
507 FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
509 // Note a released button and return
511 if (_mouseInstrument != 0)
512 _mouseInstrument->doMouseAction(_mouseButton, 1, _mouseX, _mouseY);
514 _mouseInstrument = 0;
518 // Search for a matching instrument.
519 for (int i = 0; i < (int)_instruments.size(); i++) {
520 FGPanelInstrument *inst = _instruments[i];
521 int ix = inst->getXPos();
522 int iy = inst->getYPos();
523 int iw = inst->getWidth() / 2;
524 int ih = inst->getHeight() / 2;
525 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
527 _mouseActionRepeat = MOUSE_ACTION_REPEAT_DELAY;
528 _mouseInstrument = inst;
529 _mouseButton = button;
532 // Always do the action once.
533 return _mouseInstrument->doMouseAction(_mouseButton, 0,
541 * Perform a mouse action.
544 FGPanel::doMouseAction (int button, int updown, int x, int y)
546 // FIXME: this same code appears in update()
547 int xsize = _xsize_node->getIntValue();
548 int ysize = _ysize_node->getIntValue();
549 float aspect_adjust = get_aspect_adjust(xsize, ysize);
551 // Scale for the real window size.
552 if (aspect_adjust < 1.0) {
553 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
554 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
556 x = int(((float)x / xsize) * WIN_W);
557 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
560 // Adjust for offsets.
561 x -= _x_offset->getIntValue();
562 y -= _y_offset->getIntValue();
564 // Having fixed up the coordinates, fall through to the local
565 // coordinate handler.
566 return doLocalMouseAction(button, updown, x, y);
569 void FGPanel::setDepthTest (bool enable) {
570 _enable_depth_test = enable;
578 x0(std::numeric_limits<int>::max()),
579 y0(std::numeric_limits<int>::max()),
580 x1(std::numeric_limits<int>::min()),
581 y1(std::numeric_limits<int>::min())
584 IntRect(int x, int y, int w, int h) :
585 x0(x), y0(y), x1(x + w), y1( y + h)
599 void extend(const IntRect& r)
601 x0 = std::min(x0, r.x0);
602 y0 = std::min(y0, r.y0);
603 x1 = std::max(x1, r.x1);
604 y1 = std::max(y1, r.y1);
610 void FGPanel::getLogicalExtent(int &x0, int& y0, int& x1, int &y1)
613 BOOST_FOREACH(FGPanelInstrument *inst, _instruments) {
614 inst->extendRect(result);
623 ////////////////////////////////////////////////////////////////////////.
624 // Implementation of FGPanelAction.
625 ////////////////////////////////////////////////////////////////////////
627 FGPanelAction::FGPanelAction ()
631 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h,
633 : _button(button), _x(x), _y(y), _w(w), _h(h), _repeatable(repeatable)
637 FGPanelAction::~FGPanelAction ()
639 for (unsigned int i = 0; i < 2; i++) {
640 for (unsigned int j = 0; j < _bindings[i].size(); j++)
641 delete _bindings[i][j];
646 FGPanelAction::addBinding (SGBinding * binding, int updown)
648 _bindings[updown].push_back(binding);
652 FGPanelAction::doAction (int updown)
655 if ((updown != _last_state) || (updown == 0 && _repeatable)) {
656 int nBindings = _bindings[updown].size();
657 for (int i = 0; i < nBindings; i++)
658 _bindings[updown][i]->fire();
660 _last_state = updown;
669 ////////////////////////////////////////////////////////////////////////
670 // Implementation of FGPanelTransformation.
671 ////////////////////////////////////////////////////////////////////////
673 FGPanelTransformation::FGPanelTransformation ()
678 FGPanelTransformation::~FGPanelTransformation ()
685 ////////////////////////////////////////////////////////////////////////
686 // Implementation of FGPanelInstrument.
687 ////////////////////////////////////////////////////////////////////////
690 FGPanelInstrument::FGPanelInstrument ()
696 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
702 FGPanelInstrument::~FGPanelInstrument ()
704 for (action_list_type::iterator it = _actions.begin();
705 it != _actions.end();
713 FGPanelInstrument::drawHotspots(osg::State& state)
715 for ( unsigned int i = 0; i < _actions.size(); i++ ) {
716 FGPanelAction* a = _actions[i];
717 float x1 = getXPos() + a->getX();
718 float x2 = x1 + a->getWidth();
719 float y1 = getYPos() + a->getY();
720 float y2 = y1 + a->getHeight();
722 glBegin(GL_LINE_LOOP);
732 FGPanelInstrument::setPosition (int x, int y)
739 FGPanelInstrument::setSize (int w, int h)
746 FGPanelInstrument::getXPos () const
752 FGPanelInstrument::getYPos () const
758 FGPanelInstrument::getWidth () const
764 FGPanelInstrument::getHeight () const
770 FGPanelInstrument::extendRect(IntRect& r) const
772 IntRect instRect(_x, _y, _w, _h);
775 BOOST_FOREACH(FGPanelAction* act, _actions) {
776 r.extend(IntRect(getXPos() + act->getX(),
777 getYPos() + act->getY(),
785 FGPanelInstrument::addAction (FGPanelAction * action)
787 _actions.push_back(action);
790 // Coordinates relative to centre.
792 FGPanelInstrument::doMouseAction (int button, int updown, int x, int y)
795 action_list_type::iterator it = _actions.begin();
796 action_list_type::iterator last = _actions.end();
797 for ( ; it != last; it++) {
798 if ((*it)->inArea(button, x, y) &&
799 (*it)->doAction(updown))
808 ////////////////////////////////////////////////////////////////////////
809 // Implementation of FGLayeredInstrument.
810 ////////////////////////////////////////////////////////////////////////
812 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
813 : FGPanelInstrument(x, y, w, h)
817 FGLayeredInstrument::~FGLayeredInstrument ()
819 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
826 FGLayeredInstrument::draw (osg::State& state)
831 for (int i = 0; i < (int)_layers.size(); i++) {
833 _layers[i]->draw(state);
839 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
841 int n = _layers.size();
842 if (layer->getWidth() == -1) {
843 layer->setWidth(getWidth());
845 if (layer->getHeight() == -1) {
846 layer->setHeight(getHeight());
848 _layers.push_back(layer);
853 FGLayeredInstrument::addLayer (const FGCroppedTexture &texture,
856 return addLayer(new FGTexturedLayer(texture, w, h));
860 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
862 int layer = _layers.size() - 1;
863 _layers[layer]->addTransformation(transformation);
868 ////////////////////////////////////////////////////////////////////////
869 // Implementation of FGSpecialInstrument.
870 ////////////////////////////////////////////////////////////////////////
872 FGSpecialInstrument::FGSpecialInstrument (DCLGPS* sb)
873 : FGPanelInstrument()
878 FGSpecialInstrument::~FGSpecialInstrument ()
883 FGSpecialInstrument::draw (osg::State& state)
885 complex->draw(state);
890 ////////////////////////////////////////////////////////////////////////
891 // Implementation of FGInstrumentLayer.
892 ////////////////////////////////////////////////////////////////////////
894 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
900 FGInstrumentLayer::~FGInstrumentLayer ()
902 for (transformation_list::iterator it = _transformations.begin();
903 it != _transformations.end();
911 FGInstrumentLayer::transform () const
913 transformation_list::const_iterator it = _transformations.begin();
914 transformation_list::const_iterator last = _transformations.end();
916 FGPanelTransformation *t = *it;
918 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
921 val = fmod(val, t->mod);
924 } else if (val > t->max) {
929 val = val * t->factor + t->offset;
931 val = t->table->interpolate(val) * t->factor + t->offset;
935 case FGPanelTransformation::XSHIFT:
936 glTranslatef(val, 0.0, 0.0);
938 case FGPanelTransformation::YSHIFT:
939 glTranslatef(0.0, val, 0.0);
941 case FGPanelTransformation::ROTATION:
942 glRotatef(-val, 0.0, 0.0, 1.0);
951 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
953 _transformations.push_back(transformation);
958 ////////////////////////////////////////////////////////////////////////
959 // Implementation of FGGroupLayer.
960 ////////////////////////////////////////////////////////////////////////
962 FGGroupLayer::FGGroupLayer ()
966 FGGroupLayer::~FGGroupLayer ()
968 for (unsigned int i = 0; i < _layers.size(); i++)
973 FGGroupLayer::draw (osg::State& state)
977 int nLayers = _layers.size();
978 for (int i = 0; i < nLayers; i++)
979 _layers[i]->draw(state);
984 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
986 _layers.push_back(layer);
991 ////////////////////////////////////////////////////////////////////////
992 // Implementation of FGTexturedLayer.
993 ////////////////////////////////////////////////////////////////////////
996 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
997 : FGInstrumentLayer(w, h),
1000 setTexture(texture);
1004 FGTexturedLayer::~FGTexturedLayer ()
1010 FGTexturedLayer::draw (osg::State& state)
1017 state.pushStateSet(_texture.getTexture());
1019 state.setActiveTextureUnit(0);
1020 state.setClientActiveTextureUnit(0);
1022 glBegin(GL_POLYGON);
1025 glColor4fv( emissive_panel_color );
1027 // From Curt: turn on the panel
1028 // lights after sundown.
1029 glColor4fv( panel_color );
1032 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
1033 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
1034 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
1035 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
1037 state.popStateSet();
1039 state.setActiveTextureUnit(0);
1040 state.setClientActiveTextureUnit(0);
1047 ////////////////////////////////////////////////////////////////////////
1048 // Implementation of FGTextLayer.
1049 ////////////////////////////////////////////////////////////////////////
1051 FGTextLayer::FGTextLayer (int w, int h)
1052 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
1055 _color[0] = _color[1] = _color[2] = 0.0;
1059 FGTextLayer::~FGTextLayer ()
1061 chunk_list::iterator it = _chunks.begin();
1062 chunk_list::iterator last = _chunks.end();
1063 for ( ; it != last; it++) {
1069 FGTextLayer::draw (osg::State& state)
1075 FGFontCache *fc = globals->get_fontcache();
1076 fntFont* font = fc->getTexFont(_font_name.c_str());
1078 return; // don't crash on missing fonts
1081 text_renderer.setFont(font);
1083 text_renderer.setPointSize(_pointSize);
1084 text_renderer.begin();
1085 text_renderer.start3f(0, 0, 0);
1088 double diff = (_now - _then).toUSecs();
1090 if (diff > 100000 || diff < 0 ) {
1091 // ( diff < 0 ) is a sanity check and indicates our time stamp
1092 // difference math probably overflowed. We can handle a max
1093 // difference of 35.8 minutes since the returned value is in
1094 // usec. So if the panel is left off longer than that we can
1095 // over flow the math with it is turned back on. This (diff <
1096 // 0) catches that situation, get's us out of trouble, and
1102 // Something is goofy. The code in this file renders only CCW
1103 // polygons, and I have verified that the font code in plib
1104 // renders only CCW trianbles. Yet they come out backwards.
1105 // Something around here or in plib is either changing the winding
1106 // order or (more likely) pushing a left-handed matrix onto the
1107 // stack. But I can't find it; get out the chainsaw...
1109 text_renderer.puts((char *)(_value.c_str()));
1110 glFrontFace(GL_CCW);
1112 text_renderer.end();
1113 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
1118 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1120 _chunks.push_back(chunk);
1124 FGTextLayer::setColor (float r, float g, float b)
1133 FGTextLayer::setPointSize (float size)
1139 FGTextLayer::setFontName(const std::string &name)
1141 _font_name = name + ".txf";
1142 FGFontCache *fc = globals->get_fontcache();
1143 fntFont* font = fc->getTexFont(_font_name.c_str());
1145 SG_LOG(SG_COCKPIT, SG_WARN, "unable to find font:" << name);
1151 FGTextLayer::setFont(fntFont * font)
1153 text_renderer.setFont(font);
1158 FGTextLayer::recalc_value () const
1161 chunk_list::const_iterator it = _chunks.begin();
1162 chunk_list::const_iterator last = _chunks.end();
1163 for ( ; it != last; it++) {
1164 _value += (*it)->getValue();
1170 ////////////////////////////////////////////////////////////////////////
1171 // Implementation of FGTextLayer::Chunk.
1172 ////////////////////////////////////////////////////////////////////////
1174 FGTextLayer::Chunk::Chunk (const std::string &text, const std::string &fmt)
1175 : _type(FGTextLayer::TEXT),
1176 _fmt(simgear::strutils::sanitizePrintfFormat(fmt))
1183 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1184 const std::string &fmt, float mult, float offs,
1187 _fmt(simgear::strutils::sanitizePrintfFormat(fmt)),
1193 if (type == TEXT_VALUE)
1202 FGTextLayer::Chunk::getValue () const
1208 sprintf(_buf, _fmt.c_str(), _text.c_str());
1211 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1214 double d = _offs + _node->getFloatValue() * _mult;
1215 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
1216 sprintf(_buf, _fmt.c_str(), d);
1227 ////////////////////////////////////////////////////////////////////////
1228 // Implementation of FGSwitchLayer.
1229 ////////////////////////////////////////////////////////////////////////
1231 FGSwitchLayer::FGSwitchLayer ()
1237 FGSwitchLayer::draw (osg::State& state)
1241 int nLayers = _layers.size();
1242 for (int i = 0; i < nLayers; i++) {
1243 if (_layers[i]->test()) {
1244 _layers[i]->draw(state);