Introduced event handling directly into osgManipulator::Dragger to allow it be used with a global event handler passing in events.

This commit is contained in:
Robert Osfield 2009-06-29 21:32:10 +00:00
parent 8aaedda982
commit e30e4df30c
8 changed files with 140 additions and 20 deletions

View File

@ -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();
}

View File

@ -45,7 +45,7 @@ class OSGMANIPULATOR_EXPORT AntiSquish: public osg::MatrixTransform
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const AntiSquish *>(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;

View File

@ -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;

View File

@ -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;
};

View File

@ -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
/**

View File

@ -61,6 +61,7 @@ ADD_LIBRARY(${LIB_NAME}
)
LINK_INTERNAL(${LIB_NAME}
osgViewer
osgGA
osgUtil
osg

View File

@ -126,7 +126,7 @@ void CommandManager::addSelectionsToCommand(MotionCommand& command, Dragger& dra
CommandManager::Selections CommandManager::getConnectedSelections(Dragger& dragger)
{
Selections selections = std::list< osg::ref_ptr<Selection> >();
Selections selections;
//Test if the dragger is in the list
if (_draggerSelectionMap.count(&dragger) > 0)

View File

@ -14,6 +14,8 @@
#include <osgManipulator/Dragger>
#include <osg/Material>
#include <osgGA/EventVisitor>
#include <osgViewer/View>
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<osgGA::EventVisitor*>(&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<osgViewer::View*>(&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<osgManipulator::Dragger*>(*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)
{