UiScrollBarComponent.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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 "UiScrollBarComponent.h"
  9. #include "UiNavigationHelpers.h"
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/RTTI/BehaviorContext.h>
  13. #include <LyShine/Bus/UiCanvasBus.h>
  14. #include <LyShine/Bus/UiElementBus.h>
  15. #include <LyShine/Bus/UiTransform2dBus.h>
  16. #include <LyShine/Bus/UiTransformBus.h>
  17. #include <AzCore/Time/ITime.h>
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////
  19. //! UiScrollerNotificationBus Behavior context handler class
  20. class BehaviorUiScrollerNotificationBusHandler
  21. : public UiScrollerNotificationBus::Handler
  22. , public AZ::BehaviorEBusHandler
  23. {
  24. public:
  25. AZ_EBUS_BEHAVIOR_BINDER(BehaviorUiScrollerNotificationBusHandler, "{77A20EE4-EB8D-431A-B4B1-798805801C4D}", AZ::SystemAllocator,
  26. OnScrollerValueChanging, OnScrollerValueChanged);
  27. void OnScrollerValueChanging(float value) override
  28. {
  29. Call(FN_OnScrollerValueChanging, value);
  30. }
  31. void OnScrollerValueChanged(float value) override
  32. {
  33. Call(FN_OnScrollerValueChanged, value);
  34. }
  35. };
  36. ////////////////////////////////////////////////////////////////////////////////////////////////////
  37. // PUBLIC MEMBER FUNCTIONS
  38. ////////////////////////////////////////////////////////////////////////////////////////////////////
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////
  40. UiScrollBarComponent::UiScrollBarComponent()
  41. : m_value(0.0f)
  42. , m_handleSize(0.1f)
  43. , m_minHandlePixelSize(20.0f)
  44. , m_orientation(Orientation::Horizontal)
  45. , m_isDragging(false)
  46. , m_isActive(false)
  47. , m_lastDragPoint(0.0f, 0.0f)
  48. , m_onValueChanged()
  49. , m_onValueChanging()
  50. , m_valueChangedActionName()
  51. , m_valueChangingActionName()
  52. , m_handleEntity()
  53. , m_scrollableEntity()
  54. {
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////////////////////////
  57. UiScrollBarComponent::~UiScrollBarComponent()
  58. {
  59. }
  60. ////////////////////////////////////////////////////////////////////////////////////////////////////
  61. void UiScrollBarComponent::Update(float deltaTime)
  62. {
  63. UiInteractableComponent::Update(deltaTime);
  64. if (m_isAutoFadeEnabled)
  65. {
  66. // count down secondsBeforeFade
  67. if (!m_isFading && m_secondsRemainingBeforeFade > 0)
  68. {
  69. m_secondsRemainingBeforeFade -= deltaTime;
  70. if (m_secondsRemainingBeforeFade <= 0)
  71. {
  72. m_isFading = true;
  73. // if m_secondsBeforeFade is below 0, use any leftover time for fading
  74. deltaTime = abs(m_secondsRemainingBeforeFade);
  75. }
  76. }
  77. // calculate fade and set alpha for image components
  78. if (m_isFading && m_isAutoFadeEnabled && m_currFade > 0)
  79. {
  80. float deltaFade = deltaTime * m_fadeSpeed;
  81. m_currFade -= deltaFade;
  82. if (m_currFade < 0)
  83. {
  84. m_currFade = 0;
  85. }
  86. SetImageComponentsAlpha(m_currFade);
  87. }
  88. }
  89. }
  90. ////////////////////////////////////////////////////////////////////////////////////////////////////
  91. float UiScrollBarComponent::GetValue()
  92. {
  93. return m_value;
  94. }
  95. ////////////////////////////////////////////////////////////////////////////////////////////////////
  96. void UiScrollBarComponent::SetValue(float value)
  97. {
  98. if (m_value != value)
  99. {
  100. DoSetValue(value);
  101. // Reset drag info
  102. if (m_isDragging)
  103. {
  104. ResetDragInfo();
  105. }
  106. DoChangedActions();
  107. NotifyScrollableOnValueChanged();
  108. }
  109. }
  110. ////////////////////////////////////////////////////////////////////////////////////////////////////
  111. float UiScrollBarComponent::GetHandleSize()
  112. {
  113. return m_handleSize;
  114. }
  115. ////////////////////////////////////////////////////////////////////////////////////////////////////
  116. void UiScrollBarComponent::SetHandleSize(float size)
  117. {
  118. m_handleSize = AZ::GetClamp(size, 0.0f, 1.0f);
  119. // Make sure handle size is at least the minimum size
  120. float handleParentLength = GetHandleParentLength();
  121. float minHandleSize = (handleParentLength > 0.0f) ? m_minHandlePixelSize / handleParentLength : 0.0f;
  122. minHandleSize = AZ::GetClamp(minHandleSize, 0.0f, 1.0f);
  123. m_displayedHandleSize = AZ::GetClamp(m_handleSize, minHandleSize, 1.0f);
  124. if (m_handleEntity.IsValid())
  125. {
  126. // Change handle's anchor
  127. UiTransform2dInterface::Anchors anchors;
  128. UiTransform2dBus::EventResult(anchors, m_handleEntity, &UiTransform2dBus::Events::GetAnchors);
  129. if (m_orientation == Orientation::Horizontal)
  130. {
  131. anchors.m_right = anchors.m_left + m_displayedHandleSize;
  132. }
  133. else if (m_orientation == Orientation::Vertical)
  134. {
  135. anchors.m_bottom = anchors.m_top + m_displayedHandleSize;
  136. }
  137. else
  138. {
  139. AZ_Assert(false, "unhandled scrollbar orientation");
  140. }
  141. UiTransform2dBus::Event(m_handleEntity, &UiTransform2dBus::Events::SetAnchors, anchors, false, false);
  142. // Position handle at the correct location
  143. DoSetValue(m_value);
  144. }
  145. }
  146. ////////////////////////////////////////////////////////////////////////////////////////////////////
  147. float UiScrollBarComponent::GetMinHandlePixelSize()
  148. {
  149. return m_minHandlePixelSize;
  150. }
  151. ////////////////////////////////////////////////////////////////////////////////////////////////////
  152. void UiScrollBarComponent::SetMinHandlePixelSize(float size)
  153. {
  154. m_minHandlePixelSize = size;
  155. }
  156. ////////////////////////////////////////////////////////////////////////////////////////////////////
  157. bool UiScrollBarComponent::IsAutoFadeEnabled()
  158. {
  159. return m_isAutoFadeEnabled;
  160. }
  161. ////////////////////////////////////////////////////////////////////////////////////////////////////
  162. void UiScrollBarComponent::SetAutoFadeEnabled(bool isAutoFadeEnabled)
  163. {
  164. m_isAutoFadeEnabled = isAutoFadeEnabled;
  165. }
  166. ////////////////////////////////////////////////////////////////////////////////////////////////////
  167. float UiScrollBarComponent::GetAutoFadeDelay()
  168. {
  169. return m_inactiveSecondsBeforeFade;
  170. }
  171. ////////////////////////////////////////////////////////////////////////////////////////////////////
  172. void UiScrollBarComponent::SetAutoFadeDelay(float delay)
  173. {
  174. m_inactiveSecondsBeforeFade = delay;
  175. ResetFade();
  176. }
  177. ////////////////////////////////////////////////////////////////////////////////////////////////////
  178. float UiScrollBarComponent::GetAutoFadeSpeed()
  179. {
  180. return m_fadeSpeed;
  181. }
  182. ////////////////////////////////////////////////////////////////////////////////////////////////////
  183. void UiScrollBarComponent::SetAutoFadeSpeed(float speed)
  184. {
  185. m_fadeSpeed = speed;
  186. }
  187. ////////////////////////////////////////////////////////////////////////////////////////////////////
  188. UiScrollerInterface::Orientation UiScrollBarComponent::GetOrientation()
  189. {
  190. return m_orientation;
  191. }
  192. ////////////////////////////////////////////////////////////////////////////////////////////////////
  193. void UiScrollBarComponent::SetOrientation(Orientation orientation)
  194. {
  195. m_orientation = orientation;
  196. }
  197. ////////////////////////////////////////////////////////////////////////////////////////////////////
  198. AZ::EntityId UiScrollBarComponent::GetHandleEntity()
  199. {
  200. return m_handleEntity;
  201. }
  202. ////////////////////////////////////////////////////////////////////////////////////////////////////
  203. void UiScrollBarComponent::SetHandleEntity(AZ::EntityId entityId)
  204. {
  205. m_handleEntity = entityId;
  206. }
  207. ////////////////////////////////////////////////////////////////////////////////////////////////////
  208. AZ::EntityId UiScrollBarComponent::GetScrollableEntity()
  209. {
  210. return m_scrollableEntity;
  211. }
  212. ////////////////////////////////////////////////////////////////////////////////////////////////////
  213. void UiScrollBarComponent::SetScrollableEntity(AZ::EntityId entityId)
  214. {
  215. m_scrollableEntity = entityId;
  216. if (m_scrollableEntity.IsValid())
  217. {
  218. UiScrollableToScrollerNotificationBus::Handler::BusConnect(m_scrollableEntity);
  219. }
  220. }
  221. ////////////////////////////////////////////////////////////////////////////////////////////////////
  222. UiScrollBarComponent::ValueChangeCallback UiScrollBarComponent::GetValueChangingCallback()
  223. {
  224. return m_onValueChanging;
  225. }
  226. ////////////////////////////////////////////////////////////////////////////////////////////////////
  227. void UiScrollBarComponent::SetValueChangingCallback(ValueChangeCallback onChange)
  228. {
  229. m_onValueChanging = onChange;
  230. }
  231. ////////////////////////////////////////////////////////////////////////////////////////////////////
  232. const LyShine::ActionName& UiScrollBarComponent::GetValueChangingActionName()
  233. {
  234. return m_valueChangingActionName;
  235. }
  236. ////////////////////////////////////////////////////////////////////////////////////////////////////
  237. void UiScrollBarComponent::SetValueChangingActionName(const LyShine::ActionName& actionName)
  238. {
  239. m_valueChangingActionName = actionName;
  240. }
  241. ////////////////////////////////////////////////////////////////////////////////////////////////////
  242. UiScrollBarComponent::ValueChangeCallback UiScrollBarComponent::GetValueChangedCallback()
  243. {
  244. return m_onValueChanged;
  245. }
  246. ////////////////////////////////////////////////////////////////////////////////////////////////////
  247. void UiScrollBarComponent::SetValueChangedCallback(ValueChangeCallback onChange)
  248. {
  249. m_onValueChanged = onChange;
  250. }
  251. ////////////////////////////////////////////////////////////////////////////////////////////////////
  252. const LyShine::ActionName& UiScrollBarComponent::GetValueChangedActionName()
  253. {
  254. return m_valueChangedActionName;
  255. }
  256. ////////////////////////////////////////////////////////////////////////////////////////////////////
  257. void UiScrollBarComponent::SetValueChangedActionName(const LyShine::ActionName& actionName)
  258. {
  259. m_valueChangedActionName = actionName;
  260. }
  261. ////////////////////////////////////////////////////////////////////////////////////////////////////
  262. void UiScrollBarComponent::OnValueChangingByScrollable(AZ::Vector2 value)
  263. {
  264. float axisValue = 0.0f;
  265. if (m_orientation == Orientation::Horizontal)
  266. {
  267. axisValue = value.GetX();
  268. }
  269. else if (m_orientation == Orientation::Vertical)
  270. {
  271. axisValue = value.GetY();
  272. }
  273. else
  274. {
  275. AZ_Assert(false, "unhandled scrollbar orientation");
  276. }
  277. if (m_value != axisValue)
  278. {
  279. DoSetValue(axisValue);
  280. // Reset drag info
  281. if (m_isDragging)
  282. {
  283. ResetDragInfo();
  284. }
  285. DoChangingActions();
  286. }
  287. }
  288. ////////////////////////////////////////////////////////////////////////////////////////////////////
  289. void UiScrollBarComponent::OnValueChangedByScrollable(AZ::Vector2 value)
  290. {
  291. float axisValue = 0.0f;
  292. if (m_orientation == Orientation::Horizontal)
  293. {
  294. axisValue = value.GetX();
  295. }
  296. else if (m_orientation == Orientation::Vertical)
  297. {
  298. axisValue = value.GetY();
  299. }
  300. else
  301. {
  302. AZ_Assert(false, "unhandled scrollbar orientation");
  303. }
  304. if (m_value != axisValue)
  305. {
  306. DoSetValue(axisValue);
  307. // Reset drag info
  308. if (m_isDragging)
  309. {
  310. ResetDragInfo();
  311. }
  312. DoChangedActions();
  313. }
  314. }
  315. ////////////////////////////////////////////////////////////////////////////////////////////////////
  316. void UiScrollBarComponent::OnScrollableParentToContentRatioChanged(AZ::Vector2 parentToContentRatio)
  317. {
  318. float axisParentToContentRatio = 1.0f;
  319. if (m_orientation == Orientation::Horizontal)
  320. {
  321. axisParentToContentRatio = parentToContentRatio.GetX();
  322. }
  323. else if (m_orientation == Orientation::Vertical)
  324. {
  325. axisParentToContentRatio = parentToContentRatio.GetY();
  326. }
  327. else
  328. {
  329. AZ_Assert(false, "unhandled scrollbar orientation");
  330. }
  331. SetHandleSize(axisParentToContentRatio);
  332. }
  333. ////////////////////////////////////////////////////////////////////////////////////////////////////
  334. void UiScrollBarComponent::InGamePostActivate()
  335. {
  336. SetHandleSize(m_handleSize);
  337. ResetFade();
  338. UiImageBus::EventResult(m_initialScrollBarAlpha, GetEntityId(), &UiImageBus::Events::GetAlpha);
  339. UiImageBus::EventResult(m_initialHandleAlpha, m_handleEntity, &UiImageBus::Events::GetAlpha);
  340. // Listen for canvas space rect changes
  341. UiTransformChangeNotificationBus::Handler::BusConnect(GetEntityId());
  342. }
  343. ////////////////////////////////////////////////////////////////////////////////////////////////////
  344. bool UiScrollBarComponent::HandlePressed(AZ::Vector2 point, bool& shouldStayActive)
  345. {
  346. bool handled = UiInteractableComponent::HandlePressed(point, shouldStayActive);
  347. if (handled)
  348. {
  349. m_isDragging = false;
  350. m_pressedOnHandle = false;
  351. AZ::Entity* handleParentEntity = nullptr;
  352. UiElementBus::EventResult(handleParentEntity, m_handleEntity, &UiElementBus::Events::GetParent);
  353. if (handleParentEntity)
  354. {
  355. // Check where point is relative to handle
  356. LocRelativeToHandle pointLoc = GetLocationRelativeToHandle(point);
  357. m_pressedOnHandle = (pointLoc == LocRelativeToHandle::OnHandle);
  358. if (m_pressedOnHandle)
  359. {
  360. // Store value when press occurred
  361. m_pressedValue = m_value;
  362. // Store pressed position
  363. m_pressedPosAlongAxis = GetPosAlongAxis(point);
  364. }
  365. else
  366. {
  367. // Move handle
  368. const AZ::TimeMs realTimeMs = AZ::GetRealElapsedTimeMs();
  369. m_lastMoveTime = AZ::TimeMsToSeconds(realTimeMs);
  370. m_moveDelayTime = 0.45f;
  371. MoveHandle(pointLoc);
  372. }
  373. }
  374. }
  375. return handled;
  376. }
  377. ////////////////////////////////////////////////////////////////////////////////////////////////////
  378. bool UiScrollBarComponent::HandleReleased([[maybe_unused]] AZ::Vector2 point)
  379. {
  380. if (m_isPressed && m_isHandlingEvents)
  381. {
  382. UiInteractableComponent::TriggerReleasedAction();
  383. DoChangedActions();
  384. NotifyScrollableOnValueChanged();
  385. }
  386. m_isPressed = false;
  387. m_isDragging = false;
  388. m_pressedPoint = AZ::Vector2(0.0f, 0.0f);
  389. return m_isHandlingEvents;
  390. }
  391. /////////////////////////////////////////////////////////////////
  392. bool UiScrollBarComponent::HandleEnterPressed(bool& shouldStayActive)
  393. {
  394. bool handled = UiInteractableComponent::HandleEnterPressed(shouldStayActive);
  395. if (handled)
  396. {
  397. // the scrollbar will stay active after released
  398. shouldStayActive = true;
  399. m_isActive = true;
  400. }
  401. return handled;
  402. }
  403. /////////////////////////////////////////////////////////////////
  404. bool UiScrollBarComponent::HandleAutoActivation()
  405. {
  406. if (!m_isHandlingEvents)
  407. {
  408. return false;
  409. }
  410. m_isActive = true;
  411. return true;
  412. }
  413. /////////////////////////////////////////////////////////////////
  414. bool UiScrollBarComponent::HandleKeyInputBegan(const AzFramework::InputChannel::Snapshot& inputSnapshot, AzFramework::ModifierKeyMask activeModifierKeys)
  415. {
  416. if (!m_isHandlingEvents)
  417. {
  418. return false;
  419. }
  420. // don't accept key input while in pressed state
  421. if (m_isPressed)
  422. {
  423. return false;
  424. }
  425. bool result = false;
  426. bool moved = false;
  427. const UiNavigationHelpers::Command command = UiNavigationHelpers::MapInputChannelIdToUiNavigationCommand(inputSnapshot.m_channelId, activeModifierKeys);
  428. if (command == UiNavigationHelpers::Command::Up ||
  429. command == UiNavigationHelpers::Command::Down ||
  430. command == UiNavigationHelpers::Command::Left ||
  431. command == UiNavigationHelpers::Command::Right)
  432. {
  433. if ((m_orientation == Orientation::Horizontal) && (command == UiNavigationHelpers::Command::Left || command == UiNavigationHelpers::Command::Right))
  434. {
  435. moved = MoveHandle(command == UiNavigationHelpers::Command::Left ? LocRelativeToHandle::BeforeHandle : LocRelativeToHandle::AfterHandle);
  436. result = true;
  437. }
  438. else if ((m_orientation == Orientation::Vertical) && (command == UiNavigationHelpers::Command::Up || command == UiNavigationHelpers::Command::Down))
  439. {
  440. moved = MoveHandle(command == UiNavigationHelpers::Command::Up ? LocRelativeToHandle::BeforeHandle : LocRelativeToHandle::AfterHandle);
  441. result = true;
  442. }
  443. }
  444. if (moved)
  445. {
  446. DoChangedActions();
  447. NotifyScrollableOnValueChanged();
  448. }
  449. return result;
  450. }
  451. /////////////////////////////////////////////////////////////////
  452. void UiScrollBarComponent::InputPositionUpdate(AZ::Vector2 point)
  453. {
  454. if (m_isPressed)
  455. {
  456. if (m_pressedOnHandle)
  457. {
  458. // if we are not yet in the dragging state do some tests to see if we should be
  459. if (!m_isDragging)
  460. {
  461. bool handOffDone = false;
  462. bool dragDetected = CheckForDragOrHandOffToParent(GetEntityId(), m_pressedPoint, point, 0.0f, handOffDone);
  463. if (dragDetected)
  464. {
  465. if (handOffDone)
  466. {
  467. // the drag was handed off to a parent, this scrollbar is no longer active
  468. m_isPressed = false;
  469. }
  470. else
  471. {
  472. // the drag was valid for this scrollbar, we are now dragging
  473. m_isDragging = true;
  474. m_pressedValue = m_value;
  475. m_pressedPoint = point;
  476. m_pressedPosAlongAxis = GetPosAlongAxis(m_pressedPoint);
  477. }
  478. }
  479. }
  480. // if we are now in the dragging state do the drag of the scrollbar handle
  481. if (m_isDragging)
  482. {
  483. float newValue = m_value;
  484. // Check handle size to see if there is space to scroll
  485. if (m_displayedHandleSize < 1.0f)
  486. {
  487. float handleParentLength = GetHandleParentLength();
  488. if (handleParentLength > 0.0f)
  489. {
  490. float newPosAlongAxis = GetPosAlongAxis(point);
  491. // Calculate drag distance relative to max distance
  492. float dragDistAlongAxis = newPosAlongAxis - m_pressedPosAlongAxis;
  493. float maxDragDistAlongAxis = handleParentLength - (m_displayedHandleSize * handleParentLength);
  494. float valueOffset = dragDistAlongAxis / maxDragDistAlongAxis;
  495. // Update value
  496. newValue = AZ::GetClamp(m_pressedValue + valueOffset, 0.0f, 1.0f);
  497. }
  498. }
  499. m_lastDragPoint = point;
  500. if (newValue != m_value)
  501. {
  502. DoSetValue(newValue);
  503. DoChangingActions();
  504. NotifyScrollableOnValueChanging();
  505. }
  506. }
  507. }
  508. else
  509. {
  510. if (m_handleEntity.IsValid())
  511. {
  512. // Only do something if we're over the interactable
  513. bool isPointInRect = false;
  514. UiTransformBus::EventResult(isPointInRect, GetEntityId(), &UiTransformBus::Events::IsPointInRect, point);
  515. if (isPointInRect)
  516. {
  517. // Only do something if we're on either side of the handle
  518. LocRelativeToHandle pointLoc = GetLocationRelativeToHandle(point);
  519. if (pointLoc != LocRelativeToHandle::OnHandle)
  520. {
  521. const AZ::TimeMs realTimeMs = AZ::GetRealElapsedTimeMs();
  522. const float currentTime = AZ::TimeMsToSeconds(realTimeMs);
  523. if (currentTime - m_lastMoveTime > m_moveDelayTime)
  524. {
  525. m_lastMoveTime = currentTime;
  526. m_moveDelayTime = 0.05f;
  527. MoveHandle(pointLoc);
  528. }
  529. }
  530. }
  531. }
  532. }
  533. }
  534. }
  535. /////////////////////////////////////////////////////////////////
  536. bool UiScrollBarComponent::DoesSupportDragHandOff(AZ::Vector2 startPoint)
  537. {
  538. // this component does support hand-off, so long as the start point is in its bounds
  539. bool isPointInRect = false;
  540. UiTransformBus::EventResult(isPointInRect, GetEntityId(), &UiTransformBus::Events::IsPointInRect, startPoint);
  541. return isPointInRect;
  542. }
  543. /////////////////////////////////////////////////////////////////
  544. bool UiScrollBarComponent::OfferDragHandOff(AZ::EntityId currentActiveInteractable, AZ::Vector2 startPoint, AZ::Vector2 currentPoint, float dragThreshold)
  545. {
  546. bool handedOffToParent = false;
  547. bool dragDetected = CheckForDragOrHandOffToParent(currentActiveInteractable, startPoint, currentPoint, dragThreshold, handedOffToParent);
  548. if (dragDetected)
  549. {
  550. if (!handedOffToParent)
  551. {
  552. // a drag was detected and it was not handed off to a parent, so this scrollbar is now taking the handoff
  553. m_isPressed = true;
  554. m_pressedValue = m_value;
  555. m_pressedPoint = startPoint;
  556. m_pressedPosAlongAxis = GetPosAlongAxis(m_pressedPoint);
  557. m_isDragging = true;
  558. m_lastDragPoint = m_pressedPoint;
  559. // tell the canvas that this is now the active interactable
  560. UiInteractableActiveNotificationBus::Event(
  561. currentActiveInteractable, &UiInteractableActiveNotificationBus::Events::ActiveChanged, GetEntityId(), false);
  562. }
  563. }
  564. return dragDetected;
  565. }
  566. ////////////////////////////////////////////////////////////////////////////////////////////////////
  567. void UiScrollBarComponent::LostActiveStatus()
  568. {
  569. UiInteractableComponent::LostActiveStatus();
  570. if (m_isDragging)
  571. {
  572. if (m_isHandlingEvents)
  573. {
  574. DoChangedActions();
  575. NotifyScrollableOnValueChanged();
  576. }
  577. m_isDragging = false;
  578. }
  579. m_isActive = false;
  580. }
  581. ////////////////////////////////////////////////////////////////////////////////////////////////////
  582. void UiScrollBarComponent::OnCanvasSpaceRectChanged([[maybe_unused]] AZ::EntityId entityId, const UiTransformInterface::Rect& oldRect, const UiTransformInterface::Rect& newRect)
  583. {
  584. // If old rect equals new rect, size changed due to initialization
  585. bool sizeChanged = (oldRect == newRect) || (!oldRect.GetSize().IsClose(newRect.GetSize(), 0.05f));
  586. if (sizeChanged)
  587. {
  588. SetHandleSize(m_handleSize);
  589. }
  590. }
  591. ////////////////////////////////////////////////////////////////////////////////////////////////////
  592. // PROTECTED MEMBER FUNCTIONS
  593. ////////////////////////////////////////////////////////////////////////////////////////////////////
  594. ////////////////////////////////////////////////////////////////////////////////////////////////////
  595. void UiScrollBarComponent::Activate()
  596. {
  597. UiInteractableComponent::Activate();
  598. UiScrollBarBus::Handler::BusConnect(GetEntityId());
  599. UiScrollerBus::Handler::BusConnect(GetEntityId());
  600. UiInitializationBus::Handler::BusConnect(GetEntityId());
  601. }
  602. ////////////////////////////////////////////////////////////////////////////////////////////////////
  603. void UiScrollBarComponent::Deactivate()
  604. {
  605. UiInteractableComponent::Deactivate();
  606. UiScrollBarBus::Handler::BusDisconnect(GetEntityId());
  607. UiScrollerBus::Handler::BusDisconnect(GetEntityId());
  608. UiInitializationBus::Handler::BusDisconnect(GetEntityId());
  609. UiTransformChangeNotificationBus::Handler::BusDisconnect();
  610. if (m_scrollableEntity.IsValid())
  611. {
  612. UiScrollableToScrollerNotificationBus::Handler::BusDisconnect(m_scrollableEntity);
  613. }
  614. }
  615. ////////////////////////////////////////////////////////////////////////////////////////////////////
  616. bool UiScrollBarComponent::IsAutoActivationSupported()
  617. {
  618. return true;
  619. }
  620. ////////////////////////////////////////////////////////////////////////////////////////////////////
  621. UiInteractableStatesInterface::State UiScrollBarComponent::ComputeInteractableState()
  622. {
  623. UiInteractableStatesInterface::State state = UiInteractableStatesInterface::StateNormal;
  624. if (!m_isHandlingEvents)
  625. {
  626. state = UiInteractableStatesInterface::StateDisabled;
  627. }
  628. else if (m_isPressed || m_isActive)
  629. {
  630. // Use pressed state regardless of mouse position
  631. state = UiInteractableStatesInterface::StatePressed;
  632. }
  633. else if (m_isHover)
  634. {
  635. state = UiInteractableStatesInterface::StateHover;
  636. }
  637. return state;
  638. }
  639. ////////////////////////////////////////////////////////////////////////////////////////////////////
  640. // PROTECTED STATIC MEMBER FUNCTIONS
  641. ////////////////////////////////////////////////////////////////////////////////////////////////////
  642. ////////////////////////////////////////////////////////////////////////////////////////////////////
  643. void UiScrollBarComponent::Reflect(AZ::ReflectContext* context)
  644. {
  645. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  646. if (serializeContext)
  647. {
  648. serializeContext->Class<UiScrollBarComponent, UiInteractableComponent>()
  649. ->Version(1)
  650. // Elements group
  651. ->Field("HandleEntity", &UiScrollBarComponent::m_handleEntity)
  652. // Values group
  653. ->Field("Orientation", &UiScrollBarComponent::m_orientation)
  654. ->Field("Value", &UiScrollBarComponent::m_value)
  655. ->Field("HandleSize", &UiScrollBarComponent::m_handleSize)
  656. ->Field("MinHandlePixelSize", &UiScrollBarComponent::m_minHandlePixelSize)
  657. // Actions group
  658. ->Field("ValueChangingActionName", &UiScrollBarComponent::m_valueChangingActionName)
  659. ->Field("ValueChangedActionName", &UiScrollBarComponent::m_valueChangedActionName)
  660. // Visibility group
  661. ->Field("IsAutoFadeEnabled", &UiScrollBarComponent::m_isAutoFadeEnabled)
  662. ->Field("FadeDelay", &UiScrollBarComponent::m_inactiveSecondsBeforeFade)
  663. ->Field("FadeSpeed", &UiScrollBarComponent::m_fadeSpeed);
  664. AZ::EditContext* ec = serializeContext->GetEditContext();
  665. if (ec)
  666. {
  667. auto editInfo = ec->Class<UiScrollBarComponent>("ScrollBar", "An interactable component for scrolling content that is larger than its viewing area.");
  668. editInfo->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  669. ->Attribute(AZ::Edit::Attributes::Category, "UI")
  670. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/UiScrollBar.png")
  671. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/UiScrollBar.png")
  672. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("UI", 0x27ff46b0))
  673. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  674. // Elements group
  675. {
  676. editInfo->ClassElement(AZ::Edit::ClassElements::Group, "Elements")
  677. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  678. editInfo->DataElement(AZ::Edit::UIHandlers::ComboBox, &UiScrollBarComponent::m_handleEntity, "Handle", "The child element that is the sliding handle.")
  679. ->Attribute(AZ::Edit::Attributes::EnumValues, &UiScrollBarComponent::PopulateChildEntityList);
  680. }
  681. // Values group
  682. {
  683. editInfo->ClassElement(AZ::Edit::ClassElements::Group, "Values")
  684. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  685. editInfo->DataElement(AZ::Edit::UIHandlers::ComboBox, &UiScrollBarComponent::m_orientation, "Orientation", "The way the scrollbar should be oriented.")
  686. ->EnumAttribute(Orientation::Horizontal, "Horizontal")
  687. ->EnumAttribute(Orientation::Vertical, "Vertical");
  688. editInfo->DataElement(0, &UiScrollBarComponent::m_value, "Value", "The initial value of the scrollbar.")
  689. ->Attribute(AZ::Edit::Attributes::Step, 0.1f)
  690. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  691. ->Attribute(AZ::Edit::Attributes::Max, 1.0f);
  692. editInfo->DataElement(0, &UiScrollBarComponent::m_handleSize, "Handle size", "The size of the handle relative to the scrollbar.")
  693. ->Attribute(AZ::Edit::Attributes::Step, 0.1f)
  694. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  695. ->Attribute(AZ::Edit::Attributes::Max, 1.0f);
  696. editInfo->DataElement(0, &UiScrollBarComponent::m_minHandlePixelSize, "Min handle size", "The minimum size of the handle in pixels.")
  697. ->Attribute(AZ::Edit::Attributes::Min, 0.0f);
  698. }
  699. // Actions group
  700. {
  701. editInfo->ClassElement(AZ::Edit::ClassElements::Group, "Actions")
  702. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  703. editInfo->DataElement(0, &UiScrollBarComponent::m_valueChangingActionName, "Change", "The action triggered while the value is changing.");
  704. editInfo->DataElement(0, &UiScrollBarComponent::m_valueChangedActionName, "End change", "The action triggered when the value is done changing.");
  705. }
  706. // Visibility group
  707. {
  708. editInfo->ClassElement(AZ::Edit::ClassElements::Group, "Fade")
  709. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  710. editInfo->DataElement(0, &UiScrollBarComponent::m_isAutoFadeEnabled, "Auto Fade When Not In Use", "The scrollbar will automatically fade away when not in use.")
  711. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ_CRC("RefreshEntireTree", 0xefbc823c));
  712. editInfo->DataElement(0, &UiScrollBarComponent::m_inactiveSecondsBeforeFade, "Fade Delay", "The delay in seconds before the scrollbar will begin to fade.")
  713. ->Attribute(AZ::Edit::Attributes::Visibility, &UiScrollBarComponent::m_isAutoFadeEnabled);
  714. editInfo->DataElement(0, &UiScrollBarComponent::m_fadeSpeed, "Fade Speed", "The speed in seconds at which the scrollbar will fade away.")
  715. ->Attribute(AZ::Edit::Attributes::Visibility, &UiScrollBarComponent::m_isAutoFadeEnabled);
  716. }
  717. }
  718. }
  719. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  720. if (behaviorContext)
  721. {
  722. behaviorContext->EBus<UiScrollBarBus>("UiScrollBarBus")
  723. ->Event("GetHandleSize", &UiScrollBarBus::Events::GetHandleSize)
  724. ->Event("SetHandleSize", &UiScrollBarBus::Events::SetHandleSize)
  725. ->Event("GetMinHandlePixelSize", &UiScrollBarBus::Events::GetMinHandlePixelSize)
  726. ->Event("SetMinHandlePixelSize", &UiScrollBarBus::Events::SetMinHandlePixelSize)
  727. ->Event("GetHandleEntity", &UiScrollBarBus::Events::GetHandleEntity)
  728. ->Event("SetHandleEntity", &UiScrollBarBus::Events::SetHandleEntity)
  729. ->VirtualProperty("HandleSize", "GetHandleSize", "SetHandleSize")
  730. ->VirtualProperty("MinHandlePixelSize", "GetMinHandlePixelSize", "SetMinHandlePixelSize")
  731. ->Event("IsAutoFadeEnabled", &UiScrollBarBus::Events::IsAutoFadeEnabled)
  732. ->Event("SetAutoFadeEnabled", &UiScrollBarBus::Events::SetAutoFadeEnabled)
  733. ->Event("GetAutoFadeDelay", &UiScrollBarBus::Events::GetAutoFadeDelay)
  734. ->Event("SetAutoFadeDelay", &UiScrollBarBus::Events::SetAutoFadeDelay)
  735. ->Event("GetAutoFadeSpeed", &UiScrollBarBus::Events::GetAutoFadeSpeed)
  736. ->Event("SetAutoFadeSpeed", &UiScrollBarBus::Events::SetAutoFadeSpeed)
  737. ->VirtualProperty("AutoFadeEnabled", "IsAutoFadeEnabled", "SetAutoFadeEnabled")
  738. ->VirtualProperty("AutoFadeDelay", "GetAutoFadeDelay", "SetAutoFadeDelay")
  739. ->VirtualProperty("AutoFadeSpeed", "GetAutoFadeSpeed", "SetAutoFadeSpeed");
  740. behaviorContext->Enum<(int)UiScrollerInterface::Orientation::Horizontal>("eUiScrollerOrientation_Horizontal")
  741. ->Enum<(int)UiScrollerInterface::Orientation::Vertical>("eUiScrollerOrientation_Vertical");
  742. behaviorContext->EBus<UiScrollerBus>("UiScrollerBus")
  743. ->Event("GetValue", &UiScrollerBus::Events::GetValue)
  744. ->Event("SetValue", &UiScrollerBus::Events::SetValue)
  745. ->Event("GetOrientation", &UiScrollerBus::Events::GetOrientation)
  746. ->Event("SetOrientation", &UiScrollerBus::Events::SetOrientation)
  747. ->Event("GetValueChangingActionName", &UiScrollerBus::Events::GetValueChangingActionName)
  748. ->Event("SetValueChangingActionName", &UiScrollerBus::Events::SetValueChangingActionName)
  749. ->Event("GetValueChangedActionName", &UiScrollerBus::Events::GetValueChangedActionName)
  750. ->Event("SetValueChangedActionName", &UiScrollerBus::Events::SetValueChangedActionName)
  751. ->VirtualProperty("Value", "GetValue", "SetValue");
  752. behaviorContext->EBus<UiScrollerNotificationBus>("UiScrollerNotificationBus")
  753. ->Handler<BehaviorUiScrollerNotificationBusHandler>();
  754. behaviorContext->Class<UiScrollBarComponent>()
  755. ->RequestBus("UiScrollBarBus")
  756. ->RequestBus("UiScrollerBus");
  757. }
  758. }
  759. ////////////////////////////////////////////////////////////////////////////////////////////////////
  760. // PRIVATE MEMBER FUNCTIONS
  761. ////////////////////////////////////////////////////////////////////////////////////////////////////
  762. ////////////////////////////////////////////////////////////////////////////////////////////////////
  763. UiScrollBarComponent::EntityComboBoxVec UiScrollBarComponent::PopulateChildEntityList()
  764. {
  765. EntityComboBoxVec result;
  766. // add a first entry for "None"
  767. result.push_back(AZStd::make_pair(AZ::EntityId(AZ::EntityId()), "<None>"));
  768. // Get a list of all child elements
  769. LyShine::EntityArray matchingElements;
  770. UiElementBus::Event(
  771. GetEntityId(),
  772. &UiElementBus::Events::FindDescendantElements,
  773. []([[maybe_unused]] const AZ::Entity* entity)
  774. {
  775. return true;
  776. },
  777. matchingElements);
  778. // add their names to the StringList and their IDs to the id list
  779. for (auto childEntity : matchingElements)
  780. {
  781. result.push_back(AZStd::make_pair(AZ::EntityId(childEntity->GetId()), childEntity->GetName()));
  782. }
  783. return result;
  784. }
  785. ////////////////////////////////////////////////////////////////////////////////////////////////////
  786. // calculate how much we have dragged along the axis of the scrollbar
  787. float UiScrollBarComponent::GetValidDragDistanceInPixels(AZ::Vector2 startPoint, AZ::Vector2 endPoint)
  788. {
  789. const float validDragRatio = 0.5f;
  790. // convert the drag vector to local space
  791. AZ::Matrix4x4 transformFromViewport;
  792. UiTransformBus::Event(m_entity->GetId(), &UiTransformBus::Events::GetTransformFromViewport, transformFromViewport);
  793. AZ::Vector2 dragVec = endPoint - startPoint;
  794. AZ::Vector3 dragVec3(dragVec.GetX(), dragVec.GetY(), 0.0f);
  795. AZ::Vector3 localDragVec = transformFromViewport.Multiply3x3(dragVec3);
  796. // constrain to the allowed movement directions
  797. if (m_orientation == Orientation::Horizontal)
  798. {
  799. localDragVec.SetY(0.0f);
  800. }
  801. else if (m_orientation == Orientation::Vertical)
  802. {
  803. localDragVec.SetX(0.0f);
  804. }
  805. // convert back to viewport space
  806. AZ::Matrix4x4 transformToViewport;
  807. UiTransformBus::Event(m_entity->GetId(), &UiTransformBus::Events::GetTransformToViewport, transformToViewport);
  808. AZ::Vector3 validDragVec = transformToViewport.Multiply3x3(localDragVec);
  809. float validDistance = validDragVec.GetLengthSq();
  810. float totalDistance = dragVec.GetLengthSq();
  811. // if they are not dragging mostly in a valid direction then ignore the drag
  812. if (validDistance / totalDistance < validDragRatio)
  813. {
  814. validDistance = 0.0f;
  815. }
  816. // return the valid drag distance
  817. return validDistance;
  818. }
  819. ////////////////////////////////////////////////////////////////////////////////////////////////////
  820. bool UiScrollBarComponent::CheckForDragOrHandOffToParent(AZ::EntityId currentActiveInteractable, AZ::Vector2 startPoint, AZ::Vector2 currentPoint, float childDragThreshold, bool& handOffDone)
  821. {
  822. bool result = false;
  823. AZ::EntityId parentDraggable;
  824. UiElementBus::EventResult(parentDraggable, GetEntityId(), &UiElementBus::Events::FindParentInteractableSupportingDrag, startPoint);
  825. // if this interactable is inside another interactable that supports drag then we use
  826. // a threshold value before starting a drag on this interactable
  827. const float normalDragThreshold = 0.0f;
  828. const float containedDragThreshold = 5.0f;
  829. float dragThreshold = normalDragThreshold;
  830. if (childDragThreshold > 0.0f)
  831. {
  832. dragThreshold = childDragThreshold;
  833. }
  834. else if (parentDraggable.IsValid())
  835. {
  836. dragThreshold = containedDragThreshold;
  837. }
  838. // calculate how much we have dragged along the axis of the scrollbar
  839. float validDragDistance = GetValidDragDistanceInPixels(startPoint, currentPoint);
  840. if (validDragDistance > dragThreshold)
  841. {
  842. // we dragged above the threshold value along axis of scrollbar
  843. result = true;
  844. }
  845. else if (parentDraggable.IsValid())
  846. {
  847. // offer the parent draggable the chance to become the active interactable
  848. UiInteractableBus::EventResult(
  849. handOffDone,
  850. parentDraggable,
  851. &UiInteractableBus::Events::OfferDragHandOff,
  852. currentActiveInteractable,
  853. startPoint,
  854. currentPoint,
  855. containedDragThreshold);
  856. if (handOffDone)
  857. {
  858. // interaction has been handed off to a container entity
  859. result = true;
  860. }
  861. }
  862. return result;
  863. }
  864. ////////////////////////////////////////////////////////////////////////////////////////////////////
  865. void UiScrollBarComponent::DoSetValue(float value)
  866. {
  867. m_value = AZ::GetClamp(value, 0.0f, 1.0f);
  868. if (m_handleEntity.IsValid())
  869. {
  870. // Move handle's anchors
  871. UiTransform2dInterface::Anchors anchors;
  872. UiTransform2dBus::EventResult(anchors, m_handleEntity, &UiTransform2dBus::Events::GetAnchors);
  873. if (m_orientation == Orientation::Horizontal)
  874. {
  875. anchors.m_left = (1.0f - m_displayedHandleSize) * m_value;
  876. anchors.m_right = anchors.m_left + m_displayedHandleSize;
  877. }
  878. else if (m_orientation == Orientation::Vertical)
  879. {
  880. anchors.m_top = (1.0f - m_displayedHandleSize) * m_value;
  881. anchors.m_bottom = anchors.m_top + m_displayedHandleSize;
  882. }
  883. else
  884. {
  885. AZ_Assert(false, "unhandled scrollbar orientation");
  886. }
  887. UiTransform2dBus::Event(m_handleEntity, &UiTransform2dBus::Events::SetAnchors, anchors, false, false);
  888. }
  889. }
  890. ////////////////////////////////////////////////////////////////////////////////////////////////////
  891. void UiScrollBarComponent::DoChangedActions()
  892. {
  893. if (m_onValueChanged)
  894. {
  895. m_onValueChanged(GetEntityId(), m_value);
  896. }
  897. // Tell any action listeners about the event
  898. if (!m_valueChangedActionName.empty())
  899. {
  900. AZ::EntityId canvasEntityId;
  901. UiElementBus::EventResult(canvasEntityId, GetEntityId(), &UiElementBus::Events::GetCanvasEntityId);
  902. UiCanvasNotificationBus::Event(canvasEntityId, &UiCanvasNotificationBus::Events::OnAction, GetEntityId(), m_valueChangedActionName);
  903. }
  904. NotifyListenersOnValueChanged();
  905. }
  906. ////////////////////////////////////////////////////////////////////////////////////////////////////
  907. void UiScrollBarComponent::DoChangingActions()
  908. {
  909. if (m_onValueChanging)
  910. {
  911. m_onValueChanging(GetEntityId(), m_value);
  912. }
  913. // Tell any action listeners about the event
  914. if (!m_valueChangingActionName.empty())
  915. {
  916. AZ::EntityId canvasEntityId;
  917. UiElementBus::EventResult(canvasEntityId, GetEntityId(), &UiElementBus::Events::GetCanvasEntityId);
  918. UiCanvasNotificationBus::Event(
  919. canvasEntityId, &UiCanvasNotificationBus::Events::OnAction, GetEntityId(), m_valueChangingActionName);
  920. }
  921. ResetFade();
  922. NotifyListenersOnValueChanging();
  923. }
  924. ////////////////////////////////////////////////////////////////////////////////////////////////////
  925. void UiScrollBarComponent::NotifyListenersOnValueChanged()
  926. {
  927. UiScrollerNotificationBus::Event(GetEntityId(), &UiScrollerNotificationBus::Events::OnScrollerValueChanged, m_value);
  928. }
  929. ////////////////////////////////////////////////////////////////////////////////////////////////////
  930. void UiScrollBarComponent::NotifyListenersOnValueChanging()
  931. {
  932. UiScrollerNotificationBus::Event(GetEntityId(), &UiScrollerNotificationBus::Events::OnScrollerValueChanging, m_value);
  933. }
  934. ////////////////////////////////////////////////////////////////////////////////////////////////////
  935. void UiScrollBarComponent::NotifyScrollableOnValueChanged()
  936. {
  937. UiScrollerToScrollableNotificationBus::Event(
  938. GetEntityId(), &UiScrollerToScrollableNotificationBus::Events::OnValueChangedByScroller, m_value);
  939. }
  940. ////////////////////////////////////////////////////////////////////////////////////////////////////
  941. void UiScrollBarComponent::NotifyScrollableOnValueChanging()
  942. {
  943. UiScrollerToScrollableNotificationBus::Event(
  944. GetEntityId(), &UiScrollerToScrollableNotificationBus::Events::OnValueChangingByScroller, m_value);
  945. }
  946. ////////////////////////////////////////////////////////////////////////////////////////////////////
  947. UiScrollBarComponent::LocRelativeToHandle UiScrollBarComponent::GetLocationRelativeToHandle(const AZ::Vector2& point)
  948. {
  949. // get point in the no scale/rotate canvas space
  950. AZ::Matrix4x4 transform;
  951. UiTransformBus::Event(m_handleEntity, &UiTransformBus::Events::GetTransformFromViewport, transform);
  952. AZ::Vector3 point3(point.GetX(), point.GetY(), 0.0f);
  953. point3 = transform * point3;
  954. // get the rect for this element in the same space
  955. UiTransformInterface::Rect rect;
  956. UiTransformBus::Event(m_handleEntity, &UiTransformBus::Events::GetCanvasSpaceRectNoScaleRotate, rect);
  957. LocRelativeToHandle pointLoc;
  958. if (m_orientation == Orientation::Horizontal)
  959. {
  960. float left = rect.left;
  961. float right = rect.right;
  962. // allow for "flipped" rects
  963. if (left > right)
  964. {
  965. std::swap(left, right);
  966. }
  967. if (point3.GetX() >= left && point3.GetX() <= right)
  968. {
  969. pointLoc = LocRelativeToHandle::OnHandle;
  970. }
  971. else if (point3.GetX() < left)
  972. {
  973. pointLoc = LocRelativeToHandle::BeforeHandle;
  974. }
  975. else // (point3.GetX() > right)
  976. {
  977. pointLoc = LocRelativeToHandle::AfterHandle;
  978. }
  979. }
  980. else if (m_orientation == Orientation::Vertical)
  981. {
  982. float top = rect.top;
  983. float bottom = rect.bottom;
  984. // allow for "flipped" rects
  985. if (top > bottom)
  986. {
  987. std::swap(top, bottom);
  988. }
  989. if (point3.GetY() >= top && point3.GetY() <= bottom)
  990. {
  991. pointLoc = LocRelativeToHandle::OnHandle;
  992. }
  993. else if (point3.GetY() < top)
  994. {
  995. pointLoc = LocRelativeToHandle::BeforeHandle;
  996. }
  997. else // (point3.GetY() > bottom)
  998. {
  999. pointLoc = LocRelativeToHandle::AfterHandle;
  1000. }
  1001. }
  1002. else
  1003. {
  1004. AZ_Assert(false, "unhandled scrollbar orientation");
  1005. pointLoc = LocRelativeToHandle::OnHandle;
  1006. }
  1007. return pointLoc;
  1008. }
  1009. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1010. float UiScrollBarComponent::GetHandleParentLength()
  1011. {
  1012. float handleParentLength = 0.0f;
  1013. AZ::Entity* handleParentEntity = nullptr;
  1014. UiElementBus::EventResult(handleParentEntity, m_handleEntity, &UiElementBus::Events::GetParent);
  1015. if (handleParentEntity)
  1016. {
  1017. AZ::Vector2 size;
  1018. UiTransformBus::EventResult(size, handleParentEntity->GetId(), &UiTransformBus::Events::GetCanvasSpaceSizeNoScaleRotate);
  1019. if (m_orientation == Orientation::Horizontal)
  1020. {
  1021. handleParentLength = size.GetX();
  1022. }
  1023. else if (m_orientation == Orientation::Vertical)
  1024. {
  1025. handleParentLength = size.GetY();
  1026. }
  1027. else
  1028. {
  1029. AZ_Assert(false, "unhandled scrollbar orientation");
  1030. }
  1031. }
  1032. return handleParentLength;
  1033. }
  1034. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1035. float UiScrollBarComponent::GetPosAlongAxis(AZ::Vector2 point)
  1036. {
  1037. float posAlongAxis = 0.0f;
  1038. AZ::Entity* handleParentEntity = nullptr;
  1039. UiElementBus::EventResult(handleParentEntity, m_handleEntity, &UiElementBus::Events::GetParent);
  1040. if (handleParentEntity)
  1041. {
  1042. AZ::Matrix4x4 transform;
  1043. UiTransformBus::Event(handleParentEntity->GetId(), &UiTransformBus::Events::GetTransformFromViewport, transform);
  1044. AZ::Vector3 point3(point.GetX(), point.GetY(), 0.0f);
  1045. point3 = transform * point3;
  1046. if (m_orientation == Orientation::Horizontal)
  1047. {
  1048. posAlongAxis = point3.GetX();
  1049. }
  1050. else if (m_orientation == Orientation::Vertical)
  1051. {
  1052. posAlongAxis = point3.GetY();
  1053. }
  1054. else
  1055. {
  1056. AZ_Assert(false, "unhandled scrollbar orientation");
  1057. }
  1058. }
  1059. return posAlongAxis;
  1060. }
  1061. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1062. bool UiScrollBarComponent::MoveHandle(LocRelativeToHandle pointLoc)
  1063. {
  1064. float valueStep = (m_displayedHandleSize < 1.0f) ? m_displayedHandleSize / (1.0f - m_displayedHandleSize) : 0.0f;
  1065. float newValue = m_value;
  1066. if (pointLoc == LocRelativeToHandle::BeforeHandle)
  1067. {
  1068. newValue -= valueStep;
  1069. }
  1070. else if (pointLoc == LocRelativeToHandle::AfterHandle)
  1071. {
  1072. newValue += valueStep;
  1073. }
  1074. newValue = AZ::GetClamp(newValue, 0.0f, 1.0f);
  1075. if (newValue != m_value)
  1076. {
  1077. DoSetValue(newValue);
  1078. DoChangingActions();
  1079. NotifyScrollableOnValueChanging();
  1080. return true;
  1081. }
  1082. return false;
  1083. }
  1084. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1085. void UiScrollBarComponent::ResetDragInfo()
  1086. {
  1087. m_pressedValue = m_value;
  1088. m_pressedPoint = m_lastDragPoint;
  1089. m_pressedPosAlongAxis = GetPosAlongAxis(m_pressedPoint);
  1090. }
  1091. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1092. void UiScrollBarComponent::SetImageComponentsAlpha(float fade)
  1093. {
  1094. UiImageBus::Event(GetEntityId(), &UiImageBus::Events::SetAlpha, m_initialScrollBarAlpha * fade);
  1095. UiImageBus::Event(m_handleEntity, &UiImageBus::Events::SetAlpha, m_initialHandleAlpha * fade);
  1096. }
  1097. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1098. void UiScrollBarComponent::ResetFade()
  1099. {
  1100. m_currFade = 1;
  1101. m_secondsRemainingBeforeFade = m_inactiveSecondsBeforeFade;
  1102. m_isFading = false;
  1103. SetImageComponentsAlpha(m_currFade);
  1104. }