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,
65 const osgGA::GUIEventAdapter&,
69 // convert to panel coordinates
70 osg::Matrixd m = osg::Matrixd::inverse(panel->transformMatrix());
71 picked = toOsg(info.local) * m;
72 SG_LOG( SG_INSTR, SG_DEBUG, "panel pick: " << toSG(picked) );
75 return panel->getPanel()->doLocalMouseAction(button, MOUSE_BUTTON_DOWN,
76 picked.x(), picked.y());
79 virtual void update(double dt, int keyModState)
81 panel->getPanel()->updateMouseDelay(dt);
84 virtual void buttonReleased( int,
85 const osgGA::GUIEventAdapter&,
88 panel->getPanel()->doLocalMouseAction(button, MOUSE_BUTTON_UP,
89 picked.x(), picked.y());
98 class FGPanelSwitchCallback : public osg::NodeCallback {
100 FGPanelSwitchCallback(FGPanelNode* pn) :
102 visProp(fgGetNode("/sim/panel/visibility"))
106 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
108 assert(dynamic_cast<osg::Switch*>(node));
109 osg::Switch* sw = static_cast<osg::Switch*>(node);
111 if (!visProp->getBoolValue()) {
112 sw->setValue(0, false);
117 panel->lazyLoad(); // isVisible check needs the panel loaded for auto-hide flag
118 bool enabled = panel->isVisible2d();
119 sw->setValue(0, enabled);
128 SGPropertyNode* visProp;
132 FGPanelNode::FGPanelNode(SGPropertyNode* props) :
133 _resizeToViewport(false)
136 _panelPath = props->getStringValue("path");
138 // And the corner points
139 SGPropertyNode* pt = props->getChild("bottom-left");
140 _bottomLeft[0] = pt->getFloatValue("x-m");
141 _bottomLeft[1] = pt->getFloatValue("y-m");
142 _bottomLeft[2] = pt->getFloatValue("z-m");
144 pt = props->getChild("top-left");
145 _topLeft[0] = pt->getFloatValue("x-m");
146 _topLeft[1] = pt->getFloatValue("y-m");
147 _topLeft[2] = pt->getFloatValue("z-m");
149 pt = props->getChild("bottom-right");
150 _bottomRight[0] = pt->getFloatValue("x-m");
151 _bottomRight[1] = pt->getFloatValue("y-m");
152 _bottomRight[2] = pt->getFloatValue("z-m");
154 _depthTest = props->getBoolValue("depth-test");
157 FGPanelNode::FGPanelNode() :
158 _resizeToViewport(true),
164 void FGPanelNode::setPanelPath(const std::string& panel)
166 if (panel == _panelPath) {
176 void FGPanelNode::lazyLoad()
178 if (!_panelPath.empty() && !_panel) {
179 _panel = fgReadPanel(_panelPath);
181 SG_LOG(SG_COCKPIT, SG_WARN, "failed to read panel from:" << _panelPath);
182 _panelPath = string(); // don't keep trying to read
186 _panel->setDepthTest(_depthTest);
191 void FGPanelNode::commonInit()
193 setUseDisplayList(false);
194 setDataVariance(Object::DYNAMIC);
195 getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
196 getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
199 void FGPanelNode::initWithPanel()
203 // Read out the pixel-space info
204 float panelWidth = _panel->getWidth();
205 float panelHeight = _panel->getHeight();
207 _panel->getLogicalExtent(_xmin, _ymin, _xmax, _ymax);
209 // Now generate our transformation matrix. For shorthand, use
210 // "a", "b", and "c" as our corners and "m" as the matrix. The
211 // vector u goes from a to b, v from a to c, and w is a
212 // perpendicular cross product.
213 osg::Vec3 a = _bottomLeft;
214 osg::Vec3 b = _bottomRight;
215 osg::Vec3 c = _topLeft;
220 osg::Matrix& m = _xform;
221 // Now generate a trivial basis transformation matrix. If we want
222 // to map the three unit vectors to three arbitrary vectors U, V,
223 // and W, then those just become the columns of the 3x3 matrix.
224 m(0,0) = u[0]; m(1,0) = v[0]; m(2,0) = w[0]; m(3,0) = a[0];// |Ux Vx Wx|
225 m(0,1) = u[1]; m(1,1) = v[1]; m(2,1) = w[1]; m(3,1) = a[1];//m = |Uy Vy Wy|
226 m(0,2) = u[2]; m(1,2) = v[2]; m(2,2) = w[2]; m(3,2) = a[2];// |Uz Vz Wz|
227 m(0,3) = 0; m(1,3) = 0; m(2,3) = 0; m(3,3) = 1;
229 // The above matrix maps the unit (!) square to the panel
230 // rectangle. Postmultiply scaling factors that match the
231 // pixel-space size of the panel.
233 m(0,i) *= 1.0/panelWidth;
234 m(1,i) *= 1.0/panelHeight;
240 FGPanelNode::~FGPanelNode()
244 osg::Matrix FGPanelNode::transformMatrix() const
247 return osg::Matrix();
250 if (!_resizeToViewport) {
254 double s = _panel->getAspectScale();
255 osg::Matrix m = osg::Matrix::scale(s, s, 1.0);
256 m *= osg::Matrix::translate(_panel->getXOffset(), _panel->getYOffset(), 0.0);
262 FGPanelNode::drawImplementation(osg::State& state) const
268 osg::ref_ptr<osg::RefMatrix> mv = new osg::RefMatrix;
269 mv->set(transformMatrix() * state.getModelViewMatrix());
270 state.applyModelViewMatrix(mv.get());
276 FGPanelNode::computeBound() const
280 osg::Matrix m(transformMatrix());
281 coords[0] = m.preMult(osg::Vec3(_xmin,_ymin,0));
282 coords[1] = m.preMult(osg::Vec3(_xmax,_ymin,0));
283 coords[2] = m.preMult(osg::Vec3(_xmin,_ymax,0));
286 bb.expandBy(coords[0]);
287 bb.expandBy(coords[1]);
288 bb.expandBy(coords[2]);
292 void FGPanelNode::accept(osg::PrimitiveFunctor& functor) const
295 osg::Matrix m(transformMatrix());
297 coords[0] = m.preMult(osg::Vec3(_xmin,_ymin,0));
298 coords[1] = m.preMult(osg::Vec3(_xmax,_ymin,0));
299 coords[2] = m.preMult(osg::Vec3(_xmax, _ymax, 0));
300 coords[3] = m.preMult(osg::Vec3(_xmin,_ymax,0));
302 functor.setVertexArray(4, coords);
303 functor.drawArrays( GL_QUADS, 0, 4);
306 bool FGPanelNode::isVisible2d() const
312 if (!_hideNonDefaultViews) {
313 _hideNonDefaultViews = fgGetNode("/sim/panel/hide-nonzero-view", true);
316 if (_hideNonDefaultViews->getBoolValue()) {
317 if (globals->get_viewmgr()->get_current() != 0) {
323 _autoHide2d = fgGetNode("/sim/panel/hide-nonzero-heading-offset", true);
326 if (_panel->getAutohide() && _autoHide2d->getBoolValue()) {
327 if (!globals->get_current_view()) {
331 return globals->get_current_view()->getHeadingOffset_deg() == 0;
337 static osg::Node* createGeode(FGPanelNode* panel)
339 osg::Geode* geode = new osg::Geode;
340 geode->addDrawable(panel);
342 geode->setNodeMask(SG_NODEMASK_PICK_BIT | SG_NODEMASK_2DPANEL_BIT);
344 SGSceneUserData* userData;
345 userData = SGSceneUserData::getOrCreateSceneUserData(geode);
346 userData->setPickCallback(new FGPanelPickCallback(panel));
350 class PanelPathObserver : public SGPropertyChangeListener
353 PanelPathObserver(FGPanelNode* pn) : _panelNode(pn) {}
355 virtual void valueChanged (SGPropertyNode * node)
357 _panelNode->setPanelPath(node->getStringValue());
360 FGPanelNode* _panelNode;
363 osg::Node* FGPanelNode::create2DPanelNode()
365 SGCommandMgr::instance()->addCommand("panel-mouse-click", do_panel_mouse_click);
367 SGPropertyNode* pathNode = fgGetNode("/sim/panel/path");
369 FGPanelNode* drawable = new FGPanelNode();
370 // need a global to keep the panel_mouse_click command working, sadly
371 global_panel = drawable;
373 PanelPathObserver* ppo = new PanelPathObserver(drawable);
374 pathNode->addChangeListener(ppo);
375 drawable->setPanelPath(pathNode->getStringValue());
377 osg::Switch* ps = new osg::Switch;
378 osg::StateSet* stateSet = ps->getOrCreateStateSet();
379 stateSet->setRenderBinDetails(1000, "RenderBin");
380 ps->addChild(createGeode(drawable));
382 // speed optimization?
383 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
384 stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
385 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
386 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
387 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
389 ps->setUpdateCallback(new FGPanelSwitchCallback(drawable));
393 osg::Node* FGPanelNode::load(SGPropertyNode *n)
395 FGPanelNode* drawable = new FGPanelNode(n);
396 drawable->lazyLoad(); // force load now for 2.5D panels
397 return createGeode(drawable);