3
0

SceneContextMenuActions.cpp 3.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. // AZ
  9. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  10. // GraphModel
  11. #include <GraphModel/GraphModelBus.h>
  12. // Landscape Canvas
  13. #include <Editor/Core/Core.h>
  14. #include <Editor/Nodes/BaseNode.h>
  15. #include <Editor/Menus/SceneContextMenuActions.h>
  16. namespace LandscapeCanvasEditor
  17. {
  18. FindSelectedNodesAction::FindSelectedNodesAction(QObject* parent)
  19. : GraphCanvas::ContextMenuAction("", parent)
  20. {
  21. UpdateActionState();
  22. QString tooltip = QObject::tr("Select the corresponding node(s) in the graph based on the Vegetation Entities that are selected in the Editor");
  23. setToolTip(tooltip);
  24. setStatusTip(tooltip);
  25. }
  26. GraphCanvas::ActionGroupId FindSelectedNodesAction::GetActionGroupId() const
  27. {
  28. return AZ_CRC("SceneActionGroup", 0x284f71aa);
  29. }
  30. void FindSelectedNodesAction::RefreshAction(const GraphCanvas::GraphId& graphId, const AZ::EntityId& targetId)
  31. {
  32. AZ_UNUSED(graphId);
  33. AZ_UNUSED(targetId);
  34. UpdateActionState();
  35. }
  36. GraphCanvas::ContextMenuAction::SceneReaction FindSelectedNodesAction::TriggerAction(const GraphCanvas::GraphId& graphId, [[maybe_unused]] const AZ::Vector2& scenePos)
  37. {
  38. // Find the selected Entities in the Editor
  39. AzToolsFramework::EntityIdList selectedEntities;
  40. AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(selectedEntities, &AzToolsFramework::ToolsApplicationRequests::GetSelectedEntities);
  41. // Retrieve all the nodes in our scene
  42. GraphModel::NodePtrList nodeList;
  43. GraphModelIntegration::GraphControllerRequestBus::EventResult(nodeList, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodes);
  44. // Find the nodes in our scene that correspond to the Entities
  45. GraphModel::NodePtrList nodesToSelect;
  46. for (const auto& node : nodeList)
  47. {
  48. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  49. auto it = AZStd::find(selectedEntities.begin(), selectedEntities.end(), baseNodePtr->GetVegetationEntityId());
  50. if (it != selectedEntities.end())
  51. {
  52. nodesToSelect.push_back(node);
  53. }
  54. }
  55. if (nodesToSelect.empty())
  56. {
  57. QString warningMessage = selectedEntities.size() > 1 ? QObject::tr("The selected Entities are not present in the graph") : QObject::tr("The selected Entity is not present in the graph");
  58. AZ_Warning("LandscapeCanvas", false, warningMessage.toUtf8().constData());
  59. }
  60. else
  61. {
  62. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::ClearSelection);
  63. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::SetSelected, nodesToSelect, true);
  64. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::CenterOnNodes, nodesToSelect);
  65. }
  66. return GraphCanvas::ContextMenuAction::SceneReaction::Nothing;
  67. }
  68. void FindSelectedNodesAction::UpdateActionState()
  69. {
  70. AzToolsFramework::EntityIdList selectedEntities;
  71. AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(selectedEntities, &AzToolsFramework::ToolsApplicationRequests::GetSelectedEntities);
  72. setText(selectedEntities.size() > 1 ? QObject::tr("Find Selected Entities in Graph") : QObject::tr("Find Selected Entity in Graph"));
  73. setEnabled(!selectedEntities.empty());
  74. }
  75. }