UIDraggableCamera.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(Camera))]
  4. [AddComponentMenu("NGUI/Interaction/Draggable Camera")]
  5. public class UIDraggableCamera : MonoBehaviour
  6. {
  7. public Vector2 currentMomentum
  8. {
  9. get
  10. {
  11. return this.mMomentum;
  12. }
  13. set
  14. {
  15. this.mMomentum = value;
  16. }
  17. }
  18. private void Start()
  19. {
  20. this.mCam = base.GetComponent<Camera>();
  21. this.mTrans = base.transform;
  22. this.mRoot = NGUITools.FindInParents<UIRoot>(base.gameObject);
  23. if (this.rootForBounds == null)
  24. {
  25. Debug.LogError(NGUITools.GetHierarchy(base.gameObject) + " needs the 'Root For Bounds' parameter to be set", this);
  26. base.enabled = false;
  27. }
  28. }
  29. private Vector3 CalculateConstrainOffset()
  30. {
  31. if (this.rootForBounds == null || this.rootForBounds.childCount == 0)
  32. {
  33. return Vector3.zero;
  34. }
  35. Vector3 vector = new Vector3(this.mCam.rect.xMin * (float)UICamera.ScreenWidth, this.mCam.rect.yMin * (float)UICamera.ScreenHeight, 0f);
  36. Vector3 vector2 = new Vector3(this.mCam.rect.xMax * (float)UICamera.ScreenWidth, this.mCam.rect.yMax * (float)UICamera.ScreenHeight, 0f);
  37. vector = this.mCam.ScreenToWorldPoint(vector);
  38. vector2 = this.mCam.ScreenToWorldPoint(vector2);
  39. Vector2 minRect = new Vector2(this.mBounds.min.x, this.mBounds.min.y);
  40. Vector2 maxRect = new Vector2(this.mBounds.max.x, this.mBounds.max.y);
  41. return NGUIMath.ConstrainRect(minRect, maxRect, vector, vector2);
  42. }
  43. public bool ConstrainToBounds(bool immediate)
  44. {
  45. if (this.mTrans != null && this.rootForBounds != null)
  46. {
  47. Vector3 b = this.CalculateConstrainOffset();
  48. if (b.sqrMagnitude > 0f)
  49. {
  50. if (immediate)
  51. {
  52. this.mTrans.position -= b;
  53. }
  54. else
  55. {
  56. SpringPosition springPosition = SpringPosition.Begin(base.gameObject, this.mTrans.position - b, 13f);
  57. springPosition.ignoreTimeScale = true;
  58. springPosition.worldSpace = true;
  59. }
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. public void Press(bool isPressed)
  66. {
  67. if (isPressed)
  68. {
  69. this.mDragStarted = false;
  70. }
  71. if (this.rootForBounds != null)
  72. {
  73. this.mPressed = isPressed;
  74. if (isPressed)
  75. {
  76. this.mBounds = NGUIMath.CalculateAbsoluteWidgetBounds(this.rootForBounds);
  77. this.mMomentum = Vector2.zero;
  78. this.mScroll = 0f;
  79. SpringPosition component = base.GetComponent<SpringPosition>();
  80. if (component != null)
  81. {
  82. component.enabled = false;
  83. }
  84. }
  85. else if (this.dragEffect == UIDragObject.DragEffect.MomentumAndSpring)
  86. {
  87. this.ConstrainToBounds(false);
  88. }
  89. }
  90. }
  91. public void Drag(Vector2 delta)
  92. {
  93. if (this.smoothDragStart && !this.mDragStarted)
  94. {
  95. this.mDragStarted = true;
  96. return;
  97. }
  98. UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
  99. if (this.mRoot != null)
  100. {
  101. delta *= this.mRoot.pixelSizeAdjustment;
  102. }
  103. Vector2 vector = Vector2.Scale(delta, -this.scale);
  104. this.mTrans.localPosition += vector;
  105. this.mMomentum = Vector2.Lerp(this.mMomentum, this.mMomentum + vector * (0.01f * this.momentumAmount), 0.67f);
  106. if (this.dragEffect != UIDragObject.DragEffect.MomentumAndSpring && this.ConstrainToBounds(true))
  107. {
  108. this.mMomentum = Vector2.zero;
  109. this.mScroll = 0f;
  110. }
  111. }
  112. public void Scroll(float delta)
  113. {
  114. if (base.enabled && NGUITools.GetActive(base.gameObject))
  115. {
  116. if (Mathf.Sign(this.mScroll) != Mathf.Sign(delta))
  117. {
  118. this.mScroll = 0f;
  119. }
  120. this.mScroll += delta * this.scrollWheelFactor;
  121. }
  122. }
  123. private void Update()
  124. {
  125. float deltaTime = RealTime.deltaTime;
  126. if (this.mPressed)
  127. {
  128. SpringPosition component = base.GetComponent<SpringPosition>();
  129. if (component != null)
  130. {
  131. component.enabled = false;
  132. }
  133. this.mScroll = 0f;
  134. }
  135. else
  136. {
  137. this.mMomentum += this.scale * (this.mScroll * 20f);
  138. this.mScroll = NGUIMath.SpringLerp(this.mScroll, 0f, 20f, deltaTime);
  139. if (this.mMomentum.magnitude > 0.01f)
  140. {
  141. this.mTrans.localPosition += NGUIMath.SpringDampen(ref this.mMomentum, 9f, deltaTime);
  142. this.mBounds = NGUIMath.CalculateAbsoluteWidgetBounds(this.rootForBounds);
  143. if (!this.ConstrainToBounds(this.dragEffect == UIDragObject.DragEffect.None))
  144. {
  145. SpringPosition component2 = base.GetComponent<SpringPosition>();
  146. if (component2 != null)
  147. {
  148. component2.enabled = false;
  149. }
  150. }
  151. return;
  152. }
  153. this.mScroll = 0f;
  154. }
  155. NGUIMath.SpringDampen(ref this.mMomentum, 9f, deltaTime);
  156. }
  157. public Transform rootForBounds;
  158. public Vector2 scale = Vector2.one;
  159. public float scrollWheelFactor;
  160. public UIDragObject.DragEffect dragEffect = UIDragObject.DragEffect.MomentumAndSpring;
  161. public bool smoothDragStart = true;
  162. public float momentumAmount = 35f;
  163. private Camera mCam;
  164. private Transform mTrans;
  165. private bool mPressed;
  166. private Vector2 mMomentum = Vector2.zero;
  167. private Bounds mBounds;
  168. private float mScroll;
  169. private UIRoot mRoot;
  170. private bool mDragStarted;
  171. }