UiHierarchyInteractivityToggleComponent.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "UiHierarchyInteractivityToggleComponent.h"
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <LyShine/Bus/UiElementBus.h>
  13. #include <LyShine/Bus/UiInteractableBus.h>
  14. AZ_COMPONENT_IMPL(UiHierarchyInteractivityToggleComponent, "UiHierarchyInteractivityToggleComponent", "{B8C5A864-1A98-48B9-BEBB-1FDE06E6D463}");
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. void UiHierarchyInteractivityToggleComponent::Activate()
  17. {
  18. UiHierarchyInteractivityToggleBus::Handler::BusConnect(GetEntityId());
  19. UiInitializationBus::Handler::BusConnect(GetEntityId());
  20. }
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////
  22. void UiHierarchyInteractivityToggleComponent::Deactivate()
  23. {
  24. UiHierarchyInteractivityToggleBus::Handler::BusDisconnect();
  25. }
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////
  27. void UiHierarchyInteractivityToggleComponent::InGamePostActivate()
  28. {
  29. UiInitializationBus::Handler::BusDisconnect();
  30. if (!m_isInteractionLocallyEnabled)
  31. {
  32. SetInteractivity(false);
  33. }
  34. }
  35. ////////////////////////////////////////////////////////////////////////////////////////////////////
  36. bool UiHierarchyInteractivityToggleComponent::SetInteractivity(bool enabled)
  37. {
  38. m_isInteractionLocallyEnabled = enabled;
  39. UpdateInteractiveState();
  40. return true;
  41. }
  42. ////////////////////////////////////////////////////////////////////////////////////////////////////
  43. bool UiHierarchyInteractivityToggleComponent::SetParentInteractivity(bool parentEnabled)
  44. {
  45. m_isInteractionParentEnabled = parentEnabled;
  46. UpdateInteractiveState();
  47. return true;
  48. }
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////
  50. void UiHierarchyInteractivityToggleComponent::UpdateInteractiveState()
  51. {
  52. const bool effectiveState = GetInteractiveState();
  53. // Affect the current entity.
  54. UiInteractableBus::Event(GetEntityId(), &UiInteractableInterface::SetIsHandlingEvents, effectiveState);
  55. UiInteractableBus::Event(GetEntityId(), &UiInteractableInterface::SetIsHandlingMultiTouchEvents, effectiveState);
  56. DoRecursiveSetInteractivityToChildren(GetEntityId(), effectiveState);
  57. }
  58. ////////////////////////////////////////////////////////////////////////////////////////////////////
  59. void UiHierarchyInteractivityToggleComponent::DoRecursiveSetInteractivityToChildren(const AZ::EntityId& parentId, bool parentState)
  60. {
  61. AZStd::vector<AZ::EntityId> children;
  62. UiElementBus::EventResult(children, parentId, &UiElementInterface::GetChildEntityIds);
  63. for (const AZ::EntityId& child : children)
  64. {
  65. // If has InteractivityToggleComp, will return true;
  66. bool hasGroup = false;
  67. UiHierarchyInteractivityToggleBus::EventResult(hasGroup, child, &UiHierarchyInteractivityToggleInterface::SetParentInteractivity, parentState);
  68. // Because no toggle found, affect child and recurse.
  69. if (!hasGroup)
  70. {
  71. // Affect interactable state directly
  72. UiInteractableBus::Event(child, &UiInteractableInterface::SetIsHandlingEvents, parentState);
  73. UiInteractableBus::Event(child, &UiInteractableInterface::SetIsHandlingMultiTouchEvents, parentState);
  74. // Recurse into this child's children
  75. DoRecursiveSetInteractivityToChildren(child, parentState);
  76. }
  77. }
  78. }
  79. ////////////////////////////////////////////////////////////////////////////////////////////////////
  80. void UiHierarchyInteractivityToggleComponent::Reflect(AZ::ReflectContext* context)
  81. {
  82. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  83. {
  84. serializeContext->Class<UiHierarchyInteractivityToggleComponent, AZ::Component>()
  85. ->Version(1)
  86. ->Field("LocalInteraction", &UiHierarchyInteractivityToggleComponent::m_isInteractionLocallyEnabled)
  87. ;
  88. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  89. {
  90. editContext
  91. ->Class<UiHierarchyInteractivityToggleComponent>(
  92. "HierarchyInteractivityToggle", "A grouping handler that allows interaction and rendering for the entire hierarchy of children.")
  93. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  94. ->Attribute(AZ::Edit::Attributes::Category, "UI")
  95. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  96. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("UI"))
  97. ->DataElement(
  98. AZ::Edit::UIHandlers::Default,
  99. &UiHierarchyInteractivityToggleComponent::m_isInteractionLocallyEnabled,
  100. "Is Interactive",
  101. "Whether this entity and children will be interactable.")
  102. ;
  103. }
  104. }
  105. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  106. {
  107. behaviorContext->EBus<UiHierarchyInteractivityToggleBus>("UiHierarchyInteractivityToggleBus")
  108. ->Event("Set Interactive State", &UiHierarchyInteractivityToggleInterface::SetInteractivity)
  109. ->Event("Get Interactive State", &UiHierarchyInteractivityToggleInterface::GetInteractiveState)
  110. ;
  111. }
  112. }
  113. ////////////////////////////////////////////////////////////////////////////////////////////////////
  114. void UiHierarchyInteractivityToggleComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  115. {
  116. provided.push_back(AZ_CRC_CE("UiHierarchyInteractivityToggleComponentService"));
  117. }
  118. ////////////////////////////////////////////////////////////////////////////////////////////////////
  119. void UiHierarchyInteractivityToggleComponent::GetIncompatibleServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  120. {
  121. incompatible.push_back(AZ_CRC_CE("UiHierarchyInteractivityToggleComponentService"));
  122. }
  123. ////////////////////////////////////////////////////////////////////////////////////////////////////
  124. void UiHierarchyInteractivityToggleComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  125. {
  126. required.push_back(AZ_CRC_CE("UiElementService"));
  127. }
  128. ////////////////////////////////////////////////////////////////////////////////////////////////////
  129. void UiHierarchyInteractivityToggleComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  130. {
  131. }