EditorMainWindow.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <AzCore/std/smart_ptr/make_shared.h>
  10. // GraphCanvas
  11. #include <GraphCanvas/Widgets/GraphCanvasEditor/GraphCanvasEditorDockWidget.h>
  12. // GraphModel
  13. #include <GraphModel/Integration/EditorMainWindow.h>
  14. #include <GraphModel/Model/Graph.h>
  15. namespace GraphModelIntegration
  16. {
  17. EditorMainWindow::EditorMainWindow(GraphCanvas::AssetEditorWindowConfig* config, QWidget* parent)
  18. : GraphCanvas::AssetEditorMainWindow(config, parent)
  19. {
  20. }
  21. EditorMainWindow::~EditorMainWindow()
  22. {
  23. GraphControllerNotificationBus::MultiHandler::BusDisconnect();
  24. }
  25. GraphModel::GraphPtr EditorMainWindow::GetGraphById(GraphCanvas::GraphId graphId) const
  26. {
  27. auto it = m_graphs.find(graphId);
  28. if (it != m_graphs.end())
  29. {
  30. return it->second;
  31. }
  32. return nullptr;
  33. }
  34. GraphCanvas::GraphId EditorMainWindow::GetGraphId(GraphModel::GraphPtr graph) const
  35. {
  36. auto it = AZStd::find_if(m_graphs.begin(), m_graphs.end(),
  37. [graph](decltype(m_graphs)::const_reference pair)
  38. {
  39. return graph == pair.second;
  40. });
  41. if (it != m_graphs.end())
  42. {
  43. return it->first;
  44. }
  45. return GraphCanvas::GraphId();
  46. }
  47. void EditorMainWindow::OnEditorOpened(GraphCanvas::EditorDockWidget* dockWidget)
  48. {
  49. GraphCanvas::AssetEditorMainWindow::OnEditorOpened(dockWidget);
  50. GraphCanvas::GraphId graphId = dockWidget->GetGraphId();
  51. // Create the new graph.
  52. GraphModel::GraphPtr graph = AZStd::make_shared<GraphModel::Graph>(GetGraphContext());
  53. m_graphs[graphId] = graph;
  54. // Create the controller for the new graph.
  55. GraphModelIntegration::GraphManagerRequestBus::Broadcast(&GraphModelIntegration::GraphManagerRequests::CreateGraphController, graphId, graph);
  56. // Listen for GraphController notifications on the new graph.
  57. GraphModelIntegration::GraphControllerNotificationBus::MultiHandler::BusConnect(graphId);
  58. }
  59. void EditorMainWindow::OnEditorClosing(GraphCanvas::EditorDockWidget* dockWidget)
  60. {
  61. GraphCanvas::AssetEditorMainWindow::OnEditorClosing(dockWidget);
  62. GraphCanvas::GraphId graphId = dockWidget->GetGraphId();
  63. // Stop listening for GraphController notifications for this graph.
  64. GraphModelIntegration::GraphControllerNotificationBus::MultiHandler::BusDisconnect(graphId);
  65. // Remove the controller for this graph.
  66. GraphModelIntegration::GraphManagerRequestBus::Broadcast(&GraphModelIntegration::GraphManagerRequests::DeleteGraphController, graphId);
  67. // Delete the graph that was created.
  68. m_graphs.erase(graphId);
  69. }
  70. void EditorMainWindow::OnWrapperNodeActionWidgetClicked(const AZ::EntityId& wrapperNode, const QRect& actionWidgetBoundingRect, const QPointF& scenePoint, const QPoint& screenPoint)
  71. {
  72. // Find the GraphModel::NodePtr whose action widget was clicked.
  73. GraphCanvas::GraphId graphId = GetActiveGraphCanvasGraphId();
  74. GraphModel::NodePtr node;
  75. GraphControllerRequestBus::EventResult(node, graphId, &GraphControllerRequests::GetNodeById, wrapperNode);
  76. AZ_Assert(node, "Unable to find NodePtr for the given NodeId");
  77. // Invoke the handler so that the client can handle this event if necessary.
  78. HandleWrapperNodeActionWidgetClicked(node, actionWidgetBoundingRect, scenePoint, screenPoint);
  79. }
  80. }