AnimGraphVector2ConditionTests.cpp 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <EMotionFX/CommandSystem/Source/AnimGraphParameterCommands.h>
  10. #include <EMotionFX/CommandSystem/Source/CommandManager.h>
  11. #include <EMotionFX/Source/AnimGraph.h>
  12. #include <EMotionFX/Source/AnimGraphVector2Condition.h>
  13. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  14. #include <EMotionFX/Source/AnimGraphStateTransition.h>
  15. #include <EMotionFX/Source/Parameter/FloatSliderParameter.h>
  16. #include <EMotionFX/Source/Parameter/Vector2Parameter.h>
  17. #include <EMotionFX/Source/Parameter/Parameter.h>
  18. #include <EMotionFX/Source/Parameter/ParameterFactory.h>
  19. #include <Tests/AnimGraphFixture.h>
  20. namespace EMotionFX
  21. {
  22. TEST_F(AnimGraphFixture, AnimGraphVector2Condition_MoveParameterTest)
  23. {
  24. AZStd::string result;
  25. AZStd::string commandString;
  26. CommandSystem::CommandManager commandManager;
  27. AnimGraphStateMachine* node1 = aznew AnimGraphStateMachine();
  28. m_animGraph->GetRootStateMachine()->AddChildNode(node1);
  29. m_animGraph->GetRootStateMachine()->SetEntryState(node1);
  30. AnimGraphStateMachine* node2 = aznew AnimGraphStateMachine();
  31. m_animGraph->GetRootStateMachine()->AddChildNode(node2);
  32. AnimGraphStateTransition* transition = AddTransition(node1, node2, 1.0f);
  33. AnimGraphVector2Condition* condition = aznew AnimGraphVector2Condition();
  34. transition->AddCondition(condition);
  35. m_animGraph->InitAfterLoading();
  36. // Add float slider parameter.
  37. {
  38. AZStd::unique_ptr<EMotionFX::Parameter> newParameter(EMotionFX::ParameterFactory::Create(azrtti_typeid<FloatSliderParameter>()));
  39. newParameter->SetName("Float Slider Parameter");
  40. CommandSystem::ConstructCreateParameterCommand(commandString, m_animGraph.get(), newParameter.get(), InvalidIndex);
  41. EXPECT_TRUE(commandManager.ExecuteCommand(commandString, result)) << result.c_str();
  42. }
  43. // Add Vector2 parameter.
  44. const AZStd::string parameterName = "Vector2 Parameter";
  45. {
  46. AZStd::unique_ptr<EMotionFX::Parameter> newParameter(EMotionFX::ParameterFactory::Create(azrtti_typeid<Vector2Parameter>()));
  47. newParameter->SetName(parameterName);
  48. CommandSystem::ConstructCreateParameterCommand(commandString, m_animGraph.get(), newParameter.get(), InvalidIndex);
  49. EXPECT_TRUE(commandManager.ExecuteCommand(commandString, result)) << result.c_str();
  50. }
  51. condition->SetParameterName(parameterName);
  52. condition->Reinit();
  53. AZ::Outcome<size_t> parameterIndex = m_animGraph->FindValueParameterIndexByName(parameterName);
  54. ASSERT_TRUE(parameterIndex.IsSuccess());
  55. EXPECT_EQ(parameterIndex.GetValue(), 1) << "The Vector2 parameter should be at the 2nd position.";
  56. // 1. Move the Vector2 parameter from the 2nd place to the 1st place.
  57. commandString = AZStd::string::format("AnimGraphMoveParameter -animGraphID %d -name \"%s\" -index %d ",
  58. m_animGraph->GetID(),
  59. parameterName.c_str(),
  60. 0);
  61. EXPECT_TRUE(commandManager.ExecuteCommand(commandString, result)) << result.c_str();
  62. parameterIndex = m_animGraph->FindValueParameterIndexByName(parameterName);
  63. ASSERT_TRUE(parameterIndex.IsSuccess() && parameterIndex.GetValue() == 0) << "The Vector2 parameter should now be at the 1st position.";
  64. EXPECT_EQ(parameterIndex.GetValue(), condition->GetParameterIndex().GetValue()) << "The Vector2 condition should now refer to the 1st parameter in the anim graph.";
  65. // 2. Undo.
  66. EXPECT_TRUE(commandManager.Undo(result)) << result.c_str();
  67. parameterIndex = m_animGraph->FindValueParameterIndexByName(parameterName);
  68. ASSERT_TRUE(parameterIndex.IsSuccess() && parameterIndex.GetValue() == 1) << "The Vector2 parameter should now be back at the 2nd position.";
  69. EXPECT_EQ(parameterIndex.GetValue(), condition->GetParameterIndex().GetValue()) << "The Vector2 condition should now refer to the 2nd parameter in the anim graph.";
  70. // 3. Redo.
  71. EXPECT_TRUE(commandManager.Redo(result)) << result.c_str();
  72. parameterIndex = m_animGraph->FindValueParameterIndexByName(parameterName);
  73. ASSERT_TRUE(parameterIndex.IsSuccess() && parameterIndex.GetValue() == 0) << "The Vector2 parameter should now be back at the 1st position.";
  74. EXPECT_EQ(parameterIndex.GetValue(), condition->GetParameterIndex().GetValue()) << "The Vector2 condition should now refer to the 1st parameter in the anim graph.";
  75. }
  76. } // namespace EMotionFX