diff --git a/examples/osgmanipulator/osgmanipulator.cpp b/examples/osgmanipulator/osgmanipulator.cpp index a00d38ae9..9d8dd1369 100644 --- a/examples/osgmanipulator/osgmanipulator.cpp +++ b/examples/osgmanipulator/osgmanipulator.cpp @@ -136,6 +136,8 @@ osg::Node* addDraggerToScene(osg::Node* scene, osgManipulator::CommandManager* c osgManipulator::Dragger* dragger = createDragger(name); + dragger->setHandleEvents(true); + osg::Group* root = new osg::Group; root->addChild(dragger); root->addChild(selection); @@ -260,8 +262,8 @@ class PickModeHandler : public osgGA::GUIEventHandler _mode(VIEW), _activeDragger(0) { - } - + } + bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor*) { @@ -432,7 +434,7 @@ int main( int argc, char **argv ) } else { viewer.setSceneData(loadedModel.get()); } - viewer.addEventHandler(new PickModeHandler()); + // viewer.addEventHandler(new PickModeHandler()); return viewer.run(); } diff --git a/include/osgManipulator/AntiSquish b/include/osgManipulator/AntiSquish index 2f6fb6e50..1f965ea2b 100644 --- a/include/osgManipulator/AntiSquish +++ b/include/osgManipulator/AntiSquish @@ -45,7 +45,7 @@ class OSGMANIPULATOR_EXPORT AntiSquish: public osg::MatrixTransform virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast(obj)!=NULL; } void setPivot(const osg::Vec3d& pvt) - { + { _pivot = pvt; _usePivot = true; _dirty = true; @@ -54,7 +54,7 @@ class OSGMANIPULATOR_EXPORT AntiSquish: public osg::MatrixTransform const osg::Vec3d& getPivot() { return _pivot; } void setPosition(const osg::Vec3d& pos) - { + { _position = pos; _usePosition = true; _dirty = true; diff --git a/include/osgManipulator/Constraint b/include/osgManipulator/Constraint index 0c66413bb..d899bb453 100644 --- a/include/osgManipulator/Constraint +++ b/include/osgManipulator/Constraint @@ -69,8 +69,11 @@ class OSGMANIPULATOR_EXPORT GridConstraint : public Constraint GridConstraint(osg::Node& refNode, const osg::Vec3d& origin, const osg::Vec3d& spacing); - void setOrigin(const osg::Vec3d origin) { _origin = origin; } - void setSpacing(const osg::Vec3d spacing) { _spacing = spacing; } + void setOrigin(const osg::Vec3d& origin) { _origin = origin; } + const osg::Vec3d& getOrigin() const { return _origin; } + + void setSpacing(const osg::Vec3d& spacing) { _spacing = spacing; } + const osg::Vec3d& getSpacing() const { return _spacing; } virtual bool constrain(TranslateInLineCommand& command) const; virtual bool constrain(TranslateInPlaneCommand& command) const; diff --git a/include/osgManipulator/Dragger b/include/osgManipulator/Dragger index d18138945..023634549 100644 --- a/include/osgManipulator/Dragger +++ b/include/osgManipulator/Dragger @@ -166,6 +166,16 @@ class OSGMANIPULATOR_EXPORT Dragger : public Selection virtual CompositeDragger* getComposite() { return 0; } + void setHandleEvents(bool flag); + bool getHandleEvents() const { return _handleEvents; } + + void setDraggerActive(bool active) { _draggerActive = active; } + bool getDraggerActive() const { return _draggerActive; } + + + virtual void traverse(osg::NodeVisitor& nv); + + virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa); virtual bool handle(const PointerInfo&, const osgGA::GUIEventAdapter&, osgGA::GUIActionAdapter&) { return false; } protected: @@ -173,9 +183,13 @@ class OSGMANIPULATOR_EXPORT Dragger : public Selection Dragger(); virtual ~Dragger(); - CommandManager* _commandManager; + bool _handleEvents; + bool _draggerActive; + osgManipulator::PointerInfo _pointer; - Dragger* _parentDragger; + CommandManager* _commandManager; + + Dragger* _parentDragger; }; diff --git a/include/osgPresentation/Export b/include/osgPresentation/Export index 420c3c404..f3dbd1304 100644 --- a/include/osgPresentation/Export +++ b/include/osgPresentation/Export @@ -36,15 +36,6 @@ # define OSGPRESENTATION_EXPORT #endif -/* Define NULL pointer value */ - -#ifndef NULL -#ifdef __cplusplus -#define NULL 0 -#else -#define NULL ((void *)0) -#endif -#endif /** diff --git a/src/osgManipulator/CMakeLists.txt b/src/osgManipulator/CMakeLists.txt index 0af855726..1a84d2bf0 100644 --- a/src/osgManipulator/CMakeLists.txt +++ b/src/osgManipulator/CMakeLists.txt @@ -61,6 +61,7 @@ ADD_LIBRARY(${LIB_NAME} ) LINK_INTERNAL(${LIB_NAME} + osgViewer osgGA osgUtil osg diff --git a/src/osgManipulator/CommandManager.cpp b/src/osgManipulator/CommandManager.cpp index 5736bf1f4..b9bb7b413 100644 --- a/src/osgManipulator/CommandManager.cpp +++ b/src/osgManipulator/CommandManager.cpp @@ -126,7 +126,7 @@ void CommandManager::addSelectionsToCommand(MotionCommand& command, Dragger& dra CommandManager::Selections CommandManager::getConnectedSelections(Dragger& dragger) { - Selections selections = std::list< osg::ref_ptr >(); + Selections selections; //Test if the dragger is in the list if (_draggerSelectionMap.count(&dragger) > 0) diff --git a/src/osgManipulator/Dragger.cpp b/src/osgManipulator/Dragger.cpp index a939d57d8..8af1fbc29 100644 --- a/src/osgManipulator/Dragger.cpp +++ b/src/osgManipulator/Dragger.cpp @@ -14,6 +14,8 @@ #include #include +#include +#include using namespace osgManipulator; @@ -39,7 +41,10 @@ bool PointerInfo::projectWindowXYIntoObject(const osg::Vec2d& windowCoord, osg:: return true; } -Dragger::Dragger() : _commandManager(0) +Dragger::Dragger() : + _handleEvents(false), + _draggerActive(false), + _commandManager(0) { _parentDragger = this; getOrCreateStateSet()->setDataVariance(osg::Object::DYNAMIC); @@ -49,6 +54,110 @@ Dragger::~Dragger() { } +void Dragger::setHandleEvents(bool flag) +{ + if (_handleEvents == flag) return; + + _handleEvents = flag; + + // update the number of children that require an event traversal to make sure this dragger recieves events. + if (_handleEvents) setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()+1); + else if (getNumChildrenRequiringEventTraversal()>=1) setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()-1); +} + +void Dragger::traverse(osg::NodeVisitor& nv) +{ + if (_handleEvents && nv.getVisitorType()==osg::NodeVisitor::EVENT_VISITOR) + { + osgGA::EventVisitor* ev = dynamic_cast(&nv); + if (ev) + { + for(osgGA::EventQueue::Events::iterator itr = ev->getEvents().begin(); + itr != ev->getEvents().end(); + ++itr) + { + osgGA::GUIEventAdapter* ea = itr->get(); + if (handle(*ea, *(ev->getActionAdapter()))) ea->setHandled(true); + } + } + return; + } + + Selection::traverse(nv); +} + +bool Dragger::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) +{ + if (ea.getHandled()) return false; + + osgViewer::View* view = dynamic_cast(&aa); + if (!view) return false; + + bool handled = false; + + switch (ea.getEventType()) + { + case osgGA::GUIEventAdapter::PUSH: + { + osgUtil::LineSegmentIntersector::Intersections intersections; + + _pointer.reset(); + + if (view->computeIntersections(ea.getX(),ea.getY(),intersections)) + { + _pointer.setCamera(view->getCamera()); + _pointer.setMousePosition(ea.getX(), ea.getY()); + + for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin(); + hitr != intersections.end(); + ++hitr) + { + _pointer.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint()); + } + for (osg::NodePath::iterator itr = _pointer._hitList.front().first.begin(); + itr != _pointer._hitList.front().first.end(); + ++itr) + { + osgManipulator::Dragger* dragger = dynamic_cast(*itr); + if (dragger) + { + if (dragger==this) + { + dragger->handle(_pointer, ea, aa); + dragger->setDraggerActive(true); + handled = true; + } + } + } + } + } + case osgGA::GUIEventAdapter::DRAG: + case osgGA::GUIEventAdapter::RELEASE: + { + if (_draggerActive) + { + _pointer._hitIter = _pointer._hitList.begin(); + _pointer.setCamera(view->getCamera()); + _pointer.setMousePosition(ea.getX(), ea.getY()); + + handle(_pointer, ea, aa); + + handled = true; + } + break; + } + default: + break; + } + + if (_draggerActive && ea.getEventType() == osgGA::GUIEventAdapter::RELEASE) + { + setDraggerActive(false); + _pointer.reset(); + } + + return handled; +} bool CompositeDragger::handle(const PointerInfo& pi, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {