SimpleMotionComponent.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/Component.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/Component/TickBus.h>
  12. #include <AzCore/Asset/AssetCommon.h>
  13. #include <AzCore/Script/ScriptProperty.h>
  14. #include <AzCore/std/smart_ptr/unique_ptr.h>
  15. #include <Integration/Assets/MotionAsset.h>
  16. #include <Integration/ActorComponentBus.h>
  17. #include <Integration/SimpleMotionComponentBus.h>
  18. namespace EMotionFX
  19. {
  20. namespace Integration
  21. {
  22. class SimpleMotionComponent
  23. : public AZ::Component
  24. , private AZ::Data::AssetBus::MultiHandler
  25. , private ActorComponentNotificationBus::Handler
  26. , private SimpleMotionComponentRequestBus::Handler
  27. {
  28. public:
  29. friend class EditorSimpleMotionComponent;
  30. AZ_COMPONENT(SimpleMotionComponent, "{DBE3C105-6FC1-418F-A8B1-D0F29FE8D5BD}");
  31. /**
  32. * Configuration struct for procedural configuration of SimpleMotionComponents.
  33. */
  34. struct Configuration
  35. {
  36. AZ_TYPE_INFO(Configuration, "{DA661C5F-E79E-41C3-B055-5F5A4E353F84}")
  37. Configuration();
  38. AZ::Data::Asset<MotionAsset> m_motionAsset; ///< Assigned motion asset
  39. bool m_loop; ///< Toggles looping of the motion
  40. bool m_retarget; ///< Toggles retargeting of the motion
  41. bool m_reverse; ///< Toggles reversing of the motion
  42. bool m_mirror; ///< Toggles mirroring of the motion
  43. float m_playspeed; ///< Determines the rate at which the motion is played
  44. float m_blendInTime; ///< Determines the blend in time in seconds.
  45. float m_blendOutTime; ///< Determines the blend out time in seconds.
  46. bool m_playOnActivation; ///< Determines if the motion should be played immediately
  47. bool m_inPlace; ///< Determines if the motion should be played in-place.
  48. bool m_freezeAtLastFrame = true;///< Determines if the motion will go to bind pose after finishing or freeze at the last frame.
  49. static void Reflect(AZ::ReflectContext* context);
  50. AZ::Crc32 GetBlendOutTimeVisibility() const;
  51. AZ::Crc32 GetFreezeAtLastFrameVisibility() const;
  52. };
  53. SimpleMotionComponent(const Configuration* config = nullptr);
  54. ~SimpleMotionComponent();
  55. // AZ::Component interface implementation
  56. void Init() override;
  57. void Activate() override;
  58. void Deactivate() override;
  59. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  60. {
  61. provided.push_back(AZ_CRC("EMotionFXSimpleMotionService", 0xea7a05d8));
  62. }
  63. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  64. {
  65. dependent.push_back(AZ_CRC("MeshService", 0x71d8a455));
  66. }
  67. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  68. {
  69. required.push_back(AZ_CRC("EMotionFXActorService", 0xd6e8f48d));
  70. }
  71. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  72. {
  73. incompatible.push_back(AZ_CRC("EMotionFXAnimGraphService", 0x9ec3c819));
  74. incompatible.push_back(AZ_CRC("EMotionFXSimpleMotionService", 0xea7a05d8));
  75. incompatible.push_back(AZ_CRC_CE("NonUniformScaleService"));
  76. }
  77. static void Reflect(AZ::ReflectContext* /*context*/);
  78. // SimpleMotionComponentRequestBus::Handler
  79. void LoopMotion(bool enable) override;
  80. bool GetLoopMotion() const override;
  81. void RetargetMotion(bool enable) override;
  82. void ReverseMotion(bool enable) override;
  83. void MirrorMotion(bool enable) override;
  84. void SetPlaySpeed(float speed) override;
  85. float GetPlaySpeed() const override;
  86. void PlayTime(float time) override;
  87. float GetPlayTime() const override;
  88. float GetDuration() const override;
  89. void Motion(AZ::Data::AssetId assetId) override;
  90. AZ::Data::AssetId GetMotion() const override;
  91. void BlendInTime(float time) override;
  92. float GetBlendInTime() const override;
  93. void BlendOutTime(float time) override;
  94. float GetBlendOutTime() const override;
  95. void PlayMotion() override;
  96. const EMotionFX::MotionInstance* GetMotionInstance();
  97. // AZ::Data::AssetBus::Handler
  98. void SetMotionAssetId(const AZ::Data::AssetId& assetId);
  99. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  100. void OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  101. private:
  102. // ActorComponentNotificationBus::Handler
  103. void OnActorInstanceCreated(EMotionFX::ActorInstance* actorInstance) override;
  104. void OnActorInstanceDestroyed(EMotionFX::ActorInstance* actorInstance) override;
  105. void RemoveMotionInstanceFromActor(EMotionFX::MotionInstance* motionInstance);
  106. static EMotionFX::MotionInstance* PlayMotionInternal(const EMotionFX::ActorInstance* actorInstance, const SimpleMotionComponent::Configuration& cfg, bool deleteOnZeroWeight);
  107. Configuration m_configuration; ///< Component configuration.
  108. EMotionFXPtr<EMotionFX::ActorInstance> m_actorInstance; ///< Associated actor instance (retrieved from Actor Component).
  109. EMotionFX::MotionInstance* m_motionInstance; ///< Motion to play on the actor
  110. AZ::Data::Asset<MotionAsset> m_lastMotionAsset; ///< Last active motion asset, kept alive for blending.
  111. EMotionFX::MotionInstance* m_lastMotionInstance; ///< Last active motion instance, kept alive for blending.
  112. };
  113. } // namespace Integration
  114. } // namespace EMotionFX