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/scene/model/model.hxx>
58 #include <Main/globals.hxx>
59 #include <Main/fg_props.hxx>
60 #include <Viewer/viewmgr.hxx>
61 #include <Viewer/viewer.hxx>
62 #include <Time/light.hxx>
63 #include <GUI/FGFontCache.hxx>
64 #include <Instrumentation/dclgps.hxx>
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.
76 const double MOUSE_ACTION_REPEAT_DELAY = 0.5; // 500msec initial delay
77 const double MOUSE_ACTION_REPEAT_INTERVAL = 0.1; // 10Hz repeat rate
81 ////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////
87 * Calculate the aspect adjustment for the panel.
90 get_aspect_adjust (int xsize, int ysize)
92 float ideal_aspect = float(WIN_W) / float(WIN_H);
93 float real_aspect = float(xsize) / float(ysize);
94 return (real_aspect / ideal_aspect);
97 ////////////////////////////////////////////////////////////////////////
98 // Implementation of FGTextureManager.
99 ////////////////////////////////////////////////////////////////////////
101 map<string,osg::ref_ptr<osg::Texture2D> > FGTextureManager::_textureMap;
104 FGTextureManager::createTexture (const string &relativePath, bool staticTexture)
106 osg::Texture2D* texture = _textureMap[relativePath].get();
108 SGPath tpath = globals->resolve_aircraft_path(relativePath);
109 texture = SGLoadTexture2D(staticTexture, tpath);
111 _textureMap[relativePath] = texture;
112 if (!_textureMap[relativePath].valid())
113 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
114 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath );
121 void FGTextureManager::addTexture(const string &relativePath,
122 osg::Texture2D* texture)
124 _textureMap[relativePath] = texture;
127 ////////////////////////////////////////////////////////////////////////
128 // Implementation of FGCropped Texture.
129 ////////////////////////////////////////////////////////////////////////
132 FGCroppedTexture::FGCroppedTexture ()
133 : _path(""), _texture(0),
134 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
139 FGCroppedTexture::FGCroppedTexture (const string &path,
140 float minX, float minY,
141 float maxX, float maxY)
142 : _path(path), _texture(0),
143 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
148 FGCroppedTexture::~FGCroppedTexture ()
154 FGCroppedTexture::getTexture ()
157 _texture = new osg::StateSet;
158 _texture->setTextureAttribute(0, FGTextureManager::createTexture(_path));
159 _texture->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
160 _texture->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
162 return _texture.get();
167 ////////////////////////////////////////////////////////////////////////
168 // Implementation of FGPanel.
169 ////////////////////////////////////////////////////////////////////////
171 static fntRenderer text_renderer;
172 static sgVec4 panel_color;
173 static sgVec4 emissive_panel_color = {1,1,1,1};
181 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
182 _x_offset(fgGetNode("/sim/panel/x-offset", true)),
183 _y_offset(fgGetNode("/sim/panel/y-offset", true)),
184 _jitter(fgGetNode("/sim/panel/jitter", true)),
185 _flipx(fgGetNode("/sim/panel/flip-x", true)),
186 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
187 _ysize_node(fgGetNode("/sim/startup/ysize", true)),
188 _enable_depth_test(false),
190 _drawPanelHotspots("/sim/panel-hotspots")
200 for (instrument_list_type::iterator it = _instruments.begin();
201 it != _instruments.end();
210 * Add an instrument to the panel.
213 FGPanel::addInstrument (FGPanelInstrument * instrument)
215 _instruments.push_back(instrument);
219 FGPanel::getAspectScale() const
221 // set corner-coordinates correctly
223 int xsize = _xsize_node->getIntValue();
224 int ysize = _ysize_node->getIntValue();
225 float aspect_adjust = get_aspect_adjust(xsize, ysize);
227 if (aspect_adjust < 1.0)
228 return ysize / (double) WIN_H;
230 return xsize /(double) WIN_W;
234 * Handle repeatable mouse events. Called from update() and from
235 * fgUpdate3DPanels(). This functionality needs to move into the
236 * input subsystem. Counting a tick every two frames is clumsy...
238 void FGPanel::updateMouseDelay(double dt)
244 _mouseActionRepeat -= dt;
245 while (_mouseActionRepeat < 0.0) {
246 _mouseActionRepeat += MOUSE_ACTION_REPEAT_INTERVAL;
247 _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
253 FGPanel::draw(osg::State& state)
256 // In 3D mode, it's possible that we are being drawn exactly on top
257 // of an existing polygon. Use an offset to prevent z-fighting. In
258 // 2D mode, this is a no-op.
259 static osg::ref_ptr<osg::StateSet> panelStateSet;
260 if (!panelStateSet.valid()) {
261 panelStateSet = new osg::StateSet;
262 panelStateSet->setAttributeAndModes(new osg::PolygonOffset(-1, -POFF_UNITS));
263 panelStateSet->setTextureAttribute(0, new osg::TexEnv);
265 // Draw the background
266 panelStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
267 panelStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
268 panelStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
269 panelStateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
271 osg::Material* material = new osg::Material;
272 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
273 material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
274 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
275 material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
276 material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
277 panelStateSet->setAttribute(material);
279 panelStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
280 panelStateSet->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK));
281 panelStateSet->setAttributeAndModes(new osg::Depth(osg::Depth::LEQUAL));
283 if ( _enable_depth_test )
284 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
286 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
287 state.pushStateSet(panelStateSet.get());
289 state.setActiveTextureUnit(0);
290 state.setClientActiveTextureUnit(0);
292 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
293 sgCopyVec4( panel_color, l->scene_diffuse().data());
294 if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
295 if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
296 if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
297 if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
299 glColor4fv( panel_color );
301 state.pushStateSet(_bg.get());
303 state.setActiveTextureUnit(0);
304 state.setClientActiveTextureUnit(0);
307 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
308 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
309 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
310 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
314 state.setActiveTextureUnit(0);
315 state.setClientActiveTextureUnit(0);
318 for (int i = 0; i < 4; i ++) {
319 // top row of textures...(1,3,5,7)
320 state.pushStateSet(_mbg[i*2].get());
322 state.setActiveTextureUnit(0);
323 state.setClientActiveTextureUnit(0);
326 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
327 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
328 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
329 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
333 state.setActiveTextureUnit(0);
334 state.setClientActiveTextureUnit(0);
336 // bottom row of textures...(2,4,6,8)
337 state.pushStateSet(_mbg[i*2+1].get());
339 state.setActiveTextureUnit(0);
340 state.setClientActiveTextureUnit(0);
343 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
344 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
345 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
346 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
350 state.setActiveTextureUnit(0);
351 state.setClientActiveTextureUnit(0);
356 // Draw the instruments.
357 // Syd Adams: added instrument clipping
358 instrument_list_type::const_iterator current = _instruments.begin();
359 instrument_list_type::const_iterator end = _instruments.end();
361 GLdouble blx[4]={1.0,0.0,0.0,0.0};
362 GLdouble bly[4]={0.0,1.0,0.0,0.0};
363 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
364 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
366 for ( ; current != end; current++) {
367 FGPanelInstrument * instr = *current;
369 glTranslated(instr->getXPos(), instr->getYPos(), 0);
371 int ix= instr->getWidth();
372 int iy= instr->getHeight();
374 glTranslated(-ix/2,-iy/2,0);
375 glClipPlane(GL_CLIP_PLANE0,blx);
376 glClipPlane(GL_CLIP_PLANE1,bly);
377 glEnable(GL_CLIP_PLANE0);
378 glEnable(GL_CLIP_PLANE1);
380 glTranslated(ix,iy,0);
381 glClipPlane(GL_CLIP_PLANE2,urx);
382 glClipPlane(GL_CLIP_PLANE3,ury);
383 glEnable(GL_CLIP_PLANE2);
384 glEnable(GL_CLIP_PLANE3);
391 glDisable(GL_CLIP_PLANE0);
392 glDisable(GL_CLIP_PLANE1);
393 glDisable(GL_CLIP_PLANE2);
394 glDisable(GL_CLIP_PLANE3);
398 state.setActiveTextureUnit(0);
399 state.setClientActiveTextureUnit(0);
402 // Draw yellow "hotspots" if directed to. This is a panel authoring
403 // feature; not intended to be high performance or to look good.
404 if ( _drawPanelHotspots ) {
405 static osg::ref_ptr<osg::StateSet> hotspotStateSet;
406 if (!hotspotStateSet.valid()) {
407 hotspotStateSet = new osg::StateSet;
408 hotspotStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
409 hotspotStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
412 state.pushStateSet(hotspotStateSet.get());
414 state.setActiveTextureUnit(0);
415 state.setClientActiveTextureUnit(0);
418 glPushAttrib(GL_ENABLE_BIT);
419 glDisable(GL_COLOR_MATERIAL);
422 for ( unsigned int i = 0; i < _instruments.size(); i++ )
423 _instruments[i]->drawHotspots(state);
428 getLogicalExtent(x0, y0, x1, y1);
430 glBegin(GL_LINE_LOOP);
442 state.setActiveTextureUnit(0);
443 state.setClientActiveTextureUnit(0);
449 * Set the panel's background texture.
452 FGPanel::setBackground (osg::Texture2D* texture)
454 osg::StateSet* stateSet = new osg::StateSet;
455 stateSet->setTextureAttribute(0, texture);
456 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
457 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
462 * Set the panel's multiple background textures.
465 FGPanel::setMultiBackground (osg::Texture2D* texture, int idx)
469 osg::StateSet* stateSet = new osg::StateSet;
470 stateSet->setTextureAttribute(0, texture);
471 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
472 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
473 _mbg[idx] = stateSet;
477 * Set the panel's x-offset.
480 FGPanel::setXOffset (int offset)
482 if (offset <= 0 && offset >= -_width + WIN_W)
483 _x_offset->setIntValue( offset );
488 * Set the panel's y-offset.
491 FGPanel::setYOffset (int offset)
493 if (offset <= 0 && offset >= -_height)
494 _y_offset->setIntValue( offset );
498 * Handle a mouse action in panel-local (not screen) coordinates.
499 * Used by the 3D panel code in Model/panelnode.cxx, in situations
500 * where the panel doesn't control its own screen location.
503 FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
505 // Note a released button and return
507 if (_mouseInstrument != 0)
508 _mouseInstrument->doMouseAction(_mouseButton, 1, _mouseX, _mouseY);
510 _mouseInstrument = 0;
514 // Search for a matching instrument.
515 for (int i = 0; i < (int)_instruments.size(); i++) {
516 FGPanelInstrument *inst = _instruments[i];
517 int ix = inst->getXPos();
518 int iy = inst->getYPos();
519 int iw = inst->getWidth() / 2;
520 int ih = inst->getHeight() / 2;
521 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
523 _mouseActionRepeat = MOUSE_ACTION_REPEAT_DELAY;
524 _mouseInstrument = inst;
525 _mouseButton = button;
528 // Always do the action once.
529 return _mouseInstrument->doMouseAction(_mouseButton, 0,
537 * Perform a mouse action.
540 FGPanel::doMouseAction (int button, int updown, int x, int y)
542 // FIXME: this same code appears in update()
543 int xsize = _xsize_node->getIntValue();
544 int ysize = _ysize_node->getIntValue();
545 float aspect_adjust = get_aspect_adjust(xsize, ysize);
547 // Scale for the real window size.
548 if (aspect_adjust < 1.0) {
549 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
550 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
552 x = int(((float)x / xsize) * WIN_W);
553 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
556 // Adjust for offsets.
557 x -= _x_offset->getIntValue();
558 y -= _y_offset->getIntValue();
560 // Having fixed up the coordinates, fall through to the local
561 // coordinate handler.
562 return doLocalMouseAction(button, updown, x, y);
565 void FGPanel::setDepthTest (bool enable) {
566 _enable_depth_test = enable;
574 x0(std::numeric_limits<int>::max()),
575 y0(std::numeric_limits<int>::max()),
576 x1(std::numeric_limits<int>::min()),
577 y1(std::numeric_limits<int>::min())
580 IntRect(int x, int y, int w, int h) :
581 x0(x), y0(y), x1(x + w), y1( y + h)
595 void extend(const IntRect& r)
597 x0 = std::min(x0, r.x0);
598 y0 = std::min(y0, r.y0);
599 x1 = std::max(x1, r.x1);
600 y1 = std::max(y1, r.y1);
606 void FGPanel::getLogicalExtent(int &x0, int& y0, int& x1, int &y1)
609 BOOST_FOREACH(FGPanelInstrument *inst, _instruments) {
610 inst->extendRect(result);
619 ////////////////////////////////////////////////////////////////////////.
620 // Implementation of FGPanelAction.
621 ////////////////////////////////////////////////////////////////////////
623 FGPanelAction::FGPanelAction ()
627 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h,
629 : _button(button), _x(x), _y(y), _w(w), _h(h), _repeatable(repeatable)
633 FGPanelAction::~FGPanelAction ()
635 for (unsigned int i = 0; i < 2; i++) {
636 for (unsigned int j = 0; j < _bindings[i].size(); j++)
637 delete _bindings[i][j];
642 FGPanelAction::addBinding (SGBinding * binding, int updown)
644 _bindings[updown].push_back(binding);
648 FGPanelAction::doAction (int updown)
651 if ((updown != _last_state) || (updown == 0 && _repeatable)) {
652 int nBindings = _bindings[updown].size();
653 for (int i = 0; i < nBindings; i++)
654 _bindings[updown][i]->fire();
656 _last_state = updown;
665 ////////////////////////////////////////////////////////////////////////
666 // Implementation of FGPanelTransformation.
667 ////////////////////////////////////////////////////////////////////////
669 FGPanelTransformation::FGPanelTransformation ()
674 FGPanelTransformation::~FGPanelTransformation ()
681 ////////////////////////////////////////////////////////////////////////
682 // Implementation of FGPanelInstrument.
683 ////////////////////////////////////////////////////////////////////////
686 FGPanelInstrument::FGPanelInstrument ()
692 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
698 FGPanelInstrument::~FGPanelInstrument ()
700 for (action_list_type::iterator it = _actions.begin();
701 it != _actions.end();
709 FGPanelInstrument::drawHotspots(osg::State& state)
711 for ( unsigned int i = 0; i < _actions.size(); i++ ) {
712 FGPanelAction* a = _actions[i];
713 float x1 = getXPos() + a->getX();
714 float x2 = x1 + a->getWidth();
715 float y1 = getYPos() + a->getY();
716 float y2 = y1 + a->getHeight();
718 glBegin(GL_LINE_LOOP);
728 FGPanelInstrument::setPosition (int x, int y)
735 FGPanelInstrument::setSize (int w, int h)
742 FGPanelInstrument::getXPos () const
748 FGPanelInstrument::getYPos () const
754 FGPanelInstrument::getWidth () const
760 FGPanelInstrument::getHeight () const
766 FGPanelInstrument::extendRect(IntRect& r) const
768 IntRect instRect(_x, _y, _w, _h);
771 BOOST_FOREACH(FGPanelAction* act, _actions) {
772 r.extend(IntRect(getXPos() + act->getX(),
773 getYPos() + act->getY(),
781 FGPanelInstrument::addAction (FGPanelAction * action)
783 _actions.push_back(action);
786 // Coordinates relative to centre.
788 FGPanelInstrument::doMouseAction (int button, int updown, int x, int y)
791 action_list_type::iterator it = _actions.begin();
792 action_list_type::iterator last = _actions.end();
793 for ( ; it != last; it++) {
794 if ((*it)->inArea(button, x, y) &&
795 (*it)->doAction(updown))
804 ////////////////////////////////////////////////////////////////////////
805 // Implementation of FGLayeredInstrument.
806 ////////////////////////////////////////////////////////////////////////
808 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
809 : FGPanelInstrument(x, y, w, h)
813 FGLayeredInstrument::~FGLayeredInstrument ()
815 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
822 FGLayeredInstrument::draw (osg::State& state)
827 for (int i = 0; i < (int)_layers.size(); i++) {
829 _layers[i]->draw(state);
835 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
837 int n = _layers.size();
838 if (layer->getWidth() == -1) {
839 layer->setWidth(getWidth());
841 if (layer->getHeight() == -1) {
842 layer->setHeight(getHeight());
844 _layers.push_back(layer);
849 FGLayeredInstrument::addLayer (const FGCroppedTexture &texture,
852 return addLayer(new FGTexturedLayer(texture, w, h));
856 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
858 int layer = _layers.size() - 1;
859 _layers[layer]->addTransformation(transformation);
864 ////////////////////////////////////////////////////////////////////////
865 // Implementation of FGSpecialInstrument.
866 ////////////////////////////////////////////////////////////////////////
868 FGSpecialInstrument::FGSpecialInstrument (DCLGPS* sb)
869 : FGPanelInstrument()
874 FGSpecialInstrument::~FGSpecialInstrument ()
879 FGSpecialInstrument::draw (osg::State& state)
881 complex->draw(state);
886 ////////////////////////////////////////////////////////////////////////
887 // Implementation of FGInstrumentLayer.
888 ////////////////////////////////////////////////////////////////////////
890 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
896 FGInstrumentLayer::~FGInstrumentLayer ()
898 for (transformation_list::iterator it = _transformations.begin();
899 it != _transformations.end();
907 FGInstrumentLayer::transform () const
909 transformation_list::const_iterator it = _transformations.begin();
910 transformation_list::const_iterator last = _transformations.end();
912 FGPanelTransformation *t = *it;
914 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
917 val = fmod(val, t->mod);
920 } else if (val > t->max) {
925 val = val * t->factor + t->offset;
927 val = t->table->interpolate(val) * t->factor + t->offset;
931 case FGPanelTransformation::XSHIFT:
932 glTranslatef(val, 0.0, 0.0);
934 case FGPanelTransformation::YSHIFT:
935 glTranslatef(0.0, val, 0.0);
937 case FGPanelTransformation::ROTATION:
938 glRotatef(-val, 0.0, 0.0, 1.0);
947 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
949 _transformations.push_back(transformation);
954 ////////////////////////////////////////////////////////////////////////
955 // Implementation of FGGroupLayer.
956 ////////////////////////////////////////////////////////////////////////
958 FGGroupLayer::FGGroupLayer ()
962 FGGroupLayer::~FGGroupLayer ()
964 for (unsigned int i = 0; i < _layers.size(); i++)
969 FGGroupLayer::draw (osg::State& state)
973 int nLayers = _layers.size();
974 for (int i = 0; i < nLayers; i++)
975 _layers[i]->draw(state);
980 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
982 _layers.push_back(layer);
987 ////////////////////////////////////////////////////////////////////////
988 // Implementation of FGTexturedLayer.
989 ////////////////////////////////////////////////////////////////////////
992 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
993 : FGInstrumentLayer(w, h),
1000 FGTexturedLayer::~FGTexturedLayer ()
1006 FGTexturedLayer::draw (osg::State& state)
1013 state.pushStateSet(_texture.getTexture());
1015 state.setActiveTextureUnit(0);
1016 state.setClientActiveTextureUnit(0);
1018 glBegin(GL_POLYGON);
1021 glColor4fv( emissive_panel_color );
1023 // From Curt: turn on the panel
1024 // lights after sundown.
1025 glColor4fv( panel_color );
1028 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
1029 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
1030 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
1031 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
1033 state.popStateSet();
1035 state.setActiveTextureUnit(0);
1036 state.setClientActiveTextureUnit(0);
1043 ////////////////////////////////////////////////////////////////////////
1044 // Implementation of FGTextLayer.
1045 ////////////////////////////////////////////////////////////////////////
1047 FGTextLayer::FGTextLayer (int w, int h)
1048 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
1051 _color[0] = _color[1] = _color[2] = 0.0;
1055 FGTextLayer::~FGTextLayer ()
1057 chunk_list::iterator it = _chunks.begin();
1058 chunk_list::iterator last = _chunks.end();
1059 for ( ; it != last; it++) {
1065 FGTextLayer::draw (osg::State& state)
1071 FGFontCache *fc = globals->get_fontcache();
1072 fntFont* font = fc->getTexFont(_font_name.c_str());
1074 return; // don't crash on missing fonts
1077 text_renderer.setFont(font);
1079 text_renderer.setPointSize(_pointSize);
1080 text_renderer.begin();
1081 text_renderer.start3f(0, 0, 0);
1084 double diff = (_now - _then).toUSecs();
1086 if (diff > 100000 || diff < 0 ) {
1087 // ( diff < 0 ) is a sanity check and indicates our time stamp
1088 // difference math probably overflowed. We can handle a max
1089 // difference of 35.8 minutes since the returned value is in
1090 // usec. So if the panel is left off longer than that we can
1091 // over flow the math with it is turned back on. This (diff <
1092 // 0) catches that situation, get's us out of trouble, and
1098 // Something is goofy. The code in this file renders only CCW
1099 // polygons, and I have verified that the font code in plib
1100 // renders only CCW trianbles. Yet they come out backwards.
1101 // Something around here or in plib is either changing the winding
1102 // order or (more likely) pushing a left-handed matrix onto the
1103 // stack. But I can't find it; get out the chainsaw...
1105 text_renderer.puts((char *)(_value.c_str()));
1106 glFrontFace(GL_CCW);
1108 text_renderer.end();
1109 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
1114 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1116 _chunks.push_back(chunk);
1120 FGTextLayer::setColor (float r, float g, float b)
1129 FGTextLayer::setPointSize (float size)
1135 FGTextLayer::setFontName(const string &name)
1137 _font_name = name + ".txf";
1138 FGFontCache *fc = globals->get_fontcache();
1139 fntFont* font = fc->getTexFont(_font_name.c_str());
1141 SG_LOG(SG_COCKPIT, SG_WARN, "unable to find font:" << name);
1147 FGTextLayer::setFont(fntFont * font)
1149 text_renderer.setFont(font);
1154 FGTextLayer::recalc_value () const
1157 chunk_list::const_iterator it = _chunks.begin();
1158 chunk_list::const_iterator last = _chunks.end();
1159 for ( ; it != last; it++) {
1160 _value += (*it)->getValue();
1166 ////////////////////////////////////////////////////////////////////////
1167 // Implementation of FGTextLayer::Chunk.
1168 ////////////////////////////////////////////////////////////////////////
1170 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1171 : _type(FGTextLayer::TEXT), _fmt(fmt)
1178 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1179 const string &fmt, float mult, float offs,
1181 : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
1184 if (type == TEXT_VALUE)
1193 FGTextLayer::Chunk::getValue () const
1199 sprintf(_buf, _fmt.c_str(), _text.c_str());
1202 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1205 double d = _offs + _node->getFloatValue() * _mult;
1206 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
1207 sprintf(_buf, _fmt.c_str(), d);
1218 ////////////////////////////////////////////////////////////////////////
1219 // Implementation of FGSwitchLayer.
1220 ////////////////////////////////////////////////////////////////////////
1222 FGSwitchLayer::FGSwitchLayer ()
1228 FGSwitchLayer::draw (osg::State& state)
1232 int nLayers = _layers.size();
1233 for (int i = 0; i < nLayers; i++) {
1234 if (_layers[i]->test()) {
1235 _layers[i]->draw(state);