Timeline 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef OSGANIMATION_TIMELINE
  15. #define OSGANIMATION_TIMELINE 1
  16. #include <osgAnimation/Export>
  17. #include <map>
  18. #include <vector>
  19. #include <osg/observer_ptr>
  20. #include <osg/Notify>
  21. #include <osg/Stats>
  22. #include <osgAnimation/Action>
  23. #include <osgAnimation/FrameAction>
  24. #include <osgAnimation/AnimationManagerBase>
  25. #include <osgAnimation/StatsVisitor>
  26. namespace osgAnimation
  27. {
  28. class OSGANIMATION_EXPORT Timeline : public Action
  29. {
  30. public:
  31. Timeline();
  32. Timeline(const Timeline& nc,const osg::CopyOp& op = osg::CopyOp::SHALLOW_COPY);
  33. META_Action(osgAnimation, Timeline);
  34. enum TimelineStatus
  35. {
  36. Play,
  37. Stop
  38. };
  39. TimelineStatus getStatus() const { return _state; }
  40. typedef std::vector<FrameAction> ActionList;
  41. typedef std::map<int, ActionList> ActionLayers;
  42. const ActionList& getActionLayer(int i) { return _actions[i];}
  43. unsigned int getCurrentFrame() const { return _currentFrame;}
  44. double getCurrentTime() const { return _currentFrame * 1.0 / _fps;}
  45. void play() { _state = Play; }
  46. void gotoFrame(unsigned int frame) { _currentFrame = frame; }
  47. void stop() { _state = Stop; }
  48. bool getEvaluating() const { return _evaluating;}
  49. bool isActive(Action* activeAction);
  50. void removeAction(Action* action);
  51. virtual void addActionAt(unsigned int frame, Action* action, int priority = 0);
  52. virtual void addActionAt(double t, Action* action, int priority = 0);
  53. void addActionNow(Action* action, int priority = 0);
  54. void clearActions();
  55. virtual void update(double simulationTime);
  56. void setLastFrameEvaluated(unsigned int frame) { _previousFrameEvaluated = frame; }
  57. void setEvaluating(bool state) { _evaluating = state;}
  58. void traverse(ActionVisitor& visitor);
  59. void setStats(osg::Stats* stats);
  60. osg::Stats* getStats();
  61. void collectStats(bool state);
  62. osgAnimation::StatsActionVisitor* getStatsVisitor();
  63. const ActionLayers& getActionLayers() const { return _actions; }
  64. void processPendingOperation();
  65. void setAnimationManager(AnimationManagerBase*);
  66. protected:
  67. osg::observer_ptr<AnimationManagerBase> _animationManager;
  68. ActionLayers _actions;
  69. double _lastUpdate;
  70. double _speed;
  71. unsigned int _currentFrame;
  72. unsigned int _previousFrameEvaluated;
  73. bool _initFirstFrame;
  74. TimelineStatus _state;
  75. bool _collectStats;
  76. osg::ref_ptr<osg::Stats> _stats;
  77. osg::ref_ptr<osgAnimation::StatsActionVisitor> _statsVisitor;
  78. // to manage pending operation
  79. bool _evaluating;
  80. struct Command
  81. {
  82. Command():_priority(0) {}
  83. Command(int priority, const FrameAction& action) : _priority(priority), _action(action) {}
  84. int _priority;
  85. FrameAction _action;
  86. };
  87. typedef std::vector<Command> CommandList;
  88. CommandList _addActionOperations;
  89. ActionList _removeActionOperations;
  90. void internalRemoveAction(Action* action);
  91. void internalAddAction(int priority, const FrameAction& ftl);
  92. };
  93. }
  94. #endif