SandboxIntegration.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #ifndef CRYINCLUDE_COMPONENTENTITYEDITORPLUGIN_SANDBOXINTEGRATION_H
  9. #define CRYINCLUDE_COMPONENTENTITYEDITORPLUGIN_SANDBOXINTEGRATION_H
  10. #include "ContextMenuHandlers.h"
  11. #include <AzCore/Component/ComponentApplicationBus.h>
  12. #include <AzCore/Math/Uuid.h>
  13. #include <AzCore/std/string/conversions.h>
  14. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  15. #include <AzToolsFramework/ActionManager/ActionManagerRegistrationNotificationBus.h>
  16. #include <AzToolsFramework/API/EditorWindowRequestBus.h>
  17. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  18. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  19. #include <AzToolsFramework/UI/Prefab/PrefabIntegrationManager.h>
  20. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  21. #include <AzToolsFramework/Viewport/ViewportMessages.h>
  22. // Sandbox imports.
  23. #include "../Editor/ViewManager.h"
  24. #include "../Editor/Viewport.h"
  25. #include "../Editor/Undo/IUndoManagerListener.h"
  26. #include "../Editor/Undo/IUndoObject.h"
  27. #include <QApplication>
  28. #include <QPointer>
  29. /**
  30. * Integration of ToolsApplication behavior and Cry undo/redo and selection systems
  31. * with respect to component entity operations.
  32. *
  33. * Undo/Redo
  34. * - CToolsApplicationUndoLink represents a component application undo operation within
  35. * the Sandbox undo system. When an undo-able component operation is performed, we
  36. * intercept ToolsApplicationEventBus::OnBeginUndo()/OnEndUndo() events, and in turn
  37. * create and register a link instance.
  38. * - When the user attempts to undo/redo a CToolsApplicationUndoLink event in Sandbox,
  39. * CToolsApplicationUndoLink::Undo()/Redo() is invoked, and the request is passed
  40. * to the component application via ToolsApplicationRequestBus::OnUndoPressed/OnRedoPressed,
  41. * where restoration of the previous entity snapshot is handled.
  42. *
  43. * AzToolsFramework::ToolsApplication Extensions
  44. * - Provides engine UI customizations, such as using the engine's built in asset browser
  45. * when assigning asset references to component properties.
  46. * - Handles component edit-time display requests (using the editor's drawing context).
  47. * - Handles source control requests from AZ components or component-related UI.
  48. */
  49. namespace AZ::Data
  50. {
  51. class AssetInfo;
  52. }
  53. class CToolsApplicationUndoLink;
  54. class QMenu;
  55. class QWidget;
  56. namespace AzToolsFramework
  57. {
  58. class EditorEntityAPI;
  59. class ReadOnlyEntityPublicInterface;
  60. namespace AssetBrowser
  61. {
  62. class AssetSelectionModel;
  63. }
  64. namespace Prefab
  65. {
  66. class PrefabIntegrationInterface;
  67. }
  68. }
  69. //////////////////////////////////////////////////////////////////////////
  70. class SandboxIntegrationManager
  71. : private AzToolsFramework::ToolsApplicationEvents::Bus::Handler
  72. , private AzToolsFramework::EditorRequests::Bus::Handler
  73. , private AzToolsFramework::EditorWindowRequests::Bus::Handler
  74. , private AzToolsFramework::EditorEntityContextNotificationBus::Handler
  75. , private IUndoManagerListener
  76. , private AzToolsFramework::ActionManagerRegistrationNotificationBus::Handler
  77. {
  78. public:
  79. SandboxIntegrationManager();
  80. ~SandboxIntegrationManager();
  81. void Setup();
  82. void Teardown();
  83. private:
  84. //////////////////////////////////////////////////////////////////////////
  85. //////////////////////////////////////////////////////////////////////////
  86. // AzToolsFramework::ToolsApplicationEvents::Bus::Handler overrides
  87. void OnBeginUndo(const char* label) override;
  88. void OnEndUndo(const char* label, bool changed) override;
  89. //////////////////////////////////////////////////////////////////////////
  90. //////////////////////////////////////////////////////////////////////////
  91. // AzToolsFramework::EditorRequests::Bus::Handler overrides
  92. void RegisterViewPane(const char* name, const char* category, const AzToolsFramework::ViewPaneOptions& viewOptions, const WidgetCreationFunc& widgetCreationFunc) override;
  93. void RegisterCustomViewPane(const char* name, const char* category, const AzToolsFramework::ViewPaneOptions& viewOptions) override;
  94. void UnregisterViewPane(const char* name) override;
  95. QWidget* GetViewPaneWidget(const char* viewPaneName) override;
  96. void OpenViewPane(const char* paneName) override;
  97. QDockWidget* InstanceViewPane(const char* paneName) override;
  98. void CloseViewPane(const char* paneName) override;
  99. void BrowseForAssets(AzToolsFramework::AssetBrowser::AssetSelectionModel& selection) override;
  100. void CreateEditorRepresentation(AZ::Entity* entity) override;
  101. void CloneSelection(bool& handled) override;
  102. void DeleteSelectedEntities(bool includeDescendants) override;
  103. AZ::EntityId CreateNewEntity(AZ::EntityId parentId = AZ::EntityId()) override;
  104. AZ::EntityId CreateNewEntityAsChild(AZ::EntityId parentId) override;
  105. AZ::EntityId CreateNewEntityAtPosition(const AZ::Vector3& /*pos*/, AZ::EntityId parentId = AZ::EntityId()) override;
  106. AzFramework::EntityContextId GetEntityContextId() override;
  107. QWidget* GetMainWindow() override;
  108. IEditor* GetEditor() override;
  109. void LaunchLuaEditor(const char* files) override;
  110. bool IsLevelDocumentOpen() override;
  111. AZStd::string GetLevelName() override;
  112. void OpenPinnedInspector(const AzToolsFramework::EntityIdSet& entities) override;
  113. void ClosePinnedInspector(AzToolsFramework::EntityPropertyEditor* editor) override;
  114. void GoToSelectedOrHighlightedEntitiesInViewports() override;
  115. void GoToSelectedEntitiesInViewports() override;
  116. bool CanGoToSelectedEntitiesInViewports() override;
  117. AZ::Vector3 GetWorldPositionAtViewportCenter() override;
  118. AZ::Vector3 GetWorldPositionAtViewportInteraction() const override;
  119. void ClearRedoStack() override;
  120. //////////////////////////////////////////////////////////////////////////
  121. //////////////////////////////////////////////////////////////////////////
  122. // AzToolsFramework::EditorWindowRequests::Bus::Handler
  123. QWidget* GetAppMainWindow() override;
  124. //////////////////////////////////////////////////////////////////////////
  125. //////////////////////////////////////////////////////////////////////////
  126. // AzToolsFramework::EditorEntityContextNotificationBus::Handler
  127. void OnPrepareForContextReset() override;
  128. //////////////////////////////////////////////////////////////////////////
  129. // ActionManagerRegistrationNotificationBus overrides ...
  130. void OnActionRegistrationHook() override;
  131. void OnMenuBindingHook() override;
  132. // Context menu handlers.
  133. void ContextMenu_NewEntity();
  134. void ContextMenu_Duplicate();
  135. void ContextMenu_DeleteSelected();
  136. void GetSelectedEntities(AzToolsFramework::EntityIdList& entities);
  137. void GetSelectedOrHighlightedEntities(AzToolsFramework::EntityIdList& entities);
  138. AZStd::string GetDefaultComponentViewportIcon() override
  139. {
  140. return m_defaultComponentViewportIconLocation;
  141. }
  142. AZStd::string GetDefaultComponentEditorIcon() override
  143. {
  144. return m_defaultComponentIconLocation;
  145. }
  146. AZStd::string GetDefaultEntityIcon() override
  147. {
  148. return m_defaultEntityIconLocation;
  149. }
  150. AZStd::string GetComponentEditorIcon(const AZ::Uuid& componentType, const AZ::Component* component) override;
  151. AZStd::string GetComponentTypeEditorIcon(const AZ::Uuid& componentType) override;
  152. AZStd::string GetComponentIconPath(const AZ::Uuid& componentType, AZ::Crc32 componentIconAttrib, const AZ::Component* component) override;
  153. //////////////////////////////////////////////////////////////////////////
  154. // IUndoManagerListener
  155. // Listens for Cry Undo System events.
  156. void UndoStackFlushed() override;
  157. private:
  158. void GoToEntitiesInViewports(const AzToolsFramework::EntityIdList& entityIds);
  159. bool CanGoToEntityOrChildren(const AZ::EntityId& entityId) const;
  160. private:
  161. EditorContextMenuHandler m_contextMenuBottomHandler;
  162. //! Position of the cursor when the context menu is opened inside the 3d viewport.
  163. //! note: The optional will be empty if the context menu was opened outside the 3d viewport.
  164. AZStd::optional<AzFramework::ScreenPoint> m_contextMenuViewPoint;
  165. short m_startedUndoRecordingNestingLevel; //!< Used in OnBegin/EndUndo to ensure we only accept undos we started recording
  166. const AZStd::string m_defaultComponentIconLocation = "Icons/Components/Component_Placeholder.svg";
  167. const AZStd::string m_defaultComponentViewportIconLocation = "Icons/Components/Viewport/Component_Placeholder.svg";
  168. const AZStd::string m_defaultEntityIconLocation = "Icons/Components/Viewport/Transform.svg";
  169. AzToolsFramework::EditorEntityAPI* m_editorEntityAPI = nullptr;
  170. AzToolsFramework::Prefab::PrefabIntegrationManager* m_prefabIntegrationManager = nullptr;
  171. AzToolsFramework::Prefab::PrefabIntegrationInterface* m_prefabIntegrationInterface = nullptr;
  172. AzToolsFramework::ReadOnlyEntityPublicInterface* m_readOnlyEntityPublicInterface = nullptr;
  173. };
  174. //////////////////////////////////////////////////////////////////////////
  175. class CToolsApplicationUndoLink
  176. : public IUndoObject
  177. {
  178. public:
  179. CToolsApplicationUndoLink()
  180. {
  181. }
  182. int GetSize() override
  183. {
  184. return 0;
  185. }
  186. void Undo(bool bUndo = true) override
  187. {
  188. // Always run the undo even if the flag was set to false, that just means that undo wasn't expressly desired, but can be used in cases of canceling the current super undo.
  189. // Restore previous focus after applying the undo.
  190. QPointer<QWidget> w = QApplication::focusWidget();
  191. AzToolsFramework::ToolsApplicationRequestBus::Broadcast(&AzToolsFramework::ToolsApplicationRequestBus::Events::UndoPressed);
  192. // Slice the redo stack if this wasn't due to explicit undo command
  193. if (!bUndo)
  194. {
  195. AzToolsFramework::ToolsApplicationRequestBus::Broadcast(&AzToolsFramework::ToolsApplicationRequestBus::Events::FlushRedo);
  196. }
  197. if (!w.isNull())
  198. {
  199. w->setFocus(Qt::OtherFocusReason);
  200. }
  201. }
  202. void Redo() override
  203. {
  204. // Restore previous focus after applying the undo.
  205. QPointer<QWidget> w = QApplication::focusWidget();
  206. AzToolsFramework::ToolsApplicationRequestBus::Broadcast(&AzToolsFramework::ToolsApplicationRequestBus::Events::RedoPressed);
  207. if (!w.isNull())
  208. {
  209. w->setFocus(Qt::OtherFocusReason);
  210. }
  211. }
  212. };
  213. #endif // CRYINCLUDE_COMPONENTENTITYEDITORPLUGIN_SANDBOXINTEGRATION_H