UiTestScrollBoxDataProviderComponent.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "UiTestScrollBoxDataProviderComponent.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. #include <LyShine/Bus/UiImageBus.h>
  14. #include "UiDynamicContentDatabase.h"
  15. #include "LyShineExamplesInternalBus.h"
  16. namespace LyShineExamples
  17. {
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////
  19. // PUBLIC MEMBER FUNCTIONS
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////
  22. UiTestScrollBoxDataProviderComponent::UiTestScrollBoxDataProviderComponent()
  23. {
  24. }
  25. ////////////////////////////////////////////////////////////////////////////////////////////////////
  26. UiTestScrollBoxDataProviderComponent::~UiTestScrollBoxDataProviderComponent()
  27. {
  28. }
  29. ////////////////////////////////////////////////////////////////////////////////////////////////////
  30. int UiTestScrollBoxDataProviderComponent::GetNumElements()
  31. {
  32. UiDynamicContentDatabase *uiDynamicContentDB = nullptr;
  33. LyShineExamplesInternalBus::BroadcastResult(uiDynamicContentDB, &LyShineExamplesInternalBus::Events::GetUiDynamicContentDatabase);
  34. if (uiDynamicContentDB)
  35. {
  36. return uiDynamicContentDB->GetNumColors(UiDynamicContentDatabaseInterface::ColorType::PaidColors);
  37. }
  38. return 0;
  39. }
  40. ////////////////////////////////////////////////////////////////////////////////////////////////////
  41. void UiTestScrollBoxDataProviderComponent::OnElementBecomingVisible(AZ::EntityId entityId, int index)
  42. {
  43. UiDynamicContentDatabase *uiDynamicContentDB = nullptr;
  44. LyShineExamplesInternalBus::BroadcastResult(uiDynamicContentDB, &LyShineExamplesInternalBus::Events::GetUiDynamicContentDatabase);
  45. if (uiDynamicContentDB)
  46. {
  47. if ((index >= 0) && (index < uiDynamicContentDB->GetNumColors(UiDynamicContentDatabaseInterface::ColorType::PaidColors)))
  48. {
  49. AZ::Entity* entity = nullptr;
  50. UiElementBus::EventResult(entity, entityId, &UiElementBus::Events::FindChildByName, "Name");
  51. if (entity)
  52. {
  53. AZStd::string text = uiDynamicContentDB->GetColorName(UiDynamicContentDatabaseInterface::ColorType::PaidColors, index);
  54. UiTextBus::Event(entity->GetId(), &UiTextBus::Events::SetText, text.c_str());
  55. }
  56. entity = nullptr;
  57. UiElementBus::EventResult(entity, entityId, &UiElementBus::Events::FindChildByName, "Price");
  58. if (entity)
  59. {
  60. AZStd::string text = uiDynamicContentDB->GetColorPrice(UiDynamicContentDatabaseInterface::ColorType::PaidColors, index);
  61. UiTextBus::Event(entity->GetId(), &UiTextBus::Events::SetText, text.c_str());
  62. }
  63. entity = nullptr;
  64. UiElementBus::EventResult(entity, entityId, &UiElementBus::Events::FindChildByName, "Icon");
  65. if (entity)
  66. {
  67. AZ::Color color = uiDynamicContentDB->GetColor(UiDynamicContentDatabaseInterface::ColorType::PaidColors, index);
  68. UiImageBus::Event(entity->GetId(), &UiImageBus::Events::SetColor, color);
  69. }
  70. }
  71. }
  72. }
  73. ////////////////////////////////////////////////////////////////////////////////////////////////////
  74. // PROTECTED STATIC MEMBER FUNCTIONS
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////
  77. void UiTestScrollBoxDataProviderComponent::Reflect(AZ::ReflectContext* context)
  78. {
  79. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  80. if (serializeContext)
  81. {
  82. serializeContext->Class<UiTestScrollBoxDataProviderComponent, AZ::Component>()
  83. ->Version(1);
  84. AZ::EditContext* ec = serializeContext->GetEditContext();
  85. if (ec)
  86. {
  87. auto editInfo = ec->Class<UiTestScrollBoxDataProviderComponent>("TestScrollBoxDataProvider",
  88. "Associates dynamic data with a dynamic scroll box");
  89. editInfo->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  90. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/UiTestScrollBoxDataProvider.png")
  91. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/UiTestScrollBoxDataProvider.png")
  92. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("UI"));
  93. }
  94. }
  95. }
  96. ////////////////////////////////////////////////////////////////////////////////////////////////////
  97. // PROTECTED MEMBER FUNCTIONS
  98. ////////////////////////////////////////////////////////////////////////////////////////////////////
  99. ////////////////////////////////////////////////////////////////////////////////////////////////////
  100. void UiTestScrollBoxDataProviderComponent::Activate()
  101. {
  102. UiDynamicScrollBoxDataBus::Handler::BusConnect(GetEntityId());
  103. UiDynamicScrollBoxElementNotificationBus::Handler::BusConnect(GetEntityId());
  104. }
  105. ////////////////////////////////////////////////////////////////////////////////////////////////////
  106. void UiTestScrollBoxDataProviderComponent::Deactivate()
  107. {
  108. UiDynamicScrollBoxDataBus::Handler::BusDisconnect();
  109. UiDynamicScrollBoxElementNotificationBus::Handler::BusDisconnect();
  110. }
  111. }