123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- using System;
- using System.Collections;
- using UnityEngine;
- [AddComponentMenu("NGUI/Interaction/Drag and Drop Item")]
- public class UIDragDropItem : MonoBehaviour
- {
- protected virtual void Start()
- {
- this.mTrans = base.transform;
- this.mCollider = base.gameObject.GetComponent<Collider>();
- this.mCollider2D = base.gameObject.GetComponent<Collider2D>();
- this.mButton = base.GetComponent<UIButton>();
- this.mDragScrollView = base.GetComponent<UIDragScrollView>();
- }
- protected void OnPress(bool isPressed)
- {
- if (isPressed)
- {
- this.mDragStartTime = RealTime.time + this.pressAndHoldDelay;
- this.mPressed = true;
- }
- else if (this.mPressed)
- {
- this.StopDragging(UICamera.hoveredObject);
- this.mPressed = false;
- }
- }
- protected virtual void Update()
- {
- if (this.restriction == UIDragDropItem.Restriction.PressAndHold && this.mPressed && !this.mDragging && this.mDragStartTime < RealTime.time)
- {
- this.StartDragging();
- }
- }
- protected void OnDragStart()
- {
- if (!base.enabled || this.mTouchID != -2147483648)
- {
- return;
- }
- if (this.restriction != UIDragDropItem.Restriction.None)
- {
- if (this.restriction == UIDragDropItem.Restriction.Horizontal)
- {
- Vector2 totalDelta = UICamera.currentTouch.totalDelta;
- if (Mathf.Abs(totalDelta.x) < Mathf.Abs(totalDelta.y))
- {
- return;
- }
- }
- else if (this.restriction == UIDragDropItem.Restriction.Vertical)
- {
- Vector2 totalDelta2 = UICamera.currentTouch.totalDelta;
- if (Mathf.Abs(totalDelta2.x) > Mathf.Abs(totalDelta2.y))
- {
- return;
- }
- }
- else if (this.restriction == UIDragDropItem.Restriction.PressAndHold)
- {
- return;
- }
- }
- this.StartDragging();
- }
- protected virtual void StartDragging()
- {
- if (!this.mDragging)
- {
- if (this.cloneOnDrag)
- {
- GameObject gameObject = NGUITools.AddChild(base.transform.parent.gameObject, base.gameObject);
- gameObject.transform.localPosition = base.transform.localPosition;
- gameObject.transform.localRotation = base.transform.localRotation;
- gameObject.transform.localScale = base.transform.localScale;
- UIButtonColor component = gameObject.GetComponent<UIButtonColor>();
- if (component != null)
- {
- component.defaultColor = base.GetComponent<UIButtonColor>().defaultColor;
- }
- UICamera.currentTouch.dragged = gameObject;
- UIDragDropItem component2 = gameObject.GetComponent<UIDragDropItem>();
- component2.mDragging = true;
- component2.Start();
- component2.OnDragDropStart();
- }
- else
- {
- this.mDragging = true;
- this.OnDragDropStart();
- }
- }
- }
- protected void OnDrag(Vector2 delta)
- {
- if (!this.mDragging || !base.enabled || this.mTouchID != UICamera.currentTouchID)
- {
- return;
- }
- this.OnDragDropMove(delta * this.mRoot.pixelSizeAdjustment);
- }
- protected void OnDragEnd()
- {
- if (!base.enabled || this.mTouchID != UICamera.currentTouchID)
- {
- return;
- }
- this.StopDragging(UICamera.hoveredObject);
- }
- public void StopDragging(GameObject go)
- {
- if (this.mDragging)
- {
- this.mDragging = false;
- this.OnDragDropRelease(go);
- }
- }
- protected virtual void OnDragDropStart()
- {
- if (this.mDragScrollView != null)
- {
- this.mDragScrollView.enabled = false;
- }
- if (this.mButton != null)
- {
- this.mButton.isEnabled = false;
- }
- else if (this.mCollider != null)
- {
- this.mCollider.enabled = false;
- }
- else if (this.mCollider2D != null)
- {
- this.mCollider2D.enabled = false;
- }
- this.mTouchID = UICamera.currentTouchID;
- this.mParent = this.mTrans.parent;
- this.mRoot = NGUITools.FindInParents<UIRoot>(this.mParent);
- this.mGrid = NGUITools.FindInParents<UIGrid>(this.mParent);
- this.mTable = NGUITools.FindInParents<UITable>(this.mParent);
- if (UIDragDropRoot.root != null)
- {
- this.mTrans.parent = UIDragDropRoot.root;
- }
- Vector3 localPosition = this.TaregetTrans.localPosition;
- localPosition.z = 0f;
- this.TaregetTrans.localPosition = localPosition;
- TweenPosition component = base.GetComponent<TweenPosition>();
- if (component != null)
- {
- component.enabled = false;
- }
- SpringPosition component2 = base.GetComponent<SpringPosition>();
- if (component2 != null)
- {
- component2.enabled = false;
- }
- NGUITools.MarkParentAsChanged(base.gameObject);
- if (this.mTable != null)
- {
- this.mTable.repositionNow = true;
- }
- if (this.mGrid != null)
- {
- this.mGrid.repositionNow = true;
- }
- }
- protected virtual void OnDragDropMove(Vector2 delta)
- {
- this.TaregetTrans.localPosition += delta;
- }
- protected virtual void OnDragDropRelease(GameObject surface)
- {
- if (!this.cloneOnDrag)
- {
- this.mTouchID = int.MinValue;
- if (this.mButton != null)
- {
- this.mButton.isEnabled = true;
- }
- else if (this.mCollider != null)
- {
- this.mCollider.enabled = true;
- }
- else if (this.mCollider2D != null)
- {
- this.mCollider2D.enabled = true;
- }
- UIDragDropContainer uidragDropContainer = (!surface) ? null : NGUITools.FindInParents<UIDragDropContainer>(surface);
- if (uidragDropContainer != null)
- {
- this.mTrans.parent = ((!(uidragDropContainer.reparentTarget != null)) ? uidragDropContainer.transform : uidragDropContainer.reparentTarget);
- Vector3 localPosition = this.TaregetTrans.localPosition;
- localPosition.z = 0f;
- this.TaregetTrans.localPosition = localPosition;
- }
- else
- {
- this.mTrans.parent = this.mParent;
- }
- this.mParent = this.mTrans.parent;
- this.mGrid = NGUITools.FindInParents<UIGrid>(this.mParent);
- this.mTable = NGUITools.FindInParents<UITable>(this.mParent);
- if (this.mDragScrollView != null)
- {
- base.StartCoroutine(this.EnableDragScrollView());
- }
- NGUITools.MarkParentAsChanged(base.gameObject);
- if (this.mTable != null)
- {
- this.mTable.repositionNow = true;
- }
- if (this.mGrid != null)
- {
- this.mGrid.repositionNow = true;
- }
- this.OnDragDropEnd();
- }
- else
- {
- NGUITools.Destroy(base.gameObject);
- }
- }
- protected virtual void OnDragDropEnd()
- {
- }
- protected IEnumerator EnableDragScrollView()
- {
- yield return new WaitForEndOfFrame();
- if (this.mDragScrollView != null)
- {
- this.mDragScrollView.enabled = true;
- }
- yield break;
- }
- protected virtual Transform TaregetTrans
- {
- get
- {
- return this.mTrans;
- }
- }
- public UIDragDropItem.Restriction restriction;
- public bool cloneOnDrag;
- [HideInInspector]
- public float pressAndHoldDelay = 1f;
- protected Transform mTrans;
- protected Transform mParent;
- protected Collider mCollider;
- protected Collider2D mCollider2D;
- protected UIButton mButton;
- protected UIRoot mRoot;
- protected UIGrid mGrid;
- protected UITable mTable;
- protected int mTouchID = int.MinValue;
- protected float mDragStartTime;
- protected UIDragScrollView mDragScrollView;
- protected bool mPressed;
- protected bool mDragging;
- public enum Restriction
- {
- None,
- Horizontal,
- Vertical,
- PressAndHold
- }
- }
|