3 * Copyright (C) 2013 James Turner
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,
23 #include <simgear/scene/model/SGPickAnimation.hxx>
26 #include <osg/PolygonOffset>
27 #include <osg/PolygonMode>
28 #include <osg/Material>
29 #include <osgGA/GUIEventAdapter>
31 #include <simgear/scene/util/SGPickCallback.hxx>
32 #include <simgear/scene/material/EffectGeode.hxx>
33 #include <simgear/scene/util/SGSceneUserData.hxx>
34 #include <simgear/structure/SGBinding.hxx>
35 #include <simgear/scene/util/StateAttributeFactory.hxx>
36 #include <simgear/scene/model/SGRotateTransform.hxx>
38 using namespace simgear;
40 using OpenThreads::Mutex;
41 using OpenThreads::ScopedLock;
43 static void readOptionalBindingList(const SGPropertyNode* aNode, SGPropertyNode* modelRoot,
44 const std::string& aName, SGBindingList& aBindings)
46 const SGPropertyNode* n = aNode->getChild(aName);
48 aBindings = readBindingList(n->getChildren("binding"), modelRoot);
52 class SGPickAnimation::PickCallback : public SGPickCallback {
54 PickCallback(const SGPropertyNode* configNode,
55 SGPropertyNode* modelRoot) :
56 _repeatable(configNode->getBoolValue("repeatable", false)),
57 _repeatInterval(configNode->getDoubleValue("interval-sec", 0.1))
59 std::vector<SGPropertyNode_ptr> bindings;
61 bindings = configNode->getChildren("button");
62 for (unsigned int i = 0; i < bindings.size(); ++i) {
63 _buttons.insert( bindings[i]->getIntValue() );
66 _bindingsDown = readBindingList(configNode->getChildren("binding"), modelRoot);
67 readOptionalBindingList(configNode, modelRoot, "mod-up", _bindingsUp);
68 readOptionalBindingList(configNode, modelRoot, "hovered", _hover);
71 virtual bool buttonPressed(int button, const osgGA::GUIEventAdapter* ea, const Info&)
73 if (_buttons.find(button) == _buttons.end()) {
77 fireBindingList(_bindingsDown);
78 _repeatTime = -_repeatInterval; // anti-bobble: delay start of repeat
81 virtual void buttonReleased(void)
83 fireBindingList(_bindingsUp);
85 virtual void update(double dt)
91 while (_repeatInterval < _repeatTime) {
92 _repeatTime -= _repeatInterval;
93 fireBindingList(_bindingsDown);
97 virtual bool hover(const osg::Vec2d& windowPos, const Info& info)
103 SGPropertyNode_ptr params(new SGPropertyNode);
104 params->setDoubleValue("x", windowPos.x());
105 params->setDoubleValue("y", windowPos.y());
106 fireBindingList(_hover, params.ptr());
110 SGBindingList _bindingsDown;
111 SGBindingList _bindingsUp;
112 SGBindingList _hover;
113 std::set<int> _buttons;
115 double _repeatInterval;
119 class VncVisitor : public osg::NodeVisitor {
121 VncVisitor(double x, double y, int mask) :
122 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
123 _texX(x), _texY(y), _mask(mask), _done(false)
125 SG_LOG(SG_INPUT, SG_DEBUG, "VncVisitor constructor "
126 << x << "," << y << " mask " << mask);
129 virtual void apply(osg::Node &node)
131 // Some nodes have state sets attached
132 touchStateSet(node.getStateSet());
136 // See whether we are a geode worth exploring
137 osg::Geode *g = dynamic_cast<osg::Geode*>(&node);
139 // Go find all its drawables
140 int i = g->getNumDrawables();
142 osg::Drawable *d = g->getDrawable(i);
143 if (d) touchDrawable(*d);
145 // Out of optimism, do the same for EffectGeode
146 simgear::EffectGeode *eg = dynamic_cast<simgear::EffectGeode*>(&node);
148 for (simgear::EffectGeode::DrawablesIterator di = eg->drawablesBegin();
149 di != eg->drawablesEnd(); di++) {
152 // Now see whether the EffectGeode has an Effect
153 simgear::Effect *e = eg->getEffect();
155 touchStateSet(e->getDefaultStateSet());
159 inline void touchDrawable(osg::Drawable &d)
161 osg::StateSet *ss = d.getStateSet();
165 void touchStateSet(osg::StateSet *ss)
168 osg::StateAttribute *sa = ss->getTextureAttribute(0,
169 osg::StateAttribute::TEXTURE);
171 osg::Texture *t = sa->asTexture();
173 osg::Image *img = t->getImage(0);
176 int pixX = _texX * img->s();
177 int pixY = _texY * img->t();
178 _done = img->sendPointerEvent(pixX, pixY, _mask);
179 SG_LOG(SG_INPUT, SG_DEBUG, "VncVisitor image said " << _done
180 << " to coord " << pixX << "," << pixY);
184 inline bool wasSuccessful()
196 class SGPickAnimation::VncCallback : public SGPickCallback {
198 VncCallback(const SGPropertyNode* configNode,
199 SGPropertyNode* modelRoot,
203 SG_LOG(SG_INPUT, SG_DEBUG, "Configuring VNC callback");
204 const char *cornernames[3] = {"top-left", "top-right", "bottom-left"};
205 SGVec3d *cornercoords[3] = {&_topLeft, &_toRight, &_toDown};
206 for (int c =0; c < 3; c++) {
207 const SGPropertyNode* cornerNode = configNode->getChild(cornernames[c]);
208 *cornercoords[c] = SGVec3d(
209 cornerNode->getDoubleValue("x"),
210 cornerNode->getDoubleValue("y"),
211 cornerNode->getDoubleValue("z"));
213 _toRight -= _topLeft;
215 _squaredRight = dot(_toRight, _toRight);
216 _squaredDown = dot(_toDown, _toDown);
219 virtual bool buttonPressed(int button, const osgGA::GUIEventAdapter* ea, const Info& info)
221 SGVec3d loc(info.local);
222 SG_LOG(SG_INPUT, SG_DEBUG, "VNC pressed " << button << ": " << loc);
224 _x = dot(loc, _toRight) / _squaredRight;
225 _y = dot(loc, _toDown) / _squaredDown;
226 if (_x<0) _x = 0; else if (_x > 1) _x = 1;
227 if (_y<0) _y = 0; else if (_y > 1) _y = 1;
228 VncVisitor vv(_x, _y, 1 << button);
230 return vv.wasSuccessful();
233 virtual void buttonReleased(void)
235 SG_LOG(SG_INPUT, SG_DEBUG, "VNC release");
236 VncVisitor vv(_x, _y, 0);
239 virtual void update(double dt)
244 osg::ref_ptr<osg::Group> _node;
245 SGVec3d _topLeft, _toRight, _toDown;
246 double _squaredRight, _squaredDown;
249 SGPickAnimation::SGPickAnimation(const SGPropertyNode* configNode,
250 SGPropertyNode* modelRoot) :
251 SGAnimation(configNode, modelRoot)
257 OpenThreads::Mutex colorModeUniformMutex;
258 osg::ref_ptr<osg::Uniform> colorModeUniform;
263 SGPickAnimation::innerSetupPickGroup(osg::Group* commonGroup, osg::Group& parent)
265 // Contains the normal geometry that is interactive
266 osg::ref_ptr<osg::Group> normalGroup = new osg::Group;
267 normalGroup->setName("pick normal group");
268 normalGroup->addChild(commonGroup);
270 // Used to render the geometry with just yellow edges
271 osg::Group* highlightGroup = new osg::Group;
272 highlightGroup->setName("pick highlight group");
273 highlightGroup->setNodeMask(simgear::PICK_BIT);
274 highlightGroup->addChild(commonGroup);
276 // prepare a state set that paints the edges of this object yellow
277 // The material and texture attributes are set with
278 // OVERRIDE|PROTECTED in case there is a material animation on a
279 // higher node in the scene graph, which would have its material
280 // attribute set with OVERRIDE.
281 osg::StateSet* stateSet = highlightGroup->getOrCreateStateSet();
282 osg::Texture2D* white = StateAttributeFactory::instance()->getWhiteTexture();
283 stateSet->setTextureAttributeAndModes(0, white,
284 (osg::StateAttribute::ON
285 | osg::StateAttribute::OVERRIDE
286 | osg::StateAttribute::PROTECTED));
287 osg::PolygonOffset* polygonOffset = new osg::PolygonOffset;
288 polygonOffset->setFactor(-1);
289 polygonOffset->setUnits(-1);
290 stateSet->setAttribute(polygonOffset, osg::StateAttribute::OVERRIDE);
291 stateSet->setMode(GL_POLYGON_OFFSET_LINE,
292 osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
293 osg::PolygonMode* polygonMode = new osg::PolygonMode;
294 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
295 osg::PolygonMode::LINE);
296 stateSet->setAttribute(polygonMode, osg::StateAttribute::OVERRIDE);
297 osg::Material* material = new osg::Material;
298 material->setColorMode(osg::Material::OFF);
299 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4f(0, 0, 0, 1));
300 // XXX Alpha < 1.0 in the diffuse material value is a signal to the
301 // default shader to take the alpha value from the material value
302 // and not the glColor. In many cases the pick animation geometry is
303 // transparent, so the outline would not be visible without this hack.
304 material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(0, 0, 0, .95));
305 material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4f(1, 1, 0, 1));
306 material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4f(0, 0, 0, 0));
307 stateSet->setAttribute(
308 material, osg::StateAttribute::OVERRIDE | osg::StateAttribute::PROTECTED);
309 // The default shader has a colorMode uniform that mimics the
310 // behavior of Material color mode.
311 osg::Uniform* cmUniform = 0;
313 ScopedLock<Mutex> lock(colorModeUniformMutex);
314 if (!colorModeUniform.valid()) {
315 colorModeUniform = new osg::Uniform(osg::Uniform::INT, "colorMode");
316 colorModeUniform->set(0); // MODE_OFF
317 colorModeUniform->setDataVariance(osg::Object::STATIC);
319 cmUniform = colorModeUniform.get();
321 stateSet->addUniform(cmUniform,
322 osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
324 // Only add normal geometry if configured
325 if (getConfig()->getBoolValue("visible", true))
326 parent.addChild(normalGroup.get());
327 parent.addChild(highlightGroup);
331 SGPickAnimation::createAnimationGroup(osg::Group& parent)
333 osg::Group* commonGroup = new osg::Group;
334 innerSetupPickGroup(commonGroup, parent);
335 SGSceneUserData* ud = SGSceneUserData::getOrCreateSceneUserData(commonGroup);
337 // add actions that become macro and command invocations
338 std::vector<SGPropertyNode_ptr> actions;
339 actions = getConfig()->getChildren("action");
340 for (unsigned int i = 0; i < actions.size(); ++i)
341 ud->addPickCallback(new PickCallback(actions[i], getModelRoot()));
342 // Look for the VNC sessions that want raw mouse input
343 actions = getConfig()->getChildren("vncaction");
344 for (unsigned int i = 0; i < actions.size(); ++i)
345 ud->addPickCallback(new VncCallback(actions[i], getModelRoot(),
351 ///////////////////////////////////////////////////////////////////////////
353 // insert count copies of binding list A, into the output list.
354 // effectively makes the output list fire binding A multiple times
356 static void repeatBindings(const SGBindingList& a, SGBindingList& out, int count)
359 for (int i=0; i<count; ++i) {
360 out.insert(out.end(), a.begin(), a.end());
364 static bool static_knobMouseWheelAlternateDirection = false;
366 class SGKnobAnimation::KnobPickCallback : public SGPickCallback {
368 KnobPickCallback(const SGPropertyNode* configNode,
369 SGPropertyNode* modelRoot) :
370 _direction(DIRECTION_NONE),
371 _repeatInterval(configNode->getDoubleValue("interval-sec", 0.1)),
372 _stickyShifted(false)
374 readOptionalBindingList(configNode, modelRoot, "action", _action);
375 readOptionalBindingList(configNode, modelRoot, "cw", _bindingsCW);
376 readOptionalBindingList(configNode, modelRoot, "ccw", _bindingsCCW);
378 readOptionalBindingList(configNode, modelRoot, "release", _releaseAction);
379 readOptionalBindingList(configNode, modelRoot, "hovered", _hover);
381 if (configNode->hasChild("shift-action") || configNode->hasChild("shift-cw") ||
382 configNode->hasChild("shift-ccw"))
384 // explicit shifted behaviour - just do exactly what was provided
385 readOptionalBindingList(configNode, modelRoot, "shift-action", _shiftedAction);
386 readOptionalBindingList(configNode, modelRoot, "shift-cw", _shiftedCW);
387 readOptionalBindingList(configNode, modelRoot, "shift-ccw", _shiftedCCW);
389 // default shifted behaviour - repeat normal bindings N times.
390 int shiftRepeat = configNode->getIntValue("shift-repeat", 10);
391 repeatBindings(_action, _shiftedAction, shiftRepeat);
392 repeatBindings(_bindingsCW, _shiftedCW, shiftRepeat);
393 repeatBindings(_bindingsCCW, _shiftedCCW, shiftRepeat);
394 } // of default shifted behaviour
397 virtual bool buttonPressed(int button, const osgGA::GUIEventAdapter* ea, const Info&)
399 _stickyShifted = ea->getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_SHIFT;
401 // the 'be nice to Mac / laptop' users option; alt-clicking spins the
402 // opposite direction. Should make this configurable
403 if ((button == 0) && (ea->getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_ALT)) {
407 int increaseMouseWheel = static_knobMouseWheelAlternateDirection ? 3 : 4;
408 int decreaseMouseWheel = static_knobMouseWheelAlternateDirection ? 4 : 3;
410 _direction = DIRECTION_NONE;
411 if ((button == 0) || (button == increaseMouseWheel)) {
412 _direction = DIRECTION_CLOCKWISE;
413 } else if ((button == 1) || (button == decreaseMouseWheel)) {
414 _direction = DIRECTION_ANTICLOCKWISE;
419 _repeatTime = -_repeatInterval; // anti-bobble: delay start of repeat
420 fire(_stickyShifted);
424 virtual void buttonReleased(void)
426 fireBindingList(_releaseAction);
429 virtual void update(double dt)
432 while (_repeatInterval < _repeatTime) {
433 _repeatTime -= _repeatInterval;
434 fire(_stickyShifted);
435 } // of repeat iteration
438 virtual bool hover(const osg::Vec2d& windowPos, const Info& info)
440 if (_hover.empty()) {
444 SGPropertyNode_ptr params(new SGPropertyNode);
445 params->setDoubleValue("x", windowPos.x());
446 params->setDoubleValue("y", windowPos.y());
447 fireBindingList(_hover, params.ptr());
451 void fire(bool isShifted)
453 const SGBindingList& act(isShifted ? _shiftedAction : _action);
454 const SGBindingList& cw(isShifted ? _shiftedCW : _bindingsCW);
455 const SGBindingList& ccw(isShifted ? _shiftedCCW : _bindingsCCW);
457 switch (_direction) {
458 case DIRECTION_CLOCKWISE:
459 fireBindingListWithOffset(act, 1, 1);
462 case DIRECTION_ANTICLOCKWISE:
463 fireBindingListWithOffset(act, -1, 1);
464 fireBindingList(ccw);
470 SGBindingList _action, _shiftedAction;
471 SGBindingList _releaseAction;
472 SGBindingList _bindingsCW, _shiftedCW,
473 _bindingsCCW, _shiftedCCW;
474 SGBindingList _hover;
480 DIRECTION_ANTICLOCKWISE
483 Direction _direction;
484 double _repeatInterval;
487 // FIXME - would be better to pass the current modifier state
488 // into update(), but for now let's make it sticky
492 class SGKnobAnimation::UpdateCallback : public osg::NodeCallback {
494 UpdateCallback(SGExpressiond const* animationValue) :
495 _animationValue(animationValue)
497 setName("SGKnobAnimation::UpdateCallback");
499 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
501 SGRotateTransform* transform = static_cast<SGRotateTransform*>(node);
502 transform->setAngleDeg(_animationValue->getValue());
507 SGSharedPtr<SGExpressiond const> _animationValue;
511 SGKnobAnimation::SGKnobAnimation(const SGPropertyNode* configNode,
512 SGPropertyNode* modelRoot) :
513 SGPickAnimation(configNode, modelRoot)
515 SGSharedPtr<SGExpressiond> value = read_value(configNode, modelRoot, "-deg",
516 -SGLimitsd::max(), SGLimitsd::max());
517 _animationValue = value->simplify();
520 readRotationCenterAndAxis(configNode, _center, _axis);
525 SGKnobAnimation::createAnimationGroup(osg::Group& parent)
527 SGRotateTransform* transform = new SGRotateTransform();
528 innerSetupPickGroup(transform, parent);
530 UpdateCallback* uc = new UpdateCallback(_animationValue);
531 transform->setUpdateCallback(uc);
532 transform->setCenter(_center);
533 transform->setAxis(_axis);
535 SGSceneUserData* ud = SGSceneUserData::getOrCreateSceneUserData(transform);
536 ud->setPickCallback(new KnobPickCallback(getConfig(), getModelRoot()));
541 void SGKnobAnimation::setAlternateMouseWheelDirection(bool aToggle)
543 static_knobMouseWheelAlternateDirection = aToggle;