Action 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* -*-c++-*-
  2. * Copyright (C) 2009 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_ACTION_H
  15. #define OSGANIMATION_ACTION_H
  16. #include <osgAnimation/Export>
  17. #include <osgAnimation/Animation>
  18. #include <osgAnimation/ActionVisitor>
  19. #include <osgAnimation/FrameAction>
  20. #include <iostream>
  21. #define META_Action(library,name) \
  22. virtual osg::Object* cloneType() const { return new name (); } \
  23. virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
  24. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
  25. virtual const char* className() const { return #name; } \
  26. virtual const char* libraryName() const { return #library; } \
  27. virtual void accept(osgAnimation::ActionVisitor& nv) { nv.apply(*this); } \
  28. namespace osgAnimation
  29. {
  30. class OSGANIMATION_EXPORT Action : public osg::Object
  31. {
  32. public:
  33. class Callback : public osg::Object
  34. {
  35. public:
  36. Callback(){}
  37. Callback(const Callback& nc,const osg::CopyOp& copyop) :
  38. osg::Object(nc, copyop),
  39. _nestedCallback(nc._nestedCallback) {}
  40. META_Object(osgAnimation,Callback);
  41. virtual void operator()(Action* /*action*/, osgAnimation::ActionVisitor* /*nv*/) {}
  42. Callback* getNestedCallback() { return _nestedCallback.get(); }
  43. void addNestedCallback(Callback* callback)
  44. {
  45. if (callback) {
  46. if (_nestedCallback.valid())
  47. _nestedCallback->addNestedCallback(callback);
  48. else
  49. _nestedCallback = callback;
  50. }
  51. }
  52. void removeCallback(Callback* cb)
  53. {
  54. if (!cb)
  55. return;
  56. if (_nestedCallback.get() == cb)
  57. _nestedCallback = _nestedCallback->getNestedCallback();
  58. else if (_nestedCallback.valid())
  59. _nestedCallback->removeCallback(cb);
  60. }
  61. protected:
  62. osg::ref_ptr<Callback> _nestedCallback;
  63. };
  64. typedef std::map<unsigned int, osg::ref_ptr<Callback> > FrameCallback;
  65. META_Action(osgAnimation, Action);
  66. Action();
  67. Action(const Action&,const osg::CopyOp&);
  68. void setCallback(double when, Callback* callback)
  69. {
  70. setCallback(static_cast<unsigned int>(floor(when*_fps)), callback);
  71. }
  72. void setCallback(unsigned int frame, Callback* callback)
  73. {
  74. if (_framesCallback[frame].valid())
  75. _framesCallback[frame]->addNestedCallback(callback);
  76. else
  77. _framesCallback[frame] = callback;
  78. }
  79. Callback* getCallback(unsigned int frame)
  80. {
  81. if (_framesCallback.find(frame) == _framesCallback.end())
  82. return 0;
  83. return _framesCallback[frame].get();
  84. }
  85. void removeCallback(Callback*);
  86. Callback* getFrameCallback(unsigned int frame);
  87. Callback* getFrameCallback(double time);
  88. unsigned int getFramesPerSecond() const { return _fps; }
  89. void setNumFrames(unsigned int numFrames) { _numberFrame = numFrames;}
  90. void setDuration(double duration) { _numberFrame = static_cast<unsigned int>(floor(duration * _fps)); }
  91. unsigned int getNumFrames() const { return _numberFrame;}
  92. double getDuration() const { return _numberFrame * 1.0 / _fps; }
  93. // 0 means infinite else it's the number of loop
  94. virtual void setLoop(unsigned int nb) { _loop = nb; }
  95. virtual unsigned int getLoop() const { return _loop;}
  96. // get the number of loop, the frame relative to loop.
  97. // return true if in range, and false if out of range.
  98. bool evaluateFrame(unsigned int frame, unsigned int& resultframe, unsigned int& nbloop );
  99. virtual void traverse(ActionVisitor& /*visitor*/) {}
  100. //virtual void evaluate(unsigned int frame);
  101. protected:
  102. FrameCallback _framesCallback;
  103. double _speed;
  104. unsigned int _fps;
  105. unsigned int _numberFrame;
  106. unsigned int _loop;
  107. enum Status
  108. {
  109. Play,
  110. Stop
  111. };
  112. Status _state;
  113. };
  114. }
  115. #endif