| 1 | #ifdef HAVE_CONFIG_H |
| 2 | # include <config.h> |
| 3 | #endif |
| 4 | |
| 5 | #include "panelnode.hxx" |
| 6 | |
| 7 | #include <vector> |
| 8 | #include <algorithm> |
| 9 | |
| 10 | #include <osg/Geode> |
| 11 | #include <osg/Switch> |
| 12 | #include <osg/BlendFunc> |
| 13 | |
| 14 | #include <simgear/compiler.h> |
| 15 | #include <simgear/structure/exception.hxx> |
| 16 | #include <simgear/debug/logstream.hxx> |
| 17 | |
| 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> |
| 22 | |
| 23 | #include <GUI/FlightGear_pu.h> |
| 24 | #include <Main/fg_os.hxx> |
| 25 | #include <Cockpit/panel.hxx> |
| 26 | #include <Cockpit/panel_io.hxx> |
| 27 | #include "Viewer/view.hxx" |
| 28 | #include "Viewer/viewmgr.hxx" |
| 29 | |
| 30 | using std::vector; |
| 31 | |
| 32 | class PanelTransformListener : public SGPropertyChangeListener |
| 33 | { |
| 34 | public: |
| 35 | PanelTransformListener(FGPanelNode* pn) : _panelNode(pn) {} |
| 36 | |
| 37 | virtual void valueChanged (SGPropertyNode * node) |
| 38 | { |
| 39 | _panelNode->dirtyBound(); |
| 40 | } |
| 41 | private: |
| 42 | FGPanelNode* _panelNode; |
| 43 | }; |
| 44 | |
| 45 | class PanelPathListener : public SGPropertyChangeListener |
| 46 | { |
| 47 | public: |
| 48 | PanelPathListener(FGPanelNode* pn) : _panelNode(pn) {} |
| 49 | |
| 50 | virtual void valueChanged (SGPropertyNode * node) |
| 51 | { |
| 52 | _panelNode->setPanelPath(node->getStringValue()); |
| 53 | } |
| 54 | private: |
| 55 | FGPanelNode* _panelNode; |
| 56 | }; |
| 57 | |
| 58 | class FGPanelPickCallback : public SGPickCallback { |
| 59 | public: |
| 60 | FGPanelPickCallback(FGPanelNode* p) : |
| 61 | panel(p) |
| 62 | {} |
| 63 | |
| 64 | virtual bool buttonPressed( int b, |
| 65 | const osgGA::GUIEventAdapter&, |
| 66 | const Info& info ) |
| 67 | { |
| 68 | if (!panel->getPanel()) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | button = b; |
| 73 | // convert to panel coordinates |
| 74 | osg::Matrixd m = osg::Matrixd::inverse(panel->transformMatrix()); |
| 75 | picked = toOsg(info.local) * m; |
| 76 | SG_LOG( SG_INSTR, SG_DEBUG, "panel pick: " << toSG(picked) ); |
| 77 | |
| 78 | // send to the panel |
| 79 | return panel->getPanel()->doLocalMouseAction(button, MOUSE_BUTTON_DOWN, |
| 80 | picked.x(), picked.y()); |
| 81 | } |
| 82 | |
| 83 | virtual void update(double dt, int keyModState) |
| 84 | { |
| 85 | panel->getPanel()->updateMouseDelay(dt); |
| 86 | } |
| 87 | |
| 88 | virtual void buttonReleased( int, |
| 89 | const osgGA::GUIEventAdapter&, |
| 90 | const Info* ) |
| 91 | { |
| 92 | panel->getPanel()->doLocalMouseAction(button, MOUSE_BUTTON_UP, |
| 93 | picked.x(), picked.y()); |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | FGPanelNode* panel; |
| 98 | int button; |
| 99 | osg::Vec3 picked; |
| 100 | }; |
| 101 | |
| 102 | class FGPanelSwitchCallback : public osg::NodeCallback { |
| 103 | public: |
| 104 | FGPanelSwitchCallback(FGPanelNode* pn) : |
| 105 | panel(pn), |
| 106 | visProp(fgGetNode("/sim/panel/visibility")) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
| 111 | { |
| 112 | assert(dynamic_cast<osg::Switch*>(node)); |
| 113 | osg::Switch* sw = static_cast<osg::Switch*>(node); |
| 114 | |
| 115 | if (!visProp->getBoolValue()) { |
| 116 | sw->setValue(0, false); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | panel->lazyLoad(); // isVisible check needs the panel loaded for auto-hide flag |
| 122 | bool enabled = panel->isVisible2d(); |
| 123 | sw->setValue(0, enabled); |
| 124 | if (!enabled) |
| 125 | return; |
| 126 | |
| 127 | traverse(node, nv); |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | FGPanelNode* panel; |
| 132 | SGPropertyNode_ptr visProp; |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | FGPanelNode::FGPanelNode(SGPropertyNode* props) : |
| 137 | _is2d(false), |
| 138 | _resizeToViewport(false), |
| 139 | _listener(NULL) |
| 140 | { |
| 141 | commonInit(); |
| 142 | _panelPath = props->getStringValue("path"); |
| 143 | |
| 144 | // And the corner points |
| 145 | SGPropertyNode* pt = props->getChild("bottom-left"); |
| 146 | _bottomLeft[0] = pt->getFloatValue("x-m"); |
| 147 | _bottomLeft[1] = pt->getFloatValue("y-m"); |
| 148 | _bottomLeft[2] = pt->getFloatValue("z-m"); |
| 149 | |
| 150 | pt = props->getChild("top-left"); |
| 151 | _topLeft[0] = pt->getFloatValue("x-m"); |
| 152 | _topLeft[1] = pt->getFloatValue("y-m"); |
| 153 | _topLeft[2] = pt->getFloatValue("z-m"); |
| 154 | |
| 155 | pt = props->getChild("bottom-right"); |
| 156 | _bottomRight[0] = pt->getFloatValue("x-m"); |
| 157 | _bottomRight[1] = pt->getFloatValue("y-m"); |
| 158 | _bottomRight[2] = pt->getFloatValue("z-m"); |
| 159 | |
| 160 | _depthTest = props->getBoolValue("depth-test"); |
| 161 | } |
| 162 | |
| 163 | FGPanelNode::FGPanelNode() : |
| 164 | _is2d(true), |
| 165 | _resizeToViewport(true), |
| 166 | _depthTest(false) |
| 167 | { |
| 168 | globals->get_commands()->addCommand("panel-mouse-click", this, &FGPanelNode::panelMouseClickCommand); |
| 169 | |
| 170 | SGPropertyNode* pathNode = fgGetNode("/sim/panel/path"); |
| 171 | _pathListener.reset(new PanelPathListener(this)); |
| 172 | pathNode->addChangeListener(_pathListener.get()); |
| 173 | setPanelPath(pathNode->getStringValue()); |
| 174 | |
| 175 | // for a 2D panel, various options adjust the transformation |
| 176 | // matrix. We need to pass this data on to OSG or its bounding box |
| 177 | // will be stale, and picking will break. |
| 178 | // http://code.google.com/p/flightgear-bugs/issues/detail?id=864 |
| 179 | _listener = new PanelTransformListener(this); |
| 180 | fgGetNode("/sim/panel/x-offset", true)->addChangeListener(_listener); |
| 181 | fgGetNode("/sim/panel/y-offset", true)->addChangeListener(_listener); |
| 182 | fgGetNode("/sim/startup/xsize", true)->addChangeListener(_listener); |
| 183 | fgGetNode("/sim/startup/ysize", true)->addChangeListener(_listener); |
| 184 | |
| 185 | commonInit(); |
| 186 | } |
| 187 | |
| 188 | FGPanelNode::~FGPanelNode() |
| 189 | { |
| 190 | if (_is2d) { |
| 191 | globals->get_commands()->removeCommand("panel-mouse-click"); |
| 192 | SGPropertyNode* pathNode = fgGetNode("/sim/panel/path"); |
| 193 | pathNode->removeChangeListener(_pathListener.get()); |
| 194 | } |
| 195 | |
| 196 | if (_listener) { |
| 197 | fgGetNode("/sim/panel/x-offset", true)->removeChangeListener(_listener); |
| 198 | fgGetNode("/sim/panel/y-offset", true)->removeChangeListener(_listener); |
| 199 | fgGetNode("/sim/startup/xsize", true)->removeChangeListener(_listener); |
| 200 | fgGetNode("/sim/startup/ysize", true)->removeChangeListener(_listener); |
| 201 | delete _listener; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void FGPanelNode::setPanelPath(const std::string& panel) |
| 206 | { |
| 207 | if (panel == _panelPath) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | _panelPath = panel; |
| 212 | if (_panel) { |
| 213 | _panel.clear(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void FGPanelNode::lazyLoad() |
| 218 | { |
| 219 | if (!_panelPath.empty() && !_panel) { |
| 220 | _panel = fgReadPanel(_panelPath); |
| 221 | if (!_panel) { |
| 222 | SG_LOG(SG_COCKPIT, SG_WARN, "failed to read panel from:" << _panelPath); |
| 223 | _panelPath = std::string(); // don't keep trying to read |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | _panel->setDepthTest(_depthTest); |
| 228 | initWithPanel(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void FGPanelNode::commonInit() |
| 233 | { |
| 234 | setUseDisplayList(false); |
| 235 | setDataVariance(Object::DYNAMIC); |
| 236 | getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
| 237 | getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); |
| 238 | } |
| 239 | |
| 240 | void FGPanelNode::initWithPanel() |
| 241 | { |
| 242 | int i; |
| 243 | |
| 244 | // Read out the pixel-space info |
| 245 | float panelWidth = _panel->getWidth(); |
| 246 | float panelHeight = _panel->getHeight(); |
| 247 | |
| 248 | _panel->getLogicalExtent(_xmin, _ymin, _xmax, _ymax); |
| 249 | |
| 250 | // Now generate our transformation matrix. For shorthand, use |
| 251 | // "a", "b", and "c" as our corners and "m" as the matrix. The |
| 252 | // vector u goes from a to b, v from a to c, and w is a |
| 253 | // perpendicular cross product. |
| 254 | osg::Vec3 a = _bottomLeft; |
| 255 | osg::Vec3 b = _bottomRight; |
| 256 | osg::Vec3 c = _topLeft; |
| 257 | osg::Vec3 u = b - a; |
| 258 | osg::Vec3 v = c - a; |
| 259 | osg::Vec3 w = u^v; |
| 260 | |
| 261 | osg::Matrix& m = _xform; |
| 262 | if ((u.length2() == 0.0) || (b.length2() == 0.0)) { |
| 263 | m.makeIdentity(); |
| 264 | } else { |
| 265 | // Now generate a trivial basis transformation matrix. If we want |
| 266 | // to map the three unit vectors to three arbitrary vectors U, V, |
| 267 | // and W, then those just become the columns of the 3x3 matrix. |
| 268 | m(0,0) = u[0]; m(1,0) = v[0]; m(2,0) = w[0]; m(3,0) = a[0];// |Ux Vx Wx| |
| 269 | m(0,1) = u[1]; m(1,1) = v[1]; m(2,1) = w[1]; m(3,1) = a[1];//m = |Uy Vy Wy| |
| 270 | m(0,2) = u[2]; m(1,2) = v[2]; m(2,2) = w[2]; m(3,2) = a[2];// |Uz Vz Wz| |
| 271 | m(0,3) = 0; m(1,3) = 0; m(2,3) = 0; m(3,3) = 1; |
| 272 | } |
| 273 | |
| 274 | // The above matrix maps the unit (!) square to the panel |
| 275 | // rectangle. Postmultiply scaling factors that match the |
| 276 | // pixel-space size of the panel. |
| 277 | for(i=0; i<4; ++i) { |
| 278 | m(0,i) *= 1.0/panelWidth; |
| 279 | m(1,i) *= 1.0/panelHeight; |
| 280 | } |
| 281 | |
| 282 | dirtyBound(); |
| 283 | } |
| 284 | |
| 285 | osg::Matrix FGPanelNode::transformMatrix() const |
| 286 | { |
| 287 | if (!_panel) { |
| 288 | return osg::Matrix(); |
| 289 | } |
| 290 | |
| 291 | if (!_resizeToViewport) { |
| 292 | return _xform; |
| 293 | } |
| 294 | |
| 295 | double s = _panel->getAspectScale(); |
| 296 | osg::Matrix m = osg::Matrix::scale(s, s, 1.0); |
| 297 | m *= osg::Matrix::translate(_panel->getXOffset(), _panel->getYOffset(), 0.0); |
| 298 | |
| 299 | return m; |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | FGPanelNode::drawImplementation(osg::State& state) const |
| 304 | { |
| 305 | if (!_panel) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | osg::ref_ptr<osg::RefMatrix> mv = new osg::RefMatrix; |
| 310 | mv->set(transformMatrix() * state.getModelViewMatrix()); |
| 311 | state.applyModelViewMatrix(mv.get()); |
| 312 | |
| 313 | _panel->draw(state); |
| 314 | } |
| 315 | |
| 316 | osg::BoundingBox FGPanelNode::computeBoundingBox() const |
| 317 | { |
| 318 | |
| 319 | osg::Vec3 coords[3]; |
| 320 | osg::Matrix m(transformMatrix()); |
| 321 | coords[0] = m.preMult(osg::Vec3(_xmin,_ymin,0)); |
| 322 | coords[1] = m.preMult(osg::Vec3(_xmax,_ymin,0)); |
| 323 | coords[2] = m.preMult(osg::Vec3(_xmin,_ymax,0)); |
| 324 | |
| 325 | osg::BoundingBox bb; |
| 326 | bb.expandBy(coords[0]); |
| 327 | bb.expandBy(coords[1]); |
| 328 | bb.expandBy(coords[2]); |
| 329 | return bb; |
| 330 | } |
| 331 | |
| 332 | void FGPanelNode::accept(osg::PrimitiveFunctor& functor) const |
| 333 | { |
| 334 | osg::Vec3 coords[4]; |
| 335 | osg::Matrix m(transformMatrix()); |
| 336 | |
| 337 | coords[0] = m.preMult(osg::Vec3(_xmin,_ymin,0)); |
| 338 | coords[1] = m.preMult(osg::Vec3(_xmax,_ymin,0)); |
| 339 | coords[2] = m.preMult(osg::Vec3(_xmax, _ymax, 0)); |
| 340 | coords[3] = m.preMult(osg::Vec3(_xmin,_ymax,0)); |
| 341 | |
| 342 | functor.setVertexArray(4, coords); |
| 343 | functor.drawArrays( GL_QUADS, 0, 4); |
| 344 | } |
| 345 | |
| 346 | bool FGPanelNode::isVisible2d() const |
| 347 | { |
| 348 | if (!_panel) { |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | if (!_hideNonDefaultViews) { |
| 353 | _hideNonDefaultViews = fgGetNode("/sim/panel/hide-nonzero-view", true); |
| 354 | } |
| 355 | |
| 356 | if (_hideNonDefaultViews->getBoolValue()) { |
| 357 | if (globals->get_viewmgr()->getCurrentViewIndex() != 0) { |
| 358 | return false; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if (!_autoHide2d) { |
| 363 | _autoHide2d = fgGetNode("/sim/panel/hide-nonzero-heading-offset", true); |
| 364 | } |
| 365 | |
| 366 | if (_panel->getAutohide() && _autoHide2d->getBoolValue()) { |
| 367 | if (!globals->get_current_view()) { |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | return globals->get_current_view()->getHeadingOffset_deg() == 0; |
| 372 | } |
| 373 | |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | static osg::Node* createGeode(FGPanelNode* panel) |
| 378 | { |
| 379 | osg::Geode* geode = new osg::Geode; |
| 380 | geode->addDrawable(panel); |
| 381 | |
| 382 | geode->setNodeMask(SG_NODEMASK_PICK_BIT | SG_NODEMASK_2DPANEL_BIT); |
| 383 | |
| 384 | SGSceneUserData* userData; |
| 385 | userData = SGSceneUserData::getOrCreateSceneUserData(geode); |
| 386 | userData->setPickCallback(new FGPanelPickCallback(panel)); |
| 387 | return geode; |
| 388 | } |
| 389 | |
| 390 | osg::Node* FGPanelNode::create2DPanelNode() |
| 391 | { |
| 392 | FGPanelNode* drawable = new FGPanelNode; |
| 393 | |
| 394 | osg::Switch* ps = new osg::Switch; |
| 395 | osg::StateSet* stateSet = ps->getOrCreateStateSet(); |
| 396 | stateSet->setRenderBinDetails(1000, "RenderBin"); |
| 397 | ps->addChild(createGeode(drawable)); |
| 398 | |
| 399 | // speed optimization? |
| 400 | stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF); |
| 401 | stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA)); |
| 402 | stateSet->setMode(GL_BLEND, osg::StateAttribute::ON); |
| 403 | stateSet->setMode(GL_FOG, osg::StateAttribute::OFF); |
| 404 | stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); |
| 405 | |
| 406 | ps->setUpdateCallback(new FGPanelSwitchCallback(drawable)); |
| 407 | return ps; |
| 408 | } |
| 409 | |
| 410 | osg::Node* FGPanelNode::load(SGPropertyNode *n) |
| 411 | { |
| 412 | FGPanelNode* drawable = new FGPanelNode(n); |
| 413 | drawable->lazyLoad(); // force load now for 2.5D panels |
| 414 | return createGeode(drawable); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Built-in command: pass a mouse click to the panel. |
| 419 | * |
| 420 | * button: the mouse button number, zero-based. |
| 421 | * is-down: true if the button is down, false if it is up. |
| 422 | * x-pos: the x position of the mouse click. |
| 423 | * y-pos: the y position of the mouse click. |
| 424 | */ |
| 425 | bool |
| 426 | FGPanelNode::panelMouseClickCommand(const SGPropertyNode * arg, SGPropertyNode * root) |
| 427 | { |
| 428 | return _panel->doMouseAction(arg->getIntValue("button"), |
| 429 | arg->getBoolValue("is-down") ? PU_DOWN : PU_UP, |
| 430 | arg->getIntValue("x-pos"), |
| 431 | arg->getIntValue("y-pos")); |
| 432 | } |