3
0

LyShineLoadScreen.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include "LyShineLoadScreen.h"
  9. #if AZ_LOADSCREENCOMPONENT_ENABLED
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Component/ComponentApplicationBus.h>
  12. #include <AzCore/Interface/Interface.h>
  13. #include <LyShine/Bus/UiCanvasBus.h>
  14. #include <LyShine/Animation/IUiAnimation.h>
  15. namespace LyShine
  16. {
  17. void LyShineLoadScreenComponent::Reflect(AZ::ReflectContext* context)
  18. {
  19. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  20. {
  21. serialize->Class<LyShineLoadScreenComponent, AZ::Component>()
  22. ->Version(0)
  23. ;
  24. }
  25. }
  26. void LyShineLoadScreenComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  27. {
  28. provided.emplace_back(AZ_CRC("LyShineLoadScreenService", 0xbb5eab17));
  29. }
  30. void LyShineLoadScreenComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  31. {
  32. incompatible.emplace_back(AZ_CRC("LyShineLoadScreenService", 0xbb5eab17));
  33. }
  34. void LyShineLoadScreenComponent::Init()
  35. {
  36. }
  37. void LyShineLoadScreenComponent::Activate()
  38. {
  39. LoadScreenNotificationBus::Handler::BusConnect();
  40. }
  41. void LyShineLoadScreenComponent::Deactivate()
  42. {
  43. LoadScreenUpdateNotificationBus::Handler::BusDisconnect();
  44. LoadScreenNotificationBus::Handler::BusDisconnect();
  45. }
  46. bool LyShineLoadScreenComponent::NotifyGameLoadStart(bool usingLoadingThread)
  47. {
  48. // LyShine does not support the loading thread yet.
  49. if (usingLoadingThread)
  50. {
  51. return false;
  52. }
  53. //TODO: gEnv->pRenderer is always null, fix the logic below
  54. AZ_ErrorOnce(AZ::Debug::Trace::GetDefaultSystemWindow(), false, "NotifyGameLoadStart needs to be removed/ported to use Atom");
  55. return false;
  56. #if 0
  57. if (!gEnv || gEnv->pRenderer || !AZ::Interface<ILyShine>::Get())
  58. {
  59. return false;
  60. }
  61. AZ_Assert(!m_isPlaying, "LyShineLoadScreen was somehow started before the engine loaded.");
  62. if (m_isPlaying)
  63. {
  64. return false;
  65. }
  66. if (!m_gameCanvasEntityId.IsValid())
  67. {
  68. // Load canvas.
  69. m_gameCanvasEntityId = loadFromCfg("game_load_screen_uicanvas_path", "game_load_screen_sequence_to_auto_play");
  70. }
  71. m_isPlaying = m_gameCanvasEntityId.IsValid();
  72. if (m_isPlaying)
  73. {
  74. LoadScreenUpdateNotificationBus::Handler::BusConnect();
  75. }
  76. return m_isPlaying;
  77. #endif
  78. }
  79. bool LyShineLoadScreenComponent::NotifyLevelLoadStart(bool usingLoadingThread)
  80. {
  81. // LyShine does not support the loading thread yet.
  82. if (usingLoadingThread)
  83. {
  84. return false;
  85. }
  86. AZ_ErrorOnce(AZ::Debug::Trace::GetDefaultSystemWindow(), false, "NotifyLevelLoadStart needs to be removed/ported to use Atom");
  87. return false;
  88. //TODO: gEnv->pRenderer is always null, fix the logic below
  89. #if 0
  90. if (!gEnv || gEnv->pRenderer || !AZ::Interface<ILyShine>::Get())
  91. {
  92. return false;
  93. }
  94. AZ_Assert(!m_isPlaying, "LyShineLoadScreen was not stopped before another level load started.");
  95. AZ_Assert(!m_gameCanvasEntityId.IsValid(), "LyShineLoadScreen game load canvas was not unloaded before a level load started.");
  96. if (m_isPlaying || m_gameCanvasEntityId.IsValid())
  97. {
  98. return false;
  99. }
  100. if (!m_levelCanvasEntityId.IsValid())
  101. {
  102. // Load canvas.
  103. m_levelCanvasEntityId = loadFromCfg("level_load_screen_uicanvas_path", "level_load_screen_sequence_to_auto_play");
  104. }
  105. m_isPlaying = m_levelCanvasEntityId.IsValid();
  106. if (m_isPlaying)
  107. {
  108. LoadScreenUpdateNotificationBus::Handler::BusConnect();
  109. }
  110. return m_isPlaying;
  111. #endif
  112. }
  113. void LyShineLoadScreenComponent::NotifyLoadEnd()
  114. {
  115. Reset();
  116. }
  117. void LyShineLoadScreenComponent::UpdateAndRender([[maybe_unused]] float deltaTimeInSeconds)
  118. {
  119. AZ_Assert(m_isPlaying, "LyShineLoadScreenComponent should not be connected to LoadScreenUpdateNotificationBus while not playing");
  120. AZ_ErrorOnce(AZ::Debug::Trace::GetDefaultSystemWindow(), m_isPlaying && AZ::Interface<ILyShine>::Get(), "UpdateAndRender needs to be removed/ported to use Atom");
  121. //TODO: gEnv->pRenderer is always null, fix the logic below
  122. #if 0
  123. if (m_isPlaying && gEnv && AZ::Interface<ILyShine>::Get() && gEnv->pRenderer)
  124. {
  125. AZ_Assert(GetCurrentThreadId() == gEnv->mMainThreadId, "UpdateAndRender should only be called from the main thread");
  126. // update the animation system
  127. AZ::Interface<ILyShine>::Get()->Update(deltaTimeInSeconds);
  128. // Render.
  129. gEnv->pRenderer->SetViewport(0, 0, gEnv->pRenderer->GetOverlayWidth(), gEnv->pRenderer->GetOverlayHeight());
  130. gEnv->pRenderer->BeginFrame();
  131. AZ::Interface<ILyShine>::Get()->Render();
  132. gEnv->pRenderer->EndFrame();
  133. }
  134. #endif
  135. }
  136. void LyShineLoadScreenComponent::LoadThreadUpdate([[maybe_unused]] float deltaTimeInSeconds)
  137. {
  138. AZ_Assert(false, "LyShine does not support running on the loading thread yet.")
  139. }
  140. void LyShineLoadScreenComponent::LoadThreadRender()
  141. {
  142. AZ_Assert(false, "LyShine does not support running on the loading thread yet.")
  143. }
  144. void LyShineLoadScreenComponent::Reset()
  145. {
  146. LoadScreenUpdateNotificationBus::Handler::BusDisconnect();
  147. m_isPlaying = false;
  148. if (AZ::Interface<ILyShine>::Get())
  149. {
  150. AZ::Entity* canvasEntity = nullptr;
  151. // Release the game canvas.
  152. if (m_gameCanvasEntityId.IsValid())
  153. {
  154. AZ::ComponentApplicationBus::BroadcastResult(
  155. canvasEntity, &AZ::ComponentApplicationBus::Events::FindEntity, m_gameCanvasEntityId);
  156. if (canvasEntity)
  157. {
  158. AZ::Interface<ILyShine>::Get()->ReleaseCanvas(m_gameCanvasEntityId, false);
  159. AZ::Interface<ILyShine>::Get()->OnLoadScreenUnloaded();
  160. }
  161. }
  162. // Release the level canvas.
  163. if (m_levelCanvasEntityId.IsValid())
  164. {
  165. AZ::ComponentApplicationBus::BroadcastResult(
  166. canvasEntity, &AZ::ComponentApplicationBus::Events::FindEntity, m_levelCanvasEntityId);
  167. if (canvasEntity)
  168. {
  169. AZ::Interface<ILyShine>::Get()->ReleaseCanvas(m_levelCanvasEntityId, false);
  170. AZ::Interface<ILyShine>::Get()->OnLoadScreenUnloaded();
  171. }
  172. }
  173. }
  174. m_gameCanvasEntityId.SetInvalid();
  175. m_levelCanvasEntityId.SetInvalid();
  176. // Reset CVars so they're not carried over to other levels
  177. auto ClearCvar = [](const char* cvarName)
  178. {
  179. if (gEnv && gEnv->pConsole)
  180. {
  181. if (ICVar* var = gEnv->pConsole->GetCVar(cvarName))
  182. {
  183. var->Set("");
  184. }
  185. }
  186. };
  187. ClearCvar("level_load_screen_uicanvas_path");
  188. ClearCvar("level_load_screen_sequence_to_auto_play");
  189. }
  190. AZ::EntityId LyShineLoadScreenComponent::loadFromCfg(const char* pathVarName, const char* autoPlayVarName)
  191. {
  192. ICVar* pathVar = gEnv->pConsole->GetCVar(pathVarName);
  193. AZStd::string path = pathVar ? pathVar->GetString() : "";
  194. if (path.empty())
  195. {
  196. // No canvas specified.
  197. Reset();
  198. return AZ::EntityId();
  199. }
  200. AZ::EntityId canvasId = AZ::Interface<ILyShine>::Get()->LoadCanvas(path);
  201. AZ_Warning("LoadScreenComponent", canvasId.IsValid(), "Can't load canvas: %s", path.c_str());
  202. if (!canvasId.IsValid())
  203. {
  204. // Error loading canvas.
  205. Reset();
  206. return AZ::EntityId();
  207. }
  208. UiCanvasBus::Event(canvasId, &UiCanvasBus::Events::SetKeepLoadedOnLevelUnload, true);
  209. // Set the load screen draw order so it renders in front of other canvases that may load during the level load
  210. UiCanvasBus::Event(canvasId, &UiCanvasBus::Events::SetDrawOrder, std::numeric_limits<int>::max());
  211. ICVar* autoPlayVar = gEnv->pConsole->GetCVar(autoPlayVarName);
  212. AZStd::string sequence = autoPlayVar ? autoPlayVar->GetString() : "";
  213. if (sequence.empty())
  214. {
  215. // Nothing to auto-play.
  216. return canvasId;
  217. }
  218. IUiAnimationSystem* animSystem = nullptr;
  219. UiCanvasBus::EventResult(animSystem, canvasId, &UiCanvasBus::Events::GetAnimationSystem);
  220. if (!animSystem)
  221. {
  222. // Nothing can be auto-played.
  223. return canvasId;
  224. }
  225. animSystem->PlaySequence(sequence.c_str(), nullptr, false, false);
  226. return canvasId;
  227. }
  228. } // namespace LyShine
  229. #endif // AZ_LOADSCREENCOMPONENT_ENABLED