ContextMenuHandlers.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <ContextMenuHandlers.h>
  9. #include <AzFramework/Viewport/ScreenGeometry.h>
  10. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  11. #include <AzToolsFramework/ActionManager/Action/ActionManagerInterface.h>
  12. #include <AzToolsFramework/ActionManager/Menu/MenuManagerInterface.h>
  13. #include <AzToolsFramework/Editor/ActionManagerIdentifiers/EditorActionUpdaterIdentifiers.h>
  14. #include <AzToolsFramework/Editor/ActionManagerIdentifiers/EditorContextIdentifiers.h>
  15. #include <AzToolsFramework/Editor/ActionManagerIdentifiers/EditorMenuIdentifiers.h>
  16. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  17. #include <QAction>
  18. #include <QMenu>
  19. void EditorContextMenuHandler::Setup()
  20. {
  21. AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler::BusConnect();
  22. }
  23. void EditorContextMenuHandler::Teardown()
  24. {
  25. AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler::BusDisconnect();
  26. }
  27. void EditorContextMenuHandler::OnMenuBindingHook()
  28. {
  29. auto menuManagerInterface = AZ::Interface<AzToolsFramework::MenuManagerInterface>::Get();
  30. if (!menuManagerInterface)
  31. {
  32. return;
  33. }
  34. // Entity Outliner Context Menu
  35. menuManagerInterface->AddActionToMenu(
  36. EditorIdentifiers::EntityOutlinerContextMenuIdentifier, "o3de.action.entity.openPinnedInspector", 50100);
  37. // Viewport Context Menu
  38. menuManagerInterface->AddActionToMenu(
  39. EditorIdentifiers::ViewportContextMenuIdentifier, "o3de.action.entity.openPinnedInspector", 50100);
  40. }
  41. void EditorContextMenuHandler::OnActionRegistrationHook()
  42. {
  43. auto actionManagerInterface = AZ::Interface<AzToolsFramework::ActionManagerInterface>::Get();
  44. if (!actionManagerInterface)
  45. {
  46. return;
  47. }
  48. // Open Pinned Inspector
  49. {
  50. const AZStd::string_view actionIdentifier = "o3de.action.entity.openPinnedInspector";
  51. AzToolsFramework::ActionProperties actionProperties;
  52. actionProperties.m_name = "Open Pinned Inspector";
  53. actionProperties.m_description = "Open a new instance of the Entity Inspector for the current selection.";
  54. actionProperties.m_category = "Edit";
  55. actionManagerInterface->RegisterAction(
  56. EditorIdentifiers::MainWindowActionContextIdentifier,
  57. actionIdentifier,
  58. actionProperties,
  59. []()
  60. {
  61. AzToolsFramework::EntityIdList selectedEntities;
  62. AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(
  63. selectedEntities, &AzToolsFramework::ToolsApplicationRequests::Bus::Events::GetSelectedEntities);
  64. AzToolsFramework::EntityIdSet pinnedEntities(selectedEntities.begin(), selectedEntities.end());
  65. AzToolsFramework::EditorRequestBus::Broadcast(&AzToolsFramework::EditorRequests::OpenPinnedInspector, pinnedEntities);
  66. }
  67. );
  68. actionManagerInterface->InstallEnabledStateCallback(
  69. actionIdentifier,
  70. []() -> bool
  71. {
  72. int selectedEntitiesCount;
  73. AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(
  74. selectedEntitiesCount, &AzToolsFramework::ToolsApplicationRequests::Bus::Events::GetSelectedEntitiesCount);
  75. return selectedEntitiesCount > 0;
  76. }
  77. );
  78. // Trigger update whenever entity selection changes.
  79. actionManagerInterface->AddActionToUpdater(EditorIdentifiers::EntitySelectionChangedUpdaterIdentifier, actionIdentifier);
  80. // This action is only accessible outside of Component Modes
  81. actionManagerInterface->AssignModeToAction(AzToolsFramework::DefaultActionContextModeIdentifier, actionIdentifier);
  82. }
  83. }