UIDragDropItem.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. [AddComponentMenu("NGUI/Interaction/Drag and Drop Item")]
  5. public class UIDragDropItem : MonoBehaviour
  6. {
  7. protected virtual void Start()
  8. {
  9. this.mTrans = base.transform;
  10. this.mCollider = base.gameObject.GetComponent<Collider>();
  11. this.mCollider2D = base.gameObject.GetComponent<Collider2D>();
  12. this.mButton = base.GetComponent<UIButton>();
  13. this.mDragScrollView = base.GetComponent<UIDragScrollView>();
  14. }
  15. protected void OnPress(bool isPressed)
  16. {
  17. if (isPressed)
  18. {
  19. this.mDragStartTime = RealTime.time + this.pressAndHoldDelay;
  20. this.mPressed = true;
  21. }
  22. else if (this.mPressed)
  23. {
  24. this.StopDragging(UICamera.hoveredObject);
  25. this.mPressed = false;
  26. }
  27. }
  28. protected virtual void Update()
  29. {
  30. if (this.restriction == UIDragDropItem.Restriction.PressAndHold && this.mPressed && !this.mDragging && this.mDragStartTime < RealTime.time)
  31. {
  32. this.StartDragging();
  33. }
  34. }
  35. protected void OnDragStart()
  36. {
  37. if (!base.enabled || this.mTouchID != -2147483648)
  38. {
  39. return;
  40. }
  41. if (this.restriction != UIDragDropItem.Restriction.None)
  42. {
  43. if (this.restriction == UIDragDropItem.Restriction.Horizontal)
  44. {
  45. Vector2 totalDelta = UICamera.currentTouch.totalDelta;
  46. if (Mathf.Abs(totalDelta.x) < Mathf.Abs(totalDelta.y))
  47. {
  48. return;
  49. }
  50. }
  51. else if (this.restriction == UIDragDropItem.Restriction.Vertical)
  52. {
  53. Vector2 totalDelta2 = UICamera.currentTouch.totalDelta;
  54. if (Mathf.Abs(totalDelta2.x) > Mathf.Abs(totalDelta2.y))
  55. {
  56. return;
  57. }
  58. }
  59. else if (this.restriction == UIDragDropItem.Restriction.PressAndHold)
  60. {
  61. return;
  62. }
  63. }
  64. this.StartDragging();
  65. }
  66. protected virtual void StartDragging()
  67. {
  68. if (!this.mDragging)
  69. {
  70. if (this.cloneOnDrag)
  71. {
  72. GameObject gameObject = NGUITools.AddChild(base.transform.parent.gameObject, base.gameObject);
  73. gameObject.transform.localPosition = base.transform.localPosition;
  74. gameObject.transform.localRotation = base.transform.localRotation;
  75. gameObject.transform.localScale = base.transform.localScale;
  76. UIButtonColor component = gameObject.GetComponent<UIButtonColor>();
  77. if (component != null)
  78. {
  79. component.defaultColor = base.GetComponent<UIButtonColor>().defaultColor;
  80. }
  81. UICamera.currentTouch.dragged = gameObject;
  82. UIDragDropItem component2 = gameObject.GetComponent<UIDragDropItem>();
  83. component2.mDragging = true;
  84. component2.Start();
  85. component2.OnDragDropStart();
  86. }
  87. else
  88. {
  89. this.mDragging = true;
  90. this.OnDragDropStart();
  91. }
  92. }
  93. }
  94. protected void OnDrag(Vector2 delta)
  95. {
  96. if (!this.mDragging || !base.enabled || this.mTouchID != UICamera.currentTouchID)
  97. {
  98. return;
  99. }
  100. this.OnDragDropMove(delta * this.mRoot.pixelSizeAdjustment);
  101. }
  102. protected void OnDragEnd()
  103. {
  104. if (!base.enabled || this.mTouchID != UICamera.currentTouchID)
  105. {
  106. return;
  107. }
  108. this.StopDragging(UICamera.hoveredObject);
  109. }
  110. public void StopDragging(GameObject go)
  111. {
  112. if (this.mDragging)
  113. {
  114. this.mDragging = false;
  115. this.OnDragDropRelease(go);
  116. }
  117. }
  118. protected virtual void OnDragDropStart()
  119. {
  120. if (this.mDragScrollView != null)
  121. {
  122. this.mDragScrollView.enabled = false;
  123. }
  124. if (this.mButton != null)
  125. {
  126. this.mButton.isEnabled = false;
  127. }
  128. else if (this.mCollider != null)
  129. {
  130. this.mCollider.enabled = false;
  131. }
  132. else if (this.mCollider2D != null)
  133. {
  134. this.mCollider2D.enabled = false;
  135. }
  136. this.mTouchID = UICamera.currentTouchID;
  137. this.mParent = this.mTrans.parent;
  138. this.mRoot = NGUITools.FindInParents<UIRoot>(this.mParent);
  139. this.mGrid = NGUITools.FindInParents<UIGrid>(this.mParent);
  140. this.mTable = NGUITools.FindInParents<UITable>(this.mParent);
  141. if (UIDragDropRoot.root != null)
  142. {
  143. this.mTrans.parent = UIDragDropRoot.root;
  144. }
  145. Vector3 localPosition = this.TaregetTrans.localPosition;
  146. localPosition.z = 0f;
  147. this.TaregetTrans.localPosition = localPosition;
  148. TweenPosition component = base.GetComponent<TweenPosition>();
  149. if (component != null)
  150. {
  151. component.enabled = false;
  152. }
  153. SpringPosition component2 = base.GetComponent<SpringPosition>();
  154. if (component2 != null)
  155. {
  156. component2.enabled = false;
  157. }
  158. NGUITools.MarkParentAsChanged(base.gameObject);
  159. if (this.mTable != null)
  160. {
  161. this.mTable.repositionNow = true;
  162. }
  163. if (this.mGrid != null)
  164. {
  165. this.mGrid.repositionNow = true;
  166. }
  167. }
  168. protected virtual void OnDragDropMove(Vector2 delta)
  169. {
  170. this.TaregetTrans.localPosition += delta;
  171. }
  172. protected virtual void OnDragDropRelease(GameObject surface)
  173. {
  174. if (!this.cloneOnDrag)
  175. {
  176. this.mTouchID = int.MinValue;
  177. if (this.mButton != null)
  178. {
  179. this.mButton.isEnabled = true;
  180. }
  181. else if (this.mCollider != null)
  182. {
  183. this.mCollider.enabled = true;
  184. }
  185. else if (this.mCollider2D != null)
  186. {
  187. this.mCollider2D.enabled = true;
  188. }
  189. UIDragDropContainer uidragDropContainer = (!surface) ? null : NGUITools.FindInParents<UIDragDropContainer>(surface);
  190. if (uidragDropContainer != null)
  191. {
  192. this.mTrans.parent = ((!(uidragDropContainer.reparentTarget != null)) ? uidragDropContainer.transform : uidragDropContainer.reparentTarget);
  193. Vector3 localPosition = this.TaregetTrans.localPosition;
  194. localPosition.z = 0f;
  195. this.TaregetTrans.localPosition = localPosition;
  196. }
  197. else
  198. {
  199. this.mTrans.parent = this.mParent;
  200. }
  201. this.mParent = this.mTrans.parent;
  202. this.mGrid = NGUITools.FindInParents<UIGrid>(this.mParent);
  203. this.mTable = NGUITools.FindInParents<UITable>(this.mParent);
  204. if (this.mDragScrollView != null)
  205. {
  206. base.StartCoroutine(this.EnableDragScrollView());
  207. }
  208. NGUITools.MarkParentAsChanged(base.gameObject);
  209. if (this.mTable != null)
  210. {
  211. this.mTable.repositionNow = true;
  212. }
  213. if (this.mGrid != null)
  214. {
  215. this.mGrid.repositionNow = true;
  216. }
  217. this.OnDragDropEnd();
  218. }
  219. else
  220. {
  221. NGUITools.Destroy(base.gameObject);
  222. }
  223. }
  224. protected virtual void OnDragDropEnd()
  225. {
  226. }
  227. protected IEnumerator EnableDragScrollView()
  228. {
  229. yield return new WaitForEndOfFrame();
  230. if (this.mDragScrollView != null)
  231. {
  232. this.mDragScrollView.enabled = true;
  233. }
  234. yield break;
  235. }
  236. protected virtual Transform TaregetTrans
  237. {
  238. get
  239. {
  240. return this.mTrans;
  241. }
  242. }
  243. public UIDragDropItem.Restriction restriction;
  244. public bool cloneOnDrag;
  245. [HideInInspector]
  246. public float pressAndHoldDelay = 1f;
  247. protected Transform mTrans;
  248. protected Transform mParent;
  249. protected Collider mCollider;
  250. protected Collider2D mCollider2D;
  251. protected UIButton mButton;
  252. protected UIRoot mRoot;
  253. protected UIGrid mGrid;
  254. protected UITable mTable;
  255. protected int mTouchID = int.MinValue;
  256. protected float mDragStartTime;
  257. protected UIDragScrollView mDragScrollView;
  258. protected bool mPressed;
  259. protected bool mDragging;
  260. public enum Restriction
  261. {
  262. None,
  263. Horizontal,
  264. Vertical,
  265. PressAndHold
  266. }
  267. }