DragHandle.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. namespace SRF.UI
  2. {
  3. using System;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class DragHandle : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
  8. {
  9. private CanvasScaler _canvasScaler;
  10. private float _delta;
  11. private float _startValue;
  12. public RectTransform.Axis Axis = RectTransform.Axis.Horizontal;
  13. public bool Invert = false;
  14. public float MaxSize = -1;
  15. public LayoutElement TargetLayoutElement;
  16. public RectTransform TargetRectTransform;
  17. private float Mult
  18. {
  19. get { return Invert ? -1 : 1; }
  20. }
  21. public void OnBeginDrag(PointerEventData eventData)
  22. {
  23. if (!Verify())
  24. {
  25. return;
  26. }
  27. //Debug.Log("OnBeginDrag");
  28. _startValue = GetCurrentValue();
  29. _delta = 0;
  30. }
  31. public void OnDrag(PointerEventData eventData)
  32. {
  33. if (!Verify())
  34. {
  35. return;
  36. }
  37. //Debug.Log("OnDrag");
  38. var delta = 0f;
  39. if (Axis == RectTransform.Axis.Horizontal)
  40. {
  41. delta += eventData.delta.x;
  42. }
  43. else
  44. {
  45. delta += eventData.delta.y;
  46. }
  47. if (_canvasScaler != null)
  48. {
  49. delta /= _canvasScaler.scaleFactor;
  50. }
  51. delta *= Mult;
  52. _delta += delta;
  53. SetCurrentValue(Mathf.Clamp(_startValue + _delta, GetMinSize(), GetMaxSize()));
  54. }
  55. public void OnEndDrag(PointerEventData eventData)
  56. {
  57. if (!Verify())
  58. {
  59. return;
  60. }
  61. //Debug.Log("OnEndDrag");
  62. SetCurrentValue(Mathf.Max(_startValue + _delta, GetMinSize()));
  63. _delta = 0;
  64. CommitCurrentValue();
  65. }
  66. private void Start()
  67. {
  68. Verify();
  69. _canvasScaler = GetComponentInParent<CanvasScaler>();
  70. }
  71. private bool Verify()
  72. {
  73. if (TargetLayoutElement == null && TargetRectTransform == null)
  74. {
  75. Debug.LogWarning(
  76. "DragHandle: TargetLayoutElement and TargetRectTransform are both null. Disabling behaviour.");
  77. enabled = false;
  78. return false;
  79. }
  80. return true;
  81. }
  82. private float GetCurrentValue()
  83. {
  84. if (TargetLayoutElement != null)
  85. {
  86. return Axis == RectTransform.Axis.Horizontal
  87. ? TargetLayoutElement.preferredWidth
  88. : TargetLayoutElement.preferredHeight;
  89. }
  90. if (TargetRectTransform != null)
  91. {
  92. return Axis == RectTransform.Axis.Horizontal
  93. ? TargetRectTransform.sizeDelta.x
  94. : TargetRectTransform.sizeDelta.y;
  95. }
  96. throw new InvalidOperationException();
  97. }
  98. private void SetCurrentValue(float value)
  99. {
  100. if (TargetLayoutElement != null)
  101. {
  102. if (Axis == RectTransform.Axis.Horizontal)
  103. {
  104. TargetLayoutElement.preferredWidth = value;
  105. }
  106. else
  107. {
  108. TargetLayoutElement.preferredHeight = value;
  109. }
  110. return;
  111. }
  112. if (TargetRectTransform != null)
  113. {
  114. var d = TargetRectTransform.sizeDelta;
  115. if (Axis == RectTransform.Axis.Horizontal)
  116. {
  117. d.x = value;
  118. }
  119. else
  120. {
  121. d.y = value;
  122. }
  123. TargetRectTransform.sizeDelta = d;
  124. return;
  125. }
  126. throw new InvalidOperationException();
  127. }
  128. private void CommitCurrentValue()
  129. {
  130. if (TargetLayoutElement != null)
  131. {
  132. if (Axis == RectTransform.Axis.Horizontal)
  133. {
  134. TargetLayoutElement.preferredWidth = ((RectTransform) TargetLayoutElement.transform).sizeDelta.x;
  135. }
  136. else
  137. {
  138. TargetLayoutElement.preferredHeight = ((RectTransform) TargetLayoutElement.transform).sizeDelta.y;
  139. }
  140. }
  141. }
  142. private float GetMinSize()
  143. {
  144. if (TargetLayoutElement == null)
  145. {
  146. return 0;
  147. }
  148. return Axis == RectTransform.Axis.Horizontal ? TargetLayoutElement.minWidth : TargetLayoutElement.minHeight;
  149. }
  150. private float GetMaxSize()
  151. {
  152. if (MaxSize > 0)
  153. {
  154. return MaxSize;
  155. }
  156. return float.MaxValue;
  157. }
  158. }
  159. }