openscenegraph
EventVisitor
Go to the documentation of this file.
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#include <osg/ScriptEngine>
28
29#include <osgGA/GUIEventAdapter>
30#include <osgGA/GUIEventHandler>
31#include <osgGA/GUIActionAdapter>
32#include <osgGA/EventQueue>
33
34namespace osgGA {
35
36/**
37 * Basic EventVisitor implementation for animating a scene.
38 * This visitor traverses the scene graph, calling each nodes appCallback if
39 * it exists.
40 */
41class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor
42{
43 public:
44
45 EventVisitor();
46 virtual ~EventVisitor();
47
48 META_NodeVisitor(osgGA, EventVisitor)
49
50 /** Convert 'this' into a osgGA::EventVisitor pointer if Object is a osgGA::EventVisitor, otherwise return 0.
51 * Equivalent to dynamic_cast<osgGA::EventVisitor*>(this).*/
52 virtual osgGA::EventVisitor* asEventVisitor() { return this; }
53
54 /** convert 'const this' into a const osgGA::EventVisitor pointer if Object is a osgGA::EventVisitor, otherwise return 0.
55 * Equivalent to dynamic_cast<const osgGA::EventVisitor*>(this).*/
56 virtual const osgGA::EventVisitor* asEventVisitor() const { return this; }
57
58 void setActionAdapter(osgGA::GUIActionAdapter* actionAdapter) { _actionAdapter=actionAdapter; }
59
60 osgGA::GUIActionAdapter* getActionAdapter() { return _actionAdapter; }
61
62 const osgGA::GUIActionAdapter* getActionAdapter() const { return _actionAdapter; }
63
64 void addEvent(Event* event);
65 void removeEvent(Event* event);
66
67 void setEventHandled(bool handled) { _handled = handled; }
68 bool getEventHandled() const { return _handled; }
69
70 void setEvents(const EventQueue::Events& events) { _events = events; }
71 EventQueue::Events& getEvents() { return _events; }
72 const EventQueue::Events& getEvents() const { return _events; }
73
74 public:
75
76 virtual void reset();
77
78 /** During traversal each type of node calls its callbacks and its children traversed. */
79 virtual void apply(osg::Node& node) { handle_callbacks_and_traverse(node); }
80
81
82 virtual void apply(osg::Drawable& drawable)
83 {
84 osg::Callback* callback = drawable.getEventCallback();
85 if (callback)
86 {
87 osgGA::EventHandler* eh = callback->asEventHandler();
88 if (eh)
89 {
90 callback->run(&drawable,this);
91 }
92 else
93 {
94 osg::DrawableEventCallback* drawable_callback = callback->asDrawableEventCallback();
95 osg::NodeCallback* node_callback = callback->asNodeCallback();
96 osg::CallbackObject* callback_object = callback->asCallbackObject();
97
98 if (drawable_callback) drawable_callback->event(this,&drawable);
99 if (node_callback) (*node_callback)(&drawable, this);
100 if (callback_object) callback_object->run(&drawable, this);
101
102 if (!drawable_callback && !node_callback && !callback_object) callback->run(&drawable, this);
103 }
104 }
105
106 handle_callbacks(drawable.getStateSet());
107 }
108
109 // The following overrides are technically redundant as the default implementation would eventually trickle down to
110 // apply(osg::Node&); - however defining these explicitly should save a couple of virtual function calls
111 virtual void apply(osg::Geode& node) { handle_callbacks_and_traverse(node); }
112 virtual void apply(osg::Billboard& node) { handle_callbacks_and_traverse(node); }
113 virtual void apply(osg::LightSource& node) { handle_callbacks_and_traverse(node); }
114 virtual void apply(osg::Group& node) { handle_callbacks_and_traverse(node); }
115 virtual void apply(osg::Transform& node) { handle_callbacks_and_traverse(node); }
116 virtual void apply(osg::Projection& node) { handle_callbacks_and_traverse(node); }
117 virtual void apply(osg::Switch& node) { handle_callbacks_and_traverse(node); }
118 virtual void apply(osg::LOD& node) { handle_callbacks_and_traverse(node); }
119 virtual void apply(osg::OccluderNode& node) { handle_callbacks_and_traverse(node); }
120
121
122 protected:
123
124 /** Prevent unwanted copy operator.*/
125 EventVisitor& operator = (const EventVisitor&) { return *this; }
126
127 inline void handle_callbacks(osg::StateSet* stateset)
128 {
129 if (stateset && stateset->requiresEventTraversal())
130 {
131 stateset->runEventCallbacks(this);
132 }
133 }
134
135 inline void handle_callbacks_and_traverse(osg::Node& node)
136 {
137 handle_callbacks(node.getStateSet());
138
139 osg::Callback* callback = node.getEventCallback();
140 if (callback) callback->run(&node,this);
141 else if (node.getNumChildrenRequiringEventTraversal()>0) traverse(node);
142 }
143
144
145 osgGA::GUIActionAdapter* _actionAdapter;
146
147 osg::ref_ptr<GUIEventAdapter> _accumulateEventState;
148
149 bool _handled;
150 EventQueue::Events _events;
151};
152
153}
154
155#endif
156