root/OpenSceneGraph/trunk/include/osgPresentation/SlideEventHandler @ 11164

Revision 11164, 9.4 kB (checked in by robert, 5 years ago)

Replaced use of unsigned int/enum mask combinations with int/enum mask combinations to avoid the need for casting enums to unsigned ints,
and to avoid associated warnings.

Update wrappers to reflect these changes.

Line 
1/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield
2 *
3 * This software is open source and may be redistributed and/or modified under 
4 * the terms of the GNU General Public License (GPL) version 2.0.
5 * The full license is in LICENSE.txt file included with this distribution,.
6 *
7 * This software is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * include LICENSE.txt for more details.
11*/
12
13#ifndef SLIDEEVENTHANDLER
14#define SLIDEEVENTHANDLER 1
15
16#include <osg/Switch>
17#include <osg/Timer>
18
19#include <osgGA/GUIEventHandler>
20#include <osgViewer/Viewer>
21
22#include <osgPresentation/CompileSlideCallback>
23
24namespace osgPresentation
25{
26
27/// Operations related to click to run/load/key events.
28enum Operation
29{
30    RUN,
31    LOAD,
32    EVENT,
33    JUMP
34};
35
36struct HomePosition : public virtual osg::Referenced
37{
38    HomePosition() {}
39
40    HomePosition(const osg::Vec3& in_eye, const osg::Vec3& in_center, const osg::Vec3& in_up):
41        eye(in_eye),
42        center(in_center),
43        up(in_up) {}
44
45    osg::Vec3   eye;
46    osg::Vec3   center;
47    osg::Vec3   up;
48};
49
50struct KeyPosition
51{
52    KeyPosition(unsigned int key=0, float x=FLT_MAX, float y=FLT_MAX):
53        _key((osgGA::GUIEventAdapter::KeySymbol)key),
54        _x(x),
55        _y(y) {}
56
57
58    void set(unsigned int key=0, float x=FLT_MAX, float y=FLT_MAX)
59    {
60        _key = (osgGA::GUIEventAdapter::KeySymbol)key;
61        _x = x;
62        _y = y;
63    }
64
65    osgGA::GUIEventAdapter::KeySymbol _key;
66    float        _x;
67    float        _y;
68};
69
70struct LayerCallback : public virtual osg::Referenced
71{
72    virtual void operator() (osg::Node* node) const = 0;
73};
74
75struct OSGPRESENTATION_EXPORT LayerAttributes : public virtual osg::Referenced
76{
77    LayerAttributes():_duration(0),_relativeJump(true),_slideNum(0),_layerNum(0) {}
78    LayerAttributes(double in_duration):_duration(in_duration),_relativeJump(true),_slideNum(0),_layerNum(0) {}
79
80    void setDuration(double duration) { _duration = duration; }
81    double getDuration() const { return _duration; }
82
83    typedef std::vector<KeyPosition> Keys;
84    typedef std::vector<std::string> RunStrings;
85
86    void setKeys(const Keys& keys) { _keys = keys; }
87    const Keys& getKeys() const { return _keys; }
88
89    void addKey(const KeyPosition& kp) { _keys.push_back(kp); }
90
91    void setRunStrings(const RunStrings& runStrings) { _runStrings = runStrings; }
92    const RunStrings& getRunStrings() const { return _runStrings; }
93
94    void addRunString(const std::string& runString) { _runStrings.push_back(runString); }
95
96    void setJump(bool relativeJump, int slideNum, int layerNum)
97    {
98        _relativeJump = relativeJump;
99        _slideNum = slideNum;
100        _layerNum = layerNum;
101    }
102
103    bool getRelativeJump() const { return _relativeJump; }
104    int getSlideNum() const { return _slideNum; }
105    int getLayerNum() const { return _layerNum; }
106
107    bool requiresJump() const { return _relativeJump ? (_slideNum!=0 || _layerNum!=0) : true; }
108
109    double  _duration;
110    Keys    _keys;
111    RunStrings _runStrings;
112
113    bool                                _relativeJump;
114    int                                 _slideNum;
115    int                                 _layerNum;
116
117    void addEnterCallback(LayerCallback* lc) { _enterLayerCallbacks.push_back(lc); }
118    void addLeaveCallback(LayerCallback* lc) { _leaveLayerCallbacks.push_back(lc); }
119
120    void callEnterCallbacks(osg::Node* node);
121    void callLeaveCallbacks(osg::Node* node);
122
123    typedef std::list< osg::ref_ptr<LayerCallback> > LayerCallbacks;
124    LayerCallbacks _enterLayerCallbacks;
125    LayerCallbacks _leaveLayerCallbacks;
126};
127
128struct FilePathData : public virtual osg::Referenced
129{
130    FilePathData(const osgDB::FilePathList& fpl):filePathList(fpl) {}
131
132    osgDB::FilePathList filePathList;
133};
134
135
136struct dereference_less
137{
138    template<class T, class U>
139    inline bool operator() (const T& lhs,const U& rhs) const
140    {
141        return *lhs < *rhs;
142    }
143};
144
145struct ObjectOperator : public osg::Referenced
146{
147    inline bool operator < (const ObjectOperator& rhs) const { return ptr() < rhs.ptr(); }
148
149    virtual void* ptr() const = 0;
150
151    virtual void enter() = 0;
152    virtual void maintain() = 0;
153    virtual void leave() = 0;
154    virtual void setPause(bool pause) = 0;
155    virtual void reset() = 0;
156
157    virtual ~ObjectOperator() {}
158};
159
160class OSGPRESENTATION_EXPORT ActiveOperators
161{
162public:
163    ActiveOperators();
164    ~ActiveOperators();
165   
166    void collect(osg::Node* incommingNode, osg::NodeVisitor::TraversalMode tm = osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN);
167
168    void process();
169   
170    void setPause(bool pause);
171    bool getPause() const { return _pause; }
172   
173    void reset();
174
175    typedef std::set< osg::ref_ptr<ObjectOperator>, dereference_less >  OperatorList;
176
177protected:
178
179    void processOutgoing();
180    void processIncomming();
181    void processMaintained();
182
183    bool            _pause;
184
185    OperatorList    _previous;
186    OperatorList    _current;
187
188    OperatorList    _outgoing;
189    OperatorList    _incomming;
190    OperatorList    _maintained;
191
192};
193
194class OSGPRESENTATION_EXPORT SlideEventHandler : public osgGA::GUIEventHandler
195{
196public:
197
198    SlideEventHandler(osgViewer::Viewer* viewer=0);
199   
200    static SlideEventHandler* instance();
201   
202    META_Object(osgslideshowApp,SlideEventHandler);
203
204    void set(osg::Node* model);
205
206    virtual void accept(osgGA::GUIEventHandlerVisitor& v) { v.visit(*this); }
207
208    /** Event traversal node callback method.*/
209    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
210
211    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
212   
213    virtual void getUsage(osg::ApplicationUsage& usage) const;
214   
215    osgViewer::Viewer* getViewer() { return _viewer.get(); }
216
217    enum WhichPosition
218    {
219        FIRST_POSITION = 0,
220        LAST_POSITION = -1
221    };
222
223    void compileSlide(unsigned int slideNum);
224    void releaseSlide(unsigned int slideNum);
225
226    unsigned int getNumSlides();
227   
228    int getActiveSlide() const { return _activeSlide; }
229    int getActiveLayer() const { return _activeLayer; }
230
231    bool selectSlide(int slideNum,int layerNum=FIRST_POSITION);
232    bool selectLayer(int layerNum);
233
234    bool nextLayerOrSlide();
235    bool previousLayerOrSlide();
236
237    bool nextSlide();
238    bool previousSlide();
239
240    bool nextLayer();
241    bool previousLayer();
242   
243    bool home();
244
245    void setAutoSteppingActive(bool flag = true) { _autoSteppingActive = flag; }
246    bool getAutoSteppingActive() const { return _autoSteppingActive; }
247   
248    void setTimeDelayBetweenSlides(double dt) { _timePerSlide = dt; }
249    double getTimeDelayBetweenSlides() const { return _timePerSlide; }
250   
251    double getDuration(const osg::Node* node) const;
252
253    double getCurrentTimeDelayBetweenSlides() const;
254
255    void setReleaseAndCompileOnEachNewSlide(bool flag) { _releaseAndCompileOnEachNewSlide = flag; }
256    bool getReleaseAndCompileOnEachNewSlide() const { return _releaseAndCompileOnEachNewSlide; }
257
258    void setTimeDelayOnNewSlideWithMovies(float t) { _timeDelayOnNewSlideWithMovies = t; }
259    float getTimeDelayOnNewSlideWithMovies() const { return _timeDelayOnNewSlideWithMovies; }
260
261    void setLoopPresentation(bool loop) { _loopPresentation = loop; }
262    bool getLoopPresentation() const { return _loopPresentation; }
263
264    void dispatchEvent(const KeyPosition& keyPosition);
265
266protected:
267
268    ~SlideEventHandler() {}
269    SlideEventHandler(const SlideEventHandler&,const osg::CopyOp&) {}
270
271    bool home(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
272
273    void updateAlpha(bool, bool, float x, float y);
274    void updateLight(float x, float y);
275   
276
277    osg::observer_ptr<osgViewer::Viewer>    _viewer;
278   
279    osg::observer_ptr<osg::Switch>          _showSwitch;
280    int                                     _activePresentation;
281   
282    osg::observer_ptr<osg::Switch>          _presentationSwitch;
283    int                                     _activeSlide;
284
285    osg::observer_ptr<osg::Switch>          _slideSwitch;
286    int                                     _activeLayer;
287
288    bool                                    _firstTraversal;
289    double                                  _previousTime;
290    double                                  _timePerSlide;
291    bool                                    _autoSteppingActive;
292    bool                                    _loopPresentation;
293    bool                                    _pause;
294    bool                                    _hold;
295   
296    bool                                    _updateLightActive;
297    bool                                    _updateOpacityActive;
298    float                                   _previousX, _previousY;
299   
300    bool                                    _cursorOn;
301
302    bool                                    _releaseAndCompileOnEachNewSlide;
303
304    bool                                    _firstSlideOrLayerChange;
305    osg::Timer_t                            _tickAtFirstSlideOrLayerChange;
306    osg::Timer_t                            _tickAtLastSlideOrLayerChange;
307
308    float                                   _timeDelayOnNewSlideWithMovies;
309   
310    double                                  _minimumTimeBetweenKeyPresses;
311    double                                  _timeLastKeyPresses;
312   
313    ActiveOperators                         _activeOperators;
314   
315    osg::ref_ptr<CompileSlideCallback>      _compileSlideCallback;
316
317    void updateOperators();
318       
319};
320
321}
322
323#endif
Note: See TracBrowser for help on using the browser.