AnimGraphComponentBus.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/EBus/EBus.h>
  10. #include <AzCore/Component/ComponentBus.h>
  11. #include <AzCore/Math/Vector2.h>
  12. #include <AzCore/Math/Vector3.h>
  13. #include <AzCore/Math/Quaternion.h>
  14. #include <AzCore/Asset/AssetCommon.h>
  15. namespace EMotionFX
  16. {
  17. class AnimGraphInstance;
  18. }
  19. namespace EMotionFX
  20. {
  21. namespace Integration
  22. {
  23. /**
  24. * EmotionFX Anim Graph Component Request Bus
  25. * Used for making requests to the EMotionFX Anim Graph Components.
  26. */
  27. class AnimGraphComponentRequests
  28. : public AZ::ComponentBus
  29. {
  30. public:
  31. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  32. /// Retrieves the component's live graph instance.
  33. /// \return pointer to anim graph instance.
  34. virtual EMotionFX::AnimGraphInstance* GetAnimGraphInstance() { return nullptr; }
  35. /// Retrieve parameter index for a given parameter name.
  36. /// Retrieving the index and using it to set parameter values is more performant than setting by name.
  37. /// \param parameterName - name of parameter for which to retrieve the index.
  38. /// \return parameter index
  39. virtual size_t FindParameterIndex(const char* parameterName) = 0;
  40. /// Retrieve parameter name for a given parameter index.
  41. /// \param parameterName - index of parameter for which to retrieve the name.
  42. /// \return parameter name
  43. virtual const char* FindParameterName(size_t parameterIndex) = 0;
  44. /// Updates a anim graph property given a float value.
  45. /// \param parameterIndex - index of parameter to set
  46. /// \param value - value to set
  47. virtual void SetParameterFloat(size_t parameterIndex, float value) = 0;
  48. /// Updates a anim graph property given a boolean value.
  49. /// \param parameterIndex - index of parameter to set
  50. /// \param value - value to set
  51. virtual void SetParameterBool(size_t parameterIndex, bool value) = 0;
  52. /// Updates a anim graph property given a string value.
  53. /// \param parameterIndex - index of parameter to set
  54. /// \param value - value to set
  55. virtual void SetParameterString(size_t parameterIndex, const char* value) = 0;
  56. /// Updates a anim graph property given a Vector2 value.
  57. /// \param parameterIndex - index of parameter to set
  58. /// \param value - value to set
  59. virtual void SetParameterVector2(size_t parameterIndex, const AZ::Vector2& value) = 0;
  60. /// Updates a anim graph property given a Vector3 value.
  61. /// \param parameterIndex - index of parameter to set
  62. /// \param value - value to set
  63. virtual void SetParameterVector3(size_t parameterIndex, const AZ::Vector3& value) = 0;
  64. /// Updates a anim graph property given euler rotation values.
  65. /// \param parameterIndex - index of parameter to set
  66. /// \param value - value to set
  67. virtual void SetParameterRotationEuler(size_t parameterIndex, const AZ::Vector3& value) = 0;
  68. /// Updates a anim graph property given a quaternion value.
  69. /// \param parameterIndex - index of parameter to set
  70. /// \param value - value to set
  71. virtual void SetParameterRotation(size_t parameterIndex, const AZ::Quaternion& value) = 0;
  72. /// Updates a anim graph property given a float value.
  73. /// \param parameterName - name of parameter to set
  74. /// \param value
  75. virtual void SetNamedParameterFloat(const char* parameterName, float value) = 0;
  76. /// Updates a anim graph property given a boolean value.
  77. /// \param parameterName - name of parameter to set
  78. /// \param value
  79. virtual void SetNamedParameterBool(const char* parameterName, bool value) = 0;
  80. /// Updates a anim graph property given a string value.
  81. /// \param parameterName - name of parameter to set
  82. /// \param value
  83. virtual void SetNamedParameterString(const char* parameterName, const char* value) = 0;
  84. /// Updates a anim graph property given a Vector2 value.
  85. /// \param parameterName - name of parameter to set
  86. /// \param value
  87. virtual void SetNamedParameterVector2(const char* parameterName, const AZ::Vector2& value) = 0;
  88. /// Updates a anim graph property given a Vector3 value.
  89. /// \param parameterName - name of parameter to set
  90. /// \param value
  91. virtual void SetNamedParameterVector3(const char* parameterName, const AZ::Vector3& value) = 0;
  92. /// Updates a anim graph property given euler rotation values.
  93. /// \param parameterName - name of parameter to set
  94. /// \param value
  95. virtual void SetNamedParameterRotationEuler(const char* parameterName, const AZ::Vector3& value) = 0;
  96. /// Updates a anim graph property given a quaternion value.
  97. /// \param parameterName - name of parameter to set
  98. /// \param value
  99. virtual void SetNamedParameterRotation(const char* parameterName, const AZ::Quaternion& value) = 0;
  100. /// Enable or disable debug draw visualization inside the anim graph instance.
  101. virtual void SetVisualizeEnabled(bool enabled) = 0;
  102. /// Retrieves a anim graph property as a float value.
  103. /// \param parameterIndex - index of parameter to set
  104. virtual float GetParameterFloat(size_t parameterIndex) = 0;
  105. /// Retrieves a anim graph property as a boolean value.
  106. /// \param parameterIndex - index of parameter to set
  107. virtual bool GetParameterBool(size_t parameterIndex) = 0;
  108. /// Retrieves a anim graph property given a string value.
  109. /// \param parameterIndex - index of parameter to set
  110. virtual AZStd::string GetParameterString(size_t parameterIndex) = 0;
  111. /// Retrieves a anim graph property as a Vector2 value.
  112. /// \param parameterIndex - index of parameter to set
  113. virtual AZ::Vector2 GetParameterVector2(size_t parameterIndex) = 0;
  114. /// Retrieves a anim graph property as a Vector3 value.
  115. /// \param parameterIndex - index of parameter to set
  116. virtual AZ::Vector3 GetParameterVector3(size_t parameterIndex) = 0;
  117. /// Retrieves a anim graph property given as euler rotation values.
  118. /// \param parameterIndex - index of parameter to set
  119. virtual AZ::Vector3 GetParameterRotationEuler(size_t parameterIndex) = 0;
  120. /// Retrieves a anim graph property as a quaternion value.
  121. /// \param parameterIndex - index of parameter to set
  122. virtual AZ::Quaternion GetParameterRotation(size_t parameterIndex) = 0;
  123. /// Retrieves a anim graph property as a float value.
  124. /// \param parameterName - name of parameter to get
  125. virtual float GetNamedParameterFloat(const char* parameterName) = 0;
  126. /// Retrieves a anim graph property as a boolean value.
  127. /// \param parameterName - name of parameter to get
  128. virtual bool GetNamedParameterBool(const char* parameterName) = 0;
  129. /// Retrieves a anim graph property given a string value.
  130. /// \param parameterName - name of parameter to get
  131. virtual AZStd::string GetNamedParameterString(const char* parameterName) = 0;
  132. /// Retrieves a anim graph property as a Vector2 value.
  133. /// \param parameterName - name of parameter to get
  134. virtual AZ::Vector2 GetNamedParameterVector2(const char* parameterName) = 0;
  135. /// Retrieves a anim graph property as a Vector3 value.
  136. /// \param parameterName - name of parameter to get
  137. virtual AZ::Vector3 GetNamedParameterVector3(const char* parameterName) = 0;
  138. /// Retrieves a anim graph property given as euler rotation values.
  139. /// \param parameterName - name of parameter to get
  140. virtual AZ::Vector3 GetNamedParameterRotationEuler(const char* parameterName) = 0;
  141. /// Retrieves a anim graph property as a quaternion value.
  142. /// \param parameterName - name of parameter to get
  143. virtual AZ::Quaternion GetNamedParameterRotation(const char* parameterName) = 0;
  144. /// Check whether debug visualization is enabled or not.
  145. virtual bool GetVisualizeEnabled() = 0;
  146. /// Making a request to sync the anim graph with another animg graph
  147. /// \param leaderEntityId - the entity id of another anim graph.
  148. virtual void SyncAnimGraph(AZ::EntityId leaderEntityId) = 0;
  149. /// Making a request to desync from the anim graph to its leader graph
  150. /// \param leaderEntityId - the entity id of another anim graph.
  151. virtual void DesyncAnimGraph(AZ::EntityId leaderEntityId) = 0;
  152. /// Set the name of the active motion set.
  153. virtual void SetActiveMotionSet(const char* activeMotionSetName) = 0;
  154. };
  155. using AnimGraphComponentRequestBus = AZ::EBus<AnimGraphComponentRequests>;
  156. /**
  157. * EmotionFX Anim Graph Component Notification Bus
  158. * Used for monitoring events from Anim Graph components.
  159. */
  160. class AnimGraphComponentNotifications
  161. : public AZ::ComponentBus
  162. {
  163. public:
  164. //////////////////////////////////////////////////////////////////////////
  165. /**
  166. * Custom connection policy notifies connecting listeners immediately if anim graph instance is already created.
  167. */
  168. template<class Bus>
  169. struct AssetConnectionPolicy
  170. : public AZ::EBusConnectionPolicy<Bus>
  171. {
  172. static void Connect(typename Bus::BusPtr& busPtr, typename Bus::Context& context, typename Bus::HandlerNode& handler, typename Bus::Context::ConnectLockGuard& connectLock, const typename Bus::BusIdType& id = 0)
  173. {
  174. AZ::EBusConnectionPolicy<Bus>::Connect(busPtr, context, handler, connectLock, id);
  175. EMotionFX::AnimGraphInstance* instance = nullptr;
  176. AnimGraphComponentRequestBus::EventResult(instance, id, &AnimGraphComponentRequestBus::Events::GetAnimGraphInstance);
  177. if (instance)
  178. {
  179. handler->OnAnimGraphInstanceCreated(instance);
  180. }
  181. }
  182. };
  183. template<typename Bus>
  184. using ConnectionPolicy = AssetConnectionPolicy<Bus>;
  185. //////////////////////////////////////////////////////////////////////////
  186. /// Notifies listeners when the component has created a graph instance.
  187. /// \param animGraphInstance - pointer to anim graph instance
  188. virtual void OnAnimGraphInstanceCreated(EMotionFX::AnimGraphInstance* /*animGraphInstance*/) {};
  189. /// Notifies listeners when the component is destroying a graph instance.
  190. /// \param animGraphInstance - pointer to anim graph instance
  191. virtual void OnAnimGraphInstanceDestroyed(EMotionFX::AnimGraphInstance* /*animGraphInstance*/) {};
  192. /// Notifies listeners when a float parameter changes
  193. /// \param animGraphInstance - pointer to anim graph instance
  194. /// \param parameterIndex - index of changed parameter
  195. /// \param beforeValue - value before the change
  196. /// \param afterValue - value after the change
  197. virtual void OnAnimGraphFloatParameterChanged(EMotionFX::AnimGraphInstance* /*animGraphInstance*/, [[maybe_unused]] size_t parameterIndex, [[maybe_unused]] float beforeValue, [[maybe_unused]] float afterValue) {};
  198. /// Notifies listeners when a bool parameter changes
  199. /// \param animGraphInstance - pointer to anim graph instance
  200. /// \param parameterIndex - index of changed parameter
  201. /// \param beforeValue - value before the change
  202. /// \param afterValue - value after the change
  203. virtual void OnAnimGraphBoolParameterChanged(EMotionFX::AnimGraphInstance* /*animGraphInstance*/, [[maybe_unused]] size_t parameterIndex, [[maybe_unused]] bool beforeValue, [[maybe_unused]] bool afterValue) {};
  204. /// Notifies listeners when a string parameter changes
  205. /// \param animGraphInstance - pointer to anim graph instance
  206. /// \param parameterIndex - index of changed parameter
  207. /// \param beforeValue - value before the change
  208. /// \param afterValue - value after the change
  209. virtual void OnAnimGraphStringParameterChanged(EMotionFX::AnimGraphInstance* /*animGraphInstance*/, [[maybe_unused]] size_t parameterIndex, [[maybe_unused]] const char* beforeValue, [[maybe_unused]] const char* afterValue) {};
  210. /// Notifies listeners when a vector2 parameter changes
  211. /// \param animGraphInstance - pointer to anim graph instance
  212. /// \param parameterIndex - index of changed parameter
  213. /// \param beforeValue - value before the change
  214. /// \param afterValue - value after the change
  215. virtual void OnAnimGraphVector2ParameterChanged(EMotionFX::AnimGraphInstance* /*animGraphInstance*/, [[maybe_unused]] size_t parameterIndex, [[maybe_unused]] const AZ::Vector2& beforeValue, [[maybe_unused]] const AZ::Vector2& afterValue) {};
  216. /// Notifies listeners when a vector3 parameter changes
  217. /// \param animGraphInstance - pointer to anim graph instance
  218. /// \param parameterIndex - index of changed parameter
  219. /// \param beforeValue - value before the change
  220. /// \param afterValue - value after the change
  221. virtual void OnAnimGraphVector3ParameterChanged(EMotionFX::AnimGraphInstance* /*animGraphInstance*/, [[maybe_unused]] size_t parameterIndex, [[maybe_unused]] const AZ::Vector3& beforeValue, [[maybe_unused]] const AZ::Vector3& afterValue) {};
  222. /// Notifies listeners when a rotation parameter changes
  223. /// \param animGraphInstance - pointer to anim graph instance
  224. /// \param parameterIndex - index of changed parameter
  225. /// \param beforeValue - value before the change
  226. /// \param afterValue - value after the change
  227. virtual void OnAnimGraphRotationParameterChanged(EMotionFX::AnimGraphInstance* /*animGraphInstance*/, [[maybe_unused]] size_t parameterIndex, [[maybe_unused]] const AZ::Quaternion& beforeValue, [[maybe_unused]] const AZ::Quaternion& afterValue) {};
  228. /// Notifies listeners when an another anim graph trying to sync this graph
  229. /// \param animGraphInstance - pointer to the follower anim graph instance
  230. virtual void OnAnimGraphSynced(EMotionFX::AnimGraphInstance* /*animGraphInstance(Follower)*/) {};
  231. /// Notifies listeners when an another anim graph trying to desync this graph
  232. /// \param animGraphInstance - pointer to the follower anim graph instance
  233. virtual void OnAnimGraphDesynced(EMotionFX::AnimGraphInstance* /*animGraphInstance(Follower)*/) {};
  234. };
  235. using AnimGraphComponentNotificationBus = AZ::EBus<AnimGraphComponentNotifications>;
  236. //Editor
  237. class EditorAnimGraphComponentRequests
  238. : public AZ::ComponentBus
  239. {
  240. public:
  241. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  242. /// Retrieves the component's Animgraph AssetId.
  243. /// \return assetId
  244. virtual const AZ::Data::AssetId& GetAnimGraphAssetId() = 0;
  245. /// Retrieves the component's MotionSet AssetId.
  246. /// \return assetId
  247. virtual const AZ::Data::AssetId& GetMotionSetAssetId() = 0;
  248. };
  249. using EditorAnimGraphComponentRequestBus = AZ::EBus<EditorAnimGraphComponentRequests>;
  250. } // namespace Integration
  251. } // namespace EMotionFX