BsCAnimation.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "Animation/BsAnimation.h"
  6. #include "Scene/BsComponent.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Components
  10. * @{
  11. */
  12. /**
  13. * @copydoc Animation
  14. *
  15. * @note Wraps Animation as a Component.
  16. */
  17. class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Animation,n:Animation) 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. * Determines 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. BS_SCRIPT_EXPORT(n:DefaultClip,pr:setter)
  35. void setDefaultClip(const HAnimationClip& clip);
  36. /** @copydoc setDefaultClip */
  37. BS_SCRIPT_EXPORT(n:DefaultClip,pr:getter)
  38. HAnimationClip getDefaultClip() const { return mDefaultClip; }
  39. /** @copydoc Animation::setWrapMode */
  40. BS_SCRIPT_EXPORT(n:WrapMode,pr:setter)
  41. void setWrapMode(AnimWrapMode wrapMode);
  42. /** @copydoc setWrapMode */
  43. BS_SCRIPT_EXPORT(n:WrapMode,pr:getter)
  44. AnimWrapMode getWrapMode() const { return mWrapMode; }
  45. /** @copydoc Animation::setSpeed */
  46. BS_SCRIPT_EXPORT(n:Speed,pr:setter)
  47. void setSpeed(float speed);
  48. /** @copydoc setSpeed */
  49. BS_SCRIPT_EXPORT(n:Speed,pr:getter)
  50. float getSpeed() const { return mSpeed; }
  51. /** @copydoc Animation::play */
  52. BS_SCRIPT_EXPORT(n:Play)
  53. void play(const HAnimationClip& clip);
  54. /** @copydoc Animation::blendAdditive */
  55. BS_SCRIPT_EXPORT(n:BlendAdditive)
  56. void blendAdditive(const HAnimationClip& clip, float weight, float fadeLength = 0.0f, UINT32 layer = 0);
  57. /** @copydoc Animation::blend1D */
  58. BS_SCRIPT_EXPORT(n:Blend1D)
  59. void blend1D(const Blend1DInfo& info, float t);
  60. /** @copydoc Animation::blend2D */
  61. BS_SCRIPT_EXPORT(n:Blend2D)
  62. void blend2D(const Blend2DInfo& info, const Vector2& t);
  63. /** @copydoc Animation::crossFade */
  64. BS_SCRIPT_EXPORT(n:CrossFade)
  65. void crossFade(const HAnimationClip& clip, float fadeLength);
  66. /** @copydoc Animation::sample */
  67. BS_SCRIPT_EXPORT(n:Sample)
  68. void sample(const HAnimationClip& clip, float time);
  69. /** @copydoc Animation::stop */
  70. BS_SCRIPT_EXPORT(n:Stop)
  71. void stop(UINT32 layer);
  72. /** @copydoc Animation::stopAll */
  73. BS_SCRIPT_EXPORT(n:StopAll)
  74. void stopAll();
  75. /** @copydoc Animation::isPlaying */
  76. BS_SCRIPT_EXPORT(n:IsPlaying,pr:getter)
  77. bool isPlaying() const;
  78. /** @copydoc Animation::getState */
  79. BS_SCRIPT_EXPORT(n:GetState)
  80. bool getState(const HAnimationClip& clip, AnimationClipState& state);
  81. /** @copydoc Animation::setState */
  82. BS_SCRIPT_EXPORT(n:SetState)
  83. void setState(const HAnimationClip& clip, AnimationClipState state);
  84. /**
  85. * Changes a weight of a single morph channel, determining how much of it to apply on top of the base mesh.
  86. *
  87. * @param name Name of the morph channel to modify. This depends on the mesh the animation is currently
  88. * animating.
  89. * @param weight Weight that determines how much of the channel to apply to the mesh, in range [0, 1].
  90. */
  91. BS_SCRIPT_EXPORT(n:SetMorphChannelWeight)
  92. void setMorphChannelWeight(const String& name, float weight);
  93. /** Determines bounds that will be used for animation and mesh culling. Only relevant if setUseBounds() is set to true. */
  94. BS_SCRIPT_EXPORT(n:Bounds,pr:setter)
  95. void setBounds(const AABox& bounds);
  96. /** @copydoc setBounds */
  97. BS_SCRIPT_EXPORT(n:Bounds,pr:getter)
  98. const AABox& getBounds() const { return mBounds; }
  99. /**
  100. * Determines should animation bounds be used for visibility determination (culling). If false the bounds of the
  101. * mesh attached to the relevant CRenderable component will be used instead.
  102. */
  103. BS_SCRIPT_EXPORT(n:UseBounds,pr:setter)
  104. void setUseBounds(bool enable);
  105. /** @copydoc setUseBounds */
  106. BS_SCRIPT_EXPORT(n:UseBounds,pr:getter)
  107. bool getUseBounds() const { return mUseBounds; }
  108. /** Enables or disables culling of the animation when out of view. Culled animation will not be evaluated. */
  109. BS_SCRIPT_EXPORT(n:Cull,pr:setter)
  110. void setEnableCull(bool enable);
  111. /** Checks whether the animation will be evaluated when it is out of view. */
  112. BS_SCRIPT_EXPORT(n:Cull,pr:getter)
  113. bool getEnableCull() const { return mEnableCull; }
  114. /** @copydoc Animation::getNumClips */
  115. BS_SCRIPT_EXPORT(in:true)
  116. UINT32 getNumClips() const;
  117. /** @copydoc Animation::getClip */
  118. BS_SCRIPT_EXPORT(in:true)
  119. HAnimationClip getClip(UINT32 idx) const;
  120. /** Triggered whenever an animation event is reached. */
  121. Event<void(const HAnimationClip&, const String&)> onEventTriggered;
  122. /** @name Internal
  123. * @{
  124. */
  125. /** Returns the Animation implementation wrapped by this component. */
  126. SPtr<Animation> _getInternal() const { return mInternal; }
  127. /**
  128. * Registers a new bone component, creating a new transform mapping from the bone name to the scene object the
  129. * component is attached to.
  130. */
  131. void _addBone(const HBone& bone);
  132. /** Unregisters a bone component, removing the bone -> scene object mapping. */
  133. void _removeBone(const HBone& bone);
  134. /** Called whenever the bone name the Bone component points to changes. */
  135. void _notifyBoneChanged(const HBone& bone);
  136. /**
  137. * Registers a Renderable component with the animation, should be called whenever a Renderable component is added
  138. * to the same scene object as this component.
  139. */
  140. void _registerRenderable(const HRenderable& renderable);
  141. /**
  142. * Removes renderable from the animation component. Should be called when a Renderable component is removed from
  143. * this scene object.
  144. */
  145. void _unregisterRenderable();
  146. /** Re-applies the bounds to the internal animation object, and the relevant renderable object if one exists. */
  147. void _updateBounds(bool updateRenderable = true);
  148. /**
  149. * Rebuilds internal curve -> property mapping about the currently playing animation clip. This mapping allows the
  150. * animation component to know which property to assign which values from an animation curve. This should be called
  151. * whenever playback for a new clip starts, or when clip curves change.
  152. */
  153. BS_SCRIPT_EXPORT(in:true)
  154. void _refreshClipMappings();
  155. /** @copydoc Animation::getGenericCurveValue */
  156. BS_SCRIPT_EXPORT(in:true)
  157. bool _getGenericCurveValue(UINT32 curveIdx, float& value);
  158. /**
  159. * Preview mode allows certain operations on the component to be allowed (like basic animation playback),
  160. * even when the component is not actively running. This is intended for use primarily by the animation editor.
  161. * Preview mode ends automatically when the component is enabled (i.e. starts running normally), or when
  162. * explicitly disabled. Returns true if the preview mode was enabled (which could fail if the component is
  163. * currently running).
  164. */
  165. BS_SCRIPT_EXPORT(in:true)
  166. bool _togglePreviewMode(bool enabled);
  167. /** Triggered when the list of properties animated via generic animation curves needs to be recreated (script only). */
  168. BS_SCRIPT_EXPORT(n:RebuildFloatProperties)
  169. std::function<void(const HAnimationClip&)> _scriptRebuildFloatProperties;
  170. /** Triggered when generic animation curves values need be applied to the properties they effect (script only). */
  171. BS_SCRIPT_EXPORT(n:_UpdateFloatProperties)
  172. std::function<void()> _scriptUpdateFloatProperties;
  173. /** Triggers a callback in script code when animation event is triggered (script only). */
  174. BS_SCRIPT_EXPORT(n:EventTriggered)
  175. std::function<void(const HAnimationClip&, const String&)> _scriptOnEventTriggered;
  176. /** @} */
  177. /************************************************************************/
  178. /* COMPONENT OVERRIDES */
  179. /************************************************************************/
  180. protected:
  181. friend class SceneObject;
  182. /** @copydoc Component::onInitialized() */
  183. void onInitialized() override;
  184. /** @copydoc Component::onDestroyed() */
  185. void onDestroyed() override;
  186. /** @copydoc Component::update() */
  187. void update() override;
  188. /** @copydoc Component::onDisabled() */
  189. void onDisabled() override;
  190. /** @copydoc Component::onEnabled() */
  191. void onEnabled() override;
  192. /** @copydoc Component::onTransformChanged() */
  193. void onTransformChanged(TransformChangedFlags flags) override;
  194. protected:
  195. using Component::destroyInternal;
  196. /** Creates the internal representation of the Animation and restores the values saved by the Component. */
  197. void restoreInternal(bool previewMode);
  198. /** Destroys the internal Animation representation. */
  199. void destroyInternal();
  200. /** Callback triggered whenever an animation event is triggered. */
  201. void eventTriggered(const HAnimationClip& clip, const String& name);
  202. /**
  203. * Finds any scene objects that are mapped to bone transforms. Such object's transforms will be affected by
  204. * skeleton bone animation.
  205. */
  206. void setBoneMappings();
  207. /**
  208. * Finds any curves that affect a transform of a specific scene object, and ensures that animation properly updates
  209. * those transforms. This does not include curves referencing bones.
  210. */
  211. void updateSceneObjectMapping();
  212. /** @copydoc Animation::mapCurveToSceneObject */
  213. void mapCurveToSceneObject(const String& curve, const HSceneObject& so);
  214. /** @copydoc Animation::unmapSceneObject */
  215. void unmapSceneObject(const HSceneObject& so);
  216. /** Searches child scene objects for Bone components and returns any found ones. */
  217. Vector<HBone> findChildBones();
  218. SPtr<Animation> mInternal;
  219. HRenderable mAnimatedRenderable;
  220. HAnimationClip mDefaultClip;
  221. HAnimationClip mPrimaryPlayingClip;
  222. AnimWrapMode mWrapMode;
  223. float mSpeed;
  224. bool mEnableCull;
  225. bool mUseBounds;
  226. bool mPreviewMode;
  227. AABox mBounds;
  228. Vector<SceneObjectMappingInfo> mMappingInfos;
  229. /************************************************************************/
  230. /* RTTI */
  231. /************************************************************************/
  232. public:
  233. friend class CAnimationRTTI;
  234. static RTTITypeBase* getRTTIStatic();
  235. RTTITypeBase* getRTTI() const override;
  236. protected:
  237. CAnimation(); // Serialization only
  238. };
  239. /** @} */
  240. }