UiRestBetweenRoundsComponent.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <Source/Components/UI/UiRestBetweenRoundsComponent.h>
  8. #include <Source/Components/NetworkMatchComponent.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <LyShine/Bus/UiElementBus.h>
  11. namespace MultiplayerSample
  12. {
  13. void UiRestBetweenRoundsComponent::Reflect(AZ::ReflectContext* context)
  14. {
  15. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  16. {
  17. serializeContext->Class<UiRestBetweenRoundsComponent, AZ::Component>()
  18. ->Version(2)
  19. ->Field("Rest Time Root Ui Element", &UiRestBetweenRoundsComponent::m_restTimerRootUiElement)
  20. ->Field("Number Container Ui Element", &UiRestBetweenRoundsComponent::m_numbersContainerUiElement)
  21. ;
  22. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  23. {
  24. editContext->Class<UiRestBetweenRoundsComponent>("Ui Rest Between Rounds", "Shows the rest time remaining before the new round begins")
  25. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  26. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("CanvasUI"))
  27. ->DataElement(AZ::Edit::UIHandlers::Default, &UiRestBetweenRoundsComponent::m_restTimerRootUiElement,
  28. "Rest Time Root", "The root element containing the rest timer ui.")
  29. ->DataElement(AZ::Edit::UIHandlers::Default, &UiRestBetweenRoundsComponent::m_numbersContainerUiElement,
  30. "Time remaining UI Elements", "The parent containing all the timer numbers")
  31. ;
  32. }
  33. }
  34. }
  35. void UiRestBetweenRoundsComponent::Activate()
  36. {
  37. // Wait for the NetworkMatchComponent to activate and then register to listen for rest timer network property change
  38. AZ::TickBus::Handler::BusConnect();
  39. }
  40. void UiRestBetweenRoundsComponent::OnTick([[maybe_unused]]float deltaTime, [[maybe_unused]]AZ::ScriptTimePoint time)
  41. {
  42. if (auto networkMatchComponent = AZ::Interface<INetworkMatch>::Get())
  43. {
  44. // Disconnect from tickbus once event notifications are set up
  45. networkMatchComponent->AddRoundRestTimeRemainingEventHandler(onRestTimeChangedHandler);
  46. AZ::TickBus::Handler::BusDisconnect();
  47. }
  48. }
  49. void UiRestBetweenRoundsComponent::Deactivate()
  50. {
  51. onRestTimeChangedHandler.Disconnect();
  52. }
  53. void UiRestBetweenRoundsComponent::OnRoundRestTimeRemainingChanged(RoundTimeSec secondsRemaining)
  54. {
  55. bool enableRootElement = secondsRemaining > 0.0f;
  56. UiElementBus::Event(m_restTimerRootUiElement, &UiElementBus::Events::SetIsEnabled, enableRootElement);
  57. LyShine::EntityArray numbersUiElements;
  58. UiElementBus::EventResult(numbersUiElements, m_numbersContainerUiElement, &UiElementBus::Events::GetChildElements);
  59. const size_t secondsRemainingInt = aznumeric_cast<size_t>(secondsRemaining);
  60. const size_t uiElementCount = numbersUiElements.size();
  61. for(size_t uiElementIndex = 0; uiElementIndex < uiElementCount; ++uiElementIndex)
  62. {
  63. bool enabled = (uiElementIndex == secondsRemainingInt);
  64. UiElementBus::Event(numbersUiElements[uiElementIndex]->GetId(), &UiElementBus::Events::SetIsEnabled, enabled);
  65. }
  66. }
  67. }