UiButtonComponent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "UiButtonComponent.h"
  9. #include "Sprite.h"
  10. #include <AzCore/Math/Crc.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/RTTI/BehaviorContext.h>
  14. #include <LyShine/Bus/UiElementBus.h>
  15. #include <LyShine/Bus/UiTransformBus.h>
  16. #include <LyShine/Bus/UiVisualBus.h>
  17. #include <LyShine/Bus/UiCanvasBus.h>
  18. #include <LyShine/ISprite.h>
  19. #include <LyShine/IDraw2d.h>
  20. #include <LyShine/UiSerializeHelpers.h>
  21. #include "UiSerialize.h"
  22. #include "Sprite.h"
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////
  24. //! UiButtonNotificationBus Behavior context handler class
  25. class UiButtonNotificationBusBehaviorHandler
  26. : public UiButtonNotificationBus::Handler
  27. , public AZ::BehaviorEBusHandler
  28. {
  29. public:
  30. AZ_EBUS_BEHAVIOR_BINDER(UiButtonNotificationBusBehaviorHandler, "{8CB61B57-8A99-46AE-ABAC-23384FA5BC96}", AZ::SystemAllocator,
  31. OnButtonClick);
  32. void OnButtonClick() override
  33. {
  34. Call(FN_OnButtonClick);
  35. }
  36. };
  37. ////////////////////////////////////////////////////////////////////////////////////////////////////
  38. // PUBLIC MEMBER FUNCTIONS
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////
  40. ////////////////////////////////////////////////////////////////////////////////////////////////////
  41. UiButtonComponent::UiButtonComponent()
  42. : m_onClick(nullptr)
  43. {
  44. }
  45. ////////////////////////////////////////////////////////////////////////////////////////////////////
  46. UiButtonComponent::~UiButtonComponent()
  47. {
  48. }
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////
  50. bool UiButtonComponent::HandleReleased(AZ::Vector2 point)
  51. {
  52. bool isInRect = false;
  53. UiTransformBus::EventResult(isInRect, GetEntityId(), &UiTransformBus::Events::IsPointInRect, point);
  54. if (isInRect)
  55. {
  56. return HandleReleasedCommon(point);
  57. }
  58. else
  59. {
  60. if (m_isHandlingEvents)
  61. {
  62. UiInteractableComponent::TriggerReleasedAction(true);
  63. }
  64. m_isPressed = false;
  65. return m_isHandlingEvents;
  66. }
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////////////////////////
  69. bool UiButtonComponent::HandleEnterReleased()
  70. {
  71. AZ::Vector2 point(-1.0f, -1.0f);
  72. return HandleReleasedCommon(point);
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////
  75. UiButtonComponent::OnClickCallback UiButtonComponent::GetOnClickCallback()
  76. {
  77. return m_onClick;
  78. }
  79. ////////////////////////////////////////////////////////////////////////////////////////////////////
  80. void UiButtonComponent::SetOnClickCallback(OnClickCallback onClick)
  81. {
  82. m_onClick = onClick;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////////////////////////
  85. const LyShine::ActionName& UiButtonComponent::GetOnClickActionName()
  86. {
  87. return m_actionName;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////
  90. void UiButtonComponent::SetOnClickActionName(const LyShine::ActionName& actionName)
  91. {
  92. m_actionName = actionName;
  93. }
  94. ////////////////////////////////////////////////////////////////////////////////////////////////////
  95. // PROTECTED MEMBER FUNCTIONS
  96. ////////////////////////////////////////////////////////////////////////////////////////////////////
  97. ////////////////////////////////////////////////////////////////////////////////////////////////////
  98. void UiButtonComponent::Activate()
  99. {
  100. UiInteractableComponent::Activate();
  101. UiButtonBus::Handler::BusConnect(m_entity->GetId());
  102. }
  103. ////////////////////////////////////////////////////////////////////////////////////////////////////
  104. void UiButtonComponent::Deactivate()
  105. {
  106. UiInteractableComponent::Deactivate();
  107. UiButtonBus::Handler::BusDisconnect();
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////////////////////////
  110. // PROTECTED STATIC MEMBER FUNCTIONS
  111. ////////////////////////////////////////////////////////////////////////////////////////////////////
  112. ////////////////////////////////////////////////////////////////////////////////////////////////////
  113. void UiButtonComponent::Reflect(AZ::ReflectContext* context)
  114. {
  115. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  116. if (serializeContext)
  117. {
  118. serializeContext->Class<UiButtonComponent, UiInteractableComponent>()
  119. ->Version(5, &VersionConverter)
  120. ->Field("ActionName", &UiButtonComponent::m_actionName);
  121. AZ::EditContext* ec = serializeContext->GetEditContext();
  122. if (ec)
  123. {
  124. auto editInfo = ec->Class<UiButtonComponent>("Button", "An interactable component for button behavior");
  125. editInfo->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  126. ->Attribute(AZ::Edit::Attributes::Category, "UI")
  127. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/UiButton.png")
  128. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/UiButton.png")
  129. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("UI", 0x27ff46b0))
  130. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  131. // Actions group
  132. {
  133. editInfo->ClassElement(AZ::Edit::ClassElements::Group, "Actions")
  134. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  135. editInfo->DataElement(0, &UiButtonComponent::m_actionName, "Click", "The action name triggered when the button is released");
  136. }
  137. }
  138. }
  139. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  140. if (behaviorContext)
  141. {
  142. behaviorContext->EBus<UiButtonBus>("UiButtonBus")
  143. ->Event("GetOnClickActionName", &UiButtonBus::Events::GetOnClickActionName)
  144. ->Event("SetOnClickActionName", &UiButtonBus::Events::SetOnClickActionName);
  145. behaviorContext->EBus<UiButtonNotificationBus>("UiButtonNotificationBus")
  146. ->Handler<UiButtonNotificationBusBehaviorHandler>();
  147. }
  148. }
  149. ////////////////////////////////////////////////////////////////////////////////////////////////////
  150. // PRIVATE MEMBER FUNCTIONS
  151. ////////////////////////////////////////////////////////////////////////////////////////////////////
  152. ////////////////////////////////////////////////////////////////////////////////////////////////////
  153. bool UiButtonComponent::HandleReleasedCommon(const AZ::Vector2& point)
  154. {
  155. if (m_isHandlingEvents)
  156. {
  157. // if a C++ callback is registered for OnClick then call it
  158. if (m_onClick)
  159. {
  160. // NOTE: The signature for the callback will probably change. We currently pass the point at which
  161. // mouse/touch was when released - may not be useful.
  162. m_onClick(GetEntityId(), point);
  163. }
  164. UiInteractableComponent::TriggerReleasedAction();
  165. // Tell any action listeners about the event
  166. if (!m_actionName.empty())
  167. {
  168. AZ::EntityId canvasEntityId;
  169. UiElementBus::EventResult(canvasEntityId, GetEntityId(), &UiElementBus::Events::GetCanvasEntityId);
  170. // Queue the event to prevent deletions during the input event
  171. UiCanvasNotificationBus::QueueEvent(canvasEntityId, &UiCanvasNotificationBus::Events::OnAction, GetEntityId(), m_actionName);
  172. }
  173. // Queue the event to prevent deletions during the input event
  174. UiButtonNotificationBus::QueueEvent(GetEntityId(), &UiButtonNotificationBus::Events::OnButtonClick);
  175. }
  176. m_isPressed = false;
  177. return m_isHandlingEvents;
  178. }
  179. ////////////////////////////////////////////////////////////////////////////////////////////////////
  180. // PRIVATE STATIC MEMBER FUNCTIONS
  181. ////////////////////////////////////////////////////////////////////////////////////////////////////
  182. ////////////////////////////////////////////////////////////////////////////////////////////////////
  183. bool UiButtonComponent::VersionConverter(AZ::SerializeContext& context,
  184. AZ::SerializeContext::DataElementNode& classElement)
  185. {
  186. // conversion from version 1 to 2:
  187. // - Need to convert CryString elements to AZStd::string
  188. // - Need to convert Color to Color and Alpha
  189. // conversion from version 2 to 3:
  190. // - Need to convert CryString ActionName elements to AZStd::string
  191. AZ_Assert(classElement.GetVersion() >= 3, "Unsupported UiButtonComponent version: %d", classElement.GetVersion());
  192. // conversion from version 3 to 4:
  193. // - Need to convert AZStd::string sprites to AzFramework::SimpleAssetReference<LmbrCentral::TextureAsset>
  194. if (classElement.GetVersion() < 4)
  195. {
  196. if (!LyShine::ConvertSubElementFromAzStringToAssetRef<LmbrCentral::TextureAsset>(context, classElement, "SelectedSprite"))
  197. {
  198. return false;
  199. }
  200. if (!LyShine::ConvertSubElementFromAzStringToAssetRef<LmbrCentral::TextureAsset>(context, classElement, "PressedSprite"))
  201. {
  202. return false;
  203. }
  204. if (!LyShine::ConvertSubElementFromAzStringToAssetRef<LmbrCentral::TextureAsset>(context, classElement, "DisabledSprite"))
  205. {
  206. return false;
  207. }
  208. }
  209. // Conversion from version 4 to 5:
  210. if (classElement.GetVersion() < 5)
  211. {
  212. // find the base class (AZ::Component)
  213. // NOTE: in very old versions there may not be a base class because the base class was not serialized
  214. int componentBaseClassIndex = classElement.FindElement(AZ_CRC("BaseClass1", 0xd4925735));
  215. // If there was a base class, make a copy and remove it
  216. AZ::SerializeContext::DataElementNode componentBaseClassNode;
  217. if (componentBaseClassIndex != -1)
  218. {
  219. // make a local copy of the component base class node
  220. componentBaseClassNode = classElement.GetSubElement(componentBaseClassIndex);
  221. // remove the component base class from the button
  222. classElement.RemoveElement(componentBaseClassIndex);
  223. }
  224. // Add a new base class (UiInteractableComponent)
  225. int interactableBaseClassIndex = classElement.AddElement<UiInteractableComponent>(context, "BaseClass1");
  226. AZ::SerializeContext::DataElementNode& interactableBaseClassNode = classElement.GetSubElement(interactableBaseClassIndex);
  227. // if there was previously a base class...
  228. if (componentBaseClassIndex != -1)
  229. {
  230. // copy the component base class into the new interactable base class
  231. // Since AZ::Component is now the base class of UiInteractableComponent
  232. interactableBaseClassNode.AddElement(componentBaseClassNode);
  233. }
  234. // Move the selected/hover state to the base class
  235. if (!UiSerialize::MoveToInteractableStateActions(context, classElement, "HoverStateActions",
  236. "SelectedColor", "SelectedAlpha", "SelectedSprite"))
  237. {
  238. return false;
  239. }
  240. // Move the pressed state to the base class
  241. if (!UiSerialize::MoveToInteractableStateActions(context, classElement, "PressedStateActions",
  242. "PressedColor", "PressedAlpha", "PressedSprite"))
  243. {
  244. return false;
  245. }
  246. // Move the disabled state to the base class
  247. if (!UiSerialize::MoveToInteractableStateActions(context, classElement, "DisabledStateActions",
  248. "DisabledColor", "DisabledAlpha", "DisabledSprite"))
  249. {
  250. return false;
  251. }
  252. }
  253. return true;
  254. }