MainWindow.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #pragma once
  9. #if !defined(Q_MOC_RUN)
  10. #include <CrySystemBus.h>
  11. // AZ
  12. #include <AzCore/Component/EntityBus.h>
  13. #include <AzCore/Memory/SystemAllocator.h>
  14. #include <AzCore/PlatformDef.h>
  15. #include <AzCore/std/containers/unordered_map.h>
  16. #include <AzQtComponents/Components/StyledDockWidget.h>
  17. #include <AzToolsFramework/API/EntityCompositionNotificationBus.h>
  18. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  19. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  20. #include <AzToolsFramework/Prefab/PrefabFocusNotificationBus.h>
  21. #include <AzToolsFramework/Prefab/PrefabPublicNotificationBus.h>
  22. #include <AzToolsFramework/UI/PropertyEditor/EntityPropertyEditor.hxx>
  23. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  24. // GraphModel
  25. #include <GraphModel/Integration/EditorMainWindow.h>
  26. // LandscapeCanvas
  27. #include <LandscapeCanvas/LandscapeCanvasBus.h>
  28. #endif
  29. namespace AzToolsFramework
  30. {
  31. class ReadOnlyEntityPublicInterface;
  32. namespace Prefab
  33. {
  34. class PrefabFocusPublicInterface;
  35. class PrefabPublicInterface;
  36. }
  37. }
  38. namespace LandscapeCanvasEditor
  39. {
  40. ////////////////////////////////////////////////////////////////////////
  41. // Temporary classes for using a custom Pinned Inspector as a Node Inspector
  42. // that will use the selected nodes in the graph to drive the Node Inspector
  43. // based on the corresponding Vegetation Entities. These will be removed
  44. // once a generic Node Inspector has been implemented for the base EditorMainWindow.
  45. class CustomEntityPropertyEditor
  46. : public AzToolsFramework::EntityPropertyEditor
  47. {
  48. public:
  49. AZ_CLASS_ALLOCATOR(CustomEntityPropertyEditor, AZ::SystemAllocator);
  50. CustomEntityPropertyEditor(QWidget* parent = nullptr);
  51. protected:
  52. void CloseInspectorWindow() override;
  53. QString GetEntityDetailsLabelText() const override;
  54. };
  55. class CustomNodeInspectorDockWidget
  56. : public AzQtComponents::StyledDockWidget
  57. {
  58. public:
  59. AZ_CLASS_ALLOCATOR(CustomNodeInspectorDockWidget, AZ::SystemAllocator);
  60. CustomNodeInspectorDockWidget(QWidget* parent = nullptr);
  61. CustomEntityPropertyEditor* GetEntityPropertyEditor();
  62. private:
  63. CustomEntityPropertyEditor* m_propertyEditor = nullptr;
  64. };
  65. ////////////////////////////////////////////////////////////////////////
  66. struct LandscapeCanvasConfig
  67. : GraphCanvas::AssetEditorWindowConfig
  68. {
  69. GraphCanvas::GraphCanvasTreeItem* CreateNodePaletteRoot() override;
  70. };
  71. class MainWindow
  72. : public GraphModelIntegration::EditorMainWindow
  73. , protected LandscapeCanvas::LandscapeCanvasRequestBus::Handler
  74. , private AZ::EntitySystemBus::Handler
  75. , private AzToolsFramework::EditorEntityContextNotificationBus::Handler
  76. , private AzToolsFramework::EditorPickModeNotificationBus::Handler
  77. , private AzToolsFramework::EntityCompositionNotificationBus::Handler
  78. , private AzToolsFramework::PropertyEditorEntityChangeNotificationBus::MultiHandler
  79. , private AzToolsFramework::ToolsApplicationNotificationBus::Handler
  80. , private AzToolsFramework::Prefab::PrefabFocusNotificationBus::Handler
  81. , private AzToolsFramework::Prefab::PrefabPublicNotificationBus::Handler
  82. , private CrySystemEventBus::Handler
  83. {
  84. Q_OBJECT
  85. public:
  86. AZ_CLASS_ALLOCATOR(MainWindow, AZ::SystemAllocator)
  87. explicit MainWindow(QWidget* parent = nullptr);
  88. ~MainWindow() override;
  89. private:
  90. GraphModel::GraphContextPtr GetGraphContext() const override;
  91. ////////////////////////////////////////////////////////////////////////
  92. // GraphModelIntegration::GraphControllerNotificationBus::Handler overrides
  93. void OnGraphModelNodeAdded(GraphModel::NodePtr node) override;
  94. void OnGraphModelNodeRemoved(GraphModel::NodePtr node) override;
  95. void PreOnGraphModelNodeRemoved(GraphModel::NodePtr node) override;
  96. void OnGraphModelConnectionAdded(GraphModel::ConnectionPtr connection) override;
  97. void OnGraphModelConnectionRemoved(GraphModel::ConnectionPtr connection) override;
  98. void PreOnGraphModelNodeWrapped(GraphModel::NodePtr wrapperNode, GraphModel::NodePtr node) override;
  99. void OnGraphModelNodeWrapped(GraphModel::NodePtr wrapperNode, GraphModel::NodePtr node) override;
  100. void OnGraphModelGraphModified(GraphModel::NodePtr node) override;
  101. ////////////////////////////////////////////////////////////////////////
  102. ////////////////////////////////////////////////////////////////////////
  103. // GraphModelIntegration::EditorMainWindow overrides ...
  104. void OnEditorOpened(GraphCanvas::EditorDockWidget* dockWidget) override;
  105. void OnEditorClosing(GraphCanvas::EditorDockWidget* dockWidget) override;
  106. QAction* AddFileNewAction(QMenu* menu) override;
  107. QAction* AddFileOpenAction(QMenu* menu) override;
  108. QAction* AddFileSaveAction(QMenu* menu) override;
  109. QAction* AddFileSaveAsAction(QMenu* menu) override;
  110. QMenu* AddEditMenu() override;
  111. QAction* AddEditCutAction(QMenu* menu) override;
  112. QAction* AddEditCopyAction(QMenu* menu) override;
  113. QAction* AddEditPasteAction(QMenu* menu) override;
  114. void HandleWrapperNodeActionWidgetClicked(GraphModel::NodePtr wrapperNode, const QRect& actionWidgetBoundingRect, const QPointF& scenePoint, const QPoint& screenPoint) override;
  115. GraphCanvas::Endpoint CreateNodeForProposal(const AZ::EntityId& connectionId, const GraphCanvas::Endpoint& endpoint, const QPointF& scenePoint, const QPoint& screenPoint) override;
  116. void OnSelectionChanged() override;
  117. void OnEntitiesDeserialized(const GraphCanvas::GraphSerialization& serializationTarget) override;
  118. ////////////////////////////////////////////////////////////////////////
  119. ////////////////////////////////////////////////////////////////////////
  120. // AZ::EntitySystemBus
  121. void OnEntityActivated(const AZ::EntityId& entityId) override;
  122. void OnEntityNameChanged(const AZ::EntityId& entityId, const AZStd::string& name) override;
  123. ////////////////////////////////////////////////////////////////////////
  124. bool HandleGraphOpened(const AZ::EntityId& rootEntityId, const GraphCanvas::DockWidgetId& dockWidgetId);
  125. ////////////////////////////////////////////////////////////////////////
  126. // AssetEditorRequestBus::Handler overrides
  127. GraphCanvas::ContextMenuAction::SceneReaction ShowNodeContextMenu(const AZ::EntityId& nodeId, const QPoint& screenPoint, const QPointF& scenePoint) override;
  128. ////////////////////////////////////////////////////////////////////////
  129. ////////////////////////////////////////////////////////////////////////
  130. // LandscapeCanvas::LandscapeCanvasRequestBus::Handler overrides
  131. GraphCanvas::GraphId OnGraphEntity(const AZ::EntityId& entityId) override;
  132. GraphModel::NodePtr GetNodeMatchingEntityInGraph(const GraphCanvas::GraphId& graphId, const AZ::EntityId& entityId) override;
  133. GraphModel::NodePtr GetNodeMatchingEntityComponentInGraph(const GraphCanvas::GraphId& graphId, const AZ::EntityComponentIdPair& entityComponentId) override;
  134. GraphModel::NodePtrList GetAllNodesMatchingEntity(const AZ::EntityId& entityId) override;
  135. GraphModel::NodePtrList GetAllNodesMatchingEntityComponent(const AZ::EntityComponentIdPair& entityComponentId) override;
  136. ////////////////////////////////////////////////////////////////////////
  137. GraphModel::NodePtrList GetAllNodesMatchingEntityInGraph(const GraphCanvas::GraphId& graphId, const AZ::EntityId& entityId);
  138. GraphModel::NodePtrList GetAllNodesMatchingEntityComponentInGraph(
  139. const GraphCanvas::GraphId& graphId, const AZ::EntityComponentIdPair& entityComponentId);
  140. ////////////////////////////////////////////////////////////////////////
  141. // GraphCanvas::AssetEditorMainWindow overrides
  142. bool ConfigureDefaultLayout() override;
  143. ////////////////////////////////////////////////////////////////////////
  144. ////////////////////////////////////////////////////////////////////////
  145. // AzToolsFramework::EditorEntityContextNotificationBus overrides
  146. void OnEditorEntityCreated(const AZ::EntityId& entityId) override;
  147. void OnEditorEntityDeleted(const AZ::EntityId& entityId) override;
  148. ////////////////////////////////////////////////////////////////////////
  149. void HandleEditorEntityCreated(const AZ::EntityId& entityId, GraphCanvas::GraphId graphId = GraphCanvas::GraphId());
  150. void QueuedEditorEntityDeleted(const AZ::EntityId& entityId);
  151. void HandleEditorEntityDeleted(const AZ::EntityId& entityId);
  152. ////////////////////////////////////////////////////////////////////////
  153. // AzToolsFramework::EditorPickModeNotificationBus overrides
  154. void OnEntityPickModeStarted() override;
  155. void OnEntityPickModeStopped() override;
  156. ////////////////////////////////////////////////////////////////////////
  157. ////////////////////////////////////////////////////////////////////////
  158. // AzToolsFramework::EntityCompositionNotificationBus overrides
  159. void OnEntityComponentAdded(const AZ::EntityId& entityId, const AZ::ComponentId& componentId) override;
  160. void OnEntityComponentRemoved(const AZ::EntityId& entityId, const AZ::ComponentId& componentId) override;
  161. void OnEntityComponentEnabled(const AZ::EntityId& entityId, const AZ::ComponentId& componentId) override;
  162. void OnEntityComponentDisabled(const AZ::EntityId& entityId, const AZ::ComponentId& componentId) override;
  163. ////////////////////////////////////////////////////////////////////////
  164. ////////////////////////////////////////////////////////////////////////
  165. // AzToolsFramework::PropertyEditorEntityChangeNotificationBus overrides
  166. void OnEntityComponentPropertyChanged(AZ::ComponentId changedComponentId) override;
  167. ////////////////////////////////////////////////////////////////////////
  168. ////////////////////////////////////////////////////////////////////////
  169. // AzToolsFramework::ToolsApplicationNotificationBus overrides
  170. void EntityParentChanged(AZ::EntityId entityId, AZ::EntityId newParentId, AZ::EntityId oldParentId) override;
  171. ////////////////////////////////////////////////////////////////////////
  172. //! PrefabFocusNotificationBus overrides
  173. void OnPrefabFocusChanged(AZ::EntityId previousContainerEntityId, AZ::EntityId newContainerEntityId) override;
  174. //! PrefabPublicNotificationBus overrides
  175. void OnPrefabInstancePropagationBegin() override;
  176. void OnPrefabInstancePropagationEnd() override;
  177. ////////////////////////////////////////////////////////////////////////
  178. // CrySystemEventBus overrides
  179. void OnCryEditorEndCreate() override;
  180. void OnCryEditorEndLoad() override;
  181. void OnCryEditorCloseScene() override;
  182. void OnCryEditorSceneClosed() override;
  183. ////////////////////////////////////////////////////////////////////////
  184. ////////////////////////////////////////////////////////////////////////
  185. // GraphCanvas::AssetEditorNotificationBus overrides
  186. void PostOnActiveGraphChanged() override;
  187. ////////////////////////////////////////////////////////////////////////
  188. void GetChildrenTree(const AZ::EntityId& rootEntityId, AzToolsFramework::EntityIdList& childrenList);
  189. QString GetPropertyPathForSlot(GraphModel::SlotPtr slot, GraphModel::DataType::Enum dataType, int elementIndex = 0);
  190. void UpdateConnectionData(GraphModel::ConnectionPtr connection, bool added);
  191. void HandleSetImageAssetPath(const AZ::EntityId& sourceEntityId, const AZ::EntityId& targetEntityId);
  192. AZ::u32 GetWrappedNodeLayoutOrder(GraphModel::NodePtr node);
  193. AZ::EntityId GetRootEntityIdForGraphId(const GraphCanvas::GraphId& graphId);
  194. AZ::ComponentId AddComponentTypeIdToEntity(
  195. const AZ::EntityId& entityId,
  196. AZ::TypeId componentToAddTypeId,
  197. AZStd::span<const AZ::ComponentServiceType> optionalServices = {});
  198. void AddComponentForNode(GraphModel::NodePtr node, const AZ::EntityId& entityId);
  199. void HandleNodeCreated(GraphModel::NodePtr node);
  200. void HandleNodeAdded(GraphModel::NodePtr node);
  201. using EntityIdNodeMap = AZStd::unordered_map<AZ::EntityId, GraphModel::NodePtr>;
  202. using EntityIdNodeMaps = AZStd::vector<EntityIdNodeMap>;
  203. enum EntityIdNodeMapEnum
  204. {
  205. Invalid = -1,
  206. Shapes = 0,
  207. Gradients,
  208. WrapperNodes,
  209. Count
  210. };
  211. using EntityComponentCallback = AZStd::function<void(const AZ::EntityId& entityId, AZ::Component* component, bool isDisabled)>;
  212. using NodeSlotPair = AZStd::pair<GraphModel::NodePtr, GraphModel::SlotPtr>;
  213. using ConnectionsList = AZStd::vector<AZStd::pair<NodeSlotPair, NodeSlotPair>>;
  214. void UpdateEntityIdNodeMap(GraphCanvas::GraphId, GraphModel::NodePtr node);
  215. EntityIdNodeMap* GetEntityIdNodeMap(GraphCanvas::GraphId, GraphModel::NodePtr node);
  216. void ParseNodeConnections(GraphCanvas::GraphId graphId, GraphModel::NodePtr node, ConnectionsList& connections);
  217. void UpdateConnections(GraphModel::NodePtr node);
  218. GraphCanvas::GraphId FindGraphContainingEntity(const AZ::EntityId& entityId);
  219. void EnumerateEntityComponentTree(const AZ::EntityId& rootEntityId, EntityComponentCallback callback);
  220. void InitialEntityGraph(const AZ::EntityId& entityId, GraphCanvas::GraphId graphId);
  221. GraphModel::NodePtrList RefreshEntityComponentNodes(const AZ::EntityId& targetEntityId, GraphCanvas::GraphId graphId);
  222. void PlaceNewNode(GraphCanvas::GraphId graphId, LandscapeCanvas::BaseNode::BaseNodePtr node);
  223. int GetInboundDataSlotIndex(GraphModel::NodePtr node, GraphModel::DataTypePtr dataType, GraphModel::SlotPtr targetSlot);
  224. void HandleImageAssetSlot(GraphModel::NodePtr targetNode, const EntityIdNodeMap& gradientNodeMap, ConnectionsList& connections);
  225. void HandleDeserializedNodes();
  226. //! Determines whether or not we should allow the user to interact with the graph
  227. //! This should be disabled when there is no level currently loaded
  228. void UpdateGraphEnabled();
  229. //! Return the input data slot on the node that matches the specified data type for the specified index.
  230. //! - If this is a normal slot, it will just return the appropriate slot that is found.
  231. //! - If this is an extendable slot, then slot(s) may need to be added in order to satisfy the requested index.
  232. GraphModel::SlotPtr EnsureInboundDataSlotWithIndex(GraphCanvas::GraphId graphId, GraphModel::NodePtr node, GraphModel::DataTypePtr dataType, int index);
  233. AZ::SerializeContext* m_serializeContext = nullptr;
  234. static AzFramework::EntityContextId s_editorEntityContextId;
  235. AzToolsFramework::Prefab::PrefabFocusPublicInterface* m_prefabFocusPublicInterface = nullptr;
  236. AzToolsFramework::Prefab::PrefabPublicInterface* m_prefabPublicInterface = nullptr;
  237. AzToolsFramework::ReadOnlyEntityPublicInterface* m_readOnlyEntityPublicInterface = nullptr;
  238. bool m_ignoreGraphUpdates = false;
  239. bool m_prefabPropagationInProgress = false;
  240. bool m_inObjectPickMode = false;
  241. using DeletedNodePositionsMap = AZStd::unordered_map<AZ::EntityComponentIdPair, AZ::Vector2>;
  242. AZStd::unordered_map<GraphCanvas::GraphId, DeletedNodePositionsMap> m_deletedNodePositions;
  243. GraphModel::NodePtrList m_addedWrappedNodes;
  244. GraphModel::NodePtrList m_deletedWrappedNodes;
  245. GraphModel::NodePtrList m_deserializedNodes;
  246. AzToolsFramework::EntityIdList m_queuedEntityDeletes;
  247. AzToolsFramework::EntityIdList m_queuedEntityRefresh;
  248. AzToolsFramework::EntityIdList m_ignoreEntityComponentPropertyChanges;
  249. /// Keep track of the dock widget for the graph that represents the Vegetation Entity
  250. AZStd::unordered_map<AZ::EntityId, GraphCanvas::DockWidgetId> m_dockWidgetsByEntity;
  251. /// Keep track of the EntityId/Node mappings per graph for performance reasons so that we
  252. /// don't have to parse through all the nodes in a graph to find right one when connecting
  253. /// slots based on the EntityId fields in the component properties. The mappings are tracked
  254. /// by type as well for faster lookup since the slot data types are separated (shape, gradient, area).
  255. AZStd::unordered_map<GraphCanvas::GraphId, EntityIdNodeMaps> m_entityIdNodeMapsByGraph;
  256. CustomNodeInspectorDockWidget* m_customNodeInspector = nullptr;
  257. QAction* m_fileNewAction = nullptr;
  258. };
  259. }