DemoStatisticsComponent.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "DemoStatisticsComponent.h"
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Serialization/EditContextConstants.inl>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  13. #include <LyShine/Bus/UiCanvasBus.h>
  14. #include <LyShine/Bus/UiTextBus.h>
  15. #include <LyShine/Bus/World/UiCanvasRefBus.h>
  16. namespace AppleKraken
  17. {
  18. void DemoStatisticsComponent::Activate()
  19. {
  20. DemoStatisticsNotificationBus::Handler::BusConnect();
  21. m_applesGathered = 0;
  22. m_applesFailed = 0;
  23. // default to our own EntityId if no UI entity is specified
  24. if (!m_uiEntity.IsValid())
  25. {
  26. m_uiEntity = GetEntityId();
  27. }
  28. }
  29. void DemoStatisticsComponent::Deactivate()
  30. {
  31. DemoStatisticsNotificationBus::Handler::BusDisconnect();
  32. }
  33. void DemoStatisticsComponent::Reflect(AZ::ReflectContext* context)
  34. {
  35. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  36. {
  37. serialize->Class<DemoStatisticsComponent, AZ::Component>()->Version(1)->Field("UiEntity", &DemoStatisticsComponent::m_uiEntity);
  38. if (AZ::EditContext* ec = serialize->GetEditContext())
  39. {
  40. ec->Class<DemoStatisticsComponent>("Demo Statistics", "Demo statistics for picked apples")
  41. ->ClassElement(AZ::Edit::ClassElements::EditorData, "Handles counting of interesting events and their UI")
  42. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  43. ->Attribute(AZ::Edit::Attributes::Category, "AppleKraken")
  44. ->DataElement(
  45. AZ::Edit::UIHandlers::EntityId,
  46. &DemoStatisticsComponent::m_uiEntity,
  47. "Entity with Ui canvas reference ",
  48. "Element which will display the text of statistics");
  49. }
  50. }
  51. }
  52. void DemoStatisticsComponent::AddApple(const AppleEvent& appleEvent)
  53. {
  54. if (IsFailed(appleEvent))
  55. {
  56. m_applesFailed++;
  57. }
  58. else
  59. {
  60. m_applesGathered++;
  61. }
  62. DisplayNumberOfApples();
  63. }
  64. void DemoStatisticsComponent::OnApplePickerSpawned(const AZ::EntityId& entityId)
  65. {
  66. m_applePickerStatus.push_back({ entityId, "READY" });
  67. UpdateTextField(m_applePickerCountElementName, "", m_applePickerStatus.size());
  68. DisplayStatus();
  69. }
  70. void DemoStatisticsComponent::SetApplePickerStatus(const AZ::EntityId& entityId, const AZStd::string& status)
  71. {
  72. for (ApplePickerStatus& applePicker : m_applePickerStatus)
  73. {
  74. if (applePicker.m_entityId == entityId)
  75. {
  76. applePicker.m_status = status;
  77. break;
  78. }
  79. }
  80. DisplayStatus();
  81. }
  82. bool DemoStatisticsComponent::IsFailed(const Tags& tags)
  83. {
  84. return HasTag(tags, kPickingFailedEventTag);
  85. }
  86. // TODO Currently not used, but will be soon
  87. bool DemoStatisticsComponent::IsAutomated(const Tags& tags)
  88. {
  89. return HasTag(tags, kPickingAutomatedEventTag);
  90. }
  91. bool DemoStatisticsComponent::HasTag(const AppleEvent& appleEvent, const AZStd::string& tag)
  92. {
  93. return AZStd::find(appleEvent.cbegin(), appleEvent.cend(), tag) != appleEvent.cend();
  94. }
  95. void DemoStatisticsComponent::DisplayNumberOfApples()
  96. {
  97. UpdateTextField(m_appleGatheredElementName, "", m_applesGathered);
  98. UpdateTextField(m_appleFailedElementName, "", m_applesFailed);
  99. }
  100. void DemoStatisticsComponent::DisplayStatus()
  101. {
  102. UpdateTextField(m_applePickerCountElementName, "", static_cast<uint16_t>(m_applePickerStatus.size()));
  103. int row = 0;
  104. for (const ApplePickerStatus& applePicker : m_applePickerStatus)
  105. {
  106. // increment row here so the display number starts at 1
  107. const AZStd::string& elementName = AZStd::string::format("%s%d", m_applePickerStatusElementName.c_str(), row++);
  108. const AZStd::string& label = AZStd::string::format("APPLEKRAKEN%d %s", row, applePicker.m_status.c_str());
  109. UpdateTextField(elementName, label, 0, true);
  110. }
  111. }
  112. void DemoStatisticsComponent::UpdateTextField(
  113. const AZStd::string& fieldName, const AZStd::string& label, uint16_t counter, bool labelOnly)
  114. {
  115. AZ::EntityId uiCanvas;
  116. EBUS_EVENT_ID_RESULT(uiCanvas, m_uiEntity, UiCanvasRefBus, GetCanvas);
  117. if (!uiCanvas.IsValid())
  118. {
  119. AZ_Warning("DemoStatisticsComponent", false, "%s does not have a UiCanvasRef attached\n", m_uiEntity.ToString().c_str());
  120. return;
  121. }
  122. AZ::EntityId uiTextEntity;
  123. UiCanvasBus::EventResult(uiTextEntity, uiCanvas, &UiCanvasInterface::FindElementEntityIdByName, fieldName);
  124. if (!uiTextEntity.IsValid())
  125. {
  126. AZ_Warning("DemoStatisticsComponent", false, "ui canvas doest not have a %s field\n", fieldName.c_str());
  127. return;
  128. }
  129. if (labelOnly)
  130. {
  131. UiTextBus::Event(uiTextEntity, &UiTextInterface::SetText, label);
  132. }
  133. else
  134. {
  135. UiTextBus::Event(uiTextEntity, &UiTextInterface::SetText, AZStd::string::format("%s %d", label.c_str(), counter));
  136. }
  137. }
  138. } // namespace AppleKraken