TrackViewAnimNode.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/Component/EntityBus.h>
  10. #include <AzCore/Component/EntityId.h>
  11. #include <AzCore/Component/TransformBus.h>
  12. #include <AzCore/std/containers/map.h>
  13. #include <AzCore/std/containers/vector.h>
  14. #include <AzFramework/Entity/EntityContextBus.h>
  15. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  16. #include "TrackViewNode.h"
  17. #include "TrackViewTrack.h"
  18. class CTrackViewAnimNode;
  19. class QWidget;
  20. // Represents a bundle of anim nodes
  21. class CTrackViewAnimNodeBundle
  22. {
  23. public:
  24. unsigned int GetCount() const { return static_cast<unsigned int>(m_animNodes.size()); }
  25. CTrackViewAnimNode* GetNode(const unsigned int index) { return m_animNodes[index]; }
  26. const CTrackViewAnimNode* GetNode(const unsigned int index) const { return m_animNodes[index]; }
  27. void Clear();
  28. const bool DoesContain(const CTrackViewNode* pTargetNode);
  29. void AppendAnimNode(CTrackViewAnimNode* pNode);
  30. void AppendAnimNodeBundle(const CTrackViewAnimNodeBundle& bundle);
  31. void ExpandAll(bool bAlsoExpandParentNodes = true);
  32. void CollapseAll();
  33. private:
  34. AZStd::vector<CTrackViewAnimNode*> m_animNodes;
  35. };
  36. // Callback called by animation node when its animated.
  37. class IAnimNodeAnimator
  38. {
  39. public:
  40. virtual ~IAnimNodeAnimator() {}
  41. virtual void Animate(CTrackViewAnimNode* pNode, const SAnimContext& ac) = 0;
  42. virtual void Render([[maybe_unused]] CTrackViewAnimNode* pNode, [[maybe_unused]] const SAnimContext& ac) {}
  43. // Called when binding/unbinding the owning node
  44. virtual void Bind([[maybe_unused]] CTrackViewAnimNode* pNode) {}
  45. virtual void UnBind([[maybe_unused]] CTrackViewAnimNode* pNode) {}
  46. };
  47. //////////////////////////////////////////////////////////////////////////
  48. //
  49. // This class represents a IAnimNode in TrackView and contains
  50. // the editor side code for changing it
  51. //
  52. // It does *not* have ownership of the IAnimNode, therefore deleting it
  53. // will not destroy the CryMovie track
  54. //
  55. //////////////////////////////////////////////////////////////////////////
  56. class CTrackViewAnimNode
  57. : public CTrackViewNode
  58. , public IAnimNodeOwner
  59. , public AzToolsFramework::EditorEntityContextNotificationBus::Handler
  60. , public AZ::EntityBus::Handler
  61. , private AZ::TransformNotificationBus::Handler
  62. , private AzToolsFramework::EntitySelectionEvents::Bus::Handler
  63. {
  64. public:
  65. CTrackViewAnimNode(IAnimSequence* pSequence, IAnimNode* pAnimNode, CTrackViewNode* pParentNode);
  66. ~CTrackViewAnimNode();
  67. // Rendering
  68. virtual void Render(const SAnimContext& ac);
  69. // Playback
  70. virtual void Animate(const SAnimContext& animContext);
  71. // Binding/Unbinding
  72. virtual void BindToEditorObjects();
  73. virtual void UnBindFromEditorObjects();
  74. virtual bool IsBoundToEditorObjects() const;
  75. // Console sync
  76. virtual void SyncToConsole(SAnimContext& animContext);
  77. // CTrackViewAnimNode
  78. ETrackViewNodeType GetNodeType() const override { return eTVNT_AnimNode; }
  79. // Create & remove sub anim nodes
  80. virtual CTrackViewAnimNode* CreateSubNode(
  81. const QString& name, const AnimNodeType animNodeType, AZ::EntityId entityId = AZ::EntityId(),
  82. AZ::Uuid componentTypeId = AZ::Uuid::CreateNull(), AZ::ComponentId componenId=AZ::InvalidComponentId);
  83. virtual void RemoveSubNode(CTrackViewAnimNode* pSubNode);
  84. // Create & remove sub tracks
  85. virtual CTrackViewTrack* CreateTrack(const CAnimParamType& paramType);
  86. virtual void RemoveTrack(CTrackViewTrack* pTrack);
  87. // Updates track default values before adding a new key at time, so that animated entity is not affected by adding a key
  88. void UpdateTrackDefaultValue(float time, IAnimTrack* pTrack);
  89. // Add selected entities from scene to group node
  90. virtual CTrackViewAnimNodeBundle AddSelectedEntities(const AZStd::vector<AnimParamType>& tracks);
  91. // Add current layer to group node
  92. virtual void AddCurrentLayer();
  93. // Director related
  94. virtual void SetAsActiveDirector();
  95. virtual bool IsActiveDirector() const;
  96. // Checks if anim node is part of active sequence and of an active director
  97. virtual bool IsActive();
  98. // Name setter/getter
  99. AZStd::string GetName() const override { return m_animNode->GetName(); }
  100. bool SetName(const char* pName) override;
  101. bool CanBeRenamed() const override;
  102. // Node owner setter/getter
  103. virtual void SetNodeEntityId(AZ::EntityId entityId);
  104. virtual AZ::EntityId GetNodeEntityId(const bool bSearch = true);
  105. AZ::EntityId GetAzEntityId() const { return m_animNode ? m_animNode->GetAzEntityId() : AZ::EntityId(); }
  106. bool IsBoundToAzEntity() const { return m_animNode ? m_animNode->GetAzEntityId().IsValid(): false; }
  107. // Snap time value to prev/next key in sequence
  108. bool SnapTimeToPrevKey(float& time) const override;
  109. bool SnapTimeToNextKey(float& time) const override;
  110. // Expanded state interface
  111. void SetExpanded(bool expanded) override;
  112. bool GetExpanded() const override;
  113. // Node getters
  114. CTrackViewAnimNodeBundle GetAllAnimNodes();
  115. CTrackViewAnimNodeBundle GetSelectedAnimNodes();
  116. CTrackViewAnimNodeBundle GetAllOwnedNodes(AZ::EntityId entityId);
  117. CTrackViewAnimNodeBundle GetAnimNodesByType(AnimNodeType animNodeType);
  118. CTrackViewAnimNodeBundle GetAnimNodesByName(const char* pName);
  119. // Track getters
  120. virtual CTrackViewTrackBundle GetAllTracks();
  121. virtual CTrackViewTrackBundle GetSelectedTracks();
  122. virtual CTrackViewTrackBundle GetTracksByParam(const CAnimParamType& paramType) const;
  123. // Key getters
  124. CTrackViewKeyBundle GetAllKeys() override;
  125. CTrackViewKeyBundle GetSelectedKeys() override;
  126. CTrackViewKeyBundle GetKeysInTimeRange(const float t0, const float t1) override;
  127. // Type getters
  128. AnimNodeType GetType() const;
  129. // Flags
  130. EAnimNodeFlags GetFlags() const;
  131. bool AreFlagsSetOnNodeOrAnyParent(EAnimNodeFlags flagsToCheck) const;
  132. // Disabled state
  133. void SetDisabled(bool bDisabled) override;
  134. bool IsDisabled() const override;
  135. bool CanBeEnabled() const override;
  136. // Return track assigned to the specified parameter.
  137. CTrackViewTrack* GetTrackForParameter(const CAnimParamType& paramType, uint32 index = 0) const;
  138. // Param
  139. unsigned int GetParamCount() const;
  140. CAnimParamType GetParamType(unsigned int index) const;
  141. AZStd::string GetParamName(const CAnimParamType& paramType) const;
  142. bool IsParamValid(const CAnimParamType& param) const;
  143. IAnimNode::ESupportedParamFlags GetParamFlags(const CAnimParamType& paramType) const;
  144. AnimValueType GetParamValueType(const CAnimParamType& paramType) const;
  145. void UpdateDynamicParams();
  146. // Parameter getters/setters
  147. template <class Type>
  148. bool SetParamValue(const float time, const CAnimParamType& param, const Type& value)
  149. {
  150. AZ_Assert(m_animNode, "Expected valid m_animNode");
  151. return m_animNode->SetParamValue(time, param, value);
  152. }
  153. template <class Type>
  154. bool GetParamValue(const float time, const CAnimParamType& param, Type& value)
  155. {
  156. AZ_Assert(m_animNode, "Expected valid m_animNode");
  157. return m_animNode->GetParamValue(time, param, value);
  158. }
  159. // Check if it's a group node
  160. bool IsGroupNode() const override;
  161. // Generate a new node name
  162. virtual QString GetAvailableNodeNameStartingWith(const QString& name) const;
  163. // Copy/Paste nodes
  164. virtual void CopyNodesToClipboard(const bool bOnlySelected, QWidget* context);
  165. virtual bool PasteNodesFromClipboard(QWidget* context);
  166. // Set new parent
  167. virtual void SetNewParent(CTrackViewAnimNode* pNewParent);
  168. // Check if this node may be moved to new parent
  169. virtual bool IsValidReparentingTo(CTrackViewAnimNode* pNewParent);
  170. int GetDefaultKeyTangentFlags() const { return m_animNode ? m_animNode->GetDefaultKeyTangentFlags() : SPLINE_KEY_TANGENT_UNIFIED; }
  171. void SetComponent(AZ::ComponentId componentId, const AZ::Uuid& componentTypeId);
  172. // returns the AZ::ComponentId of the component associated with this node if it is of type AnimNodeType::Component, InvalidComponentId otherwise
  173. AZ::ComponentId GetComponentId() const;
  174. // IAnimNodeOwner
  175. void MarkAsModified() override;
  176. // ~IAnimNodeOwner
  177. // Compares all of the node's track values at the given time with the associated property value and
  178. // sets a key at that time if they are different to match the latter
  179. // Returns the number of keys set
  180. int SetKeysForChangedTrackValues(float time) { return m_animNode->SetKeysForChangedTrackValues(time); }
  181. // returns true if this node is associated with an AnimNodeType::AzEntity node and contains a component with the given id
  182. bool ContainsComponentWithId(AZ::ComponentId componentId) const;
  183. //////////////////////////////////////////////////////////////////////////
  184. // AzToolsFramework::EditorEntityContextNotificationBus implementation
  185. void OnStartPlayInEditor() override;
  186. void OnStopPlayInEditor() override;
  187. //~AzToolsFramework::EditorEntityContextNotificationBus implementation
  188. //////////////////////////////////////////////////////////////////////////
  189. // AZ::EntityBus
  190. void OnEntityActivated(const AZ::EntityId& entityId) override;
  191. void OnEntityDestruction(const AZ::EntityId& entityId) override;
  192. //~AZ::EntityBus
  193. //////////////////////////////////////////////////////////////////////////
  194. //! AZ::TransformNotificationBus::Handler
  195. void OnParentChanged(AZ::EntityId oldParent, AZ::EntityId newParent) override;
  196. void OnParentTransformWillChange(AZ::Transform oldTransform, AZ::Transform newTransform) override;
  197. //////////////////////////////////////////////////////////////////////////
  198. void OnEntityRemoved();
  199. // Creates a sub-node for the given component. Returns a pointer to the created component sub-node
  200. CTrackViewAnimNode* AddComponent(const AZ::Component* component, bool disabled);
  201. // Depth-first search for TrackViewAnimNode associated with the given animNode. Returns the first match found or nullptr if not found
  202. CTrackViewAnimNode* FindNodeByAnimNode(const IAnimNode* animNode);
  203. protected:
  204. IAnimNode* GetAnimNode() { return m_animNode.get(); }
  205. private:
  206. // Copy selected keys to XML representation for clipboard
  207. void CopyKeysToClipboard(XmlNodeRef& xmlNode, const bool bOnlySelectedKeys, const bool bOnlyFromSelectedTracks) override;
  208. void CopyNodesToClipboardRec(CTrackViewAnimNode* pCurrentAnimNode, XmlNodeRef& xmlNode, const bool bOnlySelected);
  209. void PasteTracksFrom(XmlNodeRef& xmlNodeWithTracks);
  210. CTrackViewTrackBundle GetTracks(const bool bOnlySelected, const CAnimParamType& paramType) const;
  211. void PasteNodeFromClipboard(AZStd::map<int, IAnimNode*>& copiedIdToNodeMap, XmlNodeRef xmlNode);
  212. void SetPosRotScaleTracksDefaultValues(bool positionAllowed = true, bool rotationAllowed = true, bool scaleAllowed = true);
  213. bool CheckTrackAnimated(const CAnimParamType& paramType) const;
  214. // IAnimNodeOwner
  215. void OnNodeVisibilityChanged(IAnimNode* pNode, const bool bHidden) override;
  216. void OnNodeReset(IAnimNode* pNode) override;
  217. // ~IAnimNodeOwner
  218. // Helper for Is<Position/Rotation/Scale>Delegated to call internally
  219. bool IsTransformAnimParamTypeDelegated(AnimParamType animParamType) const;
  220. // EntitySelectionEvents
  221. void OnSelected() override;
  222. void OnDeselected() override;
  223. void OnSelectionChanged(bool selected);
  224. void UpdateKeyDataAfterParentChanged(const AZ::Transform& oldParentWorldTM, const AZ::Transform& newParentWorldTM);
  225. // Used to track Editor object listener registration
  226. void RegisterEditorObjectListeners(AZ::EntityId entityId);
  227. void UnRegisterEditorObjectListeners();
  228. // Helper functions
  229. static void RemoveChildNode(CTrackViewAnimNode* child);
  230. static AZ::Transform GetEntityWorldTM(AZ::EntityId entityId);
  231. static void SetParentsInChildren(CTrackViewAnimNode* currentNode);
  232. IAnimSequence* m_animSequence;
  233. AZStd::intrusive_ptr<IAnimNode> m_animNode;
  234. AZ::EntityId m_nodeEntityId;
  235. AZStd::unique_ptr<IAnimNodeAnimator> m_pNodeAnimator;
  236. // used to stash the Editor sequence and node entity Ids when we switch to game mode from the editor
  237. AZ::EntityId m_stashedAnimNodeEditorAzEntityId;
  238. AZ::EntityId m_stashedAnimSequenceEditorAzEntityId;
  239. // Used to track Editor object listener registration
  240. AZ::EntityId m_entityIdListenerRegistered;
  241. // used to return a const reference to a null Uuid
  242. static const AZ::Uuid s_nullUuid;
  243. };