TrackViewAnimNode.h 12 KB

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