| 1 | /* -*-c++-*- |
|---|
| 2 | * Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net> |
|---|
| 3 | * |
|---|
| 4 | * This library is open source and may be redistributed and/or modified under |
|---|
| 5 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 6 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 7 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 8 | * |
|---|
| 9 | * This library is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * OpenSceneGraph Public License for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | #ifndef OSGANIMATION_TIMELINE |
|---|
| 16 | #define OSGANIMATION_TIMELINE 1 |
|---|
| 17 | |
|---|
| 18 | #include <osgAnimation/Export> |
|---|
| 19 | #include <map> |
|---|
| 20 | #include <vector> |
|---|
| 21 | #include <osg/observer_ptr> |
|---|
| 22 | #include <osg/Notify> |
|---|
| 23 | #include <osg/Stats> |
|---|
| 24 | #include <osgAnimation/Action> |
|---|
| 25 | #include <osgAnimation/FrameAction> |
|---|
| 26 | #include <osgAnimation/AnimationManagerBase> |
|---|
| 27 | #include <osgAnimation/StatsVisitor> |
|---|
| 28 | |
|---|
| 29 | namespace osgAnimation |
|---|
| 30 | { |
|---|
| 31 | class OSGANIMATION_EXPORT Timeline : public Action |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | |
|---|
| 35 | Timeline(); |
|---|
| 36 | Timeline(const Timeline& nc,const osg::CopyOp& op = osg::CopyOp::SHALLOW_COPY); |
|---|
| 37 | |
|---|
| 38 | META_Action(osgAnimation, Timeline); |
|---|
| 39 | |
|---|
| 40 | enum TimelineStatus |
|---|
| 41 | { |
|---|
| 42 | Play, |
|---|
| 43 | Stop |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | TimelineStatus getStatus() const { return _state; } |
|---|
| 47 | |
|---|
| 48 | typedef std::vector<FrameAction> ActionList; |
|---|
| 49 | typedef std::map<int, ActionList> ActionLayers; |
|---|
| 50 | |
|---|
| 51 | const ActionList& getActionLayer(int i) { return _actions[i];} |
|---|
| 52 | unsigned int getCurrentFrame() const { return _currentFrame;} |
|---|
| 53 | double getCurrentTime() const { return _currentFrame * 1.0 / _fps;} |
|---|
| 54 | |
|---|
| 55 | void play() { _state = Play; } |
|---|
| 56 | void gotoFrame(unsigned int frame) { _currentFrame = frame; } |
|---|
| 57 | void stop() { _state = Stop; } |
|---|
| 58 | bool getEvaluating() const { return _evaluating;} |
|---|
| 59 | |
|---|
| 60 | bool isActive(Action* activeAction); |
|---|
| 61 | |
|---|
| 62 | void removeAction(Action* action); |
|---|
| 63 | virtual void addActionAt(unsigned int frame, Action* action, int priority = 0); |
|---|
| 64 | virtual void addActionAt(double t, Action* action, int priority = 0); |
|---|
| 65 | void addActionNow(Action* action, int priority = 0); |
|---|
| 66 | |
|---|
| 67 | void clearActions(); |
|---|
| 68 | |
|---|
| 69 | virtual void update(double simulationTime); |
|---|
| 70 | void setLastFrameEvaluated(unsigned int frame) { _previousFrameEvaluated = frame; } |
|---|
| 71 | |
|---|
| 72 | void setEvaluating(bool state) { _evaluating = state;} |
|---|
| 73 | void traverse(ActionVisitor& visitor); |
|---|
| 74 | |
|---|
| 75 | void setStats(osg::Stats* stats); |
|---|
| 76 | osg::Stats* getStats(); |
|---|
| 77 | void collectStats(bool state); |
|---|
| 78 | osgAnimation::StatsActionVisitor* getStatsVisitor(); |
|---|
| 79 | |
|---|
| 80 | const ActionLayers& getActionLayers() const { return _actions; } |
|---|
| 81 | |
|---|
| 82 | void processPendingOperation(); |
|---|
| 83 | void setAnimationManager(AnimationManagerBase*); |
|---|
| 84 | protected: |
|---|
| 85 | osg::observer_ptr<AnimationManagerBase> _animationManager; |
|---|
| 86 | ActionLayers _actions; |
|---|
| 87 | double _lastUpdate; |
|---|
| 88 | double _speed; |
|---|
| 89 | unsigned int _currentFrame; |
|---|
| 90 | unsigned int _previousFrameEvaluated; |
|---|
| 91 | bool _initFirstFrame; |
|---|
| 92 | TimelineStatus _state; |
|---|
| 93 | |
|---|
| 94 | bool _collectStats; |
|---|
| 95 | osg::ref_ptr<osg::Stats> _stats; |
|---|
| 96 | osg::ref_ptr<osgAnimation::StatsActionVisitor> _statsVisitor; |
|---|
| 97 | |
|---|
| 98 | // to manage pending operation |
|---|
| 99 | bool _evaluating; |
|---|
| 100 | |
|---|
| 101 | struct Command |
|---|
| 102 | { |
|---|
| 103 | Command():_priority(0) {} |
|---|
| 104 | Command(int priority, const FrameAction& action) : _priority(priority), _action(action) {} |
|---|
| 105 | int _priority; |
|---|
| 106 | FrameAction _action; |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | typedef std::vector<Command> CommandList; |
|---|
| 110 | CommandList _addActionOperations; |
|---|
| 111 | ActionList _removeActionOperations; |
|---|
| 112 | |
|---|
| 113 | void internalRemoveAction(Action* action); |
|---|
| 114 | void internalAddAction(int priority, const FrameAction& ftl); |
|---|
| 115 | |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | #endif |
|---|