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>
32#include <osgGA/EventQueue>
33
34namespace osgGA {
35
42{
43 public:
44
46 virtual ~EventVisitor();
47
49
50
52 virtual osgGA::EventVisitor* asEventVisitor() { return this; }
53
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
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
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
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
146
148
151};
152
153}
154
155#endif
156
Definition EventHandler:34
std::list< osg::ref_ptr< Event > > Events
Definition EventQueue:36
Definition EventVisitor:42
bool getEventHandled() const
Definition EventVisitor:68
virtual void reset()
virtual ~EventVisitor()
virtual void apply(osg::Projection &node)
Definition EventVisitor:116
const EventQueue::Events & getEvents() const
Definition EventVisitor:72
osg::ref_ptr< GUIEventAdapter > _accumulateEventState
Definition EventVisitor:147
osgGA::GUIActionAdapter * getActionAdapter()
Definition EventVisitor:60
void setEventHandled(bool handled)
Definition EventVisitor:67
virtual void apply(osg::Transform &node)
Definition EventVisitor:115
bool _handled
Definition EventVisitor:149
void addEvent(Event *event)
virtual void apply(osg::LightSource &node)
Definition EventVisitor:113
EventQueue::Events _events
Definition EventVisitor:150
void removeEvent(Event *event)
virtual void apply(osg::OccluderNode &node)
Definition EventVisitor:119
const osgGA::GUIActionAdapter * getActionAdapter() const
Definition EventVisitor:62
virtual void apply(osg::Node &node)
Definition EventVisitor:79
void setActionAdapter(osgGA::GUIActionAdapter *actionAdapter)
Definition EventVisitor:58
EventQueue::Events & getEvents()
Definition EventVisitor:71
void setEvents(const EventQueue::Events &events)
Definition EventVisitor:70
void handle_callbacks(osg::StateSet *stateset)
Definition EventVisitor:127
virtual void apply(osg::Switch &node)
Definition EventVisitor:117
osgGA::GUIActionAdapter * _actionAdapter
Definition EventVisitor:145
virtual void apply(osg::Billboard &node)
Definition EventVisitor:112
virtual void apply(osg::Drawable &drawable)
Definition EventVisitor:82
void handle_callbacks_and_traverse(osg::Node &node)
Definition EventVisitor:135
virtual void apply(osg::Geode &node)
Definition EventVisitor:111
META_NodeVisitor(osgGA, EventVisitor) virtual osgGA
Definition EventVisitor:48
virtual void apply(osg::Group &node)
Definition EventVisitor:114
virtual void apply(osg::LOD &node)
Definition EventVisitor:118
virtual const osgGA::EventVisitor * asEventVisitor() const
Definition EventVisitor:56
Definition Event:27
Definition GUIActionAdapter:62
Definition Billboard:27
Definition Callback:134
bool run(osg::Object *object, osg::Object *data)
Definition Callback:34
virtual CallbackObject * asCallbackObject()
Definition Callback:49
virtual osgGA::EventHandler * asEventHandler()
Definition Callback:70
virtual DrawableEventCallback * asDrawableEventCallback()
Definition Callback:64
virtual bool run(osg::Object *object, osg::Object *data)
Definition Callback:80
virtual NodeCallback * asNodeCallback()
Definition Callback:52
Definition Callback:320
virtual void event(osg::NodeVisitor *, osg::Drawable *)
Definition Callback:337
Definition Drawable:89
Definition Geode:29
Definition Group:29
Definition LOD:36
Definition LightSource:25
Definition Callback:215
Definition NodeVisitor:82
Definition Node:72
unsigned int getNumChildrenRequiringEventTraversal() const
Definition Node:284
osg::StateSet * getStateSet()
Definition Node:382
Callback * getEventCallback()
Definition Node:252
Definition OccluderNode:27
Definition Projection:25
Definition StateSet:46
void runEventCallbacks(osg::NodeVisitor *nv)
bool requiresEventTraversal() const
Definition StateSet:496
Definition Switch:27
Definition Transform:75
Definition ref_ptr:32
Definition Callback:21
#define OSGGA_EXPORT
Definition osgGA/Export:40