| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGGA_GUIEVENTHANDLER |
|---|
| 15 | #define OSGGA_GUIEVENTHANDLER 1 |
|---|
| 16 | |
|---|
| 17 | #include <vector> |
|---|
| 18 | |
|---|
| 19 | #include <osg/NodeCallback> |
|---|
| 20 | #include <osg/Drawable> |
|---|
| 21 | #include <osg/ApplicationUsage> |
|---|
| 22 | |
|---|
| 23 | #include <osgGA/Export> |
|---|
| 24 | #include <osgGA/GUIEventAdapter> |
|---|
| 25 | #include <osgGA/GUIActionAdapter> |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | // #define COMPILE_COMPOSITE_EVENTHANDLER |
|---|
| 29 | |
|---|
| 30 | namespace osgGA{ |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | |
|---|
| 34 | GUIEventHandler provides a basic interface for any class which wants to handle |
|---|
| 35 | a GUI Events. |
|---|
| 36 | |
|---|
| 37 | The GUIEvent is supplied by a GUIEventAdapter. Feedback resulting from the |
|---|
| 38 | handle method is supplied by a GUIActionAdapter, which allows the GUIEventHandler |
|---|
| 39 | to ask the GUI to take some action in response to an incoming event. |
|---|
| 40 | |
|---|
| 41 | For example, consider a Trackball Viewer class which takes mouse events and |
|---|
| 42 | manipulates a scene camera in response. The Trackball Viewer is a GUIEventHandler, |
|---|
| 43 | and receives the events via the handle method. If the user 'throws' the model, |
|---|
| 44 | the Trackball Viewer class can detect this via the incoming events, and |
|---|
| 45 | request that the GUI set up a timer callback to continually redraw the view. |
|---|
| 46 | This request is made via the GUIActionAdapter class. |
|---|
| 47 | |
|---|
| 48 | */ |
|---|
| 49 | |
|---|
| 50 | class OSGGA_EXPORT GUIEventHandler : public osg::NodeCallback, public osg::Drawable::EventCallback |
|---|
| 51 | { |
|---|
| 52 | public: |
|---|
| 53 | |
|---|
| 54 | GUIEventHandler() : _ignoreHandledEventsMask(GUIEventAdapter::NONE) {} |
|---|
| 55 | GUIEventHandler(const GUIEventHandler& eh,const osg::CopyOp& copyop): |
|---|
| 56 | osg::NodeCallback(eh, copyop), |
|---|
| 57 | osg::Drawable::EventCallback(eh, copyop), |
|---|
| 58 | _ignoreHandledEventsMask(eh._ignoreHandledEventsMask) {} |
|---|
| 59 | |
|---|
| 60 | META_Object(osgGA,GUIEventHandler); |
|---|
| 61 | |
|---|
| 62 | /** Event traversal node callback method.*/ |
|---|
| 63 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv); |
|---|
| 64 | |
|---|
| 65 | /** Event traversal drawable callback method.*/ |
|---|
| 66 | virtual void event(osg::NodeVisitor* nv, osg::Drawable* drawable); |
|---|
| 67 | |
|---|
| 68 | /** Handle events, return true if handled, false otherwise. */ |
|---|
| 69 | virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor*) { return handle(ea,aa); } |
|---|
| 70 | |
|---|
| 71 | /** Convenience method that only passes on to the handle(,,,) method events that either haven't been |
|---|
| 72 | * handled yet, or have been handled but haven't be set to be ignored by the IgnoreHandledEventsMask. |
|---|
| 73 | * Note, this method is an inline method, and not appropriate for users to override, override the handle(,,,) |
|---|
| 74 | * method instead.*/ |
|---|
| 75 | inline bool handleWithCheckAgainstIgnoreHandledEventsMask(const GUIEventAdapter& ea,GUIActionAdapter& aa, osg::Object* object, osg::NodeVisitor* nv) |
|---|
| 76 | { |
|---|
| 77 | if (!ea.getHandled() || |
|---|
| 78 | (ea.getEventType() & _ignoreHandledEventsMask)==0) |
|---|
| 79 | { |
|---|
| 80 | bool handled = handle(ea,aa,object,nv); |
|---|
| 81 | if (handled) ea.setHandled(true); |
|---|
| 82 | return handled; |
|---|
| 83 | } |
|---|
| 84 | else |
|---|
| 85 | { |
|---|
| 86 | return false; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** Deprecated, Handle events, return true if handled, false otherwise. */ |
|---|
| 91 | virtual bool handle(const GUIEventAdapter&,GUIActionAdapter&) { return false; } |
|---|
| 92 | |
|---|
| 93 | /** Convenience method that only passes on to the handle(,) method events that either haven't been |
|---|
| 94 | * handled yet, or have been handled but haven't be set to be ignored by the IgnoreHandledEventsMask. |
|---|
| 95 | * Note, this method is an inline method, and not appropriate for users to override, override the handle(,) |
|---|
| 96 | * method instead.*/ |
|---|
| 97 | inline bool handleWithCheckAgainstIgnoreHandledEventsMask(const GUIEventAdapter& ea,GUIActionAdapter& aa) |
|---|
| 98 | { |
|---|
| 99 | if (!ea.getHandled() || |
|---|
| 100 | (ea.getEventType() & _ignoreHandledEventsMask)==0) |
|---|
| 101 | { |
|---|
| 102 | bool handled = handle(ea,aa); |
|---|
| 103 | if (handled) ea.setHandled(true); |
|---|
| 104 | return handled; |
|---|
| 105 | } |
|---|
| 106 | else |
|---|
| 107 | { |
|---|
| 108 | return false; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** Get the keyboard and mouse usage of this manipulator.*/ |
|---|
| 113 | virtual void getUsage(osg::ApplicationUsage&) const {} |
|---|
| 114 | |
|---|
| 115 | /** Set a mask of osgGA::GUIEeventAdapter::Event to be ignored if marked as handled */ |
|---|
| 116 | void setIgnoreHandledEventsMask(unsigned int mask) { _ignoreHandledEventsMask = mask; } |
|---|
| 117 | |
|---|
| 118 | /** Get the event mask of the osgGA::GUIEeventAdapter::Event to be ignored if marked as handled */ |
|---|
| 119 | unsigned int getIgnoreHandledEventsMask() const { return _ignoreHandledEventsMask; }; |
|---|
| 120 | |
|---|
| 121 | protected: |
|---|
| 122 | unsigned int _ignoreHandledEventsMask; |
|---|
| 123 | |
|---|
| 124 | }; |
|---|
| 125 | |
|---|
| 126 | #ifdef USE_DEPRECATED_API |
|---|
| 127 | // keep for backwards compatibility |
|---|
| 128 | class GUIEventHandlerVisitor |
|---|
| 129 | { |
|---|
| 130 | public: |
|---|
| 131 | |
|---|
| 132 | void visit(GUIEventHandler&) {} |
|---|
| 133 | }; |
|---|
| 134 | #endif |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | #endif |
|---|