UiGameEntityContext.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <LyShine/UiEntityContext.h>
  10. #include <LyShine/Bus/UiGameEntityContextBus.h>
  11. #include <AzFramework/Entity/SliceEntityOwnershipService.h>
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. //! The UiGameEntityContext is used for a canvas that is loaded in game as oppoed to being
  14. //! open for editing
  15. class UiGameEntityContext
  16. : public UiEntityContext
  17. , public UiGameEntityContextBus::Handler
  18. , private AzFramework::SliceInstantiationResultBus::MultiHandler
  19. {
  20. public: // member functions
  21. UiGameEntityContext(AZ::EntityId canvasEntityId = AZ::EntityId());
  22. ~UiGameEntityContext() override;
  23. bool HandleLoadedRootSliceEntity(AZ::Entity* rootEntity, bool remapIds,
  24. AZ::SliceComponent::EntityIdToEntityIdMap* idRemapTable = nullptr);
  25. // EntityContext
  26. bool DestroyEntity(AZ::Entity* entity) override;
  27. // ~EntityContext
  28. // UiEntityContext
  29. void InitUiContext() override;
  30. void DestroyUiContext() override;
  31. bool SaveToStreamForGame(AZ::IO::GenericStream& stream, AZ::DataStream::StreamType streamType) override;
  32. bool SaveCanvasEntityToStreamForGame(AZ::Entity* canvasEntity, AZ::IO::GenericStream& stream, AZ::DataStream::StreamType streamType) override;
  33. // ~UiEntityContext
  34. // UiEntityContextRequestBus
  35. AZ::Entity* CreateUiEntity(const char* name) override;
  36. void AddUiEntity(AZ::Entity* entity) override;
  37. void AddUiEntities(const AzFramework::EntityList& entities) override;
  38. bool CloneUiEntities(const AZStd::vector<AZ::EntityId>& sourceEntities, AzFramework::EntityList& resultEntities) override;
  39. bool DestroyUiEntity(AZ::EntityId entityId) override;
  40. // ~UiEntityContextRequestBus
  41. // UiGameEntityContextBus
  42. AzFramework::SliceInstantiationTicket InstantiateDynamicSlice(
  43. const AZ::Data::Asset<AZ::Data::AssetData>& sliceAsset, const AZ::Vector2& position, bool isViewportPostion,
  44. AZ::Entity* parent, const AZ::IdUtils::Remapper<AZ::EntityId>::IdMapper& customIdMapper) override;
  45. // ~UiGameEntityContextBus
  46. // SliceInstantiationResultBus
  47. void OnSlicePreInstantiate(const AZ::Data::AssetId& sliceAssetId, const AZ::SliceComponent::SliceInstanceAddress& instance) override;
  48. void OnSliceInstantiated(const AZ::Data::AssetId& sliceAssetId, const AZ::SliceComponent::SliceInstanceAddress& instance) override;
  49. void OnSliceInstantiationFailed(const AZ::Data::AssetId& sliceAssetId) override;
  50. // ~SliceInstantiationResultBus
  51. void SetCanvasEntity(AZ::EntityId canvasEntityId) { m_canvasEntityId = canvasEntityId; }
  52. protected: // member functions
  53. void OnContextEntitiesAdded(const AzFramework::EntityList& entities) override;
  54. void InitializeEntities(const AzFramework::EntityList& entities);
  55. // Used to validate that the entities in an instantiated slice are valid entities for this context
  56. bool ValidateEntitiesAreValidForContext(const AzFramework::EntityList& entities) override;
  57. protected: // data
  58. struct InstantiatingDynamicSlice
  59. {
  60. InstantiatingDynamicSlice(const AZ::Data::Asset<AZ::Data::AssetData>& asset,
  61. const AZ::Vector2& position, bool isViewportPosition, AZ::Entity* parent)
  62. : m_asset(asset)
  63. , m_position(position)
  64. , m_isViewportPosition(isViewportPosition)
  65. , m_parent(parent) {}
  66. AZ::Data::Asset<AZ::Data::AssetData> m_asset;
  67. AZ::Vector2 m_position;
  68. bool m_isViewportPosition;
  69. AZ::Entity* m_parent;
  70. };
  71. AZStd::unordered_map<AzFramework::SliceInstantiationTicket, InstantiatingDynamicSlice> m_instantiatingDynamicSlices;
  72. AZ::EntityId m_canvasEntityId;
  73. };