root/OpenSceneGraph/trunk/include/osgGA/EventVisitor @ 13890

Revision 13890, 4.8 kB (checked in by robert, 3 hours ago)

Added simple test script for osgUI's TabWidget?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
32namespace 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 */
39class 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        void addEvent(Event* event);
55        void removeEvent(Event* event);
56
57        void setEventHandled(bool handled) { _handled = handled; }
58        bool getEventHandled() const { return _handled; }
59
60        void setEvents(const EventQueue::Events& events) { _events = events; }
61        EventQueue::Events& getEvents() { return _events; }
62        const EventQueue::Events& getEvents() const { return _events; }
63
64    public:
65
66        virtual void reset();
67
68        /** During traversal each type of node calls its callbacks and its children traversed. */
69        virtual void apply(osg::Node& node)         { handle_callbacks_and_traverse(node); }
70
71        virtual void apply(osg::Geode& node)        { handle_geode_callbacks(node); }
72        virtual void apply(osg::Billboard& node)    { handle_geode_callbacks(node); }
73
74        virtual void apply(osg::LightSource& node)  { handle_callbacks_and_traverse(node); }
75
76        virtual void apply(osg::Group& node)        { handle_callbacks_and_traverse(node); }
77        virtual void apply(osg::Transform& node)    { handle_callbacks_and_traverse(node); }
78        virtual void apply(osg::Projection& node)   { handle_callbacks_and_traverse(node); }
79        virtual void apply(osg::Switch& node)       { handle_callbacks_and_traverse(node); }
80        virtual void apply(osg::LOD& node)          { handle_callbacks_and_traverse(node); }
81        virtual void apply(osg::OccluderNode& node) { handle_callbacks_and_traverse(node); }
82
83
84    protected:
85
86        /** Prevent unwanted copy operator.*/
87        EventVisitor& operator = (const EventVisitor&) { return *this; }
88
89        inline void handle_callbacks(osg::StateSet* stateset)
90        {
91            if (stateset && stateset->requiresEventTraversal())
92            {
93                stateset->runEventCallbacks(this);
94            }
95        }
96
97        inline void handle_callbacks_and_traverse(osg::Node& node)
98        {
99            handle_callbacks(node.getStateSet());
100
101            osg::NodeCallback* callback = node.getEventCallback();
102            if (callback) (*callback)(&node,this);
103            else if (node.getNumChildrenRequiringEventTraversal()>0) traverse(node);
104        }
105
106        inline void handle_geode_callbacks(osg::Geode& node)
107        {
108            handle_callbacks(node.getStateSet());
109
110            osg::NodeCallback* callback = node.getEventCallback();
111            if (callback) (*callback)(&node,this);
112            /*else if (node.getNumChildrenRequiringEventTraversal()>0)*/
113            traverseGeode(node);
114        }
115
116        inline void traverseGeode(osg::Geode& geode)
117        {
118            traverse((osg::Node&)geode);
119
120            // Call the app callbacks on the drawables.
121            for(unsigned int i=0;i<geode.getNumDrawables();++i)
122            {
123                osg::Drawable::EventCallback* callback = geode.getDrawable(i)->getEventCallback();
124                if (callback) callback->event(this,geode.getDrawable(i));
125
126                handle_callbacks(geode.getDrawable(i)->getStateSet());
127            }
128        }
129
130        osgGA::GUIActionAdapter*        _actionAdapter;
131
132        osg::ref_ptr<GUIEventAdapter>   _accumulateEventState;
133
134        bool                            _handled;
135        EventQueue::Events              _events;
136};
137
138}
139
140#endif
141
Note: See TracBrowser for help on using the browser.