HUDComponent.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <Source/Components/UI/HUDComponent.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <LyShine/Bus/UiElementBus.h>
  12. #include <LyShine/Bus/UiTextBus.h>
  13. namespace MultiplayerSample
  14. {
  15. void HUDComponent::Activate()
  16. {
  17. m_waitForActiveNetworkMatchComponent.Enqueue(AZ::TimeMs{ 1000 }, true);
  18. }
  19. void HUDComponent::Deactivate()
  20. {
  21. m_waitForActiveNetworkMatchComponent.RemoveFromQueue();
  22. m_roundNumberHandler.Disconnect();
  23. m_roundTimerHandler.Disconnect();
  24. }
  25. void HUDComponent::Reflect(AZ::ReflectContext* context)
  26. {
  27. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  28. {
  29. serializeContext->Class<HUDComponent, AZ::Component>()
  30. ->Version(1)
  31. ->Field("RoundNumberText", &HUDComponent::m_roundNumberText)
  32. ->Field("RoundNumberId", &HUDComponent::m_roundNumberUi)
  33. ->Field("RoundTimerId", &HUDComponent::m_roundTimerUi)
  34. ->Field("RoundSecondsRemaining", &HUDComponent::m_roundSecondsRemainingUiParent)
  35. ->Field("FirstMatchParent", &HUDComponent::m_firstMatchStartingUiParent)
  36. ->Field("FirstMatchTimer", &HUDComponent::m_firstMatchStartingTimerUi);
  37. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  38. {
  39. editContext->Class<HUDComponent>("HUDComponent", "Helper component for setting up Multiplayer Sample's HUD")
  40. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  41. ->Attribute(AZ::Edit::Attributes::Category, "Multiplayer Sample UI")
  42. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  43. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("CanvasUI"))
  44. ->DataElement(AZ::Edit::UIHandlers::Default, &HUDComponent::m_roundNumberUi, "Round Number Textbox", "The ui textbox for displaying the current round number.")
  45. ->DataElement(AZ::Edit::UIHandlers::Default, &HUDComponent::m_roundTimerUi, "Round Time Textbox", "The ui textbox for displaying the time remaining in the round.")
  46. ->DataElement(AZ::Edit::UIHandlers::Default, &HUDComponent::m_roundSecondsRemainingUiParent, "Round Seconds Remaining UI Elements", "The parent ui element containing all the ui images to display the seconds remaining.")
  47. ->DataElement(AZ::Edit::UIHandlers::Default, &HUDComponent::m_firstMatchStartingUiParent, "First Match Parent", "UI to display for players while they wait for other players to join before the first match begins.")
  48. ->DataElement(AZ::Edit::UIHandlers::Default, &HUDComponent::m_firstMatchStartingTimerUi, "First Match Timer", "The ui text to display the time remaining before the first match starts.")
  49. ;
  50. }
  51. }
  52. }
  53. void HUDComponent::SetRoundNumberText(uint16_t round)
  54. {
  55. if (const INetworkMatch* netMatchComponent = AZ::Interface<INetworkMatch>::Get())
  56. {
  57. // Display the current round number.
  58. // The end of match can push the round count over the max round count, so cap it.
  59. const uint16_t totalRounds = aznumeric_cast<uint16_t>(netMatchComponent->GetTotalRoundCount());
  60. m_roundNumberText = AZStd::string::format("%d of %d", AZStd::min(round, totalRounds), totalRounds);
  61. UiTextBus::Event(m_roundNumberUi, &UiTextBus::Events::SetText, m_roundNumberText);
  62. }
  63. }
  64. void HUDComponent::SetRoundTimerText(RoundTimeSec time)
  65. {
  66. // Display a clock with the time remaining
  67. auto duration = AZStd::chrono::seconds(time);
  68. auto minutes = AZStd::chrono::duration_cast<AZStd::chrono::minutes>(duration);
  69. auto seconds = AZStd::chrono::duration_cast<AZStd::chrono::seconds>(duration - minutes);
  70. AZStd::string roundTimerText = AZStd::string::format("%02i:%02i", static_cast<int>(minutes.count()), static_cast<int>(seconds.count()));
  71. UiTextBus::Event(m_roundTimerUi, &UiTextBus::Events::SetText, roundTimerText);
  72. // Display a countdown of custom UI when the round is close to finishing
  73. if (duration.count() > 0 && duration.count() <= 10)
  74. {
  75. UiElementBus::Event(m_roundSecondsRemainingUiParent, &UiElementBus::Events::SetIsEnabled, true);
  76. AZStd::vector<AZ::EntityId> uiSecondsRemainingUIElements;
  77. UiElementBus::EventResult(uiSecondsRemainingUIElements, m_roundSecondsRemainingUiParent, &UiElementBus::Events::GetChildEntityIds);
  78. for (int i = 0; i < uiSecondsRemainingUIElements.size(); ++i)
  79. {
  80. UiElementBus::Event(uiSecondsRemainingUIElements[i], &UiElementBus::Events::SetIsEnabled, i == aznumeric_cast<int>(duration.count()));
  81. }
  82. }
  83. else
  84. {
  85. UiElementBus::Event(m_roundSecondsRemainingUiParent, &UiElementBus::Events::SetIsEnabled, false);
  86. }
  87. }
  88. void HUDComponent::UpdateFirstMatchTimerUi()
  89. {
  90. const AZ::TimeMs timeRemainingUntilMatchStartMs = AZ::Interface<INetworkMatch>::Get()->GetMatchStartHostTime() - AZ::Interface<Multiplayer::IMultiplayer>::Get()->GetCurrentHostTimeMs();
  91. // Update the UI to display the time remaining until the first match begins
  92. if (timeRemainingUntilMatchStartMs > AZ::Time::ZeroTimeMs)
  93. {
  94. UiElementBus::Event(m_firstMatchStartingUiParent, &UiElementBus::Events::SetIsEnabled, true);
  95. const AZStd::chrono::milliseconds duration(static_cast<long long>(timeRemainingUntilMatchStartMs));
  96. const auto minutes = AZStd::chrono::duration_cast<AZStd::chrono::minutes>(duration);
  97. const auto seconds = AZStd::chrono::duration_cast<AZStd::chrono::seconds>(duration - minutes);
  98. AZStd::string matchTimeText = AZStd::string::format("%02i:%02i", static_cast<int>(minutes.count()), static_cast<int>(seconds.count()));
  99. UiTextBus::Event(m_firstMatchStartingTimerUi, &UiTextBus::Events::SetText, matchTimeText);
  100. // Requeue to refresh the UI after a second if there's still time left on the clock
  101. if (!m_updateFirstMatchTimer.IsScheduled())
  102. {
  103. m_updateFirstMatchTimer.Enqueue(AZ::SecondsToTimeMs(1.0), true);
  104. }
  105. }
  106. else
  107. {
  108. UiElementBus::Event(m_firstMatchStartingUiParent, &UiElementBus::Events::SetIsEnabled, false);
  109. m_updateFirstMatchTimer.RemoveFromQueue();
  110. }
  111. }
  112. } // namespace MultiplayerSample