AnimComponentNode.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #ifndef CRYINCLUDE_CRYMOVIE_ANIMCOMPONENTNODE_H
  9. #define CRYINCLUDE_CRYMOVIE_ANIMCOMPONENTNODE_H
  10. #pragma once
  11. #include "AnimNode.h"
  12. #include "CharacterTrackAnimator.h"
  13. #include <Maestro/Bus/EditorSequenceAgentComponentBus.h>
  14. /**
  15. * CAnimComponentNode
  16. *
  17. * All animation on AZ::Entity nodes are keyed to tracks on CAnimComponentNodes.
  18. *
  19. */
  20. class CAnimComponentNode
  21. : public CAnimNode
  22. , public Maestro::EditorSequenceAgentComponentNotificationBus::Handler
  23. {
  24. public:
  25. AZ_CLASS_ALLOCATOR(CAnimComponentNode, AZ::SystemAllocator);
  26. AZ_RTTI(CAnimComponentNode, "{722F3D0D-7AEB-46B7-BF13-D5C7A828E9BD}", CAnimNode);
  27. CAnimComponentNode(const int id);
  28. CAnimComponentNode();
  29. ~CAnimComponentNode();
  30. AZ::EntityId GetParentAzEntityId() const { return m_pParentNode ? m_pParentNode->GetAzEntityId() : AZ::EntityId(); }
  31. //////////////////////////////////////////////////////////////////////////
  32. // Overrides from CAnimNode
  33. void Animate(SAnimContext& ac) override;
  34. void OnStart() override;
  35. void OnResume() override;
  36. void OnReset() override;
  37. void OnResetHard() override;
  38. //////////////////////////////////////////////////////////////////////////
  39. //////////////////////////////////////////////////////////////////////////
  40. // Overrides from IAnimNode
  41. // ComponentNodes use reflection for typing - return invalid for this pure virtual for the legacy system
  42. CAnimParamType GetParamType(unsigned int nIndex) const override;
  43. void SetComponent(AZ::ComponentId componentId, const AZ::Uuid& typeId) override;
  44. // returns the componentId of the component the node is associate with, if applicable, or a AZ::InvalidComponentId otherwise
  45. AZ::ComponentId GetComponentId() const override { return m_componentId; }
  46. int SetKeysForChangedTrackValues(float time) override;
  47. void OnStartPlayInEditor() override;
  48. void OnStopPlayInEditor() override;
  49. void SetNodeOwner(IAnimNodeOwner* pOwner) override;
  50. void SetPos(float time, const AZ::Vector3& pos) override;
  51. void SetRotate(float time, const AZ::Quaternion& quat) override;
  52. void SetScale(float time, const AZ::Vector3& scale) override;
  53. Vec3 GetPos() override;
  54. Quat GetRotate() override;
  55. Quat GetRotate(float time) override;
  56. Vec3 GetScale() override;
  57. void Activate(bool bActivate) override;
  58. //////////////////////////////////////////////////////////////////////////
  59. //////////////////////////////////////////////////////////////////////////
  60. // Override CreateTrack to handle trackMultipliers for Component Tracks
  61. IAnimTrack* CreateTrack(const CAnimParamType& paramType) override;
  62. bool RemoveTrack(IAnimTrack* pTrack) override;
  63. //////////////////////////////////////////////////////////////////////////
  64. // EditorSequenceAgentComponentNotificationBus::Handler Interface
  65. void OnSequenceAgentConnected() override;
  66. void Serialize(XmlNodeRef& xmlNode, bool bLoading, bool bLoadEmptyTracks) override;
  67. const AZ::Uuid& GetComponentTypeId() const { return m_componentTypeId; }
  68. // Skips Event Bus update of Components during animation - used for when another system is overriding a component's properties,
  69. // such as during camera interpolation between two Transforms. This will silently make the Animate() method do nothing - use only if you
  70. // know what you're doing!
  71. void SetSkipComponentAnimationUpdates(bool skipAnimationUpdates)
  72. {
  73. m_skipComponentAnimationUpdates = skipAnimationUpdates;
  74. }
  75. static void Reflect(AZ::ReflectContext* context);
  76. protected:
  77. // functions involved in the process to parse and store component behavior context animated properties
  78. void UpdateDynamicParamsInternal() override;
  79. void UpdateDynamicParams_Editor();
  80. void UpdateDynamicParams_Game();
  81. void InitializeTrackDefaultValue(IAnimTrack* pTrack, const CAnimParamType& paramType) override;
  82. bool GetParamInfoFromType(const CAnimParamType& paramId, SParamInfo& info) const override;
  83. private:
  84. enum ETransformSpaceConversionDirection
  85. {
  86. eTransformConverstionDirection_toWorldSpace = 0,
  87. eTransformConverstionDirection_toLocalSpace
  88. };
  89. // methods to convert world transforms to local transforms to account for Transform Delegate bug working solely
  90. // in world space
  91. void GetParentWorldTransform(AZ::Transform& retTransform) const;
  92. void ConvertBetweenWorldAndLocalPosition(Vec3& position, ETransformSpaceConversionDirection conversionDirection) const;
  93. void ConvertBetweenWorldAndLocalRotation(Quat& rotation, ETransformSpaceConversionDirection conversionDirection) const;
  94. void ConvertBetweenWorldAndLocalScale(Vec3& scale, ETransformSpaceConversionDirection conversionDirection) const;
  95. AZ::Vector3 TransformFromWorldToLocalPosition(const AZ::Vector3& position) const;
  96. AZ::Quaternion TransformFromWorldToLocalRotation(const AZ::Quaternion& rotation) const;
  97. AZ::Vector3 TransformFromWorldToLocalScale(const AZ::Vector3& scale) const;
  98. // Utility function to query the units for a track and set the track multiplier if needed. Returns true if track multiplier was set.
  99. bool SetTrackMultiplier(IAnimTrack* track) const;
  100. void ForceAnimKeyChangeInCharacterTrackAnimator();
  101. // typed support functions for SetKeysForChangedTrackValues()
  102. int SetKeysForChangedBoolTrackValue(IAnimTrack* track, int keyIdx, float time);
  103. int SetKeysForChangedFloatTrackValue(IAnimTrack* track, int keyIdx, float time);
  104. int SetKeysForChangedVector3TrackValue(IAnimTrack* track, int keyIdx, float time, bool applyTrackMultiplier = true, float isChangedTolerance = AZ::Constants::Tolerance);
  105. int SetKeysForChangedQuaternionTrackValue(IAnimTrack* track, int keyIdx, float time);
  106. // Helper function to set individual properties on Simple Motion Component from an AssetBlend Track.
  107. void AnimateAssetBlendSubProperties(const Maestro::AssetBlends<AZ::Data::AssetData>& assetBlendValue);
  108. class BehaviorPropertyInfo
  109. {
  110. public:
  111. BehaviorPropertyInfo() {}
  112. BehaviorPropertyInfo(const AZStd::string& name)
  113. {
  114. *this = name;
  115. }
  116. BehaviorPropertyInfo(const BehaviorPropertyInfo& other)
  117. {
  118. m_displayName = other.m_displayName;
  119. m_animNodeParamInfo.paramType = other.m_displayName;
  120. m_animNodeParamInfo.name = m_displayName;
  121. }
  122. BehaviorPropertyInfo& operator=(const AZStd::string& str)
  123. {
  124. // TODO: clean this up - this weird memory sharing was copied from legacy Cry - could be better.
  125. m_displayName = str;
  126. m_animNodeParamInfo.paramType = str; // set type to AnimParamType::ByString by assigning a string
  127. m_animNodeParamInfo.name = m_displayName;
  128. return *this;
  129. }
  130. AZStd::string m_displayName;
  131. SParamInfo m_animNodeParamInfo;
  132. };
  133. void AddPropertyToParamInfoMap(const CAnimParamType& paramType);
  134. AZ::Uuid m_componentTypeId;
  135. AZ::ComponentId m_componentId;
  136. // a mapping of CAnimParmTypes to SBehaviorPropertyInfo structs for each virtual property
  137. AZStd::unordered_map<CAnimParamType, BehaviorPropertyInfo> m_paramTypeToBehaviorPropertyInfoMap;
  138. // helper class responsible for animating Character Tracks (aka 'Animation' tracks in the TrackView UI)
  139. CCharacterTrackAnimator* m_characterTrackAnimator = nullptr;
  140. bool m_skipComponentAnimationUpdates;
  141. };
  142. #endif // CRYINCLUDE_CRYMOVIE_ANIMCOMPONENTNODE_H