3
0

AnimGraphParameterActionTests.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <AzCore/RTTI/TypeInfo.h>
  9. #include <AzTest/AzTest.h>
  10. #include <EMotionFX/CommandSystem/Source/AnimGraphParameterCommands.h>
  11. #include <EMotionFX/CommandSystem/Source/AnimGraphTriggerActionCommands.h>
  12. #include <EMotionFX/CommandSystem/Source/CommandManager.h>
  13. #include <EMotionFX/Source/AnimGraph.h>
  14. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  15. #include <EMotionFX/Source/AnimGraphStateTransition.h>
  16. #include <EMotionFX/Source/AnimGraphParameterAction.h>
  17. #include <EMotionFX/Source/AnimGraphTimeCondition.h>
  18. #include <EMotionFX/Source/EMotionFXManager.h>
  19. #include <EMotionFX/Source/Parameter/FloatSliderParameter.h>
  20. #include <EMotionFX/Source/Parameter/Parameter.h>
  21. #include <EMotionFX/Source/Parameter/ParameterFactory.h>
  22. #include <Tests/AnimGraphFixture.h>
  23. namespace EMotionFX
  24. {
  25. class AnimGraphParameterActionTests : public AnimGraphFixture
  26. {
  27. public:
  28. void ConstructGraph() override
  29. {
  30. AnimGraphFixture::ConstructGraph();
  31. // 1. Add two state machines.
  32. m_node1 = aznew AnimGraphStateMachine();
  33. m_animGraph->GetRootStateMachine()->AddChildNode(m_node1);
  34. m_animGraph->GetRootStateMachine()->SetEntryState(m_node1);
  35. m_node2 = aznew AnimGraphStateMachine();
  36. m_animGraph->GetRootStateMachine()->AddChildNode(m_node2);
  37. // 2. Add a transition and add a time condition.
  38. AnimGraphStateTransition* transition = AddTransition(m_node1, m_node2, 1.0f);
  39. AnimGraphTimeCondition* condition = aznew AnimGraphTimeCondition();
  40. condition->SetCountDownTime(0.1f);
  41. transition->AddCondition(condition);
  42. // 3. Add a parameter action.
  43. m_parameterAction = aznew AnimGraphParameterAction();
  44. TriggerActionSetup& actionSetup = transition->GetTriggerActionSetup();
  45. actionSetup.AddAction(m_parameterAction);
  46. }
  47. AnimGraphNode* m_node1 = nullptr;
  48. AnimGraphNode* m_node2 = nullptr;
  49. AnimGraphParameterAction* m_parameterAction = nullptr;
  50. };
  51. TEST_F(AnimGraphParameterActionTests, AnimGraphParameterActionTests_FloatParameter)
  52. {
  53. FloatParameter* parameter = aznew FloatSliderParameter();
  54. parameter->SetName("testFloat");
  55. parameter->SetDefaultValue(0.0f);
  56. m_animGraph->AddParameter(parameter);
  57. m_animGraphInstance->AddMissingParameterValues();
  58. const float triggerValue = 100.0f;
  59. m_parameterAction->SetParameterName("testFloat");
  60. m_parameterAction->SetTriggerValue(triggerValue);
  61. float outValue;
  62. EXPECT_TRUE(m_animGraphInstance->GetParameterValueAsFloat("testFloat", &outValue));
  63. EXPECT_NE(outValue, triggerValue);
  64. GetEMotionFX().Update(0.5f);
  65. EXPECT_TRUE(m_animGraphInstance->GetParameterValueAsFloat("testFloat", &outValue));
  66. EXPECT_EQ(outValue, triggerValue);
  67. GetEMotionFX().Update(1.0f);
  68. EXPECT_TRUE(m_animGraphInstance->GetParameterValueAsFloat("testFloat", &outValue));
  69. EXPECT_EQ(outValue, triggerValue)
  70. << "Expect the value get changed after the parameter action triggered.";
  71. }
  72. TEST_F(AnimGraphFixture, AnimGraphParameterAction_MoveParameterTest)
  73. {
  74. AZStd::string result;
  75. AZStd::string commandString;
  76. CommandSystem::CommandManager commandManager;
  77. AnimGraphStateMachine* node1 = aznew AnimGraphStateMachine();
  78. m_animGraph->GetRootStateMachine()->AddChildNode(node1);
  79. m_animGraph->GetRootStateMachine()->SetEntryState(node1);
  80. AnimGraphStateMachine* node2 = aznew AnimGraphStateMachine();
  81. m_animGraph->GetRootStateMachine()->AddChildNode(node2);
  82. AnimGraphStateTransition* transition = AddTransition(node1, node2, 1.0f);
  83. m_animGraph->InitAfterLoading();
  84. CommandSystem::AddTransitionAction(transition, azrtti_typeid<AnimGraphParameterAction>());
  85. TriggerActionSetup& actionSetup = transition->GetTriggerActionSetup();
  86. ASSERT_EQ(actionSetup.GetNumActions(), 1) << "Something went wrong adding the parameter action to the transition.";
  87. AnimGraphParameterAction* action = azdynamic_cast<AnimGraphParameterAction*>(actionSetup.GetAction(0));
  88. // Add first parameter.
  89. {
  90. AZStd::unique_ptr<EMotionFX::Parameter> newParameter(EMotionFX::ParameterFactory::Create(azrtti_typeid<FloatSliderParameter>()));
  91. newParameter->SetName("Parameter1");
  92. CommandSystem::ConstructCreateParameterCommand(commandString, m_animGraph.get(), newParameter.get(), InvalidIndex);
  93. EXPECT_TRUE(commandManager.ExecuteCommand(commandString, result)) << result.c_str();
  94. }
  95. // Add second parameter.
  96. const AZStd::string parameterName = "Parameter2";
  97. {
  98. AZStd::unique_ptr<EMotionFX::Parameter> newParameter(EMotionFX::ParameterFactory::Create(azrtti_typeid<FloatSliderParameter>()));
  99. newParameter->SetName(parameterName);
  100. CommandSystem::ConstructCreateParameterCommand(commandString, m_animGraph.get(), newParameter.get(), InvalidIndex);
  101. EXPECT_TRUE(commandManager.ExecuteCommand(commandString, result)) << result.c_str();
  102. }
  103. action->SetParameterName(parameterName);
  104. action->Reinit();
  105. AZ::Outcome<size_t> parameterIndex = m_animGraph->FindValueParameterIndexByName(parameterName);
  106. ASSERT_TRUE(parameterIndex.IsSuccess());
  107. EXPECT_EQ(parameterIndex.GetValue(), 1) << "Parameter2 should be at the 2nd position.";
  108. // 1. Move Parameter2 from the 2nd place to the 1st place.
  109. commandString = AZStd::string::format("AnimGraphMoveParameter -animGraphID %d -name \"%s\" -index %d ",
  110. m_animGraph->GetID(),
  111. parameterName.c_str(),
  112. 0);
  113. EXPECT_TRUE(commandManager.ExecuteCommand(commandString, result)) << result.c_str();
  114. parameterIndex = m_animGraph->FindValueParameterIndexByName(parameterName);
  115. ASSERT_TRUE(parameterIndex.IsSuccess() && parameterIndex.GetValue() == 0) << "Parameter2 should now be at the 1st position.";
  116. EXPECT_EQ(parameterIndex.GetValue(), action->GetParameterIndex().GetValue()) << "The action should now refer to the 1st parameter in the anim graph.";
  117. // 2. Undo.
  118. EXPECT_TRUE(commandManager.Undo(result)) << result.c_str();
  119. parameterIndex = m_animGraph->FindValueParameterIndexByName(parameterName);
  120. ASSERT_TRUE(parameterIndex.IsSuccess() && parameterIndex.GetValue() == 1) << "Parameter2 should now be back at the 2nd position.";
  121. EXPECT_EQ(parameterIndex.GetValue(), action->GetParameterIndex().GetValue()) << "The action should now refer to the 2nd parameter in the anim graph.";
  122. // 3. Redo.
  123. EXPECT_TRUE(commandManager.Redo(result)) << result.c_str();
  124. parameterIndex = m_animGraph->FindValueParameterIndexByName(parameterName);
  125. ASSERT_TRUE(parameterIndex.IsSuccess() && parameterIndex.GetValue() == 0) << "Parameter2 should now be back at the 1st position.";
  126. EXPECT_EQ(parameterIndex.GetValue(), action->GetParameterIndex().GetValue()) << "The action should now refer to the 1st parameter in the anim graph.";
  127. }
  128. }