LyShineMessagePopup.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <AzCore/Serialization/SerializeContext.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <ISystem.h>
  11. #include <LyShine/ILyShine.h>
  12. #include <LyShine/Bus/UiCanvasBus.h>
  13. #include <LyShine/Bus/UiCursorBus.h>
  14. #include <LyShine/Bus/UiElementBus.h>
  15. #include <LyShine/Bus/UiTextBus.h>
  16. #include <LyShine/Bus/UiInteractableBus.h>
  17. #include <LyShineMessagePopup.h>
  18. namespace MessagePopup
  19. {
  20. //-------------------------------------------------------------------------
  21. LyShineMessagePopup::LyShineMessagePopup()
  22. {
  23. }
  24. //-------------------------------------------------------------------------
  25. void LyShineMessagePopup::Reflect(AZ::ReflectContext* context)
  26. {
  27. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  28. {
  29. serialize->Class<LyShineMessagePopup, AZ::Component>()
  30. ->Version(1);
  31. if (AZ::EditContext* ec = serialize->GetEditContext())
  32. {
  33. ec->Class<LyShineMessagePopup>("MessagePopup", "[Description of functionality provided by this System Component]")
  34. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  35. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  36. ;
  37. }
  38. }
  39. }
  40. //-------------------------------------------------------------------------
  41. void LyShineMessagePopup::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  42. {
  43. provided.push_back(AZ_CRC_CE("MessagePopupService"));
  44. }
  45. //-------------------------------------------------------------------------
  46. void LyShineMessagePopup::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  47. {
  48. incompatible.push_back(AZ_CRC_CE("MessagePopupService"));
  49. }
  50. //-------------------------------------------------------------------------
  51. void LyShineMessagePopup::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  52. {
  53. (void)required;
  54. }
  55. //-------------------------------------------------------------------------
  56. void LyShineMessagePopup::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  57. {
  58. (void)dependent;
  59. }
  60. //-------------------------------------------------------------------------
  61. void LyShineMessagePopup::Init()
  62. {
  63. }
  64. //-------------------------------------------------------------------------
  65. void LyShineMessagePopup::Activate()
  66. {
  67. MessagePopup::MessagePopupImplBus::Handler::BusConnect();
  68. }
  69. //-------------------------------------------------------------------------
  70. void LyShineMessagePopup::Deactivate()
  71. {
  72. MessagePopup::MessagePopupImplBus::Handler::BusConnect();
  73. }
  74. //-----------------------------------------------------------------------------
  75. void LyShineMessagePopup::OnAction([[maybe_unused]] AZ::EntityId entityId, const LyShine::ActionName& actionName)
  76. {
  77. const AZ::EntityId* canvasId = UiCanvasNotificationBus::GetCurrentBusId();
  78. if (!canvasId)
  79. {
  80. return;
  81. }
  82. const auto it = m_activePopupIdsByCanvasId.find(*canvasId);
  83. if (it == m_activePopupIdsByCanvasId.end())
  84. {
  85. return;
  86. }
  87. const AZ::u32 popupId = it->second;
  88. if (actionName == "OnButton1")
  89. {
  90. MessagePopup::MessagePopupRequestBus::Broadcast(&MessagePopup::MessagePopupRequestBus::Events::HidePopup, popupId, 0);
  91. MessagePopupNotificationsBus::Broadcast(&MessagePopupNotificationsBus::Events::OnHide, popupId, 0);
  92. }
  93. else if (actionName == "OnButton2")
  94. {
  95. MessagePopup::MessagePopupRequestBus::Broadcast(&MessagePopup::MessagePopupRequestBus::Events::HidePopup, popupId, 1);
  96. MessagePopupNotificationsBus::Broadcast(&MessagePopupNotificationsBus::Events::OnHide, popupId, 1);
  97. }
  98. else if (actionName == "OnButton3")
  99. {
  100. MessagePopup::MessagePopupRequestBus::Broadcast(&MessagePopup::MessagePopupRequestBus::Events::HidePopup, popupId, 2);
  101. MessagePopupNotificationsBus::Broadcast(&MessagePopupNotificationsBus::Events::OnHide, popupId, 2);
  102. }
  103. }
  104. //-----------------------------------------------------------------------------
  105. void LyShineMessagePopup::OnShowPopup(AZ::u32 _popupID, const AZStd::string& _message, EPopupButtons _buttons, EPopupKind _kind, AZStd::function<void(int _button)> _callback, void** _popupClientID)
  106. {
  107. AZ::EntityId canvasEntityId;
  108. bool isNavigationSupported = false;
  109. if (_kind == EPopupKind_Generic)
  110. {
  111. switch (_buttons)
  112. {
  113. case EPopupButtons::EPopupButtons_NoButtons:
  114. canvasEntityId = AZ::Interface<ILyShine>::Get()->LoadCanvas("@products@/ui/canvases/defaultmessagepopup.uicanvas");
  115. break;
  116. case EPopupButtons::EPopupButtons_Confirm:
  117. canvasEntityId = AZ::Interface<ILyShine>::Get()->LoadCanvas("@products@/ui/canvases/defaultmessagepopup_confirm.uicanvas");
  118. isNavigationSupported = true;
  119. break;
  120. case EPopupButtons::EPopupButtons_YesNo:
  121. canvasEntityId = AZ::Interface<ILyShine>::Get()->LoadCanvas("@products@/ui/canvases/defaultmessagepopup_yesno.uicanvas");
  122. isNavigationSupported = true;
  123. break;
  124. }
  125. }
  126. else if (_kind == EPopupKind_Toaster)
  127. {
  128. canvasEntityId = AZ::Interface<ILyShine>::Get()->LoadCanvas("@products@/ui/canvases/toaster.uicanvas");
  129. }
  130. if (canvasEntityId.IsValid())
  131. {
  132. // Get the canvasID to send to the MessagePopupManager
  133. LyShine::CanvasId canvasId = 0;
  134. UiCanvasBus::EventResult(canvasId, canvasEntityId, &UiCanvasBus::Events::GetCanvasId);
  135. *_popupClientID = (void*)(AZ::u64)canvasId;
  136. // Enable the popup
  137. AZ::Entity* instructionsTextElement = nullptr;
  138. UiCanvasBus::Event(canvasEntityId, &UiCanvasBus::Events::SetEnabled, true);
  139. UiCanvasBus::Event(canvasEntityId, &UiCanvasBus::Events::SetKeepLoadedOnLevelUnload, true);
  140. // Set the text
  141. UiCanvasBus::EventResult(instructionsTextElement, canvasEntityId, &UiCanvasBus::Events::FindElementByName, "Text");
  142. if (instructionsTextElement != nullptr && instructionsTextElement->GetId().IsValid())
  143. {
  144. UiTextBus::Event(instructionsTextElement->GetId(), &UiTextBus::Events::SetText, _message.c_str());
  145. }
  146. // Set whether navigation is supported, and show the cursor if so
  147. UiCanvasBus::Event(canvasEntityId, &UiCanvasInterface::SetIsNavigationSupported, isNavigationSupported);
  148. if (isNavigationSupported)
  149. {
  150. UiCursorBus::Broadcast(&UiCursorInterface::IncrementVisibleCounter);
  151. }
  152. m_activePopupIdsByCanvasId[canvasEntityId] = _popupID;
  153. UiCanvasNotificationBus::MultiHandler::BusConnect(canvasEntityId);
  154. }
  155. // Having an invalid canvas will rollback to the platform messagepopup
  156. }
  157. //-----------------------------------------------------------------------------
  158. void LyShineMessagePopup::OnHidePopup(const MessagePopupInfo& _popupInfo)
  159. {
  160. // get the canvas ID in the clientdata
  161. LyShine::CanvasId canvasId = *((LyShine::CanvasId*)&_popupInfo.m_clientData);
  162. AZ::EntityId canvasEntityId = AZ::Interface<ILyShine>::Get()->FindCanvasById(canvasId);
  163. if (canvasEntityId.IsValid())
  164. {
  165. // Hide the cursor if it was shown in LyShineMessagePopup::OnShowPopup
  166. bool isNavigationSupported = false;
  167. UiCanvasBus::EventResult(isNavigationSupported, canvasEntityId, &UiCanvasInterface::GetIsNavigationSupported);
  168. if (isNavigationSupported)
  169. {
  170. UiCursorBus::Broadcast(&UiCursorInterface::DecrementVisibleCounter);
  171. }
  172. // Disable the popup
  173. UiCanvasBus::Event(canvasEntityId, &UiCanvasBus::Events::SetEnabled, false);
  174. AZ::Interface<ILyShine>::Get()->ReleaseCanvas(canvasEntityId, false);
  175. UiCanvasNotificationBus::MultiHandler::BusDisconnect(canvasEntityId);
  176. m_activePopupIdsByCanvasId.erase(canvasEntityId);
  177. }
  178. }
  179. }