5 #include "panelnode.hxx"
12 #include <osg/BlendFunc>
14 #include <simgear/compiler.h>
15 #include <simgear/structure/exception.hxx>
16 #include <simgear/debug/logstream.hxx>
18 #include <simgear/scene/util/OsgMath.hxx>
19 #include <simgear/scene/util/SGPickCallback.hxx>
20 #include <simgear/scene/util/SGSceneUserData.hxx>
21 #include <simgear/scene/util/SGNodeMasks.hxx>
25 #include <Main/fg_os.hxx>
26 #include <Cockpit/panel.hxx>
27 #include <Cockpit/panel_io.hxx>
28 #include "Viewer/viewer.hxx"
29 #include "Viewer/viewmgr.hxx"
33 class PanelTransformListener : public SGPropertyChangeListener
36 PanelTransformListener(FGPanelNode* pn) : _panelNode(pn) {}
38 virtual void valueChanged (SGPropertyNode * node)
40 _panelNode->dirtyBound();
43 FGPanelNode* _panelNode;
46 class PanelPathListener : public SGPropertyChangeListener
49 PanelPathListener(FGPanelNode* pn) : _panelNode(pn) {}
51 virtual void valueChanged (SGPropertyNode * node)
53 _panelNode->setPanelPath(node->getStringValue());
56 FGPanelNode* _panelNode;
59 class FGPanelPickCallback : public SGPickCallback {
61 FGPanelPickCallback(FGPanelNode* p) :
65 virtual bool buttonPressed( int b,
66 const osgGA::GUIEventAdapter&,
70 // convert to panel coordinates
71 osg::Matrixd m = osg::Matrixd::inverse(panel->transformMatrix());
72 picked = toOsg(info.local) * m;
73 SG_LOG( SG_INSTR, SG_DEBUG, "panel pick: " << toSG(picked) );
76 return panel->getPanel()->doLocalMouseAction(button, MOUSE_BUTTON_DOWN,
77 picked.x(), picked.y());
80 virtual void update(double dt, int keyModState)
82 panel->getPanel()->updateMouseDelay(dt);
85 virtual void buttonReleased( int,
86 const osgGA::GUIEventAdapter&,
89 panel->getPanel()->doLocalMouseAction(button, MOUSE_BUTTON_UP,
90 picked.x(), picked.y());
99 class FGPanelSwitchCallback : public osg::NodeCallback {
101 FGPanelSwitchCallback(FGPanelNode* pn) :
103 visProp(fgGetNode("/sim/panel/visibility"))
107 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
109 assert(dynamic_cast<osg::Switch*>(node));
110 osg::Switch* sw = static_cast<osg::Switch*>(node);
112 if (!visProp->getBoolValue()) {
113 sw->setValue(0, false);
118 panel->lazyLoad(); // isVisible check needs the panel loaded for auto-hide flag
119 bool enabled = panel->isVisible2d();
120 sw->setValue(0, enabled);
129 SGPropertyNode_ptr visProp;
133 FGPanelNode::FGPanelNode(SGPropertyNode* props) :
135 _resizeToViewport(false),
139 _panelPath = props->getStringValue("path");
141 // And the corner points
142 SGPropertyNode* pt = props->getChild("bottom-left");
143 _bottomLeft[0] = pt->getFloatValue("x-m");
144 _bottomLeft[1] = pt->getFloatValue("y-m");
145 _bottomLeft[2] = pt->getFloatValue("z-m");
147 pt = props->getChild("top-left");
148 _topLeft[0] = pt->getFloatValue("x-m");
149 _topLeft[1] = pt->getFloatValue("y-m");
150 _topLeft[2] = pt->getFloatValue("z-m");
152 pt = props->getChild("bottom-right");
153 _bottomRight[0] = pt->getFloatValue("x-m");
154 _bottomRight[1] = pt->getFloatValue("y-m");
155 _bottomRight[2] = pt->getFloatValue("z-m");
157 _depthTest = props->getBoolValue("depth-test");
160 FGPanelNode::FGPanelNode() :
162 _resizeToViewport(true),
165 SGCommandMgr::instance()->addCommand("panel-mouse-click", this, &FGPanelNode::panelMouseClickCommand);
167 SGPropertyNode* pathNode = fgGetNode("/sim/panel/path");
168 _pathListener.reset(new PanelPathListener(this));
169 pathNode->addChangeListener(_pathListener.get());
170 setPanelPath(pathNode->getStringValue());
172 // for a 2D panel, various options adjust the transformation
173 // matrix. We need to pass this data on to OSG or its bounding box
174 // will be stale, and picking will break.
175 // http://code.google.com/p/flightgear-bugs/issues/detail?id=864
176 _listener = new PanelTransformListener(this);
177 fgGetNode("/sim/panel/x-offset", true)->addChangeListener(_listener);
178 fgGetNode("/sim/panel/y-offset", true)->addChangeListener(_listener);
179 fgGetNode("/sim/startup/xsize", true)->addChangeListener(_listener);
180 fgGetNode("/sim/startup/ysize", true)->addChangeListener(_listener);
185 FGPanelNode::~FGPanelNode()
188 SGCommandMgr::instance()->removeCommand("panel-mouse-click");
189 SGPropertyNode* pathNode = fgGetNode("/sim/panel/path");
190 pathNode->removeChangeListener(_pathListener.get());
194 fgGetNode("/sim/panel/x-offset", true)->removeChangeListener(_listener);
195 fgGetNode("/sim/panel/y-offset", true)->removeChangeListener(_listener);
196 fgGetNode("/sim/startup/xsize", true)->removeChangeListener(_listener);
197 fgGetNode("/sim/startup/ysize", true)->removeChangeListener(_listener);
202 void FGPanelNode::setPanelPath(const std::string& panel)
204 if (panel == _panelPath) {
214 void FGPanelNode::lazyLoad()
216 if (!_panelPath.empty() && !_panel) {
217 _panel = fgReadPanel(_panelPath);
219 SG_LOG(SG_COCKPIT, SG_WARN, "failed to read panel from:" << _panelPath);
220 _panelPath = std::string(); // don't keep trying to read
224 _panel->setDepthTest(_depthTest);
229 void FGPanelNode::commonInit()
231 setUseDisplayList(false);
232 setDataVariance(Object::DYNAMIC);
233 getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
234 getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
237 void FGPanelNode::initWithPanel()
241 // Read out the pixel-space info
242 float panelWidth = _panel->getWidth();
243 float panelHeight = _panel->getHeight();
245 _panel->getLogicalExtent(_xmin, _ymin, _xmax, _ymax);
247 // Now generate our transformation matrix. For shorthand, use
248 // "a", "b", and "c" as our corners and "m" as the matrix. The
249 // vector u goes from a to b, v from a to c, and w is a
250 // perpendicular cross product.
251 osg::Vec3 a = _bottomLeft;
252 osg::Vec3 b = _bottomRight;
253 osg::Vec3 c = _topLeft;
258 osg::Matrix& m = _xform;
259 if ((u.length2() == 0.0) || (b.length2() == 0.0)) {
262 // Now generate a trivial basis transformation matrix. If we want
263 // to map the three unit vectors to three arbitrary vectors U, V,
264 // and W, then those just become the columns of the 3x3 matrix.
265 m(0,0) = u[0]; m(1,0) = v[0]; m(2,0) = w[0]; m(3,0) = a[0];// |Ux Vx Wx|
266 m(0,1) = u[1]; m(1,1) = v[1]; m(2,1) = w[1]; m(3,1) = a[1];//m = |Uy Vy Wy|
267 m(0,2) = u[2]; m(1,2) = v[2]; m(2,2) = w[2]; m(3,2) = a[2];// |Uz Vz Wz|
268 m(0,3) = 0; m(1,3) = 0; m(2,3) = 0; m(3,3) = 1;
271 // The above matrix maps the unit (!) square to the panel
272 // rectangle. Postmultiply scaling factors that match the
273 // pixel-space size of the panel.
275 m(0,i) *= 1.0/panelWidth;
276 m(1,i) *= 1.0/panelHeight;
282 osg::Matrix FGPanelNode::transformMatrix() const
285 return osg::Matrix();
288 if (!_resizeToViewport) {
292 double s = _panel->getAspectScale();
293 osg::Matrix m = osg::Matrix::scale(s, s, 1.0);
294 m *= osg::Matrix::translate(_panel->getXOffset(), _panel->getYOffset(), 0.0);
300 FGPanelNode::drawImplementation(osg::State& state) const
306 osg::ref_ptr<osg::RefMatrix> mv = new osg::RefMatrix;
307 mv->set(transformMatrix() * state.getModelViewMatrix());
308 state.applyModelViewMatrix(mv.get());
314 FGPanelNode::computeBound() const
318 osg::Matrix m(transformMatrix());
319 coords[0] = m.preMult(osg::Vec3(_xmin,_ymin,0));
320 coords[1] = m.preMult(osg::Vec3(_xmax,_ymin,0));
321 coords[2] = m.preMult(osg::Vec3(_xmin,_ymax,0));
324 bb.expandBy(coords[0]);
325 bb.expandBy(coords[1]);
326 bb.expandBy(coords[2]);
330 void FGPanelNode::accept(osg::PrimitiveFunctor& functor) const
333 osg::Matrix m(transformMatrix());
335 coords[0] = m.preMult(osg::Vec3(_xmin,_ymin,0));
336 coords[1] = m.preMult(osg::Vec3(_xmax,_ymin,0));
337 coords[2] = m.preMult(osg::Vec3(_xmax, _ymax, 0));
338 coords[3] = m.preMult(osg::Vec3(_xmin,_ymax,0));
340 functor.setVertexArray(4, coords);
341 functor.drawArrays( GL_QUADS, 0, 4);
344 bool FGPanelNode::isVisible2d() const
350 if (!_hideNonDefaultViews) {
351 _hideNonDefaultViews = fgGetNode("/sim/panel/hide-nonzero-view", true);
354 if (_hideNonDefaultViews->getBoolValue()) {
355 if (globals->get_viewmgr()->get_current() != 0) {
361 _autoHide2d = fgGetNode("/sim/panel/hide-nonzero-heading-offset", true);
364 if (_panel->getAutohide() && _autoHide2d->getBoolValue()) {
365 if (!globals->get_current_view()) {
369 return globals->get_current_view()->getHeadingOffset_deg() == 0;
375 static osg::Node* createGeode(FGPanelNode* panel)
377 osg::Geode* geode = new osg::Geode;
378 geode->addDrawable(panel);
380 geode->setNodeMask(SG_NODEMASK_PICK_BIT | SG_NODEMASK_2DPANEL_BIT);
382 SGSceneUserData* userData;
383 userData = SGSceneUserData::getOrCreateSceneUserData(geode);
384 userData->setPickCallback(new FGPanelPickCallback(panel));
388 osg::Node* FGPanelNode::create2DPanelNode()
390 FGPanelNode* drawable = new FGPanelNode;
392 osg::Switch* ps = new osg::Switch;
393 osg::StateSet* stateSet = ps->getOrCreateStateSet();
394 stateSet->setRenderBinDetails(1000, "RenderBin");
395 ps->addChild(createGeode(drawable));
397 // speed optimization?
398 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
399 stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
400 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
401 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
402 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
404 ps->setUpdateCallback(new FGPanelSwitchCallback(drawable));
408 osg::Node* FGPanelNode::load(SGPropertyNode *n)
410 FGPanelNode* drawable = new FGPanelNode(n);
411 drawable->lazyLoad(); // force load now for 2.5D panels
412 return createGeode(drawable);
416 * Built-in command: pass a mouse click to the panel.
418 * button: the mouse button number, zero-based.
419 * is-down: true if the button is down, false if it is up.
420 * x-pos: the x position of the mouse click.
421 * y-pos: the y position of the mouse click.
424 FGPanelNode::panelMouseClickCommand(const SGPropertyNode * arg)
426 return _panel->doMouseAction(arg->getIntValue("button"),
427 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
428 arg->getIntValue("x-pos"),
429 arg->getIntValue("y-pos"));