HUDComponent.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <Source/Components/NetworkMatchComponent.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <LyShine/Bus/UiTextBus.h>
  13. namespace MultiplayerSample
  14. {
  15. void HUDComponent::Activate()
  16. {
  17. netMatchComponent->RoundTimeAddEvent(m_roundTimerHandler);
  18. }
  19. #endif
  20. }
  21. void HUDComponent::Deactivate()
  22. {
  23. m_waitForActiveNetworkMatchComponent.RemoveFromQueue();
  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::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  52. {
  53. required.push_back(AZ_CRC("UiCanvasRefService"));
  54. required.push_back(AZ_CRC("NetworkMatchComponent"));
  55. }
  56. #if AZ_TRAIT_CLIENT
  57. void HUDComponent::OnCanvasLoadedIntoEntity(AZ::EntityId uiCanvasEntity)
  58. {
  59. m_uiCanvasId = uiCanvasEntity;
  60. }
  61. void HUDComponent::SetRoundNumberText(uint16_t round)
  62. {
  63. if (const NetworkMatchComponent* netMatchComponent = AZ::Interface<NetworkMatchComponent>::Get())
  64. {
  65. // Display the current round number.
  66. // The end of match can push the round count over the max round count, so cap it.
  67. const uint16_t totalRounds = netMatchComponent->GetTotalRounds();
  68. m_roundNumberText = AZStd::string::format("%d of %d", AZStd::min(round, totalRounds), totalRounds);
  69. UiTextBus::Event(m_roundNumberUi, &UiTextBus::Events::SetText, m_roundNumberText);
  70. }
  71. }
  72. void HUDComponent::SetRoundTimerText(RoundTimeSec time)
  73. {
  74. // Display a clock with the time remaining
  75. auto duration = AZStd::chrono::seconds(time);
  76. auto minutes = AZStd::chrono::duration_cast<AZStd::chrono::minutes>(duration);
  77. auto seconds = AZStd::chrono::duration_cast<AZStd::chrono::seconds>(duration - minutes);
  78. m_roundTimerText = AZStd::string::format("%02i:%02i", static_cast<int>(minutes.count()), static_cast<int>(seconds.count()));
  79. UiTextBus::Event(m_roundTimerUi, &UiTextBus::Events::SetText, m_roundTimerText);
  80. // Display a countdown of custom UI when the round is close to finishing
  81. if (duration.count() > 0 && duration.count() <= 10)
  82. {
  83. UiElementBus::Event(m_roundSecondsRemainingUiParent, &UiElementBus::Events::SetIsEnabled, true);
  84. AZStd::vector<AZ::EntityId> uiSecondsRemainingUIElements;
  85. UiElementBus::EventResult(uiSecondsRemainingUIElements, m_roundSecondsRemainingUiParent, &UiElementBus::Events::GetChildEntityIds);
  86. for (int i = 0; i < uiSecondsRemainingUIElements.size(); ++i)
  87. {
  88. UiElementBus::Event(uiSecondsRemainingUIElements[i], &UiElementBus::Events::SetIsEnabled, i == aznumeric_cast<int>(duration.count()));
  89. }
  90. }
  91. else
  92. {
  93. UiElementBus::Event(m_roundSecondsRemainingUiParent, &UiElementBus::Events::SetIsEnabled, false);
  94. }
  95. }
  96. } // namespace MultiplayerSample