GraphView.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 <AtomToolsFramework/Graph/GraphView.h>
  9. #include <AtomToolsFramework/Util/Util.h>
  10. #include <AzCore/IO/FileIO.h>
  11. #include <AzQtComponents/Components/StyleManager.h>
  12. #include <GraphCanvas/Components/Connections/ConnectionBus.h>
  13. #include <GraphCanvas/Components/MimeDataHandlerBus.h>
  14. #include <GraphCanvas/Components/Nodes/NodeBus.h>
  15. #include <GraphCanvas/Components/SceneBus.h>
  16. #include <GraphCanvas/Components/ViewBus.h>
  17. #include <GraphCanvas/GraphCanvasBus.h>
  18. #include <GraphCanvas/Types/ConstructPresets.h>
  19. #include <GraphCanvas/Widgets/AssetEditorToolbar/AssetEditorToolbar.h>
  20. #include <GraphCanvas/Widgets/EditorContextMenu/ContextMenuActions/GeneralMenuActions/GeneralMenuActions.h>
  21. #include <GraphCanvas/Widgets/EditorContextMenu/ContextMenus/BookmarkContextMenu.h>
  22. #include <GraphCanvas/Widgets/EditorContextMenu/ContextMenus/CollapsedNodeGroupContextMenu.h>
  23. #include <GraphCanvas/Widgets/EditorContextMenu/ContextMenus/CommentContextMenu.h>
  24. #include <GraphCanvas/Widgets/EditorContextMenu/ContextMenus/ConnectionContextMenu.h>
  25. #include <GraphCanvas/Widgets/EditorContextMenu/ContextMenus/NodeContextMenu.h>
  26. #include <GraphCanvas/Widgets/EditorContextMenu/ContextMenus/NodeGroupContextMenu.h>
  27. #include <GraphCanvas/Widgets/EditorContextMenu/ContextMenus/SceneContextMenu.h>
  28. #include <GraphCanvas/Widgets/EditorContextMenu/ContextMenus/SlotContextMenu.h>
  29. #include <GraphCanvas/Widgets/EditorContextMenu/EditorContextMenu.h>
  30. #include <GraphCanvas/Widgets/GraphCanvasMimeContainer.h>
  31. #include <GraphCanvas/Widgets/NodePalette/TreeItems/IconDecoratedNodePaletteTreeItem.h>
  32. #include <GraphCanvas/Widgets/NodePalette/TreeItems/NodePaletteTreeItem.h>
  33. #include <QApplication>
  34. #include <QClipboard>
  35. #include <QKeyEvent>
  36. #include <QKeySequence>
  37. #include <QMenu>
  38. #include <QMenuBar>
  39. #include <QMimeData>
  40. #include <QVBoxLayout>
  41. #include <QWindow>
  42. namespace AtomToolsFramework
  43. {
  44. GraphView::GraphView(
  45. const AZ::Crc32& toolId, const GraphCanvas::GraphId& activeGraphId, GraphViewSettingsPtr graphViewSettingsPtr, QWidget* parent)
  46. : QWidget(parent)
  47. , m_toolId(toolId)
  48. , m_graphViewSettingsPtr(graphViewSettingsPtr)
  49. {
  50. setLayout(new QVBoxLayout());
  51. m_editorToolbar = aznew GraphCanvas::AssetEditorToolbar(m_toolId);
  52. m_editorToolbar->setParent(this);
  53. layout()->addWidget(m_editorToolbar);
  54. // Screenshot
  55. m_takeScreenshot = new QToolButton(m_editorToolbar);
  56. m_takeScreenshot->setToolTip("Captures a full resolution screenshot of the entire graph or selected nodes into the clipboard");
  57. m_takeScreenshot->setIcon(QIcon(":/Icons/screenshot.png"));
  58. m_takeScreenshot->setEnabled(false);
  59. connect(m_takeScreenshot, &QToolButton::clicked, this, [this](){
  60. GraphCanvas::ViewId viewId;
  61. GraphCanvas::SceneRequestBus::EventResult(viewId, m_activeGraphId, &GraphCanvas::SceneRequests::GetViewId);
  62. GraphCanvas::ViewRequestBus::Event(viewId, &GraphCanvas::ViewRequests::ScreenshotSelection);
  63. });
  64. m_editorToolbar->AddCustomAction(m_takeScreenshot);
  65. m_graphicsView = new GraphCanvas::GraphCanvasGraphicsView(this, false);
  66. m_graphicsView->SetEditorId(m_toolId);
  67. layout()->addWidget(m_graphicsView);
  68. m_presetEditor = aznew GraphCanvas::ConstructPresetDialog(this);
  69. m_presetEditor->SetEditorId(m_toolId);
  70. m_presetWrapper = new AzQtComponents::WindowDecorationWrapper(AzQtComponents::WindowDecorationWrapper::OptionAutoTitleBarButtons, this);
  71. m_presetWrapper->setGuest(m_presetEditor);
  72. m_presetWrapper->hide();
  73. // Add a node palette for creating new nodes to the default scene context menu,
  74. // which is what is displayed when right-clicking on an empty space in the graph
  75. GraphCanvas::NodePaletteConfig nodePaletteConfig;
  76. nodePaletteConfig.m_editorId = m_toolId;
  77. nodePaletteConfig.m_mimeType = m_graphViewSettingsPtr->m_nodeMimeType.c_str();
  78. nodePaletteConfig.m_isInContextMenu = true;
  79. nodePaletteConfig.m_saveIdentifier = m_graphViewSettingsPtr->m_nodeSaveIdentifier;
  80. nodePaletteConfig.m_rootTreeItem = m_graphViewSettingsPtr->m_createNodeTreeItemsFn(m_toolId);
  81. m_sceneContextMenu = aznew GraphCanvas::SceneContextMenu(m_toolId, this);
  82. m_sceneContextMenu->AddNodePaletteMenuAction(nodePaletteConfig);
  83. // Setup the context menu with node palette for proposing a new node
  84. // when dropping a connection in an empty space in the graph
  85. nodePaletteConfig.m_rootTreeItem = m_graphViewSettingsPtr->m_createNodeTreeItemsFn(m_toolId);
  86. m_createNodeProposalContextMenu = aznew GraphCanvas::EditorContextMenu(m_toolId, this);
  87. m_createNodeProposalContextMenu->AddNodePaletteMenuAction(nodePaletteConfig);
  88. // Set up style sheet to fix highlighting in node palettes
  89. AzQtComponents::StyleManager::setStyleSheet(
  90. const_cast<GraphCanvas::NodePaletteWidget*>(m_sceneContextMenu->GetNodePalette()),
  91. QStringLiteral(":/GraphView/GraphView.qss"));
  92. AzQtComponents::StyleManager::setStyleSheet(
  93. const_cast<GraphCanvas::NodePaletteWidget*>(m_createNodeProposalContextMenu->GetNodePalette()),
  94. QStringLiteral(":/GraphView/GraphView.qss"));
  95. CreateActions();
  96. SetActiveGraphId(activeGraphId, true);
  97. }
  98. GraphView::~GraphView()
  99. {
  100. SetActiveGraphId(GraphCanvas::GraphId(), false);
  101. delete m_presetEditor;
  102. }
  103. void GraphView::SetActiveGraphId(const GraphCanvas::GraphId& activeGraphId, bool notify)
  104. {
  105. // Disconnect from any previously connecting buses.
  106. // We are enforcing that only one graph is active and connected at any given time.
  107. AtomToolsMainMenuRequestBus::Handler::BusDisconnect();
  108. GraphCanvas::AssetEditorRequestBus::Handler::BusDisconnect();
  109. GraphCanvas::SceneNotificationBus::Handler::BusDisconnect();
  110. // Update the value of the active graph ID and only reconnect the buses if it's valid.
  111. m_activeGraphId = activeGraphId;
  112. // Valid or not, update the graphics view to reference the new ID
  113. m_graphicsView->SetScene(m_activeGraphId);
  114. if (m_activeGraphId.IsValid())
  115. {
  116. AtomToolsMainMenuRequestBus::Handler::BusConnect(m_toolId);
  117. GraphCanvas::AssetEditorRequestBus::Handler::BusConnect(m_toolId);
  118. GraphCanvas::SceneNotificationBus::Handler::BusConnect(m_activeGraphId);
  119. GraphCanvas::SceneRequestBus::Event(
  120. m_activeGraphId, &GraphCanvas::SceneRequests::SetMimeType, m_graphViewSettingsPtr->m_nodeMimeType.c_str());
  121. }
  122. if (notify)
  123. {
  124. // Notify any observers connected to the asset editor buses that the active graph has changed.
  125. // We are only managing one graph at a time, not using the asset editor buses, but this will update any other system that is.
  126. GraphCanvas::AssetEditorNotificationBus::Event(m_toolId, &GraphCanvas::AssetEditorNotifications::PreOnActiveGraphChanged);
  127. GraphCanvas::AssetEditorNotificationBus::Event(m_toolId, &GraphCanvas::AssetEditorNotifications::OnActiveGraphChanged, m_activeGraphId);
  128. GraphCanvas::AssetEditorNotificationBus::Event(m_toolId, &GraphCanvas::AssetEditorNotifications::PostOnActiveGraphChanged);
  129. }
  130. // Update main window menus with commands from this view.
  131. AtomToolsMainWindowRequestBus::Event(m_toolId, &AtomToolsMainWindowRequestBus::Events::QueueUpdateMenus, true);
  132. }
  133. void GraphView::CreateActions()
  134. {
  135. auto makeAction =
  136. [&](const QString& menuName, const QString& name, const AZStd::function<void()>& fn, const QKeySequence& shortcut) -> QAction*
  137. {
  138. QAction* action = new QAction(name, this);
  139. action->setShortcut(shortcut);
  140. action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
  141. action->setProperty("menuName", menuName);
  142. QObject::connect(action, &QAction::triggered, this, fn);
  143. addAction(action);
  144. return action;
  145. };
  146. auto makeSeperator =
  147. [&](const QString& menuName) -> QAction*
  148. {
  149. QAction* action = new QAction(this);
  150. action->setSeparator(true);
  151. action->setProperty("menuName", menuName);
  152. addAction(action);
  153. return action;
  154. };
  155. makeSeperator("menuEdit");
  156. m_actionCut = makeAction("menuEdit", tr("Cut"), [this](){
  157. GraphCanvas::ScopedGraphUndoBatch undoBatch(m_activeGraphId);
  158. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::CutSelection);
  159. }, QKeySequence::Cut);
  160. m_actionCopy = makeAction("menuEdit", tr("Copy"), [this](){
  161. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::CopySelection);
  162. }, QKeySequence::Copy);
  163. m_actionPaste = makeAction("menuEdit", tr("Paste"), [this](){
  164. GraphCanvas::ScopedGraphUndoBatch undoBatch(m_activeGraphId);
  165. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::Paste);
  166. }, QKeySequence::Paste);
  167. m_actionDuplicate = makeAction("menuEdit", tr("Duplicate"), [this](){
  168. GraphCanvas::ScopedGraphUndoBatch undoBatch(m_activeGraphId);
  169. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::DuplicateSelection);
  170. }, QKeySequence(0x0 | Qt::CTRL | Qt::Key_D));
  171. m_actionDelete = makeAction("menuEdit", tr("Delete"), [this](){
  172. GraphCanvas::ScopedGraphUndoBatch undoBatch(m_activeGraphId);
  173. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::DeleteSelection);
  174. }, QKeySequence::Delete);
  175. makeSeperator("menuEdit");
  176. m_actionRemoveUnusedNodes = makeAction("menuEdit", tr("Remove Unused Nodes"), [this](){
  177. GraphCanvas::ScopedGraphUndoBatch undoBatch(m_activeGraphId);
  178. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::RemoveUnusedNodes);
  179. }, {});
  180. m_actionRemoveUnusedElements = makeAction("menuEdit", tr("Remove Unused Elements"), [this](){
  181. GraphCanvas::ScopedGraphUndoBatch undoBatch(m_activeGraphId);
  182. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::RemoveUnusedElements);
  183. }, {});
  184. makeSeperator("menuEdit");
  185. m_actionSelectAll = makeAction("menuEdit", tr("Select All"), [this](){
  186. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::SelectAll);
  187. }, QKeySequence::SelectAll);
  188. m_actionSelectInputs = makeAction("menuEdit", tr("Select Inputs"), [this](){
  189. GraphCanvas::SceneRequestBus::Event(
  190. m_activeGraphId, &GraphCanvas::SceneRequests::SelectAllRelative, GraphCanvas::ConnectionType::CT_Input);
  191. }, QKeySequence(0x0 | Qt::CTRL | Qt::Key_Left));
  192. m_actionSelectOutputs = makeAction("menuEdit", tr("Select Outputs"), [this](){
  193. GraphCanvas::SceneRequestBus::Event(
  194. m_activeGraphId, &GraphCanvas::SceneRequests::SelectAllRelative, GraphCanvas::ConnectionType::CT_Output);
  195. }, QKeySequence(0x0 | Qt::CTRL | Qt::Key_Right));
  196. m_actionSelectConnected = makeAction("menuEdit", tr("Select Connected"), [this](){
  197. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::SelectConnectedNodes);
  198. }, QKeySequence(0x0 | Qt::CTRL | Qt::Key_Up));
  199. m_actionSelectNone = makeAction("menuEdit", tr("Clear Selection"), [this](){
  200. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::ClearSelection);
  201. }, QKeySequence::Deselect);
  202. m_actionSelectEnable = makeAction("menuEdit", tr("Enable Selection"), [this](){
  203. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::EnableSelection);
  204. }, QKeySequence(0x0 | Qt::CTRL | Qt::Key_K, 0x0 | Qt::CTRL | Qt::Key_U));
  205. m_actionSelectDisable = makeAction("menuEdit", tr("Disable Selection"), [this](){
  206. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::DisableSelection);
  207. }, QKeySequence(0x0 | Qt::CTRL | Qt::Key_K, 0x0 | Qt::CTRL | Qt::Key_C));
  208. makeSeperator("menuEdit");
  209. m_actionScreenShot = makeAction("menuEdit", tr("Screenshot"), [this](){
  210. GraphCanvas::ViewId viewId;
  211. GraphCanvas::SceneRequestBus::EventResult(viewId, m_activeGraphId, &GraphCanvas::SceneRequests::GetViewId);
  212. GraphCanvas::ViewRequestBus::Event(viewId, &GraphCanvas::ViewRequests::ScreenshotSelection);
  213. }, QKeySequence(0x0 | Qt::CTRL | Qt::SHIFT | Qt::Key_P));
  214. makeSeperator("menuEdit");
  215. m_actionAlignTop = makeAction("menuEdit", tr("Align Top"), [this](){
  216. GraphCanvas::AlignConfig alignConfig;
  217. alignConfig.m_horAlign = GraphCanvas::GraphUtils::HorizontalAlignment::None;
  218. alignConfig.m_verAlign = GraphCanvas::GraphUtils::VerticalAlignment::Top;
  219. GraphCanvas::AssetEditorSettingsRequestBus::EventResult(
  220. alignConfig.m_alignTime, m_toolId, &GraphCanvas::AssetEditorSettingsRequestBus::Events::GetAlignmentTime);
  221. AlignSelected(alignConfig);
  222. }, {});
  223. m_actionAlignBottom = makeAction("menuEdit", tr("Align Bottom"), [this](){
  224. GraphCanvas::AlignConfig alignConfig;
  225. alignConfig.m_horAlign = GraphCanvas::GraphUtils::HorizontalAlignment::None;
  226. alignConfig.m_verAlign = GraphCanvas::GraphUtils::VerticalAlignment::Bottom;
  227. GraphCanvas::AssetEditorSettingsRequestBus::EventResult(
  228. alignConfig.m_alignTime, m_toolId, &GraphCanvas::AssetEditorSettingsRequestBus::Events::GetAlignmentTime);
  229. AlignSelected(alignConfig);
  230. }, {});
  231. m_actionAlignLeft = makeAction("menuEdit", tr("Align Left"), [this](){
  232. GraphCanvas::AlignConfig alignConfig;
  233. alignConfig.m_horAlign = GraphCanvas::GraphUtils::HorizontalAlignment::Left;
  234. alignConfig.m_verAlign = GraphCanvas::GraphUtils::VerticalAlignment::None;
  235. GraphCanvas::AssetEditorSettingsRequestBus::EventResult(
  236. alignConfig.m_alignTime, m_toolId, &GraphCanvas::AssetEditorSettingsRequestBus::Events::GetAlignmentTime);
  237. AlignSelected(alignConfig);
  238. }, {});
  239. m_actionAlignRight = makeAction("menuEdit", tr("Align Right"), [this](){
  240. GraphCanvas::AlignConfig alignConfig;
  241. alignConfig.m_horAlign = GraphCanvas::GraphUtils::HorizontalAlignment::Right;
  242. alignConfig.m_verAlign = GraphCanvas::GraphUtils::VerticalAlignment::None;
  243. GraphCanvas::AssetEditorSettingsRequestBus::EventResult(
  244. alignConfig.m_alignTime, m_toolId, &GraphCanvas::AssetEditorSettingsRequestBus::Events::GetAlignmentTime);
  245. AlignSelected(alignConfig);
  246. }, {});
  247. makeSeperator("menuView");
  248. m_actionPresetEditor = makeAction("menuView", tr("Presets Editor"), [this](){ OpenPresetsEditor(); }, {});
  249. makeSeperator("menuView");
  250. m_actionShowEntireGraph = makeAction("menuView", tr("Show Entire Graph"), [this](){
  251. GraphCanvas::ViewId viewId;
  252. GraphCanvas::SceneRequestBus::EventResult(viewId, m_activeGraphId, &GraphCanvas::SceneRequests::GetViewId);
  253. GraphCanvas::ViewRequestBus::Event(viewId, &GraphCanvas::ViewRequests::ShowEntireGraph);
  254. }, QKeySequence(0x0 | Qt::CTRL | Qt::SHIFT | Qt::Key_Down));
  255. m_actionZoomIn = makeAction("menuView", tr("Zoom In"), [this](){
  256. GraphCanvas::ViewId viewId;
  257. GraphCanvas::SceneRequestBus::EventResult(viewId, m_activeGraphId, &GraphCanvas::SceneRequests::GetViewId);
  258. GraphCanvas::ViewRequestBus::Event(viewId, &GraphCanvas::ViewRequests::ZoomIn);
  259. }, QKeySequence::ZoomIn);
  260. m_actionZoomOut = makeAction("menuView", tr("Zoom Out"), [this](){
  261. GraphCanvas::ViewId viewId;
  262. GraphCanvas::SceneRequestBus::EventResult(viewId, m_activeGraphId, &GraphCanvas::SceneRequests::GetViewId);
  263. GraphCanvas::ViewRequestBus::Event(viewId, &GraphCanvas::ViewRequests::ZoomOut);
  264. }, QKeySequence::ZoomOut);
  265. m_actionZoomSelection = makeAction("menuView", tr("Zoom Selection"), [this](){
  266. GraphCanvas::ViewId viewId;
  267. GraphCanvas::SceneRequestBus::EventResult(viewId, m_activeGraphId, &GraphCanvas::SceneRequests::GetViewId);
  268. GraphCanvas::ViewRequestBus::Event(viewId, &GraphCanvas::ViewRequests::CenterOnSelection);
  269. }, QKeySequence(0x0 | Qt::CTRL | Qt::SHIFT | Qt::Key_Up));
  270. makeSeperator("menuView");
  271. m_actionGotoStartOfChain = makeAction("menuView", tr("Goto Start Of Chain"), [this](){
  272. GraphCanvas::ViewId viewId;
  273. GraphCanvas::SceneRequestBus::EventResult(viewId, m_activeGraphId, &GraphCanvas::SceneRequests::GetViewId);
  274. GraphCanvas::ViewRequestBus::Event(viewId, &GraphCanvas::ViewRequests::CenterOnStartOfChain);
  275. }, QKeySequence(0x0 | Qt::CTRL | Qt::SHIFT | Qt::Key_Left));
  276. m_actionGotoEndOfChain = makeAction("menuView", tr("Goto End Of Chain"), [this](){
  277. GraphCanvas::ViewId viewId;
  278. GraphCanvas::SceneRequestBus::EventResult(viewId, m_activeGraphId, &GraphCanvas::SceneRequests::GetViewId);
  279. GraphCanvas::ViewRequestBus::Event(viewId, &GraphCanvas::ViewRequests::CenterOnEndOfChain);
  280. }, QKeySequence(0x0 | Qt::CTRL | Qt::SHIFT | Qt::Key_Right));
  281. }
  282. void GraphView::CreateMenus(QMenuBar* menuBar)
  283. {
  284. for (auto action : actions())
  285. {
  286. if (auto menuName = action->property("menuName"); menuName.isValid())
  287. {
  288. if (auto menu = menuBar->findChild<QMenu*>(menuName.toString()))
  289. {
  290. menu->addAction(action);
  291. }
  292. }
  293. }
  294. }
  295. void GraphView::UpdateMenus([[maybe_unused]] QMenuBar* menuBar)
  296. {
  297. const bool hasGraph = m_activeGraphId.IsValid();
  298. bool hasSelection = false;
  299. bool hasCopiableSelection = false;
  300. if (hasGraph)
  301. {
  302. AzToolsFramework::EntityIdList selectedItems;
  303. GraphCanvas::SceneRequestBus::EventResult(selectedItems, m_activeGraphId, &GraphCanvas::SceneRequests::GetSelectedItems);
  304. hasSelection = !selectedItems.empty();
  305. GraphCanvas::SceneRequestBus::EventResult(
  306. hasCopiableSelection, m_activeGraphId, &GraphCanvas::SceneRequests::HasCopiableSelection);
  307. }
  308. // Enable the Paste action if the clipboard (if any) has a mime type that we support
  309. AZStd::string copyMimeType;
  310. GraphCanvas::SceneRequestBus::EventResult(copyMimeType, m_activeGraphId, &GraphCanvas::SceneRequests::GetCopyMimeType);
  311. const bool canPaste = hasGraph && !copyMimeType.empty() && QApplication::clipboard()->mimeData()->hasFormat(copyMimeType.c_str());
  312. m_actionCut->setEnabled(hasCopiableSelection);
  313. m_actionCopy->setEnabled(hasCopiableSelection);
  314. m_actionPaste->setEnabled(canPaste);
  315. m_actionDelete->setEnabled(hasSelection);
  316. m_actionDuplicate->setEnabled(hasCopiableSelection);
  317. m_actionRemoveUnusedNodes->setEnabled(hasGraph);
  318. m_actionRemoveUnusedElements->setEnabled(hasGraph);
  319. m_actionSelectAll->setEnabled(hasGraph);
  320. m_actionSelectNone->setEnabled(hasSelection);
  321. m_actionSelectInputs->setEnabled(hasGraph);
  322. m_actionSelectOutputs->setEnabled(hasGraph);
  323. m_actionSelectConnected->setEnabled(hasGraph);
  324. m_actionSelectEnable->setEnabled(hasGraph);
  325. m_actionSelectDisable->setEnabled(hasGraph);
  326. m_actionScreenShot->setEnabled(hasGraph);
  327. m_actionAlignTop->setEnabled(hasSelection);
  328. m_actionAlignBottom->setEnabled(hasSelection);
  329. m_actionAlignLeft->setEnabled(hasSelection);
  330. m_actionAlignRight->setEnabled(hasSelection);
  331. m_actionPresetEditor->setEnabled(hasGraph);
  332. m_actionShowEntireGraph->setEnabled(hasGraph);
  333. m_actionZoomIn->setEnabled(hasGraph);
  334. m_actionZoomOut->setEnabled(hasGraph);
  335. m_actionZoomSelection->setEnabled(hasSelection);
  336. m_actionGotoStartOfChain->setEnabled(hasGraph);
  337. m_actionGotoEndOfChain->setEnabled(hasGraph);
  338. m_takeScreenshot->setEnabled(hasGraph);
  339. }
  340. AZ::s32 GraphView::GetMainMenuPriority() const
  341. {
  342. // Return a priority that will place menus for the view below menus for the main window
  343. return 1;
  344. }
  345. AZ::EntityId GraphView::CreateNewGraph()
  346. {
  347. return m_activeGraphId;
  348. }
  349. bool GraphView::ContainsGraph([[maybe_unused]] const GraphCanvas::GraphId& graphId) const
  350. {
  351. return m_activeGraphId == graphId;
  352. }
  353. bool GraphView::CloseGraph([[maybe_unused]] const GraphCanvas::GraphId& graphId)
  354. {
  355. return false;
  356. }
  357. GraphCanvas::ContextMenuAction::SceneReaction GraphView::ShowSceneContextMenu(const QPoint& screenPoint, const QPointF& scenePoint)
  358. {
  359. m_sceneContextMenu->ResetSourceSlotFilter();
  360. // We pass an invalid EntityId here since this is for the scene, there is no member to specify.
  361. return HandleContextMenu(*m_sceneContextMenu, AZ::EntityId(), screenPoint, scenePoint);
  362. }
  363. GraphCanvas::ContextMenuAction::SceneReaction GraphView::ShowNodeContextMenu(
  364. const AZ::EntityId& nodeId, const QPoint& screenPoint, const QPointF& scenePoint)
  365. {
  366. GraphCanvas::NodeContextMenu contextMenu(m_toolId);
  367. return HandleContextMenu(contextMenu, nodeId, screenPoint, scenePoint);
  368. }
  369. GraphCanvas::ContextMenuAction::SceneReaction GraphView::ShowCommentContextMenu(
  370. const AZ::EntityId& nodeId, const QPoint& screenPoint, const QPointF& scenePoint)
  371. {
  372. GraphCanvas::CommentContextMenu contextMenu(m_toolId);
  373. return HandleContextMenu(contextMenu, nodeId, screenPoint, scenePoint);
  374. }
  375. GraphCanvas::ContextMenuAction::SceneReaction GraphView::ShowNodeGroupContextMenu(
  376. const AZ::EntityId& groupId, const QPoint& screenPoint, const QPointF& scenePoint)
  377. {
  378. GraphCanvas::NodeGroupContextMenu contextMenu(m_toolId);
  379. return HandleContextMenu(contextMenu, groupId, screenPoint, scenePoint);
  380. }
  381. GraphCanvas::ContextMenuAction::SceneReaction GraphView::ShowCollapsedNodeGroupContextMenu(
  382. const AZ::EntityId& nodeId, const QPoint& screenPoint, const QPointF& scenePoint)
  383. {
  384. GraphCanvas::CollapsedNodeGroupContextMenu contextMenu(m_toolId);
  385. return HandleContextMenu(contextMenu, nodeId, screenPoint, scenePoint);
  386. }
  387. GraphCanvas::ContextMenuAction::SceneReaction GraphView::ShowBookmarkContextMenu(
  388. const AZ::EntityId& bookmarkId, const QPoint& screenPoint, const QPointF& scenePoint)
  389. {
  390. GraphCanvas::BookmarkContextMenu contextMenu(m_toolId);
  391. return HandleContextMenu(contextMenu, bookmarkId, screenPoint, scenePoint);
  392. }
  393. GraphCanvas::ContextMenuAction::SceneReaction GraphView::ShowConnectionContextMenu(
  394. const AZ::EntityId& connectionId, const QPoint& screenPoint, const QPointF& scenePoint)
  395. {
  396. GraphCanvas::ConnectionContextMenu contextMenu(m_toolId);
  397. return HandleContextMenu(contextMenu, connectionId, screenPoint, scenePoint);
  398. }
  399. GraphCanvas::ContextMenuAction::SceneReaction GraphView::ShowSlotContextMenu(
  400. const AZ::EntityId& slotId, const QPoint& screenPoint, const QPointF& scenePoint)
  401. {
  402. GraphCanvas::SlotContextMenu contextMenu(m_toolId);
  403. return HandleContextMenu(contextMenu, slotId, screenPoint, scenePoint);
  404. }
  405. GraphCanvas::Endpoint GraphView::CreateNodeForProposal(
  406. const AZ::EntityId& connectionId, const GraphCanvas::Endpoint& endpoint, const QPointF& scenePoint, const QPoint& screenPoint)
  407. {
  408. GraphCanvas::Endpoint retVal;
  409. m_createNodeProposalContextMenu->FilterForSourceSlot(m_activeGraphId, endpoint.GetSlotId());
  410. m_createNodeProposalContextMenu->RefreshActions(m_activeGraphId, connectionId);
  411. m_createNodeProposalContextMenu->exec(screenPoint);
  412. GraphCanvas::GraphCanvasMimeEvent* mimeEvent = m_createNodeProposalContextMenu->GetNodePalette()->GetContextMenuEvent();
  413. if (mimeEvent)
  414. {
  415. GraphCanvas::ScopedGraphUndoBatch undoBatch(m_activeGraphId);
  416. AZ::Vector2 dropPos(aznumeric_cast<float>(scenePoint.x()), aznumeric_cast<float>(scenePoint.y()));
  417. if (mimeEvent->ExecuteEvent(dropPos, dropPos, m_activeGraphId))
  418. {
  419. GraphCanvas::NodeId nodeId = mimeEvent->GetCreatedNodeId();
  420. if (nodeId.IsValid())
  421. {
  422. GraphCanvas::VisualRequestBus::Event(nodeId, &GraphCanvas::VisualRequests::SetVisible, false);
  423. retVal = HandleProposedConnection(m_activeGraphId, connectionId, endpoint, nodeId, screenPoint);
  424. }
  425. if (retVal.IsValid())
  426. {
  427. GraphCanvas::GraphUtils::CreateOpportunisticConnectionsBetween(endpoint, retVal);
  428. GraphCanvas::VisualRequestBus::Event(nodeId, &GraphCanvas::VisualRequests::SetVisible, true);
  429. AZ::Vector2 position;
  430. GraphCanvas::GeometryRequestBus::EventResult(position, retVal.GetNodeId(), &GraphCanvas::GeometryRequests::GetPosition);
  431. QPointF connectionPoint;
  432. GraphCanvas::SlotUIRequestBus::EventResult(
  433. connectionPoint, retVal.GetSlotId(), &GraphCanvas::SlotUIRequests::GetConnectionPoint);
  434. qreal verticalOffset = connectionPoint.y() - position.GetY();
  435. position.SetY(aznumeric_cast<float>(scenePoint.y() - verticalOffset));
  436. qreal horizontalOffset = connectionPoint.x() - position.GetX();
  437. position.SetX(aznumeric_cast<float>(scenePoint.x() - horizontalOffset));
  438. GraphCanvas::GeometryRequestBus::Event(retVal.GetNodeId(), &GraphCanvas::GeometryRequests::SetPosition, position);
  439. GraphCanvas::SceneNotificationBus::Event(m_activeGraphId, &GraphCanvas::SceneNotifications::PostCreationEvent);
  440. }
  441. else
  442. {
  443. GraphCanvas::GraphUtils::DeleteOutermostNode(m_activeGraphId, nodeId);
  444. }
  445. }
  446. }
  447. return retVal;
  448. }
  449. void GraphView::OnWrapperNodeActionWidgetClicked(
  450. [[maybe_unused]] const AZ::EntityId& wrapperNode,
  451. [[maybe_unused]] const QRect& actionWidgetBoundingRect,
  452. [[maybe_unused]] const QPointF& scenePoint,
  453. [[maybe_unused]] const QPoint& screenPoint)
  454. {
  455. }
  456. void GraphView::OnSelectionChanged()
  457. {
  458. AtomToolsMainWindowRequestBus::Event(m_toolId, &AtomToolsMainWindowRequestBus::Events::QueueUpdateMenus, false);
  459. }
  460. GraphCanvas::Endpoint GraphView::HandleProposedConnection(
  461. [[maybe_unused]] const GraphCanvas::GraphId& graphId,
  462. [[maybe_unused]] const GraphCanvas::ConnectionId& connectionId,
  463. const GraphCanvas::Endpoint& endpoint,
  464. const GraphCanvas::NodeId& proposedNode,
  465. const QPoint& screenPoint)
  466. {
  467. GraphCanvas::Endpoint retVal;
  468. GraphCanvas::ConnectionType connectionType = GraphCanvas::ConnectionType::CT_Invalid;
  469. GraphCanvas::SlotRequestBus::EventResult(connectionType, endpoint.GetSlotId(), &GraphCanvas::SlotRequests::GetConnectionType);
  470. GraphCanvas::NodeId currentTarget = proposedNode;
  471. while (!retVal.IsValid() && currentTarget.IsValid())
  472. {
  473. AZStd::vector<AZ::EntityId> targetSlotIds;
  474. GraphCanvas::NodeRequestBus::EventResult(targetSlotIds, currentTarget, &GraphCanvas::NodeRequests::GetSlotIds);
  475. // Find the list of endpoints on the created node that could create a valid connection
  476. // with the specified slot
  477. AZStd::list<GraphCanvas::Endpoint> endpoints;
  478. for (const auto& targetSlotId : targetSlotIds)
  479. {
  480. GraphCanvas::Endpoint proposedEndpoint(currentTarget, targetSlotId);
  481. bool canCreate = false;
  482. GraphCanvas::SlotRequestBus::EventResult(canCreate, endpoint.GetSlotId(), &GraphCanvas::SlotRequests::CanCreateConnectionTo, proposedEndpoint);
  483. if (canCreate)
  484. {
  485. GraphCanvas::SlotGroup slotGroup = GraphCanvas::SlotGroups::Invalid;
  486. GraphCanvas::SlotRequestBus::EventResult(slotGroup, targetSlotId, &GraphCanvas::SlotRequests::GetSlotGroup);
  487. bool isVisible = slotGroup != GraphCanvas::SlotGroups::Invalid;
  488. GraphCanvas::SlotLayoutRequestBus::EventResult(isVisible, currentTarget, &GraphCanvas::SlotLayoutRequests::IsSlotGroupVisible, slotGroup);
  489. if (isVisible)
  490. {
  491. endpoints.push_back(proposedEndpoint);
  492. }
  493. }
  494. }
  495. if (!endpoints.empty())
  496. {
  497. // If there is exactly one match, then we can just use that endpoint.
  498. if (endpoints.size() == 1)
  499. {
  500. retVal = endpoints.front();
  501. }
  502. // Otherwise, since there are multiple possible matches, we need to display a simple menu for the
  503. // user to select which slot they want they want to be connected to the proposed endpoint.
  504. else
  505. {
  506. QMenu menu;
  507. for (GraphCanvas::Endpoint proposedEndpoint : endpoints)
  508. {
  509. menu.addAction(aznew GraphCanvas::EndpointSelectionAction(proposedEndpoint));
  510. }
  511. QAction* result = menu.exec(screenPoint);
  512. if (result)
  513. {
  514. auto* selectedEnpointAction = static_cast<GraphCanvas::EndpointSelectionAction*>(result);
  515. retVal = selectedEnpointAction->GetEndpoint();
  516. }
  517. else
  518. {
  519. retVal.Clear();
  520. }
  521. }
  522. if (retVal.IsValid())
  523. {
  524. // Double safety check. This should be guaranteed by the previous checks. But just extra safety.
  525. bool canCreateConnection = false;
  526. GraphCanvas::SlotRequestBus::EventResult(canCreateConnection, endpoint.GetSlotId(), &GraphCanvas::SlotRequests::CanCreateConnectionTo, retVal);
  527. if (!canCreateConnection)
  528. {
  529. retVal.Clear();
  530. }
  531. }
  532. }
  533. else
  534. {
  535. retVal.Clear();
  536. }
  537. if (!retVal.IsValid())
  538. {
  539. bool isWrapped = false;
  540. GraphCanvas::NodeRequestBus::EventResult(isWrapped, currentTarget, &GraphCanvas::NodeRequests::IsWrapped);
  541. if (isWrapped)
  542. {
  543. GraphCanvas::NodeRequestBus::EventResult(currentTarget, currentTarget, &GraphCanvas::NodeRequests::GetWrappingNode);
  544. }
  545. else
  546. {
  547. currentTarget.SetInvalid();
  548. }
  549. }
  550. }
  551. return retVal;
  552. }
  553. GraphCanvas::ContextMenuAction::SceneReaction GraphView::HandleContextMenu(
  554. GraphCanvas::EditorContextMenu& editorContextMenu, const AZ::EntityId& memberId, const QPoint& screenPoint, const QPointF& scenePoint) const
  555. {
  556. AZ::Vector2 sceneVector(aznumeric_cast<float>(scenePoint.x()), aznumeric_cast<float>(scenePoint.y()));
  557. editorContextMenu.RefreshActions(m_activeGraphId, memberId);
  558. QAction* result = editorContextMenu.exec(screenPoint);
  559. if (auto contextMenuAction = qobject_cast<GraphCanvas::ContextMenuAction*>(result))
  560. {
  561. GraphCanvas::ScopedGraphUndoBatch undoBatch(m_activeGraphId);
  562. return contextMenuAction->TriggerAction(m_activeGraphId, sceneVector);
  563. }
  564. if (editorContextMenu.GetNodePalette())
  565. {
  566. // Handle creating node from any node palette embedded in an GraphCanvas::EditorContextMenu.
  567. GraphCanvas::GraphCanvasMimeEvent* mimeEvent = editorContextMenu.GetNodePalette()->GetContextMenuEvent();
  568. if (mimeEvent)
  569. {
  570. GraphCanvas::ScopedGraphUndoBatch undoBatch(m_activeGraphId);
  571. AZ::Vector2 dropPos(aznumeric_cast<float>(scenePoint.x()), aznumeric_cast<float>(scenePoint.y()));
  572. if (mimeEvent->ExecuteEvent(dropPos, dropPos, m_activeGraphId))
  573. {
  574. GraphCanvas::NodeId nodeId = mimeEvent->GetCreatedNodeId();
  575. if (nodeId.IsValid())
  576. {
  577. GraphCanvas::SceneRequestBus::Event(m_activeGraphId, &GraphCanvas::SceneRequests::ClearSelection);
  578. GraphCanvas::VisualRequestBus::Event(nodeId, &GraphCanvas::VisualRequests::SetVisible, true);
  579. GraphCanvas::SceneMemberUIRequestBus::Event(nodeId, &GraphCanvas::SceneMemberUIRequests::SetSelected, true);
  580. GraphCanvas::SceneNotificationBus::Event(m_activeGraphId, &GraphCanvas::SceneNotifications::PostCreationEvent);
  581. }
  582. }
  583. }
  584. }
  585. return GraphCanvas::ContextMenuAction::SceneReaction::Nothing;
  586. }
  587. void GraphView::AlignSelected(const GraphCanvas::AlignConfig& alignConfig)
  588. {
  589. GraphCanvas::ScopedGraphUndoBatch undoBatch(m_activeGraphId);
  590. AZStd::vector<GraphCanvas::NodeId> selectedNodes;
  591. GraphCanvas::SceneRequestBus::EventResult(selectedNodes, m_activeGraphId, &GraphCanvas::SceneRequests::GetSelectedNodes);
  592. GraphCanvas::GraphUtils::AlignNodes(selectedNodes, alignConfig);
  593. }
  594. void GraphView::OpenPresetsEditor()
  595. {
  596. if (m_presetEditor && m_presetWrapper)
  597. {
  598. QSize boundingBox = size();
  599. QPointF newPosition =
  600. mapToGlobal(QPoint(aznumeric_cast<int>(boundingBox.width() * 0.5f), aznumeric_cast<int>(boundingBox.height() * 0.5f)));
  601. m_presetEditor->show();
  602. m_presetWrapper->show();
  603. m_presetWrapper->raise();
  604. m_presetWrapper->activateWindow();
  605. QRect geometry = m_presetWrapper->geometry();
  606. QSize originalSize = geometry.size();
  607. newPosition.setX(newPosition.x() - geometry.width() * 0.5f);
  608. newPosition.setY(newPosition.y() - geometry.height() * 0.5f);
  609. geometry.setTopLeft(newPosition.toPoint());
  610. geometry.setWidth(originalSize.width());
  611. geometry.setHeight(originalSize.height());
  612. m_presetWrapper->setGeometry(geometry);
  613. }
  614. }
  615. } // namespace AtomToolsFramework
  616. #include <AtomToolsFramework/Graph/moc_GraphView.cpp>