ParametersGroupDefaultValues.cpp 7.1 KB

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