BsCAnimation.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsAnimation.h"
  6. #include "BsComponent.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Components
  10. * @{
  11. */
  12. /**
  13. * @copydoc Animation
  14. *
  15. * Wraps Animation as a Component.
  16. */
  17. class BS_CORE_EXPORT CAnimation : public Component
  18. {
  19. /** Information about scene objects bound to a specific animation curve. */
  20. struct SceneObjectMappingInfo
  21. {
  22. HSceneObject sceneObject;
  23. bool isMappedToBone;
  24. HBone bone;
  25. };
  26. public:
  27. CAnimation(const HSceneObject& parent);
  28. virtual ~CAnimation() {}
  29. /**
  30. * Sets the default clip to play as soon as the component is enabled. If more control over playing clips is needed
  31. * use the play(), blend() and crossFade() methods to queue clips for playback manually, and setState() method for
  32. * modify their states individually.
  33. */
  34. void setDefaultClip(const HAnimationClip& clip);
  35. /** @copydoc Animation::setWrapMode */
  36. void setWrapMode(AnimWrapMode wrapMode);
  37. /** @copydoc Animation::setSpeed */
  38. void setSpeed(float speed);
  39. /** @copydoc Animation::play */
  40. void play(const HAnimationClip& clip);
  41. /** @copydoc Animation::blendAdditive */
  42. void blendAdditive(const HAnimationClip& clip, float weight, float fadeLength = 0.0f, UINT32 layer = 0);
  43. /** @copydoc Animation::blend1D */
  44. void blend1D(const Blend1DInfo& info, float t);
  45. /** @copydoc Animation::blend2D */
  46. void blend2D(const Blend2DInfo& info, const Vector2& t);
  47. /** @copydoc Animation::crossFade */
  48. void crossFade(const HAnimationClip& clip, float fadeLength);
  49. /** @copydoc Animation::sample */
  50. void sample(const HAnimationClip& clip, float time);
  51. /** @copydoc Animation::stop */
  52. void stop(UINT32 layer);
  53. /** @copydoc Animation::stopAll */
  54. void stopAll();
  55. /** @copydoc Animation::isPlaying */
  56. bool isPlaying() const;
  57. /** @copydoc Animation::getState */
  58. bool getState(const HAnimationClip& clip, AnimationClipState& state);
  59. /** @copydoc Animation::setState */
  60. void setState(const HAnimationClip& clip, AnimationClipState state);
  61. /** @copydoc Animation::setMorphChannelWeight */
  62. void setMorphChannelWeight(UINT32 idx, float weight);
  63. /** Sets bounds that will be used for animation and mesh culling. Only relevant if setUseBounds() is set to true. */
  64. void setBounds(const AABox& bounds);
  65. /** Gets animation bounds. @see setBounds. */
  66. const AABox& getBounds() const { return mBounds; }
  67. /**
  68. * Determines should animation bounds be used for visibility determination (culling). If false the bounds of the
  69. * mesh attached to the relevant CRenderable component will be used instead.
  70. */
  71. void setUseBounds(bool enable);
  72. /** Checks whether animation bounds are enabled. @see setUseBounds. */
  73. bool getUseBounds() const { return mUseBounds; }
  74. /** Enables or disables culling of the animation when out of view. Culled animation will not be evaluated. */
  75. void setEnableCull(bool enable);
  76. /** Checks whether the animation will be evaluated when it is out of view. */
  77. bool getEnableCull() const { return mEnableCull; }
  78. /** Triggered whenever an animation event is reached. */
  79. Event<void(const HAnimationClip&, const String&)> onEventTriggered;
  80. /** @name Internal
  81. * @{
  82. */
  83. /** Returns the Animation implementation wrapped by this component. */
  84. SPtr<Animation> _getInternal() const { return mInternal; }
  85. /**
  86. * Registers a new bone component, creating a new transform mapping from the bone name to the scene object the
  87. * component is attached to.
  88. */
  89. void _addBone(const HBone& bone);
  90. /** Unregisters a bone component, removing the bone -> scene object mapping. */
  91. void _removeBone(const HBone& bone);
  92. /** Called whenever the bone name the Bone component points to changes. */
  93. void _notifyBoneChanged(const HBone& bone);
  94. /**
  95. * Registers a Renderable component with the animation, should be called whenever a Renderable component is added
  96. * to the same scene object as this component.
  97. */
  98. void _registerRenderable(const HRenderable& renderable);
  99. /**
  100. * Removes renderable from the animation component. Should be called when a Renderable component is removed from
  101. * this scene object.
  102. */
  103. void _unregisterRenderable();
  104. /** Re-applies the bounds to the internal animation object, and the relevant renderable object if one exists. */
  105. void _updateBounds(bool updateRenderable = true);
  106. /** @} */
  107. /************************************************************************/
  108. /* COMPONENT OVERRIDES */
  109. /************************************************************************/
  110. protected:
  111. friend class SceneObject;
  112. /** @copydoc Component::onInitialized() */
  113. void onInitialized() override;
  114. /** @copydoc Component::onDestroyed() */
  115. void onDestroyed() override;
  116. /** @copydoc Component::onDisabled() */
  117. void onDisabled() override;
  118. /** @copydoc Component::onEnabled() */
  119. void onEnabled() override;
  120. /** @copydoc Component::onTransformChanged() */
  121. void onTransformChanged(TransformChangedFlags flags) override;
  122. protected:
  123. using Component::destroyInternal;
  124. /** Creates the internal representation of the Animation and restores the values saved by the Component. */
  125. void restoreInternal();
  126. /** Destroys the internal Animation representation. */
  127. void destroyInternal();
  128. /** Callback triggered whenever an animation event is triggered. */
  129. void eventTriggered(const HAnimationClip& clip, const String& name);
  130. /**
  131. * Finds any scene objects that are mapped to bone transforms. Such object's transforms will be affected by
  132. * skeleton bone animation.
  133. */
  134. void setBoneMappings();
  135. /** Searches child scene objects for Bone components and returns any found ones. */
  136. Vector<HBone> findChildBones();
  137. SPtr<Animation> mInternal;
  138. HRenderable mAnimatedRenderable;
  139. HAnimationClip mDefaultClip;
  140. AnimWrapMode mWrapMode;
  141. float mSpeed;
  142. bool mEnableCull;
  143. bool mUseBounds;
  144. AABox mBounds;
  145. Vector<SceneObjectMappingInfo> mMappingInfos;
  146. /************************************************************************/
  147. /* RTTI */
  148. /************************************************************************/
  149. public:
  150. friend class CAnimationRTTI;
  151. static RTTITypeBase* getRTTIStatic();
  152. RTTITypeBase* getRTTI() const override;
  153. protected:
  154. CAnimation(); // Serialization only
  155. };
  156. /** @} */
  157. }