OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EventVisitor.h
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/GUIActionAdapter>
31 #include <osgGA/EventQueue>
32 
33 namespace osgGA {
34 
41 {
42  public:
43 
44  EventVisitor();
45  virtual ~EventVisitor();
46 
48 
49  void setActionAdapter(osgGA::GUIActionAdapter* actionAdapter) { _actionAdapter=actionAdapter; }
50 
51  osgGA::GUIActionAdapter* getActionAdapter() { return _actionAdapter; }
52 
53  const osgGA::GUIActionAdapter* getActionAdapter() const { return _actionAdapter; }
54 
55  void addEvent(Event* event);
56  void removeEvent(Event* event);
57 
58  void setEventHandled(bool handled) { _handled = handled; }
59  bool getEventHandled() const { return _handled; }
60 
61  void setEvents(const EventQueue::Events& events) { _events = events; }
62  EventQueue::Events& getEvents() { return _events; }
63  const EventQueue::Events& getEvents() const { return _events; }
64 
65  public:
66 
67  virtual void reset();
68 
70  virtual void apply(osg::Node& node) { handle_callbacks_and_traverse(node); }
71 
72 
73  virtual void apply(osg::Drawable& drawable)
74  {
75  osg::Callback* callback = drawable.getEventCallback();
76  if (callback)
77  {
78  osg::Drawable::EventCallback* drawable_callback = dynamic_cast<osg::Drawable::EventCallback*>(callback);
79  osg::NodeCallback* node_callback = dynamic_cast<osg::NodeCallback*>(callback);
80  osg::CallbackObject* callback_object = dynamic_cast<osg::CallbackObject*>(callback);
81 
82  if (drawable_callback) drawable_callback->event(this,&drawable);
83  if (node_callback) (*node_callback)(&drawable, this);
84 
85  if ((!drawable_callback && !node_callback) || callback_object) callback_object->run(&drawable, this);
86  }
87 
88  handle_callbacks(drawable.getStateSet());
89  }
90 
91  virtual void apply(osg::Geode& node) { handle_callbacks_and_traverse(node); }
92  virtual void apply(osg::Billboard& node) { handle_callbacks_and_traverse(node); }
93 
94  virtual void apply(osg::LightSource& node) { handle_callbacks_and_traverse(node); }
95 
96  virtual void apply(osg::Group& node) { handle_callbacks_and_traverse(node); }
97  virtual void apply(osg::Transform& node) { handle_callbacks_and_traverse(node); }
98  virtual void apply(osg::Projection& node) { handle_callbacks_and_traverse(node); }
99  virtual void apply(osg::Switch& node) { handle_callbacks_and_traverse(node); }
100  virtual void apply(osg::LOD& node) { handle_callbacks_and_traverse(node); }
101  virtual void apply(osg::OccluderNode& node) { handle_callbacks_and_traverse(node); }
102 
103 
104  protected:
105 
107  EventVisitor& operator = (const EventVisitor&) { return *this; }
108 
109  inline void handle_callbacks(osg::StateSet* stateset)
110  {
111  if (stateset && stateset->requiresEventTraversal())
112  {
113  stateset->runEventCallbacks(this);
114  }
115  }
116 
118  {
119  handle_callbacks(node.getStateSet());
120 
121  osg::Callback* callback = node.getEventCallback();
122  if (callback) callback->run(&node,this);
123  else if (node.getNumChildrenRequiringEventTraversal()>0) traverse(node);
124  }
125 
126 
128 
130 
131  bool _handled;
133 };
134 
135 }
136 
137 #endif
138 
void setEvents(const EventQueue::Events &events)
Definition: EventVisitor.h:61
const osgGA::GUIActionAdapter * getActionAdapter() const
Definition: EventVisitor.h:53
virtual void apply(osg::Geode &node)
Definition: EventVisitor.h:91
virtual void apply(osg::Drawable &drawable)
Definition: EventVisitor.h:73
EventQueue::Events _events
Definition: EventVisitor.h:132
Callback * getEventCallback()
Definition: Node.h:251
virtual void apply(osg::Transform &node)
Definition: EventVisitor.h:97
virtual void apply(osg::Node &node)
Definition: EventVisitor.h:70
void handle_callbacks(osg::StateSet *stateset)
Definition: EventVisitor.h:109
virtual void event(osg::NodeVisitor *, osg::Drawable *)
Definition: Drawable.h:299
bool run(osg::Object *object, osg::Object *data)
virtual void apply(osg::Switch &node)
Definition: EventVisitor.h:99
META_NodeVisitor(osgGA, EventVisitor) void setActionAdapter(osgGA
Definition: EventVisitor.h:47
bool requiresEventTraversal() const
Definition: StateSet.h:457
virtual void apply(osg::LOD &node)
Definition: EventVisitor.h:100
virtual void apply(osg::Billboard &node)
Definition: EventVisitor.h:92
std::list< osg::ref_ptr< Event > > Events
Definition: EventQueue.h:36
#define OSGGA_EXPORT
Definition: Export.h:40
Definition: LOD.h:35
virtual bool run(osg::Object *object, osg::Object *data)
Definition: Callback.h:42
void handle_callbacks_and_traverse(osg::Node &node)
Definition: EventVisitor.h:117
unsigned int getNumChildrenRequiringEventTraversal() const
Definition: Node.h:279
typedef void(GL_APIENTRY *GLTexImage3DProc)(GLenum target
virtual void apply(osg::LightSource &node)
Definition: EventVisitor.h:94
bool getEventHandled() const
Definition: EventVisitor.h:59
virtual void apply(osg::Group &node)
Definition: EventVisitor.h:96
osgGA::GUIActionAdapter * getActionAdapter()
Definition: EventVisitor.h:51
void setEventHandled(bool handled)
Definition: EventVisitor.h:58
virtual void apply(osg::OccluderNode &node)
Definition: EventVisitor.h:101
EventQueue::Events & getEvents()
Definition: EventVisitor.h:62
Definition: Node.h:71
const EventQueue::Events & getEvents() const
Definition: EventVisitor.h:63
void runEventCallbacks(osg::NodeVisitor *nv)
virtual void apply(osg::Projection &node)
Definition: EventVisitor.h:98
osgGA::GUIActionAdapter * _actionAdapter
Definition: EventVisitor.h:127
osg::StateSet * getStateSet()
Definition: Node.h:369
osg::ref_ptr< GUIEventAdapter > _accumulateEventState
Definition: EventVisitor.h:129