UIDragObject.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [AddComponentMenu("NGUI/Interaction/Drag Object")]
  5. public class UIDragObject : MonoBehaviour
  6. {
  7. public Vector3 dragMovement
  8. {
  9. get
  10. {
  11. return this.scale;
  12. }
  13. set
  14. {
  15. this.scale = value;
  16. }
  17. }
  18. private void OnEnable()
  19. {
  20. if (this.scrollWheelFactor != 0f)
  21. {
  22. this.scrollMomentum = this.scale * this.scrollWheelFactor;
  23. this.scrollWheelFactor = 0f;
  24. }
  25. if (this.contentRect == null && this.target != null && Application.isPlaying)
  26. {
  27. UIWidget component = this.target.GetComponent<UIWidget>();
  28. if (component != null)
  29. {
  30. this.contentRect = component;
  31. }
  32. }
  33. }
  34. private void OnDisable()
  35. {
  36. this.mStarted = false;
  37. }
  38. private void FindPanel()
  39. {
  40. this.panelRegion = ((!(this.target != null)) ? null : UIPanel.Find(this.target.transform.parent));
  41. if (this.panelRegion == null)
  42. {
  43. this.restrictWithinPanel = false;
  44. }
  45. }
  46. private void UpdateBounds()
  47. {
  48. if (this.contentRect)
  49. {
  50. Transform cachedTransform = this.panelRegion.cachedTransform;
  51. Matrix4x4 worldToLocalMatrix = cachedTransform.worldToLocalMatrix;
  52. Vector3[] worldCorners = this.contentRect.worldCorners;
  53. for (int i = 0; i < 4; i++)
  54. {
  55. worldCorners[i] = worldToLocalMatrix.MultiplyPoint3x4(worldCorners[i]);
  56. }
  57. this.mBounds = new Bounds(worldCorners[0], Vector3.zero);
  58. for (int j = 1; j < 4; j++)
  59. {
  60. this.mBounds.Encapsulate(worldCorners[j]);
  61. }
  62. }
  63. else
  64. {
  65. this.mBounds = NGUIMath.CalculateRelativeWidgetBounds(this.panelRegion.cachedTransform, this.target);
  66. }
  67. }
  68. private void OnPress(bool pressed)
  69. {
  70. if (base.enabled && NGUITools.GetActive(base.gameObject) && this.target != null)
  71. {
  72. if (pressed)
  73. {
  74. if (!this.mPressed)
  75. {
  76. this.mTouchID = UICamera.currentTouchID;
  77. this.mPressed = true;
  78. this.mStarted = false;
  79. this.CancelMovement();
  80. if (this.restrictWithinPanel && this.panelRegion == null)
  81. {
  82. this.FindPanel();
  83. }
  84. if (this.restrictWithinPanel)
  85. {
  86. this.UpdateBounds();
  87. }
  88. this.CancelSpring();
  89. Transform transform = UICamera.currentCamera.transform;
  90. this.mPlane = new Plane(((!(this.panelRegion != null)) ? transform.rotation : this.panelRegion.cachedTransform.rotation) * Vector3.back, UICamera.lastWorldPosition);
  91. }
  92. }
  93. else if (this.mPressed && this.mTouchID == UICamera.currentTouchID)
  94. {
  95. this.mPressed = false;
  96. if (this.restrictWithinPanel && this.dragEffect == UIDragObject.DragEffect.MomentumAndSpring && this.panelRegion.ConstrainTargetToBounds(this.target, ref this.mBounds, false))
  97. {
  98. this.CancelMovement();
  99. }
  100. }
  101. }
  102. }
  103. private void OnDrag(Vector2 delta)
  104. {
  105. if (this.mPressed && this.mTouchID == UICamera.currentTouchID && base.enabled && NGUITools.GetActive(base.gameObject) && this.target != null)
  106. {
  107. UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
  108. Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos);
  109. float distance = 0f;
  110. if (this.mPlane.Raycast(ray, out distance))
  111. {
  112. Vector3 point = ray.GetPoint(distance);
  113. Vector3 vector = point - this.mLastPos;
  114. this.mLastPos = point;
  115. if (!this.mStarted)
  116. {
  117. this.mStarted = true;
  118. vector = Vector3.zero;
  119. }
  120. if (vector.x != 0f || vector.y != 0f)
  121. {
  122. vector = this.target.InverseTransformDirection(vector);
  123. vector.Scale(this.scale);
  124. vector = this.target.TransformDirection(vector);
  125. }
  126. if (this.dragEffect != UIDragObject.DragEffect.None)
  127. {
  128. this.mMomentum = Vector3.Lerp(this.mMomentum, this.mMomentum + vector * (0.01f * this.momentumAmount), 0.67f);
  129. }
  130. Vector3 localPosition = this.target.localPosition;
  131. this.Move(vector);
  132. if (this.restrictWithinPanel)
  133. {
  134. this.mBounds.center = this.mBounds.center + (this.target.localPosition - localPosition);
  135. if (this.dragEffect != UIDragObject.DragEffect.MomentumAndSpring && this.panelRegion.ConstrainTargetToBounds(this.target, ref this.mBounds, true))
  136. {
  137. this.CancelMovement();
  138. }
  139. }
  140. }
  141. }
  142. }
  143. private void Move(Vector3 worldDelta)
  144. {
  145. if (this.panelRegion != null)
  146. {
  147. this.mTargetPos += worldDelta;
  148. this.target.position = this.mTargetPos;
  149. Vector3 localPosition = this.target.localPosition;
  150. localPosition.x = Mathf.Round(localPosition.x);
  151. localPosition.y = Mathf.Round(localPosition.y);
  152. this.target.localPosition = localPosition;
  153. UIScrollView component = this.panelRegion.GetComponent<UIScrollView>();
  154. if (component != null)
  155. {
  156. component.UpdateScrollbars(true);
  157. }
  158. }
  159. else
  160. {
  161. this.target.position += worldDelta;
  162. }
  163. }
  164. private void LateUpdate()
  165. {
  166. if (this.target == null)
  167. {
  168. return;
  169. }
  170. float deltaTime = RealTime.deltaTime;
  171. this.mMomentum -= this.mScroll;
  172. this.mScroll = NGUIMath.SpringLerp(this.mScroll, Vector3.zero, 20f, deltaTime);
  173. if (this.mMomentum.magnitude < 0.0001f)
  174. {
  175. return;
  176. }
  177. if (!this.mPressed)
  178. {
  179. if (this.panelRegion == null)
  180. {
  181. this.FindPanel();
  182. }
  183. this.Move(NGUIMath.SpringDampen(ref this.mMomentum, 9f, deltaTime));
  184. if (this.restrictWithinPanel && this.panelRegion != null)
  185. {
  186. this.UpdateBounds();
  187. if (this.panelRegion.ConstrainTargetToBounds(this.target, ref this.mBounds, this.dragEffect == UIDragObject.DragEffect.None))
  188. {
  189. this.CancelMovement();
  190. }
  191. else
  192. {
  193. this.CancelSpring();
  194. }
  195. }
  196. NGUIMath.SpringDampen(ref this.mMomentum, 9f, deltaTime);
  197. if (this.mMomentum.magnitude < 0.0001f)
  198. {
  199. this.CancelMovement();
  200. }
  201. }
  202. else
  203. {
  204. NGUIMath.SpringDampen(ref this.mMomentum, 9f, deltaTime);
  205. }
  206. }
  207. public void CancelMovement()
  208. {
  209. if (this.target != null)
  210. {
  211. Vector3 localPosition = this.target.localPosition;
  212. localPosition.x = (float)Mathf.RoundToInt(localPosition.x);
  213. localPosition.y = (float)Mathf.RoundToInt(localPosition.y);
  214. localPosition.z = (float)Mathf.RoundToInt(localPosition.z);
  215. this.target.localPosition = localPosition;
  216. }
  217. this.mTargetPos = ((!(this.target != null)) ? Vector3.zero : this.target.position);
  218. this.mMomentum = Vector3.zero;
  219. this.mScroll = Vector3.zero;
  220. }
  221. public void CancelSpring()
  222. {
  223. SpringPosition component = this.target.GetComponent<SpringPosition>();
  224. if (component != null)
  225. {
  226. component.enabled = false;
  227. }
  228. }
  229. private void OnScroll(float delta)
  230. {
  231. if (base.enabled && NGUITools.GetActive(base.gameObject))
  232. {
  233. this.mScroll -= this.scrollMomentum * (delta * 0.05f);
  234. }
  235. }
  236. public Transform target;
  237. public UIPanel panelRegion;
  238. public Vector3 scrollMomentum = Vector3.zero;
  239. public bool restrictWithinPanel;
  240. public UIRect contentRect;
  241. public UIDragObject.DragEffect dragEffect = UIDragObject.DragEffect.MomentumAndSpring;
  242. public float momentumAmount = 35f;
  243. [SerializeField]
  244. protected Vector3 scale = new Vector3(1f, 1f, 0f);
  245. [SerializeField]
  246. [HideInInspector]
  247. private float scrollWheelFactor;
  248. private Plane mPlane;
  249. private Vector3 mTargetPos;
  250. private Vector3 mLastPos;
  251. private Vector3 mMomentum = Vector3.zero;
  252. private Vector3 mScroll = Vector3.zero;
  253. private Bounds mBounds;
  254. private int mTouchID;
  255. private bool mStarted;
  256. private bool mPressed;
  257. public enum DragEffect
  258. {
  259. None,
  260. Momentum,
  261. MomentumAndSpring
  262. }
  263. }