MessagePopupSystemComponent.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <MessagePopupSystemComponent.h>
  12. #include <AzCore/NativeUI/NativeUIRequests.h>
  13. #include <AzCore/EBus/EBus.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/Component/ComponentApplicationBus.h>
  16. #include <AzCore/RTTI/BehaviorContext.h>
  17. namespace MessagePopup
  18. {
  19. // BehaviorContext MessagePopupNotificationsBus forwarder
  20. class MessagePopupNotificationsBusHandler : public MessagePopupNotificationsBus::Handler, public AZ::BehaviorEBusHandler
  21. {
  22. public:
  23. AZ_EBUS_BEHAVIOR_BINDER(MessagePopupNotificationsBusHandler, "{7AEDC591-41AB-4E3B-87D2-0334615427AA}", AZ::SystemAllocator,
  24. OnHide);
  25. void OnHide(AZ::u32 popupID, int buttonPressed) override
  26. {
  27. Call(FN_OnHide, popupID, buttonPressed);
  28. }
  29. };
  30. //-------------------------------------------------------------------------
  31. MessagePopupSystemComponent::MessagePopupSystemComponent()
  32. {
  33. }
  34. //-------------------------------------------------------------------------
  35. void MessagePopupSystemComponent::Reflect(AZ::ReflectContext* context)
  36. {
  37. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  38. {
  39. serialize->Class<MessagePopupSystemComponent, AZ::Component>()
  40. ->Version(1);
  41. if (AZ::EditContext* ec = serialize->GetEditContext())
  42. {
  43. ec->Class<MessagePopupSystemComponent>("MessagePopup", "[Description of functionality provided by this System Component]")
  44. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  45. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  46. ;
  47. }
  48. }
  49. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  50. if (behaviorContext)
  51. {
  52. behaviorContext->EBus<MessagePopupRequestBus>("MessagePopupRequestBus")
  53. ->Attribute(AZ::Script::Attributes::Category, "Gameplay")
  54. ->Event("ShowToasterPopup", &MessagePopupRequests::ShowToasterPopup,
  55. { {
  56. { "Message", "The message to display. Localization ID can be used as well." },
  57. { "Duration", "Number of seconds before closing the window" }
  58. } }
  59. )
  60. ->Attribute(AZ::Script::Attributes::ToolTip, "Show a information message window on bottom right of the screen for a short period of time.")
  61. ->Event("ShowPopup", &MessagePopupRequests::ShowPopup,
  62. { {
  63. { "Message", "The message to display. Localization ID can be used as well." },
  64. { "Button kind", "0:OK, 1:Yes/No 2:no buttons" }
  65. } }
  66. )
  67. ->Event("HidePopup", &MessagePopupRequests::HidePopup,
  68. { {
  69. { "Popup ID", "The ID of the popup you get from the Result of a ShowPopup" },
  70. { "Button Pressed", "Which button to simulate pressing?" },
  71. } }
  72. );
  73. behaviorContext->EBus<MessagePopupNotificationsBus>("MessagePopupNotificationsBus")
  74. ->Attribute(AZ::Script::Attributes::Category, "Gameplay")
  75. ->Handler<MessagePopupNotificationsBusHandler>()
  76. ;
  77. }
  78. }
  79. //-------------------------------------------------------------------------
  80. void MessagePopupSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  81. {
  82. provided.push_back(AZ_CRC_CE("MessagePopupSystemComponentService"));
  83. }
  84. //-------------------------------------------------------------------------
  85. void MessagePopupSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  86. {
  87. incompatible.push_back(AZ_CRC_CE("MessagePopupSystemComponentService"));
  88. }
  89. //-------------------------------------------------------------------------
  90. void MessagePopupSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  91. {
  92. (void)required;
  93. }
  94. //-------------------------------------------------------------------------
  95. void MessagePopupSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  96. {
  97. (void)dependent;
  98. }
  99. //-------------------------------------------------------------------------
  100. void MessagePopupSystemComponent::Init()
  101. {
  102. }
  103. //-------------------------------------------------------------------------
  104. void MessagePopupSystemComponent::Activate()
  105. {
  106. MessagePopupRequestBus::Handler::BusConnect();
  107. }
  108. //-------------------------------------------------------------------------
  109. void MessagePopupSystemComponent::Deactivate()
  110. {
  111. MessagePopupRequestBus::Handler::BusDisconnect();
  112. }
  113. //-------------------------------------------------------------------------
  114. AZ::u32 MessagePopupSystemComponent::ShowPopup(const AZStd::string& _message, EPopupButtons _buttons)
  115. {
  116. return InternalShowPopup(_message, _buttons, EPopupKind_Generic, nullptr, 0.0f);
  117. }
  118. //-------------------------------------------------------------------------
  119. AZ::u32 MessagePopupSystemComponent::ShowPopupWithCallback(const AZStd::string& _message, EPopupButtons _buttons, AZStd::function<void(int _button)> _callback)
  120. {
  121. return InternalShowPopup(_message, _buttons, EPopupKind_Generic, _callback, 0.0f);
  122. }
  123. //-------------------------------------------------------------------------
  124. AZ::u32 MessagePopupSystemComponent::ShowToasterPopup(const AZStd::string& _message, float _showTime)
  125. {
  126. return InternalShowPopup(_message, EPopupButtons_NoButtons, EPopupKind_Toaster, nullptr, _showTime);
  127. }
  128. //-------------------------------------------------------------------------
  129. AZ::u32 MessagePopupSystemComponent::ShowToasterPopupWithCallback(const AZStd::string& _message, float _showTime, AZStd::function<void(int _button)> _callback)
  130. {
  131. return InternalShowPopup(_message, EPopupButtons_NoButtons, EPopupKind_Toaster, _callback, _showTime);
  132. }
  133. //-------------------------------------------------------------------------
  134. AZ::u32 MessagePopupSystemComponent::InternalShowPopup(const AZStd::string& _message, EPopupButtons _buttons, EPopupKind _kind, AZStd::function<void(int _button)> _callback, float _showTime)
  135. {
  136. void *clientData = nullptr;
  137. AZ::u32 popupID = m_PopupsManager.CreatePopup();
  138. // First try to send the request to a valid MessagePopup implementation.
  139. // If none consumed it and return the clientID then use the platform specific default popup
  140. MessagePopup::MessagePopupImplBus::Broadcast(&MessagePopup::MessagePopupImplBus::Events::OnShowPopup, popupID, _message, _buttons, _kind, _callback, &clientData);
  141. if (clientData != nullptr)
  142. {
  143. m_PopupsManager.SetPopupData(popupID, clientData, _callback, _showTime);
  144. }
  145. else
  146. {
  147. bool res = ShowNativePopup(_message, _buttons, _kind, _callback);
  148. if (res)
  149. {
  150. m_PopupsManager.SetPopupData(popupID, nullptr, _callback, _showTime);
  151. }
  152. else
  153. {
  154. m_PopupsManager.RemovePopup(popupID);
  155. return InvalidId;
  156. }
  157. }
  158. return popupID;
  159. }
  160. //-------------------------------------------------------------------------
  161. bool MessagePopupSystemComponent::ShowNativePopup(const AZStd::string& _message, MessagePopup::EPopupButtons _buttons, [[maybe_unused]] MessagePopup::EPopupKind _kind, AZStd::function<void(int _button)> _callback)
  162. {
  163. switch (_buttons)
  164. {
  165. case MessagePopup::EPopupButtons_NoButtons:
  166. AZ_Assert(false, "Invalid option since we cannot Hide a native Popup");
  167. return false;
  168. case MessagePopup::EPopupButtons_Confirm:
  169. AZ::NativeUI::NativeUIRequestBus::Broadcast(&AZ::NativeUI::NativeUIRequestBus::Events::DisplayOkDialog, "", _message.c_str(), true);
  170. if (_callback)
  171. {
  172. _callback(0);
  173. }
  174. return true;
  175. case MessagePopup::EPopupButtons_YesNo:
  176. {
  177. AZStd::string resultStr;
  178. AZ::NativeUI::NativeUIRequestBus::BroadcastResult(resultStr, &AZ::NativeUI::NativeUIRequestBus::Events::DisplayYesNoDialog, "", _message.c_str(), false);
  179. if (_callback)
  180. {
  181. if (resultStr == "Yes")
  182. _callback(0);
  183. else
  184. _callback(1);
  185. }
  186. return true;
  187. }
  188. }
  189. // return true means we consumed it
  190. return false;
  191. }
  192. //-------------------------------------------------------------------------
  193. bool MessagePopupSystemComponent::HidePopup(AZ::u32 _popupID, int _buttonPressed)
  194. {
  195. MessagePopupInfo* popupInfo = m_PopupsManager.GetPopupInfo(_popupID);
  196. if (popupInfo == nullptr)
  197. {
  198. return false;
  199. }
  200. void *clientData = m_PopupsManager.GetPopupClientData(_popupID);
  201. // First try to send the request to a valid MessagePopup implementation.
  202. // If none consumed it and return the clientID then use the platform specific default popup
  203. MessagePopup::MessagePopupImplBus::Broadcast(&MessagePopup::MessagePopupImplBus::Events::OnHidePopup, *popupInfo);
  204. if (clientData == nullptr)
  205. {
  206. // No way to remove the native UI
  207. AZ_Assert(false, "Invalid option since we cannot Hide a native Popup.");
  208. }
  209. // Process the callback function
  210. if (popupInfo->IsValid() && popupInfo->m_callback)
  211. {
  212. popupInfo->m_callback(_buttonPressed);
  213. }
  214. m_PopupsManager.RemovePopup(_popupID);
  215. return true;
  216. }
  217. //-------------------------------------------------------------------------
  218. AZ::u32 MessagePopupSystemComponent::GetNumActivePopups() const
  219. {
  220. return m_PopupsManager.GetNumActivePopups();
  221. }
  222. }