LyShine.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/ILyShine.h>
  10. #include <LyShine/Bus/UiCursorBus.h>
  11. #include <AzCore/Math/Vector2.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <AzFramework/Input/Events/InputChannelEventListener.h>
  14. #include <AzFramework/Input/Events/InputTextEventListener.h>
  15. #include <Atom/Bootstrap/BootstrapNotificationBus.h>
  16. #include <Atom/RPI.Public/ViewportContextBus.h>
  17. #include <Atom/RPI.Reflect/Image/Image.h>
  18. #include "LyShinePassDataBus.h"
  19. #if !defined(_RELEASE)
  20. #define LYSHINE_INTERNAL_UNIT_TEST
  21. #endif
  22. #if defined(_RELEASE) && defined(LYSHINE_INTERNAL_UNIT_TEST)
  23. #error "Internal unit test enabled on release build! Please disable."
  24. #endif
  25. class CDraw2d;
  26. class UiRenderer;
  27. class UiCanvasManager;
  28. struct IConsoleCmdArgs;
  29. ////////////////////////////////////////////////////////////////////////////////////////////////////
  30. //! CLyShine is the full implementation of the ILyShine interface
  31. class CLyShine
  32. : public ILyShine
  33. , public UiCursorBus::Handler
  34. , public AzFramework::InputChannelEventListener
  35. , public AzFramework::InputTextEventListener
  36. , public AZ::TickBus::Handler
  37. , public AZ::RPI::ViewportContextNotificationBus::Handler
  38. , protected AZ::Render::Bootstrap::NotificationBus::Handler
  39. , protected LyShinePassDataRequestBus::Handler
  40. {
  41. public:
  42. //! Create the LyShine object, the given system pointer is stored internally
  43. CLyShine();
  44. // ILyShine
  45. ~CLyShine() override;
  46. void Release() override;
  47. IDraw2d* GetDraw2d() override;
  48. AZ::EntityId CreateCanvas() override;
  49. AZ::EntityId LoadCanvas(const AZStd::string& assetIdPathname) override;
  50. AZ::EntityId CreateCanvasInEditor(UiEntityContext* entityContext) override;
  51. AZ::EntityId LoadCanvasInEditor(const AZStd::string& assetIdPathname, const AZStd::string& sourceAssetPathname, UiEntityContext* entityContext) override;
  52. AZ::EntityId ReloadCanvasFromXml(const AZStd::string& xmlString, UiEntityContext* entityContext) override;
  53. AZ::EntityId FindCanvasById(LyShine::CanvasId id) override;
  54. AZ::EntityId FindLoadedCanvasByPathName(const AZStd::string& assetIdPathname) override;
  55. void ReleaseCanvas(AZ::EntityId canvas, bool forEditor) override;
  56. void ReleaseCanvasDeferred(AZ::EntityId canvas) override;
  57. ISprite* LoadSprite(const AZStd::string& pathname) override;
  58. ISprite* CreateSprite(const AZ::Data::Asset<AZ::RPI::AttachmentImageAsset>& attachmentImageAsset) override;
  59. bool DoesSpriteTextureAssetExist(const AZStd::string& pathname) override;
  60. AZ::Data::Instance<AZ::RPI::Image> LoadTexture(const AZStd::string& pathname) override;
  61. void PostInit() override;
  62. void SetViewportSize(AZ::Vector2 viewportSize) override;
  63. void Update(float deltaTimeInSeconds) override;
  64. void Render() override;
  65. void ExecuteQueuedEvents() override;
  66. void Reset() override;
  67. void OnLevelUnload() override;
  68. void OnLoadScreenUnloaded() override;
  69. // ~ILyShine
  70. // UiCursorInterface
  71. void IncrementVisibleCounter() override;
  72. void DecrementVisibleCounter() override;
  73. bool IsUiCursorVisible() override;
  74. void SetUiCursor(const char* cursorImagePath) override;
  75. AZ::Vector2 GetUiCursorPosition() override;
  76. void SetUiCursorPosition(const AZ::Vector2&) override;
  77. // ~UiCursorInterface
  78. // InputChannelEventListener
  79. bool OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel) override;
  80. // ~InputChannelEventListener
  81. // InputTextEventListener
  82. bool OnInputTextEventFiltered(const AZStd::string& textUTF8) override;
  83. // ~InputTextEventListener
  84. // TickEvents
  85. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  86. int GetTickOrder() override;
  87. // ~TickEvents
  88. // AZ::RPI::ViewportContextNotificationBus::Handler overrides...
  89. void OnRenderTick() override;
  90. // AZ::Render::Bootstrap::NotificationBus
  91. void OnBootstrapSceneReady(AZ::RPI::Scene* bootstrapScene) override;
  92. // ~AZ::Render::Bootstrap::NotificationBus
  93. // LyShinePassDataRequestBus
  94. LyShine::AttachmentImagesAndDependencies GetRenderTargets() override;
  95. // ~LyShinePassDataRequestBus
  96. // Get the UIRenderer for the game (which is owned by CLyShine). This is not exposed outside the gem.
  97. UiRenderer* GetUiRenderer();
  98. // Get/set the UIRenderer for the Editor (which is owned by CLyShine). This is not exposed outside the gem.
  99. UiRenderer* GetUiRendererForEditor();
  100. void SetUiRendererForEditor(AZStd::shared_ptr<UiRenderer> uiRenderer);
  101. public: // static member functions
  102. #if defined(LYSHINE_INTERNAL_UNIT_TEST)
  103. static void RunUnitTests(IConsoleCmdArgs* cmdArgs);
  104. #endif
  105. private: // member functions
  106. AZ_DISABLE_COPY_MOVE(CLyShine);
  107. void LoadUiCursor();
  108. void RenderUiCursor();
  109. private: // static member functions
  110. #ifndef _RELEASE
  111. static void DebugReportDrawCalls(IConsoleCmdArgs* cmdArgs);
  112. #endif
  113. private: // data
  114. std::unique_ptr<CDraw2d> m_draw2d; // using a pointer rather than an instance to avoid including Draw2d.h
  115. std::unique_ptr<UiRenderer> m_uiRenderer; // using a pointer rather than an instance to avoid including UiRenderer.h
  116. AZStd::shared_ptr<UiRenderer> m_uiRendererForEditor;
  117. std::unique_ptr<UiCanvasManager> m_uiCanvasManager;
  118. AZStd::string m_cursorImagePathToLoad;
  119. AZ::Data::Instance<AZ::RPI::Image> m_uiCursorTexture;
  120. int m_uiCursorVisibleCounter;
  121. bool m_updatingLoadedCanvases = false; // guard against nested updates
  122. // Console variables
  123. #ifndef _RELEASE
  124. DeclareStaticConstIntCVar(CV_ui_DisplayTextureData, 0);
  125. DeclareStaticConstIntCVar(CV_ui_DisplayCanvasData, 0);
  126. DeclareStaticConstIntCVar(CV_ui_DisplayDrawCallData, 0);
  127. DeclareStaticConstIntCVar(CV_ui_DisplayElemBounds, 0);
  128. DeclareStaticConstIntCVar(CV_ui_DisplayElemBoundsCanvasIndex, -1);
  129. #endif
  130. #if defined(LYSHINE_INTERNAL_UNIT_TEST)
  131. DeclareStaticConstIntCVar(CV_ui_RunUnitTestsOnStartup, 0);
  132. #endif
  133. };