HUDComponent.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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("RoundTimerText", &HUDComponent::m_roundTimerText)
  34. ->Field("RoundTimerId", &HUDComponent::m_roundTimerUi)
  35. ->Field("RoundSecondsRemaining", &HUDComponent::m_roundSecondsRemainingUiParent)
  36. ;
  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. ;
  48. }
  49. }
  50. }
  51. void HUDComponent::SetRoundNumberText(uint16_t round)
  52. {
  53. if (const INetworkMatch* netMatchComponent = AZ::Interface<INetworkMatch>::Get())
  54. {
  55. // Display the current round number.
  56. // The end of match can push the round count over the max round count, so cap it.
  57. const uint16_t totalRounds = aznumeric_cast<uint16_t>(netMatchComponent->GetTotalRoundCount());
  58. m_roundNumberText = AZStd::string::format("%d of %d", AZStd::min(round, totalRounds), totalRounds);
  59. UiTextBus::Event(m_roundNumberUi, &UiTextBus::Events::SetText, m_roundNumberText);
  60. }
  61. }
  62. void HUDComponent::SetRoundTimerText(RoundTimeSec time)
  63. {
  64. // Display a clock with the time remaining
  65. auto duration = AZStd::chrono::seconds(time);
  66. auto minutes = AZStd::chrono::duration_cast<AZStd::chrono::minutes>(duration);
  67. auto seconds = AZStd::chrono::duration_cast<AZStd::chrono::seconds>(duration - minutes);
  68. m_roundTimerText = AZStd::string::format("%02i:%02i", static_cast<int>(minutes.count()), static_cast<int>(seconds.count()));
  69. UiTextBus::Event(m_roundTimerUi, &UiTextBus::Events::SetText, m_roundTimerText);
  70. // Display a countdown of custom UI when the round is close to finishing
  71. if (duration.count() > 0 && duration.count() <= 10)
  72. {
  73. UiElementBus::Event(m_roundSecondsRemainingUiParent, &UiElementBus::Events::SetIsEnabled, true);
  74. AZStd::vector<AZ::EntityId> uiSecondsRemainingUIElements;
  75. UiElementBus::EventResult(uiSecondsRemainingUIElements, m_roundSecondsRemainingUiParent, &UiElementBus::Events::GetChildEntityIds);
  76. for (int i = 0; i < uiSecondsRemainingUIElements.size(); ++i)
  77. {
  78. UiElementBus::Event(uiSecondsRemainingUIElements[i], &UiElementBus::Events::SetIsEnabled, i == aznumeric_cast<int>(duration.count()));
  79. }
  80. }
  81. else
  82. {
  83. UiElementBus::Event(m_roundSecondsRemainingUiParent, &UiElementBus::Events::SetIsEnabled, false);
  84. }
  85. }
  86. } // namespace MultiplayerSample