DragHandle.cs 3.9 KB

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