ISceneNodeAnimator.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef IRR_I_SCENE_NODE_ANIMATOR_H_INCLUDED
  5. #define IRR_I_SCENE_NODE_ANIMATOR_H_INCLUDED
  6. #include "ESceneNodeAnimatorTypes.h"
  7. #include "IAttributeExchangingObject.h"
  8. #include "IAttributes.h"
  9. #include "IEventReceiver.h"
  10. namespace irr
  11. {
  12. namespace io
  13. {
  14. class IAttributes;
  15. } // end namespace io
  16. namespace scene
  17. {
  18. class ISceneNode;
  19. class ISceneManager;
  20. //! Animates a scene node. Can animate position, rotation, material, and so on.
  21. /** A scene node animator is able to animate a scene node in a very simple way. It may
  22. change its position, rotation, scale and/or material. There are lots of animators
  23. to choose from. You can create scene node animators with the ISceneManager interface.
  24. */
  25. class ISceneNodeAnimator : public io::IAttributeExchangingObject, public IEventReceiver
  26. {
  27. public:
  28. ISceneNodeAnimator() : IsEnabled(true), PauseTimeSum(0), PauseTimeStart(0), StartTime(0)
  29. {
  30. }
  31. //! Animates a scene node.
  32. /** \param node Node to animate.
  33. \param timeMs Current time in milliseconds. */
  34. virtual void animateNode(ISceneNode* node, u32 timeMs) =0;
  35. //! Creates a clone of this animator.
  36. /** Please note that you will have to drop
  37. (IReferenceCounted::drop()) the returned pointer after calling this. */
  38. virtual ISceneNodeAnimator* createClone(ISceneNode* node,
  39. ISceneManager* newManager=0) =0;
  40. //! Returns true if this animator receives events.
  41. /** When attached to an active camera, this animator will be
  42. able to respond to events such as mouse and keyboard events. */
  43. virtual bool isEventReceiverEnabled() const
  44. {
  45. return false;
  46. }
  47. //! Event receiver, override this function for camera controlling animators
  48. virtual bool OnEvent(const SEvent& event) IRR_OVERRIDE
  49. {
  50. return false;
  51. }
  52. //! Returns type of the scene node animator
  53. virtual ESCENE_NODE_ANIMATOR_TYPE getType() const
  54. {
  55. return ESNAT_UNKNOWN;
  56. }
  57. //! Returns if the animator has finished.
  58. /** This is only valid for non-looping animators with a discrete end state.
  59. \return true if the animator has finished, false if it is still running. */
  60. virtual bool hasFinished(void) const
  61. {
  62. return false;
  63. }
  64. //! Reset a time-based movement by changing the starttime.
  65. /** By default most animators start on object creation.
  66. This value is ignored by animators which don't work with a starttime.
  67. Known problems: CSceneNodeAnimatorRotation currently overwrites this value constantly (might be changed in the future).
  68. \param time Commonly you will use irr::ITimer::getTime().
  69. \param resetPauseTime Reset internal pause time for enabling/disabling animators as well
  70. */
  71. virtual void setStartTime(u32 time, bool resetPauseTime=true)
  72. {
  73. StartTime = time;
  74. if ( resetPauseTime )
  75. {
  76. PauseTimeStart = 0;
  77. PauseTimeSum = 0;
  78. }
  79. }
  80. //! Get the starttime.
  81. /** This will return 0 for by animators which don't work with a starttime unless a starttime was manually set */
  82. virtual irr::u32 getStartTime() const
  83. {
  84. return StartTime;
  85. }
  86. //! Sets the enabled state of this element.
  87. /**
  88. \param enabled When set to false ISceneNodes will not update the animator anymore.
  89. Animators themselves usually don't care. So manual calls to animateNode still work.
  90. \param timeNow When set to values > 0 on enabling and disabling an internal timer will be increased by the time disabled time.
  91. Animator decide themselves how to handle that timer, but generally setting it will allow you to pause an animator, so it
  92. will continue at the same position when you enable it again. To use that pass irr::ITimer::getTime() as value.
  93. Animators with no timers will just ignore this.
  94. */
  95. virtual void setEnabled(bool enabled, u32 timeNow=0)
  96. {
  97. if ( enabled == IsEnabled )
  98. return;
  99. IsEnabled = enabled;
  100. if ( enabled )
  101. {
  102. if ( timeNow > 0 && PauseTimeStart > 0 )
  103. PauseTimeSum += timeNow-PauseTimeStart;
  104. }
  105. else
  106. {
  107. PauseTimeStart = timeNow;
  108. }
  109. }
  110. virtual bool isEnabled() const
  111. {
  112. return IsEnabled;
  113. }
  114. //! Writes attributes of the scene node animator.
  115. virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const IRR_OVERRIDE
  116. {
  117. out->addBool("IsEnabled", IsEnabled);
  118. // timers not serialized as they usually depend on system-time which is different on each application start.
  119. }
  120. //! Reads attributes of the scene node animator.
  121. virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) IRR_OVERRIDE
  122. {
  123. IsEnabled = in->getAttributeAsBool("IsEnabled", IsEnabled);
  124. PauseTimeSum = 0;
  125. PauseTimeStart = 0;
  126. }
  127. protected:
  128. /** This method can be used by clone() implementations of
  129. derived classes
  130. \param toCopyFrom The animator from which the values are copied */
  131. void cloneMembers(const ISceneNodeAnimator* toCopyFrom)
  132. {
  133. IsEnabled = toCopyFrom->IsEnabled;
  134. PauseTimeSum = toCopyFrom->IsEnabled;
  135. PauseTimeStart = toCopyFrom->PauseTimeStart;
  136. StartTime = toCopyFrom->StartTime;
  137. }
  138. bool IsEnabled; //! Only enabled animators are updated
  139. u32 PauseTimeSum; //! Sum up time which the animator was disabled
  140. u32 PauseTimeStart; //! Last time setEnabled(false) was called with a timer > 0
  141. u32 StartTime; //! Used by animators which are time-based, ignored otherwise.
  142. };
  143. } // end namespace scene
  144. } // end namespace irr
  145. #endif