From d5cc0e966f63a7647d93dd1317166c94206baace Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 14 Sep 2007 10:44:46 +0000 Subject: [PATCH] Introduce GUIEventHandler::handleWithCheckAgainstIgnoreHandledEventsMask() methods to help make it easier to get event handles to ingore events that have already been handled. --- include/osgGA/GUIEventAdapter | 4 +-- include/osgGA/GUIEventHandler | 52 +++++++++++++++++++++++++---- src/osgGA/GUIEventHandler.cpp | 7 ++-- src/osgViewer/CompositeViewer.cpp | 4 +-- src/osgViewer/GraphicsWindowX11.cpp | 2 +- src/osgViewer/Viewer.cpp | 4 +-- 6 files changed, 55 insertions(+), 18 deletions(-) diff --git a/include/osgGA/GUIEventAdapter b/include/osgGA/GUIEventAdapter index f3d997969..1076968aa 100644 --- a/include/osgGA/GUIEventAdapter +++ b/include/osgGA/GUIEventAdapter @@ -262,7 +262,7 @@ public: /** Set whether this event has been handled by an event handler or not.*/ - void setHandled(bool handled) { _handled = handled; } + void setHandled(bool handled) const { _handled = handled; } /** Get whether this event has been handled by an event handler or not.*/ bool getHandled() const { return _handled; } @@ -403,7 +403,7 @@ public: /** Force users to create on heap, so that multiple referencing is safe.*/ virtual ~GUIEventAdapter(); - bool _handled; + mutable bool _handled; EventType _eventType; double _time; diff --git a/include/osgGA/GUIEventHandler b/include/osgGA/GUIEventHandler index 5be7533f9..38efecfdd 100644 --- a/include/osgGA/GUIEventHandler +++ b/include/osgGA/GUIEventHandler @@ -51,8 +51,9 @@ class OSGGA_EXPORT GUIEventHandler : public osg::NodeCallback, public osg::Drawa { public: - GUIEventHandler() : _ignoreUsedEventsMask(GUIEventAdapter::NONE) {} - GUIEventHandler(const GUIEventHandler&,const osg::CopyOp&) {} + GUIEventHandler() : _ignoreHandledEventsMask(GUIEventAdapter::NONE) {} + GUIEventHandler(const GUIEventHandler& eh,const osg::CopyOp&): + _ignoreHandledEventsMask(eh._ignoreHandledEventsMask) {} META_Object(osgGA,GUIEventHandler); @@ -65,19 +66,58 @@ public: /** Handle events, return true if handled, false otherwise. */ virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor*) { return handle(ea,aa); } - /** deprecated, Handle events, return true if handled, false otherwise. */ + /** Convnience method that only passes on to the handle(,,,) method events that either haven't been + * handled yet, or have been handled but haven't be set to be ignored by the IgnoreHandledEventsMask. + * Note, this method is an inline method, and not appropriate for users to override, override the handle(,,,) + * method instead.*/ + inline bool handleWithCheckAgainstIgnoreHandledEventsMask(const GUIEventAdapter& ea,GUIActionAdapter& aa, osg::Object* object, osg::NodeVisitor* nv) + { + if (!ea.getHandled() || + (ea.getEventType() & _ignoreHandledEventsMask)==0) + { + bool handled = handle(ea,aa,object,nv); + if (handled) ea.setHandled(true); + return handled; + } + else + { + return false; + } + } + + /** Deprecated, Handle events, return true if handled, false otherwise. */ virtual bool handle(const GUIEventAdapter&,GUIActionAdapter&) { return false; } + /** Convnience method that only passes on to the handle(,) method events that either haven't been + * handled yet, or have been handled but haven't be set to be ignored by the IgnoreHandledEventsMask. + * Note, this method is an inline method, and not appropriate for users to override, override the handle(,) + * method instead.*/ + inline bool handleWithCheckAgainstIgnoreHandledEventsMask(const GUIEventAdapter& ea,GUIActionAdapter& aa) + { + if (!ea.getHandled() || + (ea.getEventType() & _ignoreHandledEventsMask)==0) + { + bool handled = handle(ea,aa); + if (handled) ea.setHandled(true); + return handled; + } + else + { + return false; + } + } + /** Get the keyboard and mouse usage of this manipulator.*/ virtual void getUsage(osg::ApplicationUsage&) const {} /** Set a mask of osgGA::GUIEeventAdapter::Event to be ignored if marked as handled */ - void setIgnoreUsedEventsMask(unsigned int mask) { _ignoreUsedEventsMask = mask; } + void setIgnoreHandledEventsMask(unsigned int mask) { _ignoreHandledEventsMask = mask; } - unsigned int getIgnoreUsedEventsMask() const { return _ignoreUsedEventsMask; }; + /** Get the event mask of the osgGA::GUIEeventAdapter::Event to be ignored if marked as handled */ + unsigned int getIgnoreHandledEventsMask() const { return _ignoreHandledEventsMask; }; protected: - unsigned int _ignoreUsedEventsMask; + unsigned int _ignoreHandledEventsMask; }; diff --git a/src/osgGA/GUIEventHandler.cpp b/src/osgGA/GUIEventHandler.cpp index 5e12d7757..7bd373720 100644 --- a/src/osgGA/GUIEventHandler.cpp +++ b/src/osgGA/GUIEventHandler.cpp @@ -26,10 +26,7 @@ void GUIEventHandler::operator()(osg::Node* node, osg::NodeVisitor* nv) itr != ev->getEvents().end(); ++itr) { - if (handle(*(*itr), *(ev->getActionAdapter()), node, nv)) - { - (*itr)->setHandled(true); - } + handleWithCheckAgainstIgnoreHandledEventsMask(*(*itr), *(ev->getActionAdapter()), node, nv); } } if (node->getNumChildrenRequiringEventTraversal()>0) traverse(node,nv); @@ -44,7 +41,7 @@ void GUIEventHandler::event(osg::NodeVisitor* nv, osg::Drawable* drawable) itr != ev->getEvents().end(); ++itr) { - handle(*(*itr), *(ev->getActionAdapter()), drawable, nv); + handleWithCheckAgainstIgnoreHandledEventsMask(*(*itr), *(ev->getActionAdapter()), drawable, nv); } } } diff --git a/src/osgViewer/CompositeViewer.cpp b/src/osgViewer/CompositeViewer.cpp index 8147a7c17..1515e6041 100644 --- a/src/osgViewer/CompositeViewer.cpp +++ b/src/osgViewer/CompositeViewer.cpp @@ -950,12 +950,12 @@ void CompositeViewer::eventTraversal() hitr != view->getEventHandlers().end(); ++hitr) { - if ((*hitr)->handle( *event, *view, 0, 0)) event->setHandled(true); + (*hitr)->handleWithCheckAgainstIgnoreHandledEventsMask( *event, *view, 0, 0); } if (view->getCameraManipulator()) { - if (view->getCameraManipulator()->handle( *event, *view)) event->setHandled(true); + view->getCameraManipulator()->handleWithCheckAgainstIgnoreHandledEventsMask( *event, *view); } } } diff --git a/src/osgViewer/GraphicsWindowX11.cpp b/src/osgViewer/GraphicsWindowX11.cpp index 4e052778a..d2db12925 100644 --- a/src/osgViewer/GraphicsWindowX11.cpp +++ b/src/osgViewer/GraphicsWindowX11.cpp @@ -457,7 +457,7 @@ void GraphicsWindowX11::init() getEventQueue()->setCurrentEventState(osgGA::GUIEventAdapter::getAccumulatedEventState().get()); - WindowData* inheritedWindowData = dynamic_cast(_traits->inheritedWindowData.get()); + // WindowData* inheritedWindowData = dynamic_cast(_traits->inheritedWindowData.get()); _display = XOpenDisplay(_traits->displayName().c_str()); diff --git a/src/osgViewer/Viewer.cpp b/src/osgViewer/Viewer.cpp index c79027349..73ac11f27 100644 --- a/src/osgViewer/Viewer.cpp +++ b/src/osgViewer/Viewer.cpp @@ -1327,12 +1327,12 @@ void Viewer::eventTraversal() hitr != _eventHandlers.end(); ++hitr) { - if ((*hitr)->handle( *event, *this, 0, 0)) event->setHandled(true); + (*hitr)->handleWithCheckAgainstIgnoreHandledEventsMask( *event, *this, 0, 0); } if (_cameraManipulator.valid()) { - if (_cameraManipulator->handle( *event, *this)) event->setHandled(true); + _cameraManipulator->handleWithCheckAgainstIgnoreHandledEventsMask( *event, *this); } }