UiCanvasManager.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/RTTI/TypeInfo.h>
  10. #include <LyShine/Bus/UiCanvasManagerBus.h>
  11. #include <LyShine/Bus/UiCanvasBus.h>
  12. #include <LyShine/UiEntityContext.h>
  13. #include "LyShinePassDataBus.h"
  14. #include <IFont.h>
  15. class UiCanvasComponent;
  16. namespace AZ
  17. {
  18. class SerializeContext;
  19. }
  20. namespace AzFramework
  21. {
  22. class InputChannel;
  23. }
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////
  25. class UiCanvasManager
  26. : protected UiCanvasManagerBus::Handler
  27. , protected UiCanvasOrderNotificationBus::Handler
  28. , protected UiCanvasEnabledStateNotificationBus::Handler
  29. , protected FontNotificationBus::Handler
  30. , private AzFramework::AssetCatalogEventBus::Handler
  31. {
  32. public: // member functions
  33. //! Constructor, constructed by the LyShine class
  34. UiCanvasManager();
  35. ~UiCanvasManager() override;
  36. public: // member functions
  37. // UiCanvasManagerBus interface implementation
  38. AZ::EntityId CreateCanvas() override;
  39. AZ::EntityId LoadCanvas(const AZStd::string& canvasPathname) override;
  40. void UnloadCanvas(AZ::EntityId canvasEntityId) override;
  41. AZ::EntityId FindLoadedCanvasByPathName(const AZStd::string& canvasPathname, bool loadIfNotFound = false) override;
  42. CanvasEntityList GetLoadedCanvases() override;
  43. void SetLocalUserIdInputFilterForAllCanvases(AzFramework::LocalUserId localUserId) override;
  44. // ~UiCanvasManagerBus
  45. // UiCanvasOrderNotificationBus
  46. void OnCanvasDrawOrderChanged(AZ::EntityId canvasEntityId) override;
  47. // ~UiCanvasOrderNotificationBus
  48. // UiCanvasEnabledStateNotificationBus
  49. void OnCanvasEnabledStateChanged(AZ::EntityId canvasEntityId, bool enabled) override;
  50. // ~UiCanvasEnabledStateNotificationBus
  51. // FontNotifications
  52. void OnFontsReloaded() override;
  53. void OnFontTextureUpdated(IFFont* font) override;
  54. // ~FontNotifications
  55. // AssetCatalogEventBus::Handler
  56. void OnCatalogAssetChanged(const AZ::Data::AssetId& assetId) override;
  57. // ~AssetCatalogEventBus::Handler
  58. AZ::EntityId CreateCanvasInEditor(UiEntityContext* entityContext);
  59. AZ::EntityId LoadCanvasInEditor(const AZStd::string& assetIdPathname, const AZStd::string& sourceAssetPathname, UiEntityContext* entityContext);
  60. AZ::EntityId ReloadCanvasFromXml(const AZStd::string& xmlString, UiEntityContext* entityContext);
  61. void ReleaseCanvas(AZ::EntityId canvas, bool forEditor);
  62. // Wait until canvas processing is completed before deleting the UI canvas to prevent deleting a UI canvas
  63. // from an active entity within that UI canvas, such as unloading a UI canvas from a script canvas that is
  64. // on an element in that UI canvas (Used when UI canvas is loaded in game)
  65. void ReleaseCanvasDeferred(AZ::EntityId canvas);
  66. AZ::EntityId FindCanvasById(LyShine::CanvasId id);
  67. void SetTargetSizeForLoadedCanvases(AZ::Vector2 viewportSize);
  68. void UpdateLoadedCanvases(float deltaTimeInSeconds);
  69. void RenderLoadedCanvases();
  70. void DestroyLoadedCanvases(bool keepCrossLevelCanvases);
  71. void OnLoadScreenUnloaded();
  72. // These functions handle events for all canvases loaded in the game
  73. bool HandleInputEventForLoadedCanvases(const AzFramework::InputChannel& inputChannel);
  74. bool HandleTextEventForLoadedCanvases(const AZStd::string& textUTF8);
  75. // Get the render targets used by all currently loaded UI Canvases
  76. void GetRenderTargets(LyShine::AttachmentImagesAndDependencies& attachmentImagesAndDependencies);
  77. #ifndef _RELEASE
  78. void DebugDisplayCanvasData(int setting) const;
  79. void DebugDisplayDrawCallData() const;
  80. void DebugReportDrawCalls(const AZStd::string& name) const;
  81. void DebugDisplayElemBounds(int canvasIndexFilter) const;
  82. #endif
  83. private: // member functions
  84. AZ_DISABLE_COPY_MOVE(UiCanvasManager);
  85. void SortCanvasesByDrawOrder();
  86. UiCanvasComponent* FindCanvasComponentByPathname(const AZStd::string& name);
  87. UiCanvasComponent* FindEditorCanvasComponentByPathname(const AZStd::string& name);
  88. // Handle input event for all loaded canvases
  89. bool HandleInputEventForLoadedCanvases(const AzFramework::InputChannel::Snapshot& inputSnapshot,
  90. const AZ::Vector2& viewportPos,
  91. AzFramework::ModifierKeyMask activeModifierKeys,
  92. bool isPositional);
  93. // Handle input event for all in world canvases (canvases that render to a texture)
  94. bool HandleInputEventForInWorldCanvases(const AzFramework::InputChannel::Snapshot& inputSnapshot, const AZ::Vector2& viewportPos);
  95. // Generate and handle a mouse position input event for all loaded canvases
  96. void GenerateMousePositionInputEvent();
  97. AZ::EntityId LoadCanvasInternal(const AZStd::string& assetIdPathname, bool forEditor, const AZStd::string& fullSourceAssetPathname, UiEntityContext* entityContext,
  98. const AZ::SliceComponent::EntityIdToEntityIdMap* previousRemapTable = nullptr, AZ::EntityId previousCanvasId = AZ::EntityId());
  99. void QueueCanvasForDeletion(AZ::EntityId canvasEntityId);
  100. void DeleteCanvasesQueuedForDeletion();
  101. #ifndef _RELEASE
  102. AZStd::string DebugGetElementName(AZ::EntityId entityId, int maxLength) const;
  103. #endif
  104. private: // types
  105. typedef std::vector<UiCanvasComponent*> CanvasList; //!< Sorted by draw order
  106. private: // data
  107. CanvasList m_loadedCanvases; // UI Canvases loaded in game
  108. CanvasList m_loadedCanvasesInEditor; // UI Canvases loaded in editor
  109. AZ::Vector2 m_latestViewportSize; // The most recent viewport size
  110. int m_recursionGuardCount = 0; // incremented while updating or doing input handling for canvases
  111. AZStd::vector<AZ::EntityId> m_canvasesQueuedForDeletion;
  112. bool m_fontTextureHasChanged = false;
  113. AzFramework::LocalUserId m_localUserIdInputFilter; // The local user id to filter UI input on
  114. // Indicates whether to generate a mouse position input event on the next canvas update.
  115. // Used to update the canvas' interactable hover states even when the mouse position hasn't changed
  116. bool m_generateMousePositionInputEvent = false;
  117. };