UiEditorEntityContext.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include <AzCore/Math/Uuid.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/ComponentApplicationBus.h>
  12. #include <AzCore/Asset/AssetCommon.h>
  13. #include <AzCore/Slice/SliceComponent.h>
  14. #include <AzFramework/Entity/EntityContext.h>
  15. #include <AzFramework/Entity/SliceEntityOwnershipService.h>
  16. #include <AzFramework/Asset/AssetCatalogBus.h>
  17. #include <AzToolsFramework/Entity/EditorEntityContextPickingBus.h>
  18. #include <Cry_Vector2.h>
  19. #include <LyShine/UiEntityContext.h>
  20. #include "UiEditorEntityContextBus.h"
  21. namespace AZ
  22. {
  23. class SerializeContext;
  24. }
  25. namespace AzFramework
  26. {
  27. class EntityContext;
  28. }
  29. class EditorWindow;
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////
  31. //! The UIEditorEntityContext extends the UiEditorContext to add functionality only needed when
  32. //! a UI canvas is loaded in the UI Editor.
  33. class UiEditorEntityContext
  34. : public UiEntityContext
  35. , public AZ::Data::AssetBus::MultiHandler
  36. , private UiEditorEntityContextRequestBus::Handler
  37. , private AzToolsFramework::EditorEntityContextPickingRequestBus::Handler
  38. , private AzFramework::AssetCatalogEventBus::Handler
  39. , private AzFramework::SliceInstantiationResultBus::MultiHandler
  40. {
  41. public: // member functions
  42. UiEditorEntityContext(EditorWindow* editorWindow);
  43. ~UiEditorEntityContext() override;
  44. bool HandleLoadedRootSliceEntity(AZ::Entity* rootEntity, bool remapIds, AZ::SliceComponent::EntityIdToEntityIdMap* idRemapTable = nullptr);
  45. // UiEntityContext
  46. void InitUiContext() override;
  47. void DestroyUiContext() override;
  48. bool SaveToStreamForGame(AZ::IO::GenericStream& stream, AZ::DataStream::StreamType streamType) override;
  49. bool SaveCanvasEntityToStreamForGame(AZ::Entity* canvasEntity, AZ::IO::GenericStream& stream, AZ::DataStream::StreamType streamType) override;
  50. // ~UiEntityContext
  51. // UiEntityContextRequestBus
  52. AZ::SliceComponent* GetUiRootSlice() override;
  53. AZ::Entity* CreateUiEntity(const char* name) override;
  54. void AddUiEntity(AZ::Entity* entity) override;
  55. void AddUiEntities(const AzFramework::EntityList& entities) override;
  56. bool CloneUiEntities(const AZStd::vector<AZ::EntityId>& sourceEntities, AzFramework::EntityList& resultEntities) override;
  57. bool DestroyUiEntity(AZ::EntityId entityId) override;
  58. // ~UiEntityContextRequestBus
  59. // EditorEntityContextPickingRequestBus
  60. bool SupportsViewportEntityIdPicking() override;
  61. // ~EditorEntityContextPickingRequestBus
  62. // UiEditorEntityContextRequestBus
  63. AZ::SliceComponent::SliceInstanceAddress CloneEditorSliceInstance(AZ::SliceComponent::SliceInstanceAddress sourceInstance) override;
  64. AzFramework::SliceInstantiationTicket InstantiateEditorSlice(const AZ::Data::Asset<AZ::Data::AssetData>& sliceAsset, AZ::Vector2 viewportPosition) override;
  65. AzFramework::SliceInstantiationTicket InstantiateEditorSliceAtChildIndex(const AZ::Data::Asset<AZ::Data::AssetData>& sliceAsset,
  66. AZ::Vector2 viewportPosition,
  67. int childIndex) override;
  68. void RestoreSliceEntity(AZ::Entity* entity, const AZ::SliceComponent::EntityRestoreInfo& info) override;
  69. void QueueSliceReplacement(const char* targetPath,
  70. const AZStd::unordered_map<AZ::EntityId, AZ::EntityId>& selectedToAssetMap,
  71. const AZStd::unordered_set<AZ::EntityId>& entitiesInSelection,
  72. AZ::Entity* commonParent, AZ::Entity* insertBefore) override;
  73. void DeleteElements(AzToolsFramework::EntityIdList elements) override;
  74. bool HasPendingRequests() override;
  75. bool IsInstantiatingSlices() override;
  76. void DetachSliceEntities(const AzToolsFramework::EntityIdList& entities) override;
  77. // ~UiEditorEntityContextRequestBus
  78. // AzFramework::SliceInstantiationResultBus
  79. void OnSlicePreInstantiate(const AZ::Data::AssetId& sliceAssetId, const AZ::SliceComponent::SliceInstanceAddress& sliceAddress) override;
  80. void OnSliceInstantiated(const AZ::Data::AssetId& sliceAssetId, const AZ::SliceComponent::SliceInstanceAddress& sliceAddress) override;
  81. void OnSliceInstantiationFailed(const AZ::Data::AssetId& sliceAssetId) override;
  82. // ~AzFramework::SliceInstantiationResultBus
  83. // AssetCatalogEventBus::Handler
  84. void OnCatalogAssetAdded(const AZ::Data::AssetId& assetId) override;
  85. // ~AssetCatalogEventBus::Handler
  86. // EntityContextRequestBus
  87. void ResetContext() override;
  88. // ~EntityContextRequestBus
  89. AZStd::string GetErrorMessage() const { return m_errorMessage; }
  90. protected: // types
  91. struct InstantiatingEditorSliceParams
  92. {
  93. InstantiatingEditorSliceParams(const AZ::Vector2& viewportPosition, int childIndex = -1)
  94. {
  95. m_viewportPosition = viewportPosition;
  96. m_childIndex = childIndex;
  97. }
  98. AZ::Vector2 m_viewportPosition;
  99. int m_childIndex;
  100. };
  101. protected: // member functions
  102. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  103. void OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  104. void OnContextEntitiesAdded(const AzFramework::EntityList& entities) override;
  105. // Used to validate that the entities in an instantiated slice are valid entities for this context
  106. bool ValidateEntitiesAreValidForContext(const AzFramework::EntityList& entities) override;
  107. void SetupUiEntity(AZ::Entity* entity);
  108. void InitializeEntities(const AzFramework::EntityList& entities);
  109. protected: // data
  110. using InstantiatingSlicePair = AZStd::pair<AZ::Data::Asset<AZ::Data::AssetData>, InstantiatingEditorSliceParams>;
  111. AZStd::vector<InstantiatingSlicePair> m_instantiatingSlices;
  112. private: // types
  113. //! Tracks a queued slice replacement, which is a deferred operation.
  114. //! If the asset has not yet been processed (a new asset), we need
  115. //! to defer before attempting a load.
  116. struct QueuedSliceReplacement
  117. {
  118. ~QueuedSliceReplacement() = default;
  119. QueuedSliceReplacement() = default;
  120. QueuedSliceReplacement(const QueuedSliceReplacement&) = delete;
  121. QueuedSliceReplacement& operator=(const QueuedSliceReplacement&) = delete;
  122. void Setup(const char* path,
  123. const AZStd::unordered_map<AZ::EntityId, AZ::EntityId>& selectedToAssetMap,
  124. const AZStd::unordered_set<AZ::EntityId>& entitiesInSelection,
  125. AZ::Entity* commonParent, AZ::Entity* insertBefore)
  126. {
  127. m_path = path;
  128. m_selectedToAssetMap = selectedToAssetMap;
  129. m_entitiesInSelection.clear();
  130. m_entitiesInSelection.insert(entitiesInSelection.begin(), entitiesInSelection.end());
  131. m_commonParent = commonParent;
  132. m_insertBefore = insertBefore;
  133. }
  134. bool IsValid() const;
  135. void Reset();
  136. void Finalize(const AZ::SliceComponent::SliceInstanceAddress& instanceAddress, EditorWindow* editorWindow);
  137. AZStd::string m_path;
  138. AZStd::unordered_map<AZ::EntityId, AZ::EntityId> m_selectedToAssetMap;
  139. AZStd::unordered_set<AZ::EntityId> m_entitiesInSelection;
  140. AZ::Entity* m_commonParent;
  141. AZ::Entity* m_insertBefore;
  142. AzFramework::SliceInstantiationTicket m_ticket;
  143. };
  144. private: // member functions
  145. void GetTopLevelEntities(const AZStd::unordered_set<AZ::EntityId>& entities, AZStd::unordered_set<AZ::EntityId>& topLevelEntities);
  146. private: // data
  147. EditorWindow* m_editorWindow;
  148. //! List of selected entities prior to entering game.
  149. AZStd::vector<AZ::EntityId> m_selectedBeforeStartingGame;
  150. QueuedSliceReplacement m_queuedSliceReplacement;
  151. //! Slice entity restore requests, which can be deferred if asset wasn't loaded at request time.
  152. struct SliceEntityRestoreRequest
  153. {
  154. AZ::Entity* m_entity;
  155. AZ::SliceComponent::EntityRestoreInfo m_restoreInfo;
  156. AZ::Data::Asset<AZ::Data::AssetData> m_asset;
  157. };
  158. AZStd::vector<SliceEntityRestoreRequest> m_queuedSliceEntityRestores;
  159. AZ::ComponentTypeList m_requiredEditorComponentTypes;
  160. AZStd::string m_errorMessage;
  161. AZ::Data::AssetId m_rootAssetId;
  162. };