]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGPickAnimation.cxx
68da16fbd13847520c911c0d8a8c2372560081b4
[simgear.git] / simgear / scene / model / SGPickAnimation.cxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2013 James Turner
4  *
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.
9  *
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.
14  *
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,
18  * MA 02110-1301, USA.
19  *
20  */
21      
22      
23 #include <simgear/scene/model/SGPickAnimation.hxx>
24
25 #include <osg/Geode>
26 #include <osg/PolygonOffset>
27 #include <osg/PolygonMode>
28 #include <osg/Material>
29 #include <osgGA/GUIEventAdapter>
30
31 #include <simgear/sg_inlines.h>
32 #include <simgear/scene/util/SGPickCallback.hxx>
33 #include <simgear/scene/material/EffectGeode.hxx>
34 #include <simgear/scene/util/SGSceneUserData.hxx>
35 #include <simgear/structure/SGBinding.hxx>
36 #include <simgear/scene/util/StateAttributeFactory.hxx>
37 #include <simgear/scene/model/SGRotateTransform.hxx>
38 #include <simgear/scene/model/SGTranslateTransform.hxx>
39
40 using namespace simgear;
41
42 using OpenThreads::Mutex;
43 using OpenThreads::ScopedLock;
44
45 static void readOptionalBindingList(const SGPropertyNode* aNode, SGPropertyNode* modelRoot,
46     const std::string& aName, SGBindingList& aBindings)
47 {
48     const SGPropertyNode* n = aNode->getChild(aName);
49     if (n)
50         aBindings = readBindingList(n->getChildren("binding"), modelRoot);
51     
52 }
53
54
55 osg::Vec2d eventToWindowCoords(const osgGA::GUIEventAdapter* ea)
56 {
57     using namespace osg;
58     const GraphicsContext* gc = ea->getGraphicsContext();
59     const GraphicsContext::Traits* traits = gc->getTraits() ;
60     // Scale x, y to the dimensions of the window
61     double x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
62          * (double)traits->width);
63     double y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
64          * (double)traits->height);
65     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
66         y = (double)traits->height - y;
67     
68     return osg::Vec2d(x, y);
69 }
70
71  class SGPickAnimation::PickCallback : public SGPickCallback {
72  public:
73    PickCallback(const SGPropertyNode* configNode,
74                 SGPropertyNode* modelRoot) :
75      SGPickCallback(PriorityPanel),
76      _repeatable(configNode->getBoolValue("repeatable", false)),
77      _repeatInterval(configNode->getDoubleValue("interval-sec", 0.1))
78    {
79      std::vector<SGPropertyNode_ptr> bindings;
80
81      bindings = configNode->getChildren("button");
82      for (unsigned int i = 0; i < bindings.size(); ++i) {
83        _buttons.insert( bindings[i]->getIntValue() );
84      }
85
86      _bindingsDown = readBindingList(configNode->getChildren("binding"), modelRoot);
87      readOptionalBindingList(configNode, modelRoot, "mod-up", _bindingsUp);
88      
89      
90      if (configNode->hasChild("cursor")) {
91        _cursorName = configNode->getStringValue("cursor");
92      }
93    }
94      
95      void addHoverBindings(const SGPropertyNode* hoverNode,
96                              SGPropertyNode* modelRoot)
97      {
98          _hover = readBindingList(hoverNode->getChildren("binding"), modelRoot);
99      }
100      
101    virtual bool buttonPressed(int button, const osgGA::GUIEventAdapter* ea, const Info&)
102    {
103        if (_buttons.find(button) == _buttons.end()) {
104            return false;
105        }
106        
107      fireBindingList(_bindingsDown);
108      _repeatTime = -_repeatInterval;    // anti-bobble: delay start of repeat
109      return true;
110    }
111    virtual void buttonReleased(int keyModState)
112    {
113        SG_UNUSED(keyModState);
114        fireBindingList(_bindingsUp);
115    }
116      
117    virtual void update(double dt, int keyModState)
118    {
119      SG_UNUSED(keyModState);
120      if (!_repeatable)
121        return;
122
123      _repeatTime += dt;
124      while (_repeatInterval < _repeatTime) {
125        _repeatTime -= _repeatInterval;
126          fireBindingList(_bindingsDown);
127      }
128    }
129    
130    virtual bool hover(const osg::Vec2d& windowPos, const Info& info)
131    {
132        if (_hover.empty()) {
133            return false;
134        }
135        
136        SGPropertyNode_ptr params(new SGPropertyNode);
137        params->setDoubleValue("x", windowPos.x());
138        params->setDoubleValue("y", windowPos.y());
139        fireBindingList(_hover, params.ptr());
140        return true;
141    }
142    
143    std::string getCursor() const
144    { return _cursorName; }
145  private:
146    SGBindingList _bindingsDown;
147    SGBindingList _bindingsUp;
148    SGBindingList _hover;
149    std::set<int> _buttons;
150    bool _repeatable;
151    double _repeatInterval;
152    double _repeatTime;
153    std::string _cursorName;
154  };
155
156  class VncVisitor : public osg::NodeVisitor {
157   public:
158    VncVisitor(double x, double y, int mask) :
159      osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
160      _texX(x), _texY(y), _mask(mask), _done(false)
161    {
162      SG_LOG(SG_INPUT, SG_DEBUG, "VncVisitor constructor "
163        << x << "," << y << " mask " << mask);
164    }
165
166    virtual void apply(osg::Node &node)
167    {
168      // Some nodes have state sets attached
169      touchStateSet(node.getStateSet());
170      if (!_done)
171        traverse(node);
172      if (_done) return;
173      // See whether we are a geode worth exploring
174      osg::Geode *g = dynamic_cast<osg::Geode*>(&node);
175      if (!g) return;
176      // Go find all its drawables
177      int i = g->getNumDrawables();
178      while (--i >= 0) {
179        osg::Drawable *d = g->getDrawable(i);
180        if (d) touchDrawable(*d);
181      }
182      // Out of optimism, do the same for EffectGeode
183      simgear::EffectGeode *eg = dynamic_cast<simgear::EffectGeode*>(&node);
184      if (!eg) return;
185      for (simgear::EffectGeode::DrawablesIterator di = eg->drawablesBegin();
186           di != eg->drawablesEnd(); di++) {
187        touchDrawable(**di);
188      }
189      // Now see whether the EffectGeode has an Effect
190      simgear::Effect *e = eg->getEffect();
191      if (e) {
192        touchStateSet(e->getDefaultStateSet());
193      }
194    }
195
196    inline void touchDrawable(osg::Drawable &d)
197    {
198      osg::StateSet *ss = d.getStateSet();
199      touchStateSet(ss);
200    }
201
202    void touchStateSet(osg::StateSet *ss)
203    {
204      if (!ss) return;
205      osg::StateAttribute *sa = ss->getTextureAttribute(0,
206        osg::StateAttribute::TEXTURE);
207      if (!sa) return;
208      osg::Texture *t = sa->asTexture();
209      if (!t) return;
210      osg::Image *img = t->getImage(0);
211      if (!img) return;
212      if (!_done) {
213        int pixX = _texX * img->s();
214        int pixY = _texY * img->t();
215        _done = img->sendPointerEvent(pixX, pixY, _mask);
216        SG_LOG(SG_INPUT, SG_DEBUG, "VncVisitor image said " << _done
217          << " to coord " << pixX << "," << pixY);
218      }
219    }
220
221    inline bool wasSuccessful()
222    {
223      return _done;
224    }
225
226   private:
227    double _texX, _texY;
228    int _mask;
229    bool _done;
230  };
231
232 ///////////////////////////////////////////////////////////////////////////////
233
234  class SGPickAnimation::VncCallback : public SGPickCallback {
235  public:
236    VncCallback(const SGPropertyNode* configNode,
237                 SGPropertyNode* modelRoot,
238                 osg::Group *node)
239        : _node(node)
240    {
241      SG_LOG(SG_INPUT, SG_DEBUG, "Configuring VNC callback");
242      const char *cornernames[3] = {"top-left", "top-right", "bottom-left"};
243      SGVec3d *cornercoords[3] = {&_topLeft, &_toRight, &_toDown};
244      for (int c =0; c < 3; c++) {
245        const SGPropertyNode* cornerNode = configNode->getChild(cornernames[c]);
246        *cornercoords[c] = SGVec3d(
247          cornerNode->getDoubleValue("x"),
248          cornerNode->getDoubleValue("y"),
249          cornerNode->getDoubleValue("z"));
250      }
251      _toRight -= _topLeft;
252      _toDown -= _topLeft;
253      _squaredRight = dot(_toRight, _toRight);
254      _squaredDown = dot(_toDown, _toDown);
255    }
256
257    virtual bool buttonPressed(int button, const osgGA::GUIEventAdapter* ea, const Info& info)
258    {
259      SGVec3d loc(info.local);
260      SG_LOG(SG_INPUT, SG_DEBUG, "VNC pressed " << button << ": " << loc);
261      loc -= _topLeft;
262      _x = dot(loc, _toRight) / _squaredRight;
263      _y = dot(loc, _toDown) / _squaredDown;
264      if (_x<0) _x = 0; else if (_x > 1) _x = 1;
265      if (_y<0) _y = 0; else if (_y > 1) _y = 1;
266      VncVisitor vv(_x, _y, 1 << button);
267      _node->accept(vv);
268      return vv.wasSuccessful();
269
270    }
271    virtual void buttonReleased(int keyModState)
272    {
273      SG_UNUSED(keyModState);
274      SG_LOG(SG_INPUT, SG_DEBUG, "VNC release");
275      VncVisitor vv(_x, _y, 0);
276      _node->accept(vv);
277    }
278
279  private:
280    double _x, _y;
281    osg::ref_ptr<osg::Group> _node;
282    SGVec3d _topLeft, _toRight, _toDown;
283    double _squaredRight, _squaredDown;
284  };
285
286 ///////////////////////////////////////////////////////////////////////////////
287
288  SGPickAnimation::SGPickAnimation(const SGPropertyNode* configNode,
289                                   SGPropertyNode* modelRoot) :
290    SGAnimation(configNode, modelRoot)
291  {
292  }
293
294  namespace
295  {
296  OpenThreads::Mutex colorModeUniformMutex;
297  osg::ref_ptr<osg::Uniform> colorModeUniform;
298  }
299
300
301 void 
302 SGPickAnimation::innerSetupPickGroup(osg::Group* commonGroup, osg::Group& parent)
303 {
304     // Contains the normal geometry that is interactive
305     osg::ref_ptr<osg::Group> normalGroup = new osg::Group;
306     normalGroup->setName("pick normal group");
307     normalGroup->addChild(commonGroup);
308     
309     // Used to render the geometry with just yellow edges
310     osg::Group* highlightGroup = new osg::Group;
311     highlightGroup->setName("pick highlight group");
312     highlightGroup->setNodeMask(simgear::PICK_BIT);
313     highlightGroup->addChild(commonGroup);
314     
315     // prepare a state set that paints the edges of this object yellow
316     // The material and texture attributes are set with
317     // OVERRIDE|PROTECTED in case there is a material animation on a
318     // higher node in the scene graph, which would have its material
319     // attribute set with OVERRIDE.
320     osg::StateSet* stateSet = highlightGroup->getOrCreateStateSet();
321     osg::Texture2D* white = StateAttributeFactory::instance()->getWhiteTexture();
322     stateSet->setTextureAttributeAndModes(0, white,
323                                           (osg::StateAttribute::ON
324                                            | osg::StateAttribute::OVERRIDE
325                                            | osg::StateAttribute::PROTECTED));
326     osg::PolygonOffset* polygonOffset = new osg::PolygonOffset;
327     polygonOffset->setFactor(-1);
328     polygonOffset->setUnits(-1);
329     stateSet->setAttribute(polygonOffset, osg::StateAttribute::OVERRIDE);
330     stateSet->setMode(GL_POLYGON_OFFSET_LINE,
331                       osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
332     osg::PolygonMode* polygonMode = new osg::PolygonMode;
333     polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
334                          osg::PolygonMode::LINE);
335     stateSet->setAttribute(polygonMode, osg::StateAttribute::OVERRIDE);
336     osg::Material* material = new osg::Material;
337     material->setColorMode(osg::Material::OFF);
338     material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4f(0, 0, 0, 1));
339     // XXX Alpha < 1.0 in the diffuse material value is a signal to the
340     // default shader to take the alpha value from the material value
341     // and not the glColor. In many cases the pick animation geometry is
342     // transparent, so the outline would not be visible without this hack.
343     material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(0, 0, 0, .95));
344     material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4f(1, 1, 0, 1));
345     material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4f(0, 0, 0, 0));
346     stateSet->setAttribute(
347                            material, osg::StateAttribute::OVERRIDE | osg::StateAttribute::PROTECTED);
348     // The default shader has a colorMode uniform that mimics the
349     // behavior of Material color mode.
350     osg::Uniform* cmUniform = 0;
351     {
352         ScopedLock<Mutex> lock(colorModeUniformMutex);
353         if (!colorModeUniform.valid()) {
354             colorModeUniform = new osg::Uniform(osg::Uniform::INT, "colorMode");
355             colorModeUniform->set(0); // MODE_OFF
356             colorModeUniform->setDataVariance(osg::Object::STATIC);
357         }
358         cmUniform = colorModeUniform.get();
359     }
360     stateSet->addUniform(cmUniform,
361                          osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
362     
363     // Only add normal geometry if configured
364     if (getConfig()->getBoolValue("visible", true))
365         parent.addChild(normalGroup.get());
366     parent.addChild(highlightGroup);
367 }
368
369  osg::Group*
370  SGPickAnimation::createAnimationGroup(osg::Group& parent)
371  {
372      osg::Group* commonGroup = new osg::Group;
373      innerSetupPickGroup(commonGroup, parent);
374      SGSceneUserData* ud = SGSceneUserData::getOrCreateSceneUserData(commonGroup);
375
376      PickCallback* pickCb = NULL;
377      
378    // add actions that become macro and command invocations
379    std::vector<SGPropertyNode_ptr> actions;
380    actions = getConfig()->getChildren("action");
381    for (unsigned int i = 0; i < actions.size(); ++i) {
382      pickCb = new PickCallback(actions[i], getModelRoot());
383      ud->addPickCallback(pickCb);
384    }
385      
386    if (getConfig()->hasChild("hovered")) {
387      if (!pickCb) {
388        // make a trivial PickCallback to hang the hovered off of
389        SGPropertyNode_ptr dummyNode(new SGPropertyNode);
390        pickCb = new PickCallback(dummyNode.ptr(), getModelRoot());
391      }
392        
393      pickCb->addHoverBindings(getConfig()->getNode("hovered"), getModelRoot());
394    }
395      
396    // Look for the VNC sessions that want raw mouse input
397    actions = getConfig()->getChildren("vncaction");
398    for (unsigned int i = 0; i < actions.size(); ++i)
399      ud->addPickCallback(new VncCallback(actions[i], getModelRoot(),
400        &parent));
401
402    return commonGroup;
403 }
404
405 ///////////////////////////////////////////////////////////////////////////
406
407 // insert count copies of binding list A, into the output list.
408 // effectively makes the output list fire binding A multiple times
409 // in sequence
410 static void repeatBindings(const SGBindingList& a, SGBindingList& out, int count)
411 {
412     out.clear();
413     for (int i=0; i<count; ++i) {
414         out.insert(out.end(), a.begin(), a.end());
415     }
416 }
417
418 static bool static_knobMouseWheelAlternateDirection = false;
419 static bool static_knobDragAlternateAxis = false;
420 static double static_dragSensitivity = 1.0;
421
422 class KnobSliderPickCallback : public SGPickCallback {
423 public:
424     enum Direction
425     {
426         DIRECTION_NONE,
427         DIRECTION_INCREASE,
428         DIRECTION_DECREASE
429     };
430     
431     enum DragDirection
432     {
433         DRAG_DEFAULT = 0,
434         DRAG_VERTICAL,
435         DRAG_HORIZONTAL
436     };
437
438     
439     KnobSliderPickCallback(const SGPropertyNode* configNode,
440                  SGPropertyNode* modelRoot) :
441         SGPickCallback(PriorityPanel),
442         _direction(DIRECTION_NONE),
443         _repeatInterval(configNode->getDoubleValue("interval-sec", 0.1)),
444         _dragDirection(DRAG_DEFAULT)
445     {
446         readOptionalBindingList(configNode, modelRoot, "action", _action);
447         readOptionalBindingList(configNode, modelRoot, "increase", _bindingsIncrease);
448         readOptionalBindingList(configNode, modelRoot, "decrease", _bindingsDecrease);
449         
450         readOptionalBindingList(configNode, modelRoot, "release", _releaseAction);
451         readOptionalBindingList(configNode, modelRoot, "hovered", _hover);
452         
453         if (configNode->hasChild("shift-action") || configNode->hasChild("shift-increase") ||
454             configNode->hasChild("shift-decrease"))
455         {
456         // explicit shifted behaviour - just do exactly what was provided
457             readOptionalBindingList(configNode, modelRoot, "shift-action", _shiftedAction);
458             readOptionalBindingList(configNode, modelRoot, "shift-increase", _shiftedIncrease);
459             readOptionalBindingList(configNode, modelRoot, "shift-decrease", _shiftedDecrease);
460         } else {
461             // default shifted behaviour - repeat normal bindings N times.
462             int shiftRepeat = configNode->getIntValue("shift-repeat", 10);
463             repeatBindings(_action, _shiftedAction, shiftRepeat);
464             repeatBindings(_bindingsIncrease, _shiftedIncrease, shiftRepeat);
465             repeatBindings(_bindingsDecrease, _shiftedDecrease, shiftRepeat);
466         } // of default shifted behaviour
467         
468         _dragScale = configNode->getDoubleValue("drag-scale-px", 10.0);
469         std::string dragDir = configNode->getStringValue("drag-direction");
470         if (dragDir == "vertical") {
471             _dragDirection = DRAG_VERTICAL;
472         } else if (dragDir == "horizontal") {
473             _dragDirection = DRAG_HORIZONTAL;
474         }
475       
476         if (configNode->hasChild("cursor")) {
477             _cursorName = configNode->getStringValue("cursor");
478         } else {
479           DragDirection dir = effectiveDragDirection();
480           if (dir == DRAG_VERTICAL) {
481             _cursorName = "drag-vertical";
482           } else if (dir == DRAG_HORIZONTAL) {
483             _cursorName = "drag-horizontal";
484           }
485         }
486     }
487     
488     virtual bool buttonPressed(int button, const osgGA::GUIEventAdapter* ea, const Info&)
489     {        
490         // the 'be nice to Mac / laptop' users option; alt-clicking spins the
491         // opposite direction. Should make this configurable
492         if ((button == 0) && (ea->getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_ALT)) {
493             button = 1;
494         }
495         
496         int increaseMouseWheel = static_knobMouseWheelAlternateDirection ? 3 : 4;
497         int decreaseMouseWheel = static_knobMouseWheelAlternateDirection ? 4 : 3;
498             
499         _direction = DIRECTION_NONE;
500         if ((button == 0) || (button == increaseMouseWheel)) {
501             _direction = DIRECTION_INCREASE;
502         } else if ((button == 1) || (button == decreaseMouseWheel)) {
503             _direction = DIRECTION_DECREASE;
504         } else {
505             return false;
506         }
507         
508         _lastFirePos = eventToWindowCoords(ea);
509     // delay start of repeat, makes dragging more usable
510         _repeatTime = -_repeatInterval;    
511         _hasDragged = false;
512         return true;
513     }
514     
515     virtual void buttonReleased(int keyModState)
516     {
517         // for *clicks*, we only fire on button release
518         if (!_hasDragged) {
519             fire(keyModState & osgGA::GUIEventAdapter::MODKEY_SHIFT, _direction);
520         }
521         
522         fireBindingList(_releaseAction);
523     }
524   
525     DragDirection effectiveDragDirection() const
526     {
527       if (_dragDirection == DRAG_DEFAULT) {
528         // respect the current default settings - this allows runtime
529         // setting of the default drag direction.
530         return static_knobDragAlternateAxis ? DRAG_VERTICAL : DRAG_HORIZONTAL;
531       }
532       
533       return _dragDirection;
534   }
535   
536     virtual void mouseMoved(const osgGA::GUIEventAdapter* ea)
537     {
538         _mousePos = eventToWindowCoords(ea);
539         osg::Vec2d deltaMouse = _mousePos - _lastFirePos;
540         
541         if (!_hasDragged) {
542             
543             double manhattanDist = deltaMouse.x() * deltaMouse.x()  + deltaMouse.y() * deltaMouse.y();
544             if (manhattanDist < 5) {
545                 // don't do anything, just input noise
546                 return;
547             }
548             
549         // user is dragging, disable repeat behaviour
550             _hasDragged = true;
551         }
552       
553         double delta = (effectiveDragDirection() == DRAG_VERTICAL) ? deltaMouse.y() : deltaMouse.x();
554     // per-animation scale factor lets the aircraft author tune for expectations,
555     // eg heading setting vs 5-state switch.
556     // then we scale by a global sensitivity, which the user can set.
557         delta *= static_dragSensitivity / _dragScale;
558         
559         if (fabs(delta) >= 1.0) {
560             // determine direction from sign of delta
561             Direction dir = (delta > 0.0) ? DIRECTION_INCREASE : DIRECTION_DECREASE;
562             fire(ea->getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_SHIFT, dir);
563             _lastFirePos = _mousePos;
564         }
565     }
566     
567     virtual void update(double dt, int keyModState)
568     {
569         if (_hasDragged) {
570             return;
571         }
572         
573         _repeatTime += dt;
574         while (_repeatInterval < _repeatTime) {
575             _repeatTime -= _repeatInterval;
576             fire(keyModState & osgGA::GUIEventAdapter::MODKEY_SHIFT, _direction);
577         } // of repeat iteration
578     }
579
580     virtual bool hover(const osg::Vec2d& windowPos, const Info& info)
581     {
582         if (_hover.empty()) {
583             return false;
584         }
585        
586         SGPropertyNode_ptr params(new SGPropertyNode);
587         params->setDoubleValue("x", windowPos.x());
588         params->setDoubleValue("y", windowPos.y());
589         fireBindingList(_hover, params.ptr());
590         return true;
591     }
592   
593     void setCursor(const std::string& aName)
594     {
595       _cursorName = aName;
596     }
597   
598     virtual std::string getCursor() const
599     { return _cursorName; }
600   
601 private:
602     void fire(bool isShifted, Direction dir)
603     {
604         const SGBindingList& act(isShifted ? _shiftedAction : _action);
605         const SGBindingList& incr(isShifted ? _shiftedIncrease : _bindingsIncrease);
606         const SGBindingList& decr(isShifted ? _shiftedDecrease : _bindingsDecrease);
607         
608         switch (dir) {
609             case DIRECTION_INCREASE:
610                 fireBindingListWithOffset(act,  1, 1);
611                 fireBindingList(incr);
612                 break;
613             case DIRECTION_DECREASE:
614                 fireBindingListWithOffset(act, -1, 1);
615                 fireBindingList(decr);
616                 break;
617             default: break;
618         }
619     }
620     
621     SGBindingList _action, _shiftedAction;
622     SGBindingList _releaseAction;
623     SGBindingList _bindingsIncrease, _shiftedIncrease,
624         _bindingsDecrease, _shiftedDecrease;
625     SGBindingList _hover;
626     
627         
628     Direction _direction;
629     double _repeatInterval;
630     double _repeatTime;
631     
632     DragDirection _dragDirection;
633     bool _hasDragged; ///< has the mouse been dragged since the press?
634     osg::Vec2d _mousePos, ///< current window coords location of the mouse
635         _lastFirePos; ///< mouse location where we last fired the bindings
636     double _dragScale;
637   
638     std::string _cursorName;
639 };
640
641 ///////////////////////////////////////////////////////////////////////////////
642
643 class SGKnobAnimation::UpdateCallback : public osg::NodeCallback {
644 public:
645     UpdateCallback(SGExpressiond const* animationValue) :
646         _animationValue(animationValue)
647     {
648         setName("SGKnobAnimation::UpdateCallback");
649     }
650     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
651     {
652         SGRotateTransform* transform = static_cast<SGRotateTransform*>(node);
653         transform->setAngleDeg(_animationValue->getValue());
654         traverse(node, nv);
655     }
656     
657 private:
658     SGSharedPtr<SGExpressiond const> _animationValue;
659 };
660
661
662 SGKnobAnimation::SGKnobAnimation(const SGPropertyNode* configNode,
663                                  SGPropertyNode* modelRoot) :
664     SGPickAnimation(configNode, modelRoot)
665 {
666     SGSharedPtr<SGExpressiond> value = read_value(configNode, modelRoot, "-deg",
667                                                   -SGLimitsd::max(), SGLimitsd::max());
668     _animationValue = value->simplify();
669     
670     
671     readRotationCenterAndAxis(configNode, _center, _axis);
672 }
673
674
675 osg::Group*
676 SGKnobAnimation::createAnimationGroup(osg::Group& parent)
677 {
678     SGRotateTransform* transform = new SGRotateTransform();
679     innerSetupPickGroup(transform, parent);
680     
681     UpdateCallback* uc = new UpdateCallback(_animationValue);
682     transform->setUpdateCallback(uc);
683     transform->setCenter(_center);
684     transform->setAxis(_axis);
685         
686     SGSceneUserData* ud = SGSceneUserData::getOrCreateSceneUserData(transform);
687     ud->setPickCallback(new KnobSliderPickCallback(getConfig(), getModelRoot()));
688     
689     return transform;
690 }
691
692 void SGKnobAnimation::setAlternateMouseWheelDirection(bool aToggle)
693 {
694     static_knobMouseWheelAlternateDirection = aToggle;
695 }
696
697 void SGKnobAnimation::setAlternateDragAxis(bool aToggle)
698 {
699     static_knobDragAlternateAxis = aToggle;
700 }
701
702 void SGKnobAnimation::setDragSensitivity(double aFactor)
703 {
704     static_dragSensitivity = aFactor;
705 }
706
707 ///////////////////////////////////////////////////////////////////////////////
708
709 class SGSliderAnimation::UpdateCallback : public osg::NodeCallback {
710 public:
711     UpdateCallback(SGExpressiond const* animationValue) :
712     _animationValue(animationValue)
713     {
714         setName("SGSliderAnimation::UpdateCallback");
715     }
716     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
717     {
718         SGTranslateTransform* transform = static_cast<SGTranslateTransform*>(node);
719         transform->setValue(_animationValue->getValue());
720
721         traverse(node, nv);
722     }
723     
724 private:
725     SGSharedPtr<SGExpressiond const> _animationValue;
726 };
727
728
729 SGSliderAnimation::SGSliderAnimation(const SGPropertyNode* configNode,
730                                  SGPropertyNode* modelRoot) :
731     SGPickAnimation(configNode, modelRoot)
732 {
733     SGSharedPtr<SGExpressiond> value = read_value(configNode, modelRoot, "-m",
734                                                   -SGLimitsd::max(), SGLimitsd::max());
735     _animationValue = value->simplify();
736     
737     _axis = readTranslateAxis(configNode);
738 }
739
740 osg::Group*
741 SGSliderAnimation::createAnimationGroup(osg::Group& parent)
742 {
743     SGTranslateTransform* transform = new SGTranslateTransform();
744     innerSetupPickGroup(transform, parent);
745     
746     UpdateCallback* uc = new UpdateCallback(_animationValue);
747     transform->setUpdateCallback(uc);
748     transform->setAxis(_axis);
749     
750     SGSceneUserData* ud = SGSceneUserData::getOrCreateSceneUserData(transform);
751     ud->setPickCallback(new KnobSliderPickCallback(getConfig(), getModelRoot()));
752     
753     return transform;
754 }