3
0

MessagePopupManager.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <MessagePopupManager.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/EBus/EBus.h>
  12. namespace MessagePopup
  13. {
  14. // Global dynamic unique identifier factory. One ID for each popup
  15. static AZ::u32 g_GlobalUniqueIDBank = 1;
  16. //-------------------------------------------------------------------------
  17. MessagePopupManager::MessagePopupManager()
  18. {
  19. }
  20. //-------------------------------------------------------------------------
  21. AZ::u32 MessagePopupManager::CreatePopup()
  22. {
  23. AZ::u32 thisID = g_GlobalUniqueIDBank; // do it need to be thread safe?
  24. g_GlobalUniqueIDBank++;
  25. m_currentPopups[thisID] = new MessagePopupInfo();
  26. if (!AZ::TickBus::Handler::BusIsConnected())
  27. {
  28. AZ::TickBus::Handler::BusConnect();
  29. }
  30. return thisID;
  31. }
  32. //-------------------------------------------------------------------------
  33. bool MessagePopupManager::SetPopupData(AZ::u32 _popupID, void *_clientID, AZStd::function<void(int _button)> _callback, float _showTime)
  34. {
  35. if (m_currentPopups.find(_popupID) != m_currentPopups.end())
  36. {
  37. m_currentPopups[_popupID]->SetData(_clientID, _callback, _showTime);
  38. return true;
  39. }
  40. return false;
  41. }
  42. //-------------------------------------------------------------------------
  43. bool MessagePopupManager::RemovePopup(AZ::u32 _popupID)
  44. {
  45. CurrentPopupsMap::iterator iter = m_currentPopups.find(_popupID);
  46. bool retVal = false;
  47. if (iter != m_currentPopups.end())
  48. {
  49. delete iter->second;
  50. m_currentPopups.erase(_popupID);
  51. retVal = true;
  52. }
  53. if (m_currentPopups.size() == 0)
  54. {
  55. AZ::TickBus::Handler::BusDisconnect();
  56. }
  57. return retVal;
  58. }
  59. //-------------------------------------------------------------------------
  60. void* MessagePopupManager::GetPopupClientData(AZ::u32 _popupID)
  61. {
  62. if (m_currentPopups.find(_popupID) != m_currentPopups.end())
  63. {
  64. return m_currentPopups[_popupID]->m_clientData;
  65. }
  66. return nullptr;
  67. }
  68. //-------------------------------------------------------------------------
  69. MessagePopupInfo* MessagePopupManager::GetPopupInfo(AZ::u32 _popupID)
  70. {
  71. if (m_currentPopups.find(_popupID) != m_currentPopups.end())
  72. {
  73. return m_currentPopups[_popupID];
  74. }
  75. return nullptr;
  76. }
  77. //-------------------------------------------------------------------------
  78. void MessagePopupManager::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint scriptTimePoint)
  79. {
  80. for (CurrentPopupsMap::iterator iter = m_currentPopups.begin(); iter != m_currentPopups.end(); )
  81. {
  82. if ((*iter).second->m_showTime > 0.0f)
  83. {
  84. (*iter).second->m_showTime -= deltaTime;
  85. if ((*iter).second->m_showTime <= 0.0f)
  86. {
  87. AZ::u32 thisID = (*iter).first;
  88. ++iter; // Get the next now since HidePopup will remove the popup from the list. (avoiding the creation of a new list to be processed after)
  89. MessagePopup::MessagePopupRequestBus::Broadcast(&MessagePopup::MessagePopupRequestBus::Events::HidePopup, thisID, 0);
  90. continue;
  91. }
  92. }
  93. ++iter;
  94. }
  95. }
  96. }