1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSGGA_EVENTVISITOR
15#define OSGGA_EVENTVISITOR 1
17#include <osg/NodeVisitor>
20#include <osg/Billboard>
23#include <osg/LightSource>
24#include <osg/Transform>
25#include <osg/Projection>
26#include <osg/OccluderNode>
27#include <osg/ScriptEngine>
29#include <osgGA/GUIEventAdapter>
30#include <osgGA/GUIEventHandler>
31#include <osgGA/GUIActionAdapter>
32#include <osgGA/EventQueue>
37 * Basic EventVisitor implementation for animating a scene.
38 * This visitor traverses the scene graph, calling each nodes appCallback if
41class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor
46 virtual ~EventVisitor();
48 META_NodeVisitor(osgGA, EventVisitor)
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; }
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; }
58 void setActionAdapter(osgGA::GUIActionAdapter* actionAdapter) { _actionAdapter=actionAdapter; }
60 osgGA::GUIActionAdapter* getActionAdapter() { return _actionAdapter; }
62 const osgGA::GUIActionAdapter* getActionAdapter() const { return _actionAdapter; }
64 void addEvent(Event* event);
65 void removeEvent(Event* event);
67 void setEventHandled(bool handled) { _handled = handled; }
68 bool getEventHandled() const { return _handled; }
70 void setEvents(const EventQueue::Events& events) { _events = events; }
71 EventQueue::Events& getEvents() { return _events; }
72 const EventQueue::Events& getEvents() const { return _events; }
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); }
82 virtual void apply(osg::Drawable& drawable)
84 osg::Callback* callback = drawable.getEventCallback();
87 osgGA::EventHandler* eh = callback->asEventHandler();
90 callback->run(&drawable,this);
94 osg::DrawableEventCallback* drawable_callback = callback->asDrawableEventCallback();
95 osg::NodeCallback* node_callback = callback->asNodeCallback();
96 osg::CallbackObject* callback_object = callback->asCallbackObject();
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);
102 if (!drawable_callback && !node_callback && !callback_object) callback->run(&drawable, this);
106 handle_callbacks(drawable.getStateSet());
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); }
124 /** Prevent unwanted copy operator.*/
125 EventVisitor& operator = (const EventVisitor&) { return *this; }
127 inline void handle_callbacks(osg::StateSet* stateset)
129 if (stateset && stateset->requiresEventTraversal())
131 stateset->runEventCallbacks(this);
135 inline void handle_callbacks_and_traverse(osg::Node& node)
137 handle_callbacks(node.getStateSet());
139 osg::Callback* callback = node.getEventCallback();
140 if (callback) callback->run(&node,this);
141 else if (node.getNumChildrenRequiringEventTraversal()>0) traverse(node);
145 osgGA::GUIActionAdapter* _actionAdapter;
147 osg::ref_ptr<GUIEventAdapter> _accumulateEventState;
150 EventQueue::Events _events;