UiNavigationSettings.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "UiNavigationSettings.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. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // PUBLIC MEMBER FUNCTIONS
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////
  17. UiNavigationSettings::UiNavigationSettings()
  18. : m_navigationMode(NavigationMode::Automatic)
  19. , m_onUpEntity()
  20. , m_onDownEntity()
  21. , m_onLeftEntity()
  22. , m_onRightEntity()
  23. {
  24. }
  25. ////////////////////////////////////////////////////////////////////////////////////////////////////
  26. UiNavigationSettings::~UiNavigationSettings()
  27. {
  28. }
  29. ////////////////////////////////////////////////////////////////////////////////////////////////////
  30. UiNavigationInterface::NavigationMode UiNavigationSettings::GetNavigationMode()
  31. {
  32. return m_navigationMode;
  33. }
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////
  35. void UiNavigationSettings::SetNavigationMode(NavigationMode navigationMode)
  36. {
  37. m_navigationMode = navigationMode;
  38. }
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////
  40. AZ::EntityId UiNavigationSettings::GetOnUpEntity()
  41. {
  42. return m_onUpEntity;
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////
  45. void UiNavigationSettings::SetOnUpEntity(AZ::EntityId entityId)
  46. {
  47. m_onUpEntity = entityId;
  48. }
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////
  50. AZ::EntityId UiNavigationSettings::GetOnDownEntity()
  51. {
  52. return m_onDownEntity;
  53. }
  54. ////////////////////////////////////////////////////////////////////////////////////////////////////
  55. void UiNavigationSettings::SetOnDownEntity(AZ::EntityId entityId)
  56. {
  57. m_onDownEntity = entityId;
  58. }
  59. ////////////////////////////////////////////////////////////////////////////////////////////////////
  60. AZ::EntityId UiNavigationSettings::GetOnLeftEntity()
  61. {
  62. return m_onLeftEntity;
  63. }
  64. ////////////////////////////////////////////////////////////////////////////////////////////////////
  65. void UiNavigationSettings::SetOnLeftEntity(AZ::EntityId entityId)
  66. {
  67. m_onLeftEntity = entityId;
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////////////////////////
  70. AZ::EntityId UiNavigationSettings::GetOnRightEntity()
  71. {
  72. return m_onRightEntity;
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////
  75. void UiNavigationSettings::SetOnRightEntity(AZ::EntityId entityId)
  76. {
  77. m_onRightEntity = entityId;
  78. }
  79. ////////////////////////////////////////////////////////////////////////////////////////////////////
  80. void UiNavigationSettings::Activate(AZ::EntityId entityId, GetNavigableEntitiesFn getNavigableFn)
  81. {
  82. m_entityId = entityId;
  83. m_getNavigableEntitiesFunction = getNavigableFn;
  84. UiNavigationBus::Handler::BusConnect(entityId);
  85. }
  86. ////////////////////////////////////////////////////////////////////////////////////////////////////
  87. void UiNavigationSettings::Deactivate()
  88. {
  89. UiNavigationBus::Handler::BusDisconnect();
  90. }
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////
  92. // PUBLIC STATIC MEMBER FUNCTIONS
  93. ////////////////////////////////////////////////////////////////////////////////////////////////////
  94. ////////////////////////////////////////////////////////////////////////////////////////////////////
  95. void UiNavigationSettings::Reflect(AZ::ReflectContext* context)
  96. {
  97. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  98. if (serializeContext)
  99. {
  100. serializeContext->Class<UiNavigationSettings>()
  101. ->Version(1)
  102. ->Field("NavigationMode", &UiNavigationSettings::m_navigationMode)
  103. ->Field("OnUpEntity", &UiNavigationSettings::m_onUpEntity)
  104. ->Field("OnDownEntity", &UiNavigationSettings::m_onDownEntity)
  105. ->Field("OnLeftEntity", &UiNavigationSettings::m_onLeftEntity)
  106. ->Field("OnRightEntity", &UiNavigationSettings::m_onRightEntity);
  107. AZ::EditContext* ec = serializeContext->GetEditContext();
  108. if (ec)
  109. {
  110. auto editInfo = ec->Class<UiNavigationSettings>("Navigation", "Navigation settings");
  111. editInfo->DataElement(AZ::Edit::UIHandlers::ComboBox, &UiNavigationSettings::m_navigationMode, "Mode",
  112. "Determines how the next element to receive focus is chosen when a navigation event occurs")
  113. ->EnumAttribute(NavigationMode::Automatic, "Automatic")
  114. ->EnumAttribute(NavigationMode::Custom, "Custom")
  115. ->EnumAttribute(NavigationMode::None, "None")
  116. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ_CRC("RefreshEntireTree", 0xefbc823c));
  117. editInfo->DataElement(AZ::Edit::UIHandlers::ComboBox, &UiNavigationSettings::m_onUpEntity, "Up Element", "The element to receive focus on an up event")
  118. ->Attribute(AZ::Edit::Attributes::EnumValues, &UiNavigationSettings::PopulateNavigableEntityList)
  119. ->Attribute(AZ::Edit::Attributes::Visibility, &UiNavigationSettings::IsNavigationModeCustom);
  120. editInfo->DataElement(AZ::Edit::UIHandlers::ComboBox, &UiNavigationSettings::m_onDownEntity, "Down Element", "The element to receive focus on a down event")
  121. ->Attribute(AZ::Edit::Attributes::EnumValues, &UiNavigationSettings::PopulateNavigableEntityList)
  122. ->Attribute(AZ::Edit::Attributes::Visibility, &UiNavigationSettings::IsNavigationModeCustom);
  123. editInfo->DataElement(AZ::Edit::UIHandlers::ComboBox, &UiNavigationSettings::m_onLeftEntity, "Left Element", "The element to receive focus on a left event")
  124. ->Attribute(AZ::Edit::Attributes::EnumValues, &UiNavigationSettings::PopulateNavigableEntityList)
  125. ->Attribute(AZ::Edit::Attributes::Visibility, &UiNavigationSettings::IsNavigationModeCustom);
  126. editInfo->DataElement(AZ::Edit::UIHandlers::ComboBox, &UiNavigationSettings::m_onRightEntity, "Right Element", "The element to receive focus on a right event")
  127. ->Attribute(AZ::Edit::Attributes::EnumValues, &UiNavigationSettings::PopulateNavigableEntityList)
  128. ->Attribute(AZ::Edit::Attributes::Visibility, &UiNavigationSettings::IsNavigationModeCustom);
  129. }
  130. }
  131. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  132. if (behaviorContext)
  133. {
  134. behaviorContext->Enum<(int)UiNavigationInterface::NavigationMode::Automatic>("eUiNavigationMode_Automatic")
  135. ->Enum<(int)UiNavigationInterface::NavigationMode::Custom>("eUiNavigationMode_Custom")
  136. ->Enum<(int)UiNavigationInterface::NavigationMode::None>("eUiNavigationMode_None");
  137. behaviorContext->EBus<UiNavigationBus>("UiNavigationBus")
  138. ->Event("GetNavigationMode", &UiNavigationBus::Events::GetNavigationMode)
  139. ->Event("SetNavigationMode", &UiNavigationBus::Events::SetNavigationMode)
  140. ->Event("GetOnUpEntity", &UiNavigationBus::Events::GetOnUpEntity)
  141. ->Event("SetOnUpEntity", &UiNavigationBus::Events::SetOnUpEntity)
  142. ->Event("GetOnDownEntity", &UiNavigationBus::Events::GetOnDownEntity)
  143. ->Event("SetOnDownEntity", &UiNavigationBus::Events::SetOnDownEntity)
  144. ->Event("GetOnLeftEntity", &UiNavigationBus::Events::GetOnLeftEntity)
  145. ->Event("SetOnLeftEntity", &UiNavigationBus::Events::SetOnLeftEntity)
  146. ->Event("GetOnRightEntity", &UiNavigationBus::Events::GetOnRightEntity)
  147. ->Event("SetOnRightEntity", &UiNavigationBus::Events::SetOnRightEntity);
  148. }
  149. }
  150. ////////////////////////////////////////////////////////////////////////////////////////////////////
  151. // PRIVATE MEMBER FUNCTIONS
  152. ////////////////////////////////////////////////////////////////////////////////////////////////////
  153. ////////////////////////////////////////////////////////////////////////////////////////////////////
  154. UiNavigationSettings::EntityComboBoxVec UiNavigationSettings::PopulateNavigableEntityList()
  155. {
  156. EntityComboBoxVec result;
  157. // Add a first entry for "None"
  158. result.push_back(AZStd::make_pair(AZ::EntityId(), "<None>"));
  159. // Get a list of all navigable elements using callback function
  160. LyShine::EntityArray navigableElements = m_getNavigableEntitiesFunction(m_entityId);
  161. // Sort the elements by name
  162. AZStd::sort(navigableElements.begin(), navigableElements.end(),
  163. [](const AZ::Entity* e1, const AZ::Entity* e2) { return e1->GetName() < e2->GetName(); });
  164. // Add their names to the StringList and their IDs to the id list
  165. for (auto navigableEntity : navigableElements)
  166. {
  167. result.push_back(AZStd::make_pair(navigableEntity->GetId(), navigableEntity->GetName()));
  168. }
  169. return result;
  170. }
  171. ////////////////////////////////////////////////////////////////////////////////////////////////////
  172. bool UiNavigationSettings::IsNavigationModeCustom() const
  173. {
  174. return (m_navigationMode == NavigationMode::Custom);
  175. }