| 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_EVENTVISITOR |
|---|
| 15 | #define OSGGA_EVENTVISITOR 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/NodeVisitor> |
|---|
| 18 | #include <osg/Node> |
|---|
| 19 | #include <osg/Geode> |
|---|
| 20 | #include <osg/Billboard> |
|---|
| 21 | #include <osg/LOD> |
|---|
| 22 | #include <osg/Switch> |
|---|
| 23 | #include <osg/LightSource> |
|---|
| 24 | #include <osg/Transform> |
|---|
| 25 | #include <osg/Projection> |
|---|
| 26 | #include <osg/OccluderNode> |
|---|
| 27 | |
|---|
| 28 | #include <osgGA/GUIEventAdapter> |
|---|
| 29 | #include <osgGA/GUIActionAdapter> |
|---|
| 30 | #include <osgGA/EventQueue> |
|---|
| 31 | |
|---|
| 32 | namespace osgGA { |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Basic EventVisitor implementation for animating a scene. |
|---|
| 36 | * This visitor traverses the scene graph, calling each nodes appCallback if |
|---|
| 37 | * it exists. |
|---|
| 38 | */ |
|---|
| 39 | class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor |
|---|
| 40 | { |
|---|
| 41 | public: |
|---|
| 42 | |
|---|
| 43 | EventVisitor(); |
|---|
| 44 | virtual ~EventVisitor(); |
|---|
| 45 | |
|---|
| 46 | META_NodeVisitor("osgGA","EventVisitor") |
|---|
| 47 | |
|---|
| 48 | void setActionAdapter(osgGA::GUIActionAdapter* actionAdapter) { _actionAdapter=actionAdapter; } |
|---|
| 49 | |
|---|
| 50 | osgGA::GUIActionAdapter* getActionAdapter() { return _actionAdapter; } |
|---|
| 51 | |
|---|
| 52 | const osgGA::GUIActionAdapter* getActionAdapter() const { return _actionAdapter; } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | typedef std::list< osg::ref_ptr<GUIEventAdapter> > EventList; |
|---|
| 56 | |
|---|
| 57 | void addEvent(GUIEventAdapter* event); |
|---|
| 58 | void removeEvent(GUIEventAdapter* event); |
|---|
| 59 | |
|---|
| 60 | void setEventHandled(bool handled) { _handled = handled; } |
|---|
| 61 | bool getEventHandled() const { return _handled; } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | void setEvents(const EventQueue::Events& events) { _events = events; } |
|---|
| 65 | EventQueue::Events& getEvents() { return _events; } |
|---|
| 66 | const EventQueue::Events& getEvents() const { return _events; } |
|---|
| 67 | |
|---|
| 68 | public: |
|---|
| 69 | |
|---|
| 70 | virtual void reset(); |
|---|
| 71 | |
|---|
| 72 | /** During traversal each type of node calls its callbacks and its children traversed. */ |
|---|
| 73 | virtual void apply(osg::Node& node) { handle_callbacks_and_traverse(node); } |
|---|
| 74 | |
|---|
| 75 | virtual void apply(osg::Geode& node) { handle_geode_callbacks(node); } |
|---|
| 76 | virtual void apply(osg::Billboard& node) { handle_geode_callbacks(node); } |
|---|
| 77 | |
|---|
| 78 | virtual void apply(osg::LightSource& node) { handle_callbacks_and_traverse(node); } |
|---|
| 79 | |
|---|
| 80 | virtual void apply(osg::Group& node) { handle_callbacks_and_traverse(node); } |
|---|
| 81 | virtual void apply(osg::Transform& node) { handle_callbacks_and_traverse(node); } |
|---|
| 82 | virtual void apply(osg::Projection& node) { handle_callbacks_and_traverse(node); } |
|---|
| 83 | virtual void apply(osg::Switch& node) { handle_callbacks_and_traverse(node); } |
|---|
| 84 | virtual void apply(osg::LOD& node) { handle_callbacks_and_traverse(node); } |
|---|
| 85 | virtual void apply(osg::OccluderNode& node) { handle_callbacks_and_traverse(node); } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | protected: |
|---|
| 89 | |
|---|
| 90 | /** Prevent unwanted copy operator.*/ |
|---|
| 91 | EventVisitor& operator = (const EventVisitor&) { return *this; } |
|---|
| 92 | |
|---|
| 93 | inline void handle_callbacks(osg::StateSet* stateset) |
|---|
| 94 | { |
|---|
| 95 | if (stateset && stateset->requiresEventTraversal()) |
|---|
| 96 | { |
|---|
| 97 | stateset->runEventCallbacks(this); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | inline void handle_callbacks_and_traverse(osg::Node& node) |
|---|
| 102 | { |
|---|
| 103 | handle_callbacks(node.getStateSet()); |
|---|
| 104 | |
|---|
| 105 | osg::NodeCallback* callback = node.getEventCallback(); |
|---|
| 106 | if (callback) (*callback)(&node,this); |
|---|
| 107 | else if (node.getNumChildrenRequiringEventTraversal()>0) traverse(node); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | inline void handle_geode_callbacks(osg::Geode& node) |
|---|
| 111 | { |
|---|
| 112 | handle_callbacks(node.getStateSet()); |
|---|
| 113 | |
|---|
| 114 | osg::NodeCallback* callback = node.getEventCallback(); |
|---|
| 115 | if (callback) (*callback)(&node,this); |
|---|
| 116 | /*else if (node.getNumChildrenRequiringEventTraversal()>0)*/ |
|---|
| 117 | traverseGeode(node); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | inline void traverseGeode(osg::Geode& geode) |
|---|
| 121 | { |
|---|
| 122 | traverse((osg::Node&)geode); |
|---|
| 123 | |
|---|
| 124 | // Call the app callbacks on the drawables. |
|---|
| 125 | for(unsigned int i=0;i<geode.getNumDrawables();++i) |
|---|
| 126 | { |
|---|
| 127 | osg::Drawable::EventCallback* callback = geode.getDrawable(i)->getEventCallback(); |
|---|
| 128 | if (callback) callback->event(this,geode.getDrawable(i)); |
|---|
| 129 | |
|---|
| 130 | handle_callbacks(geode.getDrawable(i)->getStateSet()); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | osgGA::GUIActionAdapter* _actionAdapter; |
|---|
| 135 | |
|---|
| 136 | osg::ref_ptr<GUIEventAdapter> _accumulateEventState; |
|---|
| 137 | |
|---|
| 138 | bool _handled; |
|---|
| 139 | EventQueue::Events _events; |
|---|
| 140 | |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | #endif |
|---|
| 146 | |
|---|