RKScrollRect.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using UnityEngine.EventSystems;
  2. using UnityEngine;
  3. using System;
  4. namespace Rokid.UXR.Interaction
  5. {
  6. /// <summary>
  7. /// 自定义RKScrollRect 拓展了近场抓取逻辑
  8. /// </summary>
  9. [Obsolete]
  10. public class RKScrollRect : UGUIScrollRect, IGesPinchBeginDrag, IGesPinchDrag, IGesPinchEndDrag
  11. {
  12. [SerializeField]
  13. private bool m_NearDragging = false;
  14. public void OnGesPinchBeginDrag(PointerEventData eventData)
  15. {
  16. RKLog.Debug($"====RKScrollRect==== OnNearGesBeginDrag");
  17. if (eventData.button != PointerEventData.InputButton.Left)
  18. return;
  19. if (!IsActive())
  20. return;
  21. UpdateBounds();
  22. m_PointerStartLocalCursor = viewRect.InverseTransformPoint(eventData.pointerCurrentRaycast.worldPosition);
  23. m_ContentStartPosition = m_Content.anchoredPosition;
  24. m_NearDragging = true;
  25. m_Dragging = true;
  26. }
  27. public void OnGesPinchDrag(PointerEventData eventData)
  28. {
  29. if (!m_NearDragging)
  30. return;
  31. if (eventData.button != PointerEventData.InputButton.Left)
  32. return;
  33. if (!IsActive())
  34. return;
  35. Vector2 localCursor;
  36. localCursor = viewRect.InverseTransformPoint(eventData.pointerCurrentRaycast.worldPosition);
  37. UpdateBounds();
  38. var pointerDelta = localCursor - m_PointerStartLocalCursor;
  39. Vector2 position = m_ContentStartPosition + pointerDelta;
  40. // Offset to get content into place in the view.
  41. Vector2 offset = CalculateOffset(position - m_Content.anchoredPosition);
  42. RKLog.Debug($"====RKScrollRect==== OnNearDrag");
  43. position += offset;
  44. if (m_MovementType == MovementType.Elastic)
  45. {
  46. if (offset.x != 0)
  47. position.x = position.x - RubberDelta(offset.x, m_ViewBounds.size.x);
  48. if (offset.y != 0)
  49. position.y = position.y - RubberDelta(offset.y, m_ViewBounds.size.y);
  50. }
  51. SetContentAnchoredPosition(position);
  52. }
  53. public void OnGesPinchEndDrag(PointerEventData eventData)
  54. {
  55. RKLog.Debug("====RKScrollRect==== OnNearGesEndDrag");
  56. if (eventData.button != PointerEventData.InputButton.Left)
  57. return;
  58. m_NearDragging = false;
  59. m_Dragging = false;
  60. }
  61. public override void OnScroll(PointerEventData data)
  62. {
  63. if (m_NearDragging)
  64. return;
  65. base.OnScroll(data);
  66. }
  67. public override void OnInitializePotentialDrag(PointerEventData eventData)
  68. {
  69. if (m_NearDragging)
  70. return;
  71. base.OnInitializePotentialDrag(eventData);
  72. }
  73. public override void OnBeginDrag(PointerEventData eventData)
  74. {
  75. if (m_NearDragging)
  76. return;
  77. RKLog.Debug($"====RKScrollRect==== OnBeginDrag");
  78. base.OnBeginDrag(eventData);
  79. }
  80. public override void OnDrag(PointerEventData eventData)
  81. {
  82. if (m_NearDragging)
  83. return;
  84. RKLog.Debug($"====RKScrollRect==== OnDrag");
  85. base.OnDrag(eventData);
  86. }
  87. public override void OnEndDrag(PointerEventData eventData)
  88. {
  89. if (m_NearDragging)
  90. return;
  91. RKLog.Debug($"====RKScrollRect==== OnEndDrag");
  92. base.OnEndDrag(eventData);
  93. }
  94. }
  95. }