3
0

UiDropdownOptionComponent.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "UiDropdownOptionComponent.h"
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/std/sort.h>
  13. #include <LyShine/Bus/UiTransformBus.h>
  14. #include <LyShine/Bus/UiElementBus.h>
  15. #include <LyShine/Bus/UiCanvasBus.h>
  16. #include <LyShine/Bus/UiDropdownBus.h>
  17. #include <LyShine/UiSerializeHelpers.h>
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////
  19. //! UiDropdownNotificationBusBehaviorHandler Behavior context handler class
  20. class UiDropdownOptionNotificationBusBehaviorHandler
  21. : public UiDropdownOptionNotificationBus::Handler
  22. , public AZ::BehaviorEBusHandler
  23. {
  24. public:
  25. AZ_EBUS_BEHAVIOR_BINDER(UiDropdownOptionNotificationBusBehaviorHandler, "{3A13D6AF-70BF-4C8D-ACD3-A098FDC8D0C4}", AZ::SystemAllocator,
  26. OnDropdownOptionSelected);
  27. void OnDropdownOptionSelected() override
  28. {
  29. Call(FN_OnDropdownOptionSelected);
  30. }
  31. };
  32. ////////////////////////////////////////////////////////////////////////////////////////////////////
  33. // PUBLIC MEMBER FUNCTIONS
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////
  35. ////////////////////////////////////////////////////////////////////////////////////////////////////
  36. UiDropdownOptionComponent::UiDropdownOptionComponent()
  37. : m_owningDropdown()
  38. , m_textElement()
  39. , m_iconElement()
  40. {
  41. }
  42. ////////////////////////////////////////////////////////////////////////////////////////////////////
  43. UiDropdownOptionComponent::~UiDropdownOptionComponent()
  44. {
  45. }
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////
  47. AZ::EntityId UiDropdownOptionComponent::GetOwningDropdown()
  48. {
  49. return m_owningDropdown;
  50. }
  51. ////////////////////////////////////////////////////////////////////////////////////////////////////
  52. void UiDropdownOptionComponent::SetOwningDropdown(AZ::EntityId owningDropdown)
  53. {
  54. m_owningDropdown = owningDropdown;
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////
  57. AZ::EntityId UiDropdownOptionComponent::GetTextElement()
  58. {
  59. return m_textElement;
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////////////////////////
  62. void UiDropdownOptionComponent::SetTextElement(AZ::EntityId textElement)
  63. {
  64. m_textElement = textElement;
  65. }
  66. ////////////////////////////////////////////////////////////////////////////////////////////////////
  67. AZ::EntityId UiDropdownOptionComponent::GetIconElement()
  68. {
  69. return m_iconElement;
  70. }
  71. ////////////////////////////////////////////////////////////////////////////////////////////////////
  72. void UiDropdownOptionComponent::SetIconElement(AZ::EntityId iconElement)
  73. {
  74. m_iconElement = iconElement;
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////
  77. void UiDropdownOptionComponent::InGamePostActivate()
  78. {
  79. }
  80. ////////////////////////////////////////////////////////////////////////////////////////////////////
  81. void UiDropdownOptionComponent::OnReleased()
  82. {
  83. // Tell our dropdown that we were selected
  84. UiDropdownBus::Event(m_owningDropdown, &UiDropdownBus::Events::SetValue, GetEntityId());
  85. // Tell our listeners that we were selected
  86. UiDropdownOptionNotificationBus::Event(GetEntityId(), &UiDropdownOptionNotificationBus::Events::OnDropdownOptionSelected);
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////////////////////////
  89. // PROTECTED MEMBER FUNCTIONS
  90. ////////////////////////////////////////////////////////////////////////////////////////////////////
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////
  92. void UiDropdownOptionComponent::Activate()
  93. {
  94. UiDropdownOptionBus::Handler::BusConnect(GetEntityId());
  95. UiInitializationBus::Handler::BusConnect(GetEntityId());
  96. UiInteractableNotificationBus::Handler::BusConnect(GetEntityId());
  97. }
  98. ////////////////////////////////////////////////////////////////////////////////////////////////////
  99. void UiDropdownOptionComponent::Deactivate()
  100. {
  101. UiDropdownOptionBus::Handler::BusDisconnect(GetEntityId());
  102. UiInitializationBus::Handler::BusDisconnect(GetEntityId());
  103. UiInteractableNotificationBus::Handler::BusDisconnect(GetEntityId());
  104. }
  105. ////////////////////////////////////////////////////////////////////////////////////////////////////
  106. // PROTECTED STATIC MEMBER FUNCTIONS
  107. ////////////////////////////////////////////////////////////////////////////////////////////////////
  108. void UiDropdownOptionComponent::Reflect(AZ::ReflectContext* context)
  109. {
  110. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  111. if (serializeContext)
  112. {
  113. serializeContext->Class<UiDropdownOptionComponent, AZ::Component>()
  114. ->Version(1)
  115. // Elements group
  116. ->Field("OwningDropdown", &UiDropdownOptionComponent::m_owningDropdown)
  117. ->Field("TextElement", &UiDropdownOptionComponent::m_textElement)
  118. ->Field("IconElement", &UiDropdownOptionComponent::m_iconElement);
  119. AZ::EditContext* ec = serializeContext->GetEditContext();
  120. if (ec)
  121. {
  122. auto editInfo = ec->Class<UiDropdownOptionComponent>("DropdownOption", "An interactable component for DropdownOption behavior.");
  123. editInfo->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  124. ->Attribute(AZ::Edit::Attributes::Category, "UI")
  125. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/UiDropdownOption.png")
  126. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/UiDropdownOption.png")
  127. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("UI", 0x27ff46b0))
  128. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  129. // Elements group
  130. {
  131. editInfo->ClassElement(AZ::Edit::ClassElements::Group, "Elements")
  132. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  133. editInfo->DataElement(AZ::Edit::UIHandlers::ComboBox, &UiDropdownOptionComponent::m_owningDropdown, "Owning Dropdown", "The dropdown this option belongs to (does not have to be its parent dropdown).")
  134. ->Attribute(AZ::Edit::Attributes::EnumValues, &UiDropdownOptionComponent::PopulateDropdownsEntityList);
  135. editInfo->DataElement(AZ::Edit::UIHandlers::ComboBox, &UiDropdownOptionComponent::m_textElement, "Text Element", "The text element to use to show this option is selected.")
  136. ->Attribute(AZ::Edit::Attributes::EnumValues, &UiDropdownOptionComponent::PopulateChildEntityList);
  137. editInfo->DataElement(AZ::Edit::UIHandlers::ComboBox, &UiDropdownOptionComponent::m_iconElement, "Icon Element", "The icon element to use to show this option is selected.")
  138. ->Attribute(AZ::Edit::Attributes::EnumValues, &UiDropdownOptionComponent::PopulateChildEntityList);
  139. }
  140. }
  141. }
  142. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  143. if (behaviorContext)
  144. {
  145. behaviorContext->EBus<UiDropdownOptionBus>("UiDropdownOptionBus")
  146. ->Event("GetOwningDropdown", &UiDropdownOptionBus::Events::GetOwningDropdown)
  147. ->Event("SetOwningDropdown", &UiDropdownOptionBus::Events::SetOwningDropdown)
  148. ->Event("GetTextElement", &UiDropdownOptionBus::Events::GetTextElement)
  149. ->Event("SetTextElement", &UiDropdownOptionBus::Events::SetTextElement)
  150. ->Event("GetIconElement", &UiDropdownOptionBus::Events::GetIconElement)
  151. ->Event("SetIconElement", &UiDropdownOptionBus::Events::SetIconElement);
  152. behaviorContext->EBus<UiDropdownOptionNotificationBus>("UiDropdownOptionNotificationBus")
  153. ->Handler<UiDropdownOptionNotificationBusBehaviorHandler>();
  154. }
  155. }
  156. ////////////////////////////////////////////////////////////////////////////////////////////////////
  157. // PRIVATE MEMBER FUNCTIONS
  158. ////////////////////////////////////////////////////////////////////////////////////////////////////
  159. ////////////////////////////////////////////////////////////////////////////////////////////////////
  160. UiDropdownOptionComponent::EntityComboBoxVec UiDropdownOptionComponent::PopulateDropdownsEntityList()
  161. {
  162. EntityComboBoxVec result;
  163. // add a first entry for "None"
  164. result.push_back(AZStd::make_pair(AZ::EntityId(AZ::EntityId()), "<None>"));
  165. // Get a list of all elements in the canvas with the dropdown component
  166. AZ::EntityId canvasEntityId;
  167. UiElementBus::EventResult(canvasEntityId, GetEntityId(), &UiElementBus::Events::GetCanvasEntityId);
  168. LyShine::EntityArray dropdowns;
  169. UiCanvasBus::Event(
  170. canvasEntityId,
  171. &UiCanvasBus::Events::FindElements,
  172. [](const AZ::Entity* entity)
  173. {
  174. return UiDropdownBus::FindFirstHandler(entity->GetId()) != nullptr;
  175. },
  176. dropdowns);
  177. // Sort the elements by name
  178. AZStd::sort(dropdowns.begin(), dropdowns.end(),
  179. [](const AZ::Entity* e1, const AZ::Entity* e2) { return e1->GetName() < e2->GetName(); });
  180. // add their names to the StringList and their IDs to the id list
  181. for (auto dropdown : dropdowns)
  182. {
  183. result.push_back(AZStd::make_pair(AZ::EntityId(dropdown->GetId()), dropdown->GetName()));
  184. }
  185. return result;
  186. }
  187. ////////////////////////////////////////////////////////////////////////////////////////////////////
  188. UiDropdownOptionComponent::EntityComboBoxVec UiDropdownOptionComponent::PopulateChildEntityList()
  189. {
  190. EntityComboBoxVec result;
  191. // add a first entry for "None"
  192. result.push_back(AZStd::make_pair(AZ::EntityId(AZ::EntityId()), "<None>"));
  193. // Get a list of all child elements
  194. LyShine::EntityArray children;
  195. UiElementBus::EventResult(children, GetEntityId(), &UiElementBus::Events::GetChildElements);
  196. // add their names to the StringList and their IDs to the id list
  197. for (auto childEntity : children)
  198. {
  199. result.push_back(AZStd::make_pair(AZ::EntityId(childEntity->GetId()), childEntity->GetName()));
  200. }
  201. return result;
  202. }