#include <osg/PolygonMode>
#include <osg/Material>
#include <osgGA/GUIEventAdapter>
-
+
+#include <simgear/sg_inlines.h>
#include <simgear/scene/util/SGPickCallback.hxx>
#include <simgear/scene/material/EffectGeode.hxx>
#include <simgear/scene/util/SGSceneUserData.hxx>
#include <simgear/structure/SGBinding.hxx>
#include <simgear/scene/util/StateAttributeFactory.hxx>
#include <simgear/scene/model/SGRotateTransform.hxx>
+#include <simgear/scene/model/SGTranslateTransform.hxx>
using namespace simgear;
}
+
+osg::Vec2d eventToWindowCoords(const osgGA::GUIEventAdapter* ea)
+{
+ using namespace osg;
+ const GraphicsContext* gc = ea->getGraphicsContext();
+ const GraphicsContext::Traits* traits = gc->getTraits() ;
+ // Scale x, y to the dimensions of the window
+ double x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
+ * (double)traits->width);
+ double y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
+ * (double)traits->height);
+ if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
+ y = (double)traits->height - y;
+
+ return osg::Vec2d(x, y);
+}
+
class SGPickAnimation::PickCallback : public SGPickCallback {
public:
PickCallback(const SGPropertyNode* configNode,
_repeatTime = -_repeatInterval; // anti-bobble: delay start of repeat
return true;
}
- virtual void buttonReleased(void)
+ virtual void buttonReleased(int keyModState)
{
+ SG_UNUSED(keyModState);
fireBindingList(_bindingsUp);
}
- virtual void update(double dt)
+
+ virtual void update(double dt, int keyModState)
{
+ SG_UNUSED(keyModState);
if (!_repeatable)
return;
bool _done;
};
+///////////////////////////////////////////////////////////////////////////////
class SGPickAnimation::VncCallback : public SGPickCallback {
public:
return vv.wasSuccessful();
}
- virtual void buttonReleased(void)
+ virtual void buttonReleased(int keyModState)
{
+ SG_UNUSED(keyModState);
SG_LOG(SG_INPUT, SG_DEBUG, "VNC release");
VncVisitor vv(_x, _y, 0);
_node->accept(vv);
}
- virtual void update(double dt)
- {
- }
+
private:
double _x, _y;
osg::ref_ptr<osg::Group> _node;
double _squaredRight, _squaredDown;
};
+///////////////////////////////////////////////////////////////////////////////
+
SGPickAnimation::SGPickAnimation(const SGPropertyNode* configNode,
SGPropertyNode* modelRoot) :
SGAnimation(configNode, modelRoot)
}
static bool static_knobMouseWheelAlternateDirection = false;
+static bool static_knobDragAlternateAxis = false;
-class SGKnobAnimation::KnobPickCallback : public SGPickCallback {
+class KnobSliderPickCallback : public SGPickCallback {
public:
- KnobPickCallback(const SGPropertyNode* configNode,
+ KnobSliderPickCallback(const SGPropertyNode* configNode,
SGPropertyNode* modelRoot) :
SGPickCallback(PriorityPanel),
_direction(DIRECTION_NONE),
- _repeatInterval(configNode->getDoubleValue("interval-sec", 0.1)),
- _stickyShifted(false)
+ _repeatInterval(configNode->getDoubleValue("interval-sec", 0.1))
{
readOptionalBindingList(configNode, modelRoot, "action", _action);
- readOptionalBindingList(configNode, modelRoot, "cw", _bindingsCW);
- readOptionalBindingList(configNode, modelRoot, "ccw", _bindingsCCW);
+ readOptionalBindingList(configNode, modelRoot, "increase", _bindingsCW);
+ readOptionalBindingList(configNode, modelRoot, "decrease", _bindingsCCW);
readOptionalBindingList(configNode, modelRoot, "release", _releaseAction);
readOptionalBindingList(configNode, modelRoot, "hovered", _hover);
- if (configNode->hasChild("shift-action") || configNode->hasChild("shift-cw") ||
- configNode->hasChild("shift-ccw"))
+ if (configNode->hasChild("shift-action") || configNode->hasChild("shift-increase") ||
+ configNode->hasChild("shift-decrease"))
{
// explicit shifted behaviour - just do exactly what was provided
readOptionalBindingList(configNode, modelRoot, "shift-action", _shiftedAction);
- readOptionalBindingList(configNode, modelRoot, "shift-cw", _shiftedCW);
- readOptionalBindingList(configNode, modelRoot, "shift-ccw", _shiftedCCW);
+ readOptionalBindingList(configNode, modelRoot, "shift-increase", _shiftedCW);
+ readOptionalBindingList(configNode, modelRoot, "shift-decrease", _shiftedCCW);
} else {
// default shifted behaviour - repeat normal bindings N times.
int shiftRepeat = configNode->getIntValue("shift-repeat", 10);
repeatBindings(_bindingsCW, _shiftedCW, shiftRepeat);
repeatBindings(_bindingsCCW, _shiftedCCW, shiftRepeat);
} // of default shifted behaviour
+
+ _dragScale = configNode->getDoubleValue("drag-scale-px", 10.0);
}
virtual bool buttonPressed(int button, const osgGA::GUIEventAdapter* ea, const Info&)
- {
- _stickyShifted = ea->getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_SHIFT;
-
+ {
// the 'be nice to Mac / laptop' users option; alt-clicking spins the
// opposite direction. Should make this configurable
if ((button == 0) && (ea->getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_ALT)) {
return false;
}
- _repeatTime = -_repeatInterval; // anti-bobble: delay start of repeat
- fire(_stickyShifted);
+ _lastFirePos = eventToWindowCoords(ea);
+ // delay start of repeat, makes dragging more usable
+ _repeatTime = -_repeatInterval;
+ _hasDragged = false;
return true;
}
- virtual void buttonReleased(void)
+ virtual void buttonReleased(int keyModState)
{
+ // for *clicks*, we only fire on button release
+ if (!_hasDragged) {
+ // std::cout << "no drag, firing" << std::endl;
+ fire(keyModState & osgGA::GUIEventAdapter::MODKEY_SHIFT);
+ }
+
fireBindingList(_releaseAction);
}
- virtual void update(double dt)
+ virtual void mouseMoved(const osgGA::GUIEventAdapter* ea)
{
+ _mousePos = eventToWindowCoords(ea);;
+ osg::Vec2d deltaMouse = _mousePos - _lastFirePos;
+
+ if (!_hasDragged) {
+
+ double manhattanDist = deltaMouse.x() * deltaMouse.x() + deltaMouse.y() * deltaMouse.y();
+ if (manhattanDist < 5) {
+ // don't do anything, just input noise
+ return;
+ }
+
+ // user is dragging, disable repeat behaviour
+ _hasDragged = true;
+ }
+
+ double delta = static_knobDragAlternateAxis ? deltaMouse.y() : deltaMouse.x();
+ delta /= _dragScale;
+ if (fabs(delta) >= 1.0) {
+ // determine direction from sign of delta
+ _direction = (delta > 0.0) ? DIRECTION_CLOCKWISE : DIRECTION_ANTICLOCKWISE;
+ fire(ea->getModKeyMask());
+ _lastFirePos = _mousePos;
+ }
+ }
+
+ virtual void update(double dt, int keyModState)
+ {
+ if (_hasDragged) {
+ return;
+ }
+
_repeatTime += dt;
while (_repeatInterval < _repeatTime) {
_repeatTime -= _repeatInterval;
- fire(_stickyShifted);
+ fire(keyModState & osgGA::GUIEventAdapter::MODKEY_SHIFT);
} // of repeat iteration
}
double _repeatInterval;
double _repeatTime;
- // FIXME - would be better to pass the current modifier state
- // into update(), but for now let's make it sticky
- bool _stickyShifted;
+ bool _hasDragged; ///< has the mouse been dragged since the press?
+ osg::Vec2d _mousePos, ///< current window coords location of the mouse
+ _lastFirePos; ///< mouse location where we last fired the bindings
+ double _dragScale;
};
+///////////////////////////////////////////////////////////////////////////////
+
class SGKnobAnimation::UpdateCallback : public osg::NodeCallback {
public:
UpdateCallback(SGExpressiond const* animationValue) :
transform->setAxis(_axis);
SGSceneUserData* ud = SGSceneUserData::getOrCreateSceneUserData(transform);
- ud->setPickCallback(new KnobPickCallback(getConfig(), getModelRoot()));
+ ud->setPickCallback(new KnobSliderPickCallback(getConfig(), getModelRoot()));
return transform;
}
static_knobMouseWheelAlternateDirection = aToggle;
}
+void SGKnobAnimation::setAlternateDragAxis(bool aToggle)
+{
+ static_knobDragAlternateAxis = aToggle;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+class SGSliderAnimation::UpdateCallback : public osg::NodeCallback {
+public:
+ UpdateCallback(SGExpressiond const* animationValue) :
+ _animationValue(animationValue)
+ {
+ setName("SGSliderAnimation::UpdateCallback");
+ }
+ virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
+ {
+ SGTranslateTransform* transform = static_cast<SGTranslateTransform*>(node);
+ transform->setValue(_animationValue->getValue());
+
+ traverse(node, nv);
+ }
+
+private:
+ SGSharedPtr<SGExpressiond const> _animationValue;
+};
+
+
+SGSliderAnimation::SGSliderAnimation(const SGPropertyNode* configNode,
+ SGPropertyNode* modelRoot) :
+ SGPickAnimation(configNode, modelRoot)
+{
+ SGSharedPtr<SGExpressiond> value = read_value(configNode, modelRoot, "-m",
+ -SGLimitsd::max(), SGLimitsd::max());
+ _animationValue = value->simplify();
+
+ _axis = readTranslateAxis(configNode);
+}
+
+osg::Group*
+SGSliderAnimation::createAnimationGroup(osg::Group& parent)
+{
+ SGTranslateTransform* transform = new SGTranslateTransform();
+ innerSetupPickGroup(transform, parent);
+
+ UpdateCallback* uc = new UpdateCallback(_animationValue);
+ transform->setUpdateCallback(uc);
+ transform->setAxis(_axis);
+
+ SGSceneUserData* ud = SGSceneUserData::getOrCreateSceneUserData(transform);
+ ud->setPickCallback(new KnobSliderPickCallback(getConfig(), getModelRoot()));
+
+ return transform;
+}
} else if (type == "knob") {
SGKnobAnimation animInst(configNode, modelRoot);
animInst.apply(node);
+ } else if (type == "slider") {
+ SGSliderAnimation animInst(configNode, modelRoot);
+ animInst.apply(node);
} else if (type == "range") {
SGRangeAnimation animInst(configNode, modelRoot);
animInst.apply(node);
else
_initialValue = 0;
- if (configNode->hasValue("axis/x1-m")) {
- SGVec3d v1, v2;
- v1[0] = configNode->getDoubleValue("axis/x1-m", 0);
- v1[1] = configNode->getDoubleValue("axis/y1-m", 0);
- v1[2] = configNode->getDoubleValue("axis/z1-m", 0);
- v2[0] = configNode->getDoubleValue("axis/x2-m", 0);
- v2[1] = configNode->getDoubleValue("axis/y2-m", 0);
- v2[2] = configNode->getDoubleValue("axis/z2-m", 0);
- _axis = v2 - v1;
- } else {
- _axis[0] = configNode->getDoubleValue("axis/x", 0);
- _axis[1] = configNode->getDoubleValue("axis/y", 0);
- _axis[2] = configNode->getDoubleValue("axis/z", 0);
- }
- if (8*SGLimitsd::min() < norm(_axis))
- _axis = normalize(_axis);
+ _axis = readTranslateAxis(configNode);
}
osg::Group*
center[2] = configNode->getDoubleValue("center/z-m", center[2]);
}
+SGVec3d readTranslateAxis(const SGPropertyNode* configNode)
+{
+ SGVec3d axis;
+
+ if (configNode->hasValue("axis/x1-m")) {
+ SGVec3d v1, v2;
+ v1[0] = configNode->getDoubleValue("axis/x1-m", 0);
+ v1[1] = configNode->getDoubleValue("axis/y1-m", 0);
+ v1[2] = configNode->getDoubleValue("axis/z1-m", 0);
+ v2[0] = configNode->getDoubleValue("axis/x2-m", 0);
+ v2[1] = configNode->getDoubleValue("axis/y2-m", 0);
+ v2[2] = configNode->getDoubleValue("axis/z2-m", 0);
+ axis = v2 - v1;
+ } else {
+ axis[0] = configNode->getDoubleValue("axis/x", 0);
+ axis[1] = configNode->getDoubleValue("axis/y", 0);
+ axis[2] = configNode->getDoubleValue("axis/z", 0);
+ }
+ if (8*SGLimitsd::min() < norm(axis))
+ axis = normalize(axis);
+
+ return axis;
+}
+
SGRotateAnimation::SGRotateAnimation(const SGPropertyNode* configNode,
SGPropertyNode* modelRoot) :
SGAnimation(configNode, modelRoot)