AnimGraphUIFixture.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <Tests/UI/UIFixture.h>
  9. #include <Tests/UI/AnimGraphUIFixture.h>
  10. #include <Integration/System/SystemCommon.h>
  11. #include <EMotionFX/Source/AnimGraphManager.h>
  12. #include <EMotionFX/Source/AnimGraphObject.h>
  13. #include <EMotionStudio/EMStudioSDK/Source/EMStudioManager.h>
  14. #include <EMotionStudio/EMStudioSDK/Source/PluginManager.h>
  15. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/BlendGraphViewWidget.h>
  16. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/BlendGraphWidget.h>
  17. #include <EMotionStudio/Plugins/StandardPlugins/Source/AnimGraph/AnimGraphPlugin.h>
  18. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  19. #include <AzQtComponents/Components/StyleManager.h>
  20. #include <AzQtComponents/Utilities/QtPluginPaths.h>
  21. #include <EMotionFX/Source/AnimGraphObject.h>
  22. #include <EMotionFX/Source/AnimGraphReferenceNode.h>
  23. #include <EMotionFX/Source/AnimGraphObjectFactory.h>
  24. #include <EMotionStudio/EMStudioSDK/Source/EMStudioManager.h>
  25. #include <GraphCanvas/Widgets/NodePalette/NodePaletteTreeView.h>
  26. #include <QApplication>
  27. #include <QModelIndex>
  28. #include <QWidget>
  29. namespace EMotionFX
  30. {
  31. void AnimGraphUIFixture::SetUp()
  32. {
  33. UIFixture::SetUp();
  34. m_animGraphPlugin = static_cast<EMStudio::AnimGraphPlugin*>(EMStudio::GetPluginManager()->FindActivePlugin(EMStudio::AnimGraphPlugin::CLASS_ID));
  35. ASSERT_TRUE(m_animGraphPlugin) << "Anim graph plugin not found.";
  36. EXPECT_FALSE(m_animGraphPlugin->GetActiveAnimGraph()) << "No anim graph should be activated.";
  37. EXPECT_EQ(0, EMotionFX::GetAnimGraphManager().GetNumAnimGraphs()) << "Anim graph manager should contain 0 anim graph.";
  38. QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  39. m_blendGraphWidget = m_animGraphPlugin->GetGraphWidget();
  40. ASSERT_TRUE(m_blendGraphWidget) << "BlendGraphWidget not found";
  41. }
  42. EMotionFX::AnimGraph* AnimGraphUIFixture::CreateAnimGraph() const
  43. {
  44. auto addAnimGraphAction = m_animGraphPlugin->GetViewWidget()->findChild<QAction*>("EMFX.BlendGraphViewWidget.NewButton");
  45. if (!addAnimGraphAction)
  46. {
  47. return nullptr;
  48. }
  49. addAnimGraphAction->trigger();
  50. auto animGraph = m_animGraphPlugin->GetActiveAnimGraph();
  51. // The empty graph should contain one node (the root statemachine).
  52. EXPECT_TRUE(animGraph && animGraph->GetNumNodes() == 1) << "An empty anim graph should be activated.";
  53. EXPECT_EQ(1, EMotionFX::GetAnimGraphManager().GetNumAnimGraphs()) << "Anim graph manager should contain 1 anim graph.";
  54. return animGraph;
  55. }
  56. EMStudio::NodeGraph* AnimGraphUIFixture::GetActiveNodeGraph() const
  57. {
  58. return m_blendGraphWidget->GetActiveGraph();
  59. }
  60. EMotionFX::AnimGraphNode* AnimGraphUIFixture::CreateAnimGraphNode(const AZStd::string &type, const AZStd::string &args, const AnimGraph* animGraph) const
  61. {
  62. // Creates an AnimGraphNode by executing command through the CommandManager.
  63. // Parameters:
  64. // AZStd::string type - the id string of the node type to create
  65. // AZStd::string args [optional] - optional string arguments to pass to the command.
  66. // AnimGraph* animGraph [optional] - The AnimGraph to add the new Node to. Defaults to the current Active AnimGraph.
  67. const AnimGraph* targetAnimGraph = (animGraph ? animGraph : m_animGraphPlugin->GetActiveAnimGraph()); //AnimGraph to add Node to
  68. const AZStd::string cmd = "AnimGraphCreateNode AnimGraphID " + AZStd::to_string(targetAnimGraph->GetID()) + " -type " + type + " " + args;
  69. size_t nodeCount = targetAnimGraph->GetNumNodes(); //node count before creating a new node
  70. AZStd::string result;
  71. EXPECT_TRUE(CommandSystem::GetCommandManager()->ExecuteCommand(cmd, result)) << result.c_str();
  72. EXPECT_EQ(targetAnimGraph->GetNumNodes(), nodeCount + 1) << "Expected one more anim graph node after running command: " << cmd.c_str();
  73. return targetAnimGraph->GetNode(targetAnimGraph->GetNumNodes() - 1);
  74. }
  75. AnimGraphNode* AnimGraphUIFixture::AddNodeToAnimGraph(EMotionFX::AnimGraph* animGraph, const QString& nodeTypeName)
  76. {
  77. if (!animGraph)
  78. {
  79. return nullptr;
  80. }
  81. // Launch the node graph context menu.
  82. const AZStd::vector<EMotionFX::AnimGraphNode*>& selectedAnimGraphNodes = GetActiveNodeGraph()->GetSelectedAnimGraphNodes();
  83. m_blendGraphWidget->OnContextMenuEvent(m_blendGraphWidget, QPoint(0, 0), QPoint(0, 0), m_animGraphPlugin, selectedAnimGraphNodes, true, false, m_animGraphPlugin->GetActionFilter());
  84. // Instantiate the node from the tree in context menu
  85. auto* tree = UIFixture::GetFirstChildOfType<GraphCanvas::NodePaletteTreeView>(m_blendGraphWidget);
  86. if (!tree)
  87. {
  88. return nullptr;
  89. }
  90. const QModelIndex idx = UIFixture::GetIndexFromName(tree, nodeTypeName);
  91. if (!idx.isValid())
  92. {
  93. return nullptr;
  94. }
  95. // Selection should spawn the node
  96. tree->setCurrentIndex(idx);
  97. const EMotionFX::AnimGraphNode* currentNode = GetActiveNodeGraph()->GetModelIndex().data(EMStudio::AnimGraphModel::ROLE_NODE_POINTER).value<EMotionFX::AnimGraphNode*>();
  98. const size_t numNodesAfter = currentNode->GetNumChildNodes();
  99. if (numNodesAfter == 0)
  100. {
  101. return nullptr;
  102. }
  103. return currentNode->GetChildNode(0);
  104. }
  105. } // namespace EMotionFX