AzEntityNode.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "AnimNode.h"
  10. //#include "SoundTrack.h"
  11. #include "StlUtils.h"
  12. #include <AzCore/Component/Entity.h>
  13. #include <LyShine/Bus/UiAnimationBus.h>
  14. // remove comment to enable the check.
  15. //#define CHECK_FOR_TOO_MANY_ONPROPERTY_SCRIPT_CALLS
  16. class CUiAnimAzEntityNode
  17. : public CUiAnimNode
  18. , public UiAnimNodeBus::Handler
  19. {
  20. struct SScriptPropertyParamInfo;
  21. struct SAnimState;
  22. public:
  23. AZ_CLASS_ALLOCATOR(CUiAnimAzEntityNode, AZ::SystemAllocator)
  24. AZ_RTTI(CUiAnimAzEntityNode, "{1C6FAEE1-92E4-42ED-8EEB-3483C36A0B77}", CUiAnimNode);
  25. CUiAnimAzEntityNode(const int id);
  26. CUiAnimAzEntityNode();
  27. ~CUiAnimAzEntityNode();
  28. void EnableEntityPhysics(bool bEnable);
  29. EUiAnimNodeType GetType() const override { return eUiAnimNodeType_AzEntity; }
  30. void AddTrack(IUiAnimTrack* track) override;
  31. //////////////////////////////////////////////////////////////////////////
  32. // Overrides from CUiAnimNode
  33. //////////////////////////////////////////////////////////////////////////
  34. // UiAnimNodeInterface
  35. AZ::EntityId GetAzEntityId() override { return m_entityId; };
  36. void SetAzEntity(AZ::Entity* entity) override { m_entityId = entity->GetId(); }
  37. // ~UiAnimNodeInterface
  38. void StillUpdate() override;
  39. void Animate(SUiAnimContext& ec) override;
  40. void CreateDefaultTracks() override;
  41. bool SetParamValueAz(float time, const UiAnimParamData& param, float value) override;
  42. bool SetParamValueAz(float time, const UiAnimParamData& param, bool value) override;
  43. bool SetParamValueAz(float time, const UiAnimParamData& param, int value) override;
  44. bool SetParamValueAz(float time, const UiAnimParamData& param, unsigned int value) override;
  45. bool SetParamValueAz(float time, const UiAnimParamData& param, const AZ::Vector2& value) override;
  46. bool SetParamValueAz(float time, const UiAnimParamData& param, const AZ::Vector3& value) override;
  47. bool SetParamValueAz(float time, const UiAnimParamData& param, const AZ::Vector4& value) override;
  48. bool SetParamValueAz(float time, const UiAnimParamData& param, const AZ::Color& value) override;
  49. bool GetParamValueAz(float time, const UiAnimParamData& param, float& value) override;
  50. void PrecacheStatic(float startTime) override;
  51. void PrecacheDynamic(float time) override;
  52. Vec3 GetPos() { return m_pos; };
  53. Quat GetRotate() { return m_rotate; };
  54. Vec3 GetScale() { return m_scale; };
  55. void Activate(bool bActivate) override;
  56. IUiAnimTrack* GetTrackForAzField(const UiAnimParamData& param) const override;
  57. IUiAnimTrack* CreateTrackForAzField(const UiAnimParamData& param) override;
  58. //////////////////////////////////////////////////////////////////////////
  59. void Serialize(XmlNodeRef& xmlNode, bool bLoading, bool bLoadEmptyTracks) override;
  60. void InitPostLoad(IUiAnimSequence* pSequence, bool remapIds, LyShine::EntityIdMap* entityIdMap) override;
  61. void OnReset() override;
  62. void OnResetHard() override;
  63. void OnStart() override;
  64. void OnPause() override;
  65. void OnStop() override;
  66. //////////////////////////////////////////////////////////////////////////
  67. unsigned int GetParamCount() const override;
  68. CUiAnimParamType GetParamType(unsigned int nIndex) const override;
  69. AZStd::string GetParamName(const CUiAnimParamType& param) const override;
  70. AZStd::string GetParamNameForTrack(const CUiAnimParamType& param, const IUiAnimTrack* track) const override;
  71. static int GetParamCountStatic();
  72. static bool GetParamInfoStatic(int nIndex, SParamInfo& info);
  73. static void Reflect(AZ::SerializeContext* serializeContext);
  74. protected:
  75. bool GetParamInfoFromType(const CUiAnimParamType& paramId, SParamInfo& info) const override;
  76. //! Given the class data definition and a track for a field within it,
  77. //! compute the offset for the field and set it in the track
  78. const AZ::SerializeContext::ClassElement* ComputeOffsetFromElementName(
  79. const AZ::SerializeContext::ClassData* classData,
  80. IUiAnimTrack* track,
  81. size_t baseOffset);
  82. //! This is called on load to compute the offset into the component for each track
  83. void ComputeOffsetsFromElementNames();
  84. void ReleaseSounds();
  85. // functions involved in the process to parse and store lua animated properties
  86. void UpdateDynamicParams() override;
  87. virtual void UpdateDynamicParams_Editor();
  88. virtual void UpdateDynamicParams_PureGame();
  89. enum EUpdateEntityFlags
  90. {
  91. eUpdateEntity_Position = 1 << 0,
  92. eUpdateEntity_Rotation = 1 << 1,
  93. eUpdateEntity_Animation = 1 << 2,
  94. };
  95. protected:
  96. Vec3 m_pos;
  97. Quat m_rotate;
  98. Vec3 m_scale;
  99. private:
  100. IUiAnimTrack* CreateVectorTrack(const UiAnimParamData& param, EUiAnimValue valueType, int numElements);
  101. //! Reference to AZ entity
  102. AZ::EntityId m_entityId;
  103. //! Pointer to target animation node.
  104. AZStd::intrusive_ptr<IUiAnimNode> m_target;
  105. // Cached parameters of node at given time.
  106. float m_time;
  107. Vec3 m_velocity;
  108. Vec3 m_angVelocity;
  109. //! Last animated key in Entity track.
  110. int m_lastEntityKey;
  111. bool m_bWasTransRot;
  112. bool m_visible;
  113. bool m_bInitialPhysicsStatus;
  114. // Pos/rot noise parameters
  115. struct Noise
  116. {
  117. float m_amp;
  118. float m_freq;
  119. Vec3 Get(float time) const;
  120. Noise()
  121. : m_amp(0.0f)
  122. , m_freq(0.0f) {}
  123. };
  124. Noise m_posNoise;
  125. Noise m_rotNoise;
  126. struct SScriptPropertyParamInfo
  127. {
  128. AZStd::string variableName;
  129. AZStd::string displayName;
  130. bool isVectorTable;
  131. SParamInfo animNodeParamInfo;
  132. };
  133. AZStd::vector<SScriptPropertyParamInfo> m_entityScriptPropertiesParamInfos;
  134. using TScriptPropertyParamInfoMap = AZStd::unordered_map<AZStd::string, size_t, stl::hash_string_caseless<AZStd::string>, stl::equality_string_caseless<AZStd::string>>;
  135. TScriptPropertyParamInfoMap m_nameToScriptPropertyParamInfo;
  136. #ifdef CHECK_FOR_TOO_MANY_ONPROPERTY_SCRIPT_CALLS
  137. uint32 m_OnPropertyCalls;
  138. #endif
  139. };