ParametersGroupDefaultValues.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include <AzTest/AzTest.h>
  9. #include <string>
  10. #include <type_traits>
  11. #include <QTreeWidget>
  12. #include <EMotionFX/Source/AnimGraphManager.h>
  13. #include <EMotionFX/Source/EMotionFXManager.h>
  14. #include <EMotionFX/Source/Parameter/RotationParameter.h>
  15. #include <EMotionFX/Source/Parameter/StringParameter.h>
  16. #include <EMotionFX/Source/Parameter/Vector2Parameter.h>
  17. #include <EMotionFX/Source/Parameter/Vector3Parameter.h>
  18. #include <EMotionFX/Source/Parameter/Vector4Parameter.h>
  19. #include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/EMStudioManager.h>
  20. #include <EMotionFX/Tools/EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/AnimGraphPlugin.h>
  21. #include <EMotionFX/Tools/EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/ParameterWindow.h>
  22. #include <MCore/Source/AttributeQuaternion.h>
  23. #include <MCore/Source/AttributeString.h>
  24. #include <MCore/Source/AttributeVector2.h>
  25. #include <MCore/Source/AttributeVector3.h>
  26. #include <MCore/Source/AttributeVector4.h>
  27. #include <Tests/UI/CommandRunnerFixture.h>
  28. #include <Tests/Matchers.h>
  29. #include <Tests/TestAssetCode/SimpleActors.h>
  30. #include <Tests/TestAssetCode/ActorFactory.h>
  31. namespace EMotionFX
  32. {
  33. namespace
  34. {
  35. template <class ValueType>
  36. ValueType GetExpectedValue();
  37. template <>
  38. AZ::Quaternion GetExpectedValue<AZ::Quaternion>()
  39. {
  40. return AZ::Quaternion::CreateRotationX(0.5f);
  41. }
  42. template <>
  43. AZStd::string GetExpectedValue<AZStd::string>()
  44. {
  45. return "New default value for a string";
  46. }
  47. template <>
  48. AZ::Vector4 GetExpectedValue<AZ::Vector4>()
  49. {
  50. return AZ::Vector4(2.0f, 3.0f, 4.0f, 5.0f);
  51. }
  52. template <>
  53. AZ::Vector3 GetExpectedValue<AZ::Vector3>()
  54. {
  55. return AZ::Vector3(2.0f, 3.0f, 4.0f);
  56. }
  57. template <>
  58. AZ::Vector2 GetExpectedValue<AZ::Vector2>()
  59. {
  60. return AZ::Vector2(2.0f, 3.0f);
  61. }
  62. template <class ValueType>
  63. void TestEquality(const ValueType& lhs, const ValueType& rhs)
  64. {
  65. EXPECT_THAT(lhs, IsClose(rhs));
  66. }
  67. template <>
  68. void TestEquality<AZStd::string>(const AZStd::string& lhs, const AZStd::string& rhs)
  69. {
  70. EXPECT_EQ(lhs, rhs);
  71. }
  72. template <class ValueType>
  73. void TestInequality(const ValueType& lhs, const ValueType& rhs)
  74. {
  75. EXPECT_THAT(lhs, ::testing::Not(IsClose(rhs)));
  76. }
  77. template <>
  78. void TestInequality<AZStd::string>(const AZStd::string& lhs, const AZStd::string& rhs)
  79. {
  80. EXPECT_NE(lhs, rhs);
  81. }
  82. } // namespace
  83. template <typename T>
  84. class CanSetParameterToDefaultValueWhenInGroupFixture
  85. : public CommandRunnerFixture
  86. {
  87. public:
  88. using TestParameterT = T;
  89. };
  90. template <class ParameterType, class AttributeType>
  91. class TestParameterT
  92. {
  93. public:
  94. using ParameterT = ParameterType;
  95. using AttributeT = AttributeType;
  96. // Use the return type from the attribute's GetValue method as the type
  97. // that this attribute contains
  98. using ValueT = AZStd::remove_const_t<AZStd::remove_reference_t<decltype(AZStd::declval<AttributeType>().GetValue())>>;
  99. };
  100. using RotationParameterT = TestParameterT<RotationParameter, MCore::AttributeQuaternion>;
  101. using StringParameterT = TestParameterT<StringParameter, MCore::AttributeString>;
  102. using Vector2ParameterT = TestParameterT<Vector2Parameter, MCore::AttributeVector2>;
  103. using Vector3ParameterT = TestParameterT<Vector3Parameter, MCore::AttributeVector3>;
  104. using Vector4ParameterT = TestParameterT<Vector4Parameter, MCore::AttributeVector4>;
  105. using TypesToTest = ::testing::Types<
  106. RotationParameterT,
  107. StringParameterT,
  108. Vector2ParameterT,
  109. Vector3ParameterT,
  110. Vector4ParameterT
  111. >;
  112. TYPED_TEST_CASE(CanSetParameterToDefaultValueWhenInGroupFixture, TypesToTest);
  113. TYPED_TEST(CanSetParameterToDefaultValueWhenInGroupFixture, CanSetParameterToDefaultValueWhenInGroup)
  114. {
  115. using ParameterT = typename TestFixture::TestParameterT::ParameterT;
  116. using AttributeT = typename TestFixture::TestParameterT::AttributeT;
  117. using ValueT = typename TestFixture::TestParameterT::ValueT;
  118. AZStd::unique_ptr<Actor> actor = ActorFactory::CreateAndInit<SimpleJointChainActor>(1);
  119. ActorInstance* actorInstance = ActorInstance::Create(actor.get());
  120. this->ExecuteCommands({
  121. R"(Select -actorInstanceID )" + std::to_string(actorInstance->GetID()),
  122. R"(CreateMotionSet -name CanSetParameterToDefaultValueWhenInGroupMotionSet -motionSetID 200)",
  123. R"(CreateAnimGraph -animGraphID 100)",
  124. R"(AnimGraphAddGroupParameter -animGraphID 100 -name GroupParam)",
  125. R"(AnimGraphCreateParameter -animGraphID 100 -parent GroupParam -name Param -type )" + std::string(azrtti_typeid<ParameterT>().ToFixedString().c_str()),
  126. R"(ActivateAnimGraph -animGraphID 100 -motionSetID 200 -actorInstanceID )" + std::to_string(actorInstance->GetID()),
  127. });
  128. AnimGraph* animGraph = GetEMotionFX().GetAnimGraphManager()->FindAnimGraphByID(100);
  129. const ValueParameter* valueParameter = animGraph->FindValueParameter(0);
  130. const ParameterT* defaultValueParameter = azdynamic_cast<const ParameterT*>(valueParameter);
  131. ASSERT_TRUE(defaultValueParameter) << "Found parameter does not inherit from DefaultValueParameter";
  132. const ValueT expectedValue = GetExpectedValue<ValueT>();
  133. TestInequality(defaultValueParameter->GetDefaultValue(), expectedValue);
  134. AnimGraphInstance* animGraphInstance = animGraph->GetAnimGraphInstance(0);
  135. auto instanceValue = static_cast<AttributeT*>(animGraphInstance->GetParameterValue(static_cast<uint32>(animGraph->FindValueParameterIndex(valueParameter).GetValue())));
  136. ASSERT_EQ(instanceValue->GetType(), AttributeT::TYPE_ID);
  137. // Set the parameter's current value
  138. instanceValue->SetValue(expectedValue);
  139. auto animGraphPlugin = static_cast<EMStudio::AnimGraphPlugin*>(EMStudio::GetPluginManager()->FindActivePlugin(EMStudio::AnimGraphPlugin::CLASS_ID));
  140. ASSERT_TRUE(animGraphPlugin) << "Expected to find the AnimGraph plugin. Is it loaded?";
  141. QTreeWidget* treeWidget = animGraphPlugin->GetParameterWindow()->findChild<QTreeWidget*>("AnimGraphParamWindow");
  142. ASSERT_TRUE(treeWidget) << "Expected to find the QTreeWidget inside the AnimGraph plugin's parameter window";
  143. const QTreeWidgetItem* groupParameterItem = treeWidget->topLevelItem(0);
  144. QTreeWidgetItem* valueParameterItem = groupParameterItem->child(0);
  145. valueParameterItem->setSelected(true);
  146. // Make the current value of the parameter from the current animgraph the parameter's default value
  147. animGraphPlugin->GetParameterWindow()->OnMakeDefaultValue();
  148. TestEquality(defaultValueParameter->GetDefaultValue(), expectedValue);
  149. this->ExecuteCommands({
  150. R"(RemoveAnimGraph -animGraphId )" + std::to_string(animGraph->GetID())
  151. });
  152. actorInstance->Destroy();
  153. }
  154. } // namespace EMotionFX