/Project/Assets/NGUI/Scripts/Interaction/UIDragObject.cs

https://bitbucket.org/golivegaming/lalkamal-neelkamal · C# · 232 lines · 135 code · 44 blank · 53 comment · 57 complexity · 03513a73abb072cfb68f27f115b523ff MD5 · raw file

  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2012 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using System.Collections;
  7. /// <summary>
  8. /// Allows dragging of the specified target object by mouse or touch, optionally limiting it to be within the UIPanel's clipped rectangle.
  9. /// </summary>
  10. [AddComponentMenu("NGUI/Interaction/Drag Object")]
  11. public class UIDragObject : IgnoreTimeScale
  12. {
  13. public enum DragEffect
  14. {
  15. None,
  16. Momentum,
  17. MomentumAndSpring,
  18. }
  19. /// <summary>
  20. /// Target object that will be dragged.
  21. /// </summary>
  22. public Transform target;
  23. /// <summary>
  24. /// Scale value applied to the drag delta. Set X or Y to 0 to disallow dragging in that direction.
  25. /// </summary>
  26. public Vector3 scale = Vector3.one;
  27. /// <summary>
  28. /// Effect the scroll wheel will have on the momentum.
  29. /// </summary>
  30. public float scrollWheelFactor = 0f;
  31. /// <summary>
  32. /// Whether the dragging will be restricted to be within the parent panel's bounds.
  33. /// </summary>
  34. public bool restrictWithinPanel = false;
  35. /// <summary>
  36. /// Effect to apply when dragging.
  37. /// </summary>
  38. public DragEffect dragEffect = DragEffect.MomentumAndSpring;
  39. /// <summary>
  40. /// How much momentum gets applied when the press is released after dragging.
  41. /// </summary>
  42. public float momentumAmount = 35f;
  43. Plane mPlane;
  44. Vector3 mLastPos;
  45. UIPanel mPanel;
  46. bool mPressed = false;
  47. Vector3 mMomentum = Vector3.zero;
  48. float mScroll = 0f;
  49. Bounds mBounds;
  50. /// <summary>
  51. /// Find the panel responsible for this object.
  52. /// </summary>
  53. void FindPanel ()
  54. {
  55. mPanel = (target != null) ? UIPanel.Find(target.transform, false) : null;
  56. if (mPanel == null) restrictWithinPanel = false;
  57. }
  58. /// <summary>
  59. /// Create a plane on which we will be performing the dragging.
  60. /// </summary>
  61. void OnPress (bool pressed)
  62. {
  63. if (enabled && gameObject.active && target != null)
  64. {
  65. mPressed = pressed;
  66. if (pressed)
  67. {
  68. if (restrictWithinPanel && mPanel == null) FindPanel();
  69. // Calculate the bounds
  70. if (restrictWithinPanel) mBounds = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, target);
  71. // Remove all momentum on press
  72. mMomentum = Vector3.zero;
  73. mScroll = 0f;
  74. // Disable the spring movement
  75. SpringPosition sp = target.GetComponent<SpringPosition>();
  76. if (sp != null) sp.enabled = false;
  77. // Remember the hit position
  78. mLastPos = UICamera.lastHit.point;
  79. // Create the plane to drag along
  80. Transform trans = UICamera.currentCamera.transform;
  81. mPlane = new Plane((mPanel != null ? mPanel.cachedTransform.rotation : trans.rotation) * Vector3.back, mLastPos);
  82. }
  83. else if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None && dragEffect == DragEffect.MomentumAndSpring)
  84. {
  85. mPanel.ConstrainTargetToBounds(target, ref mBounds, false);
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// Drag the object along the plane.
  91. /// </summary>
  92. void OnDrag (Vector2 delta)
  93. {
  94. if (enabled && gameObject.active && target != null)
  95. {
  96. UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
  97. Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos);
  98. float dist = 0f;
  99. if (mPlane.Raycast(ray, out dist))
  100. {
  101. Vector3 currentPos = ray.GetPoint(dist);
  102. Vector3 offset = currentPos - mLastPos;
  103. mLastPos = currentPos;
  104. if (offset.x != 0f || offset.y != 0f)
  105. {
  106. offset = target.InverseTransformDirection(offset);
  107. offset.Scale(scale);
  108. offset = target.TransformDirection(offset);
  109. }
  110. // Adjust the momentum
  111. mMomentum = Vector3.Lerp(mMomentum, mMomentum + offset * (0.01f * momentumAmount), 0.67f);
  112. // We want to constrain the UI to be within bounds
  113. if (restrictWithinPanel)
  114. {
  115. // Adjust the position and bounds
  116. Vector3 localPos = target.localPosition;
  117. target.position += offset;
  118. mBounds.center = mBounds.center + (target.localPosition - localPos);
  119. // Constrain the UI to the bounds, and if done so, eliminate the momentum
  120. if (dragEffect != DragEffect.MomentumAndSpring && mPanel.clipping != UIDrawCall.Clipping.None &&
  121. mPanel.ConstrainTargetToBounds(target, ref mBounds, true))
  122. {
  123. mMomentum = Vector3.zero;
  124. mScroll = 0f;
  125. }
  126. }
  127. else
  128. {
  129. // Adjust the position
  130. target.position += offset;
  131. }
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// Apply the dragging momentum.
  137. /// </summary>
  138. void LateUpdate ()
  139. {
  140. float delta = UpdateRealTimeDelta();
  141. if (target == null) return;
  142. if (mPressed)
  143. {
  144. // Disable the spring movement
  145. SpringPosition sp = target.GetComponent<SpringPosition>();
  146. if (sp != null) sp.enabled = false;
  147. mScroll = 0f;
  148. }
  149. else
  150. {
  151. mMomentum += scale * (-mScroll * 0.05f);
  152. mScroll = NGUIMath.SpringLerp(mScroll, 0f, 20f, delta);
  153. if (mMomentum.magnitude > 0.0001f)
  154. {
  155. // Apply the momentum
  156. if (mPanel == null) FindPanel();
  157. if (mPanel != null)
  158. {
  159. target.position += NGUIMath.SpringDampen(ref mMomentum, 9f, delta);
  160. if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None)
  161. {
  162. mBounds = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, target);
  163. if (!mPanel.ConstrainTargetToBounds(target, ref mBounds, dragEffect == DragEffect.None))
  164. {
  165. SpringPosition sp = target.GetComponent<SpringPosition>();
  166. if (sp != null) sp.enabled = false;
  167. }
  168. }
  169. return;
  170. }
  171. }
  172. else mScroll = 0f;
  173. }
  174. // Dampen the momentum
  175. NGUIMath.SpringDampen(ref mMomentum, 9f, delta);
  176. }
  177. /// <summary>
  178. /// If the object should support the scroll wheel, do it.
  179. /// </summary>
  180. void OnScroll (float delta)
  181. {
  182. if (enabled && gameObject.active)
  183. {
  184. if (Mathf.Sign(mScroll) != Mathf.Sign(delta)) mScroll = 0f;
  185. mScroll += delta * scrollWheelFactor;
  186. }
  187. }
  188. }