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 static FGPanelNode* global_panel = NULL;
36 * Built-in command: pass a mouse click to the panel.
38 * button: the mouse button number, zero-based.
39 * is-down: true if the button is down, false if it is up.
40 * x-pos: the x position of the mouse click.
41 * y-pos: the y position of the mouse click.
44 do_panel_mouse_click (const SGPropertyNode * arg)
47 return global_panel->getPanel()
48 ->doMouseAction(arg->getIntValue("button"),
49 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
50 arg->getIntValue("x-pos"),
51 arg->getIntValue("y-pos"));
58 class FGPanelPickCallback : public SGPickCallback {
60 FGPanelPickCallback(FGPanelNode* p) :
64 virtual bool buttonPressed(int b, const osgGA::GUIEventAdapter*, const Info& info)
67 // convert to panel coordinates
68 osg::Matrixd m = osg::Matrixd::inverse(panel->transformMatrix());
69 picked = toOsg(info.local) * m;
70 SG_LOG( SG_INSTR, SG_DEBUG, "panel pick: " << toSG(picked) );
73 return panel->getPanel()->doLocalMouseAction(button, MOUSE_BUTTON_DOWN,
74 picked.x(), picked.y());
77 virtual void update(double dt)
79 panel->getPanel()->updateMouseDelay(dt);
82 virtual void buttonReleased(void)
84 panel->getPanel()->doLocalMouseAction(button, MOUSE_BUTTON_UP,
85 picked.x(), picked.y());
94 class FGPanelSwitchCallback : public osg::NodeCallback {
96 FGPanelSwitchCallback(FGPanelNode* pn) :
98 visProp(fgGetNode("/sim/panel/visibility"))
102 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
104 assert(dynamic_cast<osg::Switch*>(node));
105 osg::Switch* sw = static_cast<osg::Switch*>(node);
107 if (!visProp->getBoolValue()) {
108 sw->setValue(0, false);
113 panel->lazyLoad(); // isVisible check needs the panel loaded for auto-hide flag
114 bool enabled = panel->isVisible2d();
115 sw->setValue(0, enabled);
124 SGPropertyNode* visProp;
128 FGPanelNode::FGPanelNode(SGPropertyNode* props) :
129 _resizeToViewport(false)
132 _panelPath = props->getStringValue("path");
134 // And the corner points
135 SGPropertyNode* pt = props->getChild("bottom-left");
136 _bottomLeft[0] = pt->getFloatValue("x-m");
137 _bottomLeft[1] = pt->getFloatValue("y-m");
138 _bottomLeft[2] = pt->getFloatValue("z-m");
140 pt = props->getChild("top-left");
141 _topLeft[0] = pt->getFloatValue("x-m");
142 _topLeft[1] = pt->getFloatValue("y-m");
143 _topLeft[2] = pt->getFloatValue("z-m");
145 pt = props->getChild("bottom-right");
146 _bottomRight[0] = pt->getFloatValue("x-m");
147 _bottomRight[1] = pt->getFloatValue("y-m");
148 _bottomRight[2] = pt->getFloatValue("z-m");
150 _depthTest = props->getBoolValue("depth-test");
153 FGPanelNode::FGPanelNode() :
154 _resizeToViewport(true),
160 void FGPanelNode::setPanelPath(const std::string& panel)
162 if (panel == _panelPath) {
172 void FGPanelNode::lazyLoad()
174 if (!_panelPath.empty() && !_panel) {
175 _panel = fgReadPanel(_panelPath);
177 SG_LOG(SG_COCKPIT, SG_WARN, "failed to read panel from:" << _panelPath);
178 _panelPath = string(); // don't keep trying to read
182 _panel->setDepthTest(_depthTest);
187 void FGPanelNode::commonInit()
189 setUseDisplayList(false);
190 setDataVariance(Object::DYNAMIC);
191 getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
192 getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
195 void FGPanelNode::initWithPanel()
199 // Read out the pixel-space info
200 float panelWidth = _panel->getWidth();
201 float panelHeight = _panel->getHeight();
203 _panel->getLogicalExtent(_xmin, _ymin, _xmax, _ymax);
205 // Now generate our transformation matrix. For shorthand, use
206 // "a", "b", and "c" as our corners and "m" as the matrix. The
207 // vector u goes from a to b, v from a to c, and w is a
208 // perpendicular cross product.
209 osg::Vec3 a = _bottomLeft;
210 osg::Vec3 b = _bottomRight;
211 osg::Vec3 c = _topLeft;
216 osg::Matrix& m = _xform;
217 // Now generate a trivial basis transformation matrix. If we want
218 // to map the three unit vectors to three arbitrary vectors U, V,
219 // and W, then those just become the columns of the 3x3 matrix.
220 m(0,0) = u[0]; m(1,0) = v[0]; m(2,0) = w[0]; m(3,0) = a[0];// |Ux Vx Wx|
221 m(0,1) = u[1]; m(1,1) = v[1]; m(2,1) = w[1]; m(3,1) = a[1];//m = |Uy Vy Wy|
222 m(0,2) = u[2]; m(1,2) = v[2]; m(2,2) = w[2]; m(3,2) = a[2];// |Uz Vz Wz|
223 m(0,3) = 0; m(1,3) = 0; m(2,3) = 0; m(3,3) = 1;
225 // The above matrix maps the unit (!) square to the panel
226 // rectangle. Postmultiply scaling factors that match the
227 // pixel-space size of the panel.
229 m(0,i) *= 1.0/panelWidth;
230 m(1,i) *= 1.0/panelHeight;
236 FGPanelNode::~FGPanelNode()
240 osg::Matrix FGPanelNode::transformMatrix() const
243 return osg::Matrix();
246 if (!_resizeToViewport) {
250 double s = _panel->getAspectScale();
251 osg::Matrix m = osg::Matrix::scale(s, s, 1.0);
252 m *= osg::Matrix::translate(_panel->getXOffset(), _panel->getYOffset(), 0.0);
258 FGPanelNode::drawImplementation(osg::State& state) const
264 osg::ref_ptr<osg::RefMatrix> mv = new osg::RefMatrix;
265 mv->set(transformMatrix() * state.getModelViewMatrix());
266 state.applyModelViewMatrix(mv.get());
272 FGPanelNode::computeBound() const
276 osg::Matrix m(transformMatrix());
277 coords[0] = m.preMult(osg::Vec3(_xmin,_ymin,0));
278 coords[1] = m.preMult(osg::Vec3(_xmax,_ymin,0));
279 coords[2] = m.preMult(osg::Vec3(_xmin,_ymax,0));
282 bb.expandBy(coords[0]);
283 bb.expandBy(coords[1]);
284 bb.expandBy(coords[2]);
288 void FGPanelNode::accept(osg::PrimitiveFunctor& functor) const
291 osg::Matrix m(transformMatrix());
293 coords[0] = m.preMult(osg::Vec3(_xmin,_ymin,0));
294 coords[1] = m.preMult(osg::Vec3(_xmax,_ymin,0));
295 coords[2] = m.preMult(osg::Vec3(_xmax, _ymax, 0));
296 coords[3] = m.preMult(osg::Vec3(_xmin,_ymax,0));
298 functor.setVertexArray(4, coords);
299 functor.drawArrays( GL_QUADS, 0, 4);
302 bool FGPanelNode::isVisible2d() const
308 if (!_hideNonDefaultViews) {
309 _hideNonDefaultViews = fgGetNode("/sim/panel/hide-nonzero-view", true);
312 if (_hideNonDefaultViews->getBoolValue()) {
313 if (globals->get_viewmgr()->get_current() != 0) {
319 _autoHide2d = fgGetNode("/sim/panel/hide-nonzero-heading-offset", true);
322 if (_panel->getAutohide() && _autoHide2d->getBoolValue()) {
323 if (!globals->get_current_view()) {
327 return globals->get_current_view()->getHeadingOffset_deg() == 0;
333 static osg::Node* createGeode(FGPanelNode* panel)
335 osg::Geode* geode = new osg::Geode;
336 geode->addDrawable(panel);
338 geode->setNodeMask(SG_NODEMASK_PICK_BIT | SG_NODEMASK_2DPANEL_BIT);
340 SGSceneUserData* userData;
341 userData = SGSceneUserData::getOrCreateSceneUserData(geode);
342 userData->setPickCallback(new FGPanelPickCallback(panel));
346 class PanelPathObserver : public SGPropertyChangeListener
349 PanelPathObserver(FGPanelNode* pn) : _panelNode(pn) {}
351 virtual void valueChanged (SGPropertyNode * node)
353 _panelNode->setPanelPath(node->getStringValue());
356 FGPanelNode* _panelNode;
359 osg::Node* FGPanelNode::create2DPanelNode()
361 SGCommandMgr::instance()->addCommand("panel-mouse-click", do_panel_mouse_click);
363 SGPropertyNode* pathNode = fgGetNode("/sim/panel/path");
365 FGPanelNode* drawable = new FGPanelNode();
366 // need a global to keep the panel_mouse_click command working, sadly
367 global_panel = drawable;
369 PanelPathObserver* ppo = new PanelPathObserver(drawable);
370 pathNode->addChangeListener(ppo);
371 drawable->setPanelPath(pathNode->getStringValue());
373 osg::Switch* ps = new osg::Switch;
374 osg::StateSet* stateSet = ps->getOrCreateStateSet();
375 stateSet->setRenderBinDetails(1000, "RenderBin");
376 ps->addChild(createGeode(drawable));
378 // speed optimization?
379 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
380 stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
381 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
382 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
383 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
385 ps->setUpdateCallback(new FGPanelSwitchCallback(drawable));
389 osg::Node* FGPanelNode::load(SGPropertyNode *n)
391 FGPanelNode* drawable = new FGPanelNode(n);
392 drawable->lazyLoad(); // force load now for 2.5D panels
393 return createGeode(drawable);