ISceneNodeAnimator.h 5.6 KB

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