UIScrollView.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [RequireComponent(typeof(UIPanel))]
  5. [AddComponentMenu("NGUI/Interaction/Scroll View")]
  6. public class UIScrollView : MonoBehaviour
  7. {
  8. public UIPanel panel
  9. {
  10. get
  11. {
  12. return this.mPanel;
  13. }
  14. }
  15. public bool isDragging
  16. {
  17. get
  18. {
  19. return this.mPressed && this.mDragStarted;
  20. }
  21. }
  22. public virtual Bounds bounds
  23. {
  24. get
  25. {
  26. if (!this.mCalculatedBounds)
  27. {
  28. this.mCalculatedBounds = true;
  29. this.mTrans = base.transform;
  30. this.mBounds = NGUIMath.CalculateRelativeWidgetBounds(this.mTrans, this.mTrans);
  31. }
  32. return this.mBounds;
  33. }
  34. }
  35. public bool canMoveHorizontally
  36. {
  37. get
  38. {
  39. return this.movement == UIScrollView.Movement.Horizontal || this.movement == UIScrollView.Movement.Unrestricted || (this.movement == UIScrollView.Movement.Custom && this.customMovement.x != 0f);
  40. }
  41. }
  42. public bool canMoveVertically
  43. {
  44. get
  45. {
  46. return this.movement == UIScrollView.Movement.Vertical || this.movement == UIScrollView.Movement.Unrestricted || (this.movement == UIScrollView.Movement.Custom && this.customMovement.y != 0f);
  47. }
  48. }
  49. public virtual bool shouldMoveHorizontally
  50. {
  51. get
  52. {
  53. float num = this.bounds.size.x;
  54. if (this.mPanel.clipping == UIDrawCall.Clipping.SoftClip)
  55. {
  56. num += this.mPanel.clipSoftness.x * 2f;
  57. }
  58. return Mathf.RoundToInt(num - this.mPanel.width) > 0;
  59. }
  60. }
  61. public virtual bool shouldMoveVertically
  62. {
  63. get
  64. {
  65. float num = this.bounds.size.y;
  66. if (this.mPanel.clipping == UIDrawCall.Clipping.SoftClip)
  67. {
  68. num += this.mPanel.clipSoftness.y * 2f;
  69. }
  70. return Mathf.RoundToInt(num - this.mPanel.height) > 0;
  71. }
  72. }
  73. protected virtual bool shouldMove
  74. {
  75. get
  76. {
  77. if (!this.disableDragIfFits)
  78. {
  79. return true;
  80. }
  81. if (this.mPanel == null)
  82. {
  83. this.mPanel = base.GetComponent<UIPanel>();
  84. }
  85. Vector4 finalClipRegion = this.mPanel.finalClipRegion;
  86. Bounds bounds = this.bounds;
  87. float num = (finalClipRegion.z != 0f) ? (finalClipRegion.z * 0.5f) : ((float)UICamera.ScreenWidth);
  88. float num2 = (finalClipRegion.w != 0f) ? (finalClipRegion.w * 0.5f) : ((float)UICamera.ScreenHeight);
  89. if (this.canMoveHorizontally)
  90. {
  91. if (bounds.min.x < finalClipRegion.x - num)
  92. {
  93. return true;
  94. }
  95. if (bounds.max.x > finalClipRegion.x + num)
  96. {
  97. return true;
  98. }
  99. }
  100. if (this.canMoveVertically)
  101. {
  102. if (bounds.min.y < finalClipRegion.y - num2)
  103. {
  104. return true;
  105. }
  106. if (bounds.max.y > finalClipRegion.y + num2)
  107. {
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. }
  114. public Vector3 currentMomentum
  115. {
  116. get
  117. {
  118. return this.mMomentum;
  119. }
  120. set
  121. {
  122. this.mMomentum = value;
  123. this.mShouldMove = true;
  124. }
  125. }
  126. private void Awake()
  127. {
  128. this.mTrans = base.transform;
  129. this.mPanel = base.GetComponent<UIPanel>();
  130. if (this.mPanel.clipping == UIDrawCall.Clipping.None)
  131. {
  132. this.mPanel.clipping = UIDrawCall.Clipping.ConstrainButDontClip;
  133. }
  134. if (this.movement != UIScrollView.Movement.Custom && this.scale.sqrMagnitude > 0.001f)
  135. {
  136. if (this.scale.x == 1f && this.scale.y == 0f)
  137. {
  138. this.movement = UIScrollView.Movement.Horizontal;
  139. }
  140. else if (this.scale.x == 0f && this.scale.y == 1f)
  141. {
  142. this.movement = UIScrollView.Movement.Vertical;
  143. }
  144. else if (this.scale.x == 1f && this.scale.y == 1f)
  145. {
  146. this.movement = UIScrollView.Movement.Unrestricted;
  147. }
  148. else
  149. {
  150. this.movement = UIScrollView.Movement.Custom;
  151. this.customMovement.x = this.scale.x;
  152. this.customMovement.y = this.scale.y;
  153. }
  154. this.scale = Vector3.zero;
  155. }
  156. if (this.contentPivot == UIWidget.Pivot.TopLeft && this.relativePositionOnReset != Vector2.zero)
  157. {
  158. this.contentPivot = NGUIMath.GetPivot(new Vector2(this.relativePositionOnReset.x, 1f - this.relativePositionOnReset.y));
  159. this.relativePositionOnReset = Vector2.zero;
  160. }
  161. }
  162. private void OnEnable()
  163. {
  164. UIScrollView.list.Add(this);
  165. if (this.mStarted && Application.isPlaying)
  166. {
  167. this.CheckScrollbars();
  168. }
  169. }
  170. private void Start()
  171. {
  172. this.mStarted = true;
  173. if (Application.isPlaying)
  174. {
  175. this.CheckScrollbars();
  176. }
  177. }
  178. private void CheckScrollbars()
  179. {
  180. if (this.horizontalScrollBar != null)
  181. {
  182. EventDelegate.Add(this.horizontalScrollBar.onChange, new EventDelegate.Callback(this.OnScrollBar));
  183. this.horizontalScrollBar.alpha = ((this.showScrollBars != UIScrollView.ShowCondition.Always && !this.shouldMoveHorizontally) ? 0f : 1f);
  184. }
  185. if (this.verticalScrollBar != null)
  186. {
  187. EventDelegate.Add(this.verticalScrollBar.onChange, new EventDelegate.Callback(this.OnScrollBar));
  188. this.verticalScrollBar.alpha = ((this.showScrollBars != UIScrollView.ShowCondition.Always && !this.shouldMoveVertically) ? 0f : 1f);
  189. }
  190. }
  191. private void OnDisable()
  192. {
  193. UIScrollView.list.Remove(this);
  194. }
  195. public bool RestrictWithinBounds(bool instant)
  196. {
  197. return this.RestrictWithinBounds(instant, true, true);
  198. }
  199. public bool RestrictWithinBounds(bool instant, bool horizontal, bool vertical)
  200. {
  201. Bounds bounds = this.bounds;
  202. Vector3 vector = this.mPanel.CalculateConstrainOffset(bounds.min, bounds.max);
  203. if (!horizontal)
  204. {
  205. vector.x = 0f;
  206. }
  207. if (!vertical)
  208. {
  209. vector.y = 0f;
  210. }
  211. if (vector.sqrMagnitude > 0.1f)
  212. {
  213. if (!instant && this.dragEffect == UIScrollView.DragEffect.MomentumAndSpring)
  214. {
  215. Vector3 pos = this.mTrans.localPosition + vector;
  216. pos.x = Mathf.Round(pos.x);
  217. pos.y = Mathf.Round(pos.y);
  218. SpringPanel.Begin(this.mPanel.gameObject, pos, 13f).strength = 8f;
  219. }
  220. else
  221. {
  222. this.MoveRelative(vector);
  223. if (Mathf.Abs(vector.x) > 0.01f)
  224. {
  225. this.mMomentum.x = 0f;
  226. }
  227. if (Mathf.Abs(vector.y) > 0.01f)
  228. {
  229. this.mMomentum.y = 0f;
  230. }
  231. if (Mathf.Abs(vector.z) > 0.01f)
  232. {
  233. this.mMomentum.z = 0f;
  234. }
  235. this.mScroll = 0f;
  236. }
  237. return true;
  238. }
  239. return false;
  240. }
  241. public void DisableSpring()
  242. {
  243. SpringPanel component = base.GetComponent<SpringPanel>();
  244. if (component != null)
  245. {
  246. component.enabled = false;
  247. }
  248. }
  249. public void UpdateScrollbars()
  250. {
  251. this.UpdateScrollbars(true);
  252. }
  253. public virtual void UpdateScrollbars(bool recalculateBounds)
  254. {
  255. if (this.mPanel == null)
  256. {
  257. return;
  258. }
  259. if (this.horizontalScrollBar != null || this.verticalScrollBar != null)
  260. {
  261. if (recalculateBounds)
  262. {
  263. this.mCalculatedBounds = false;
  264. this.mShouldMove = this.shouldMove;
  265. }
  266. Bounds bounds = this.bounds;
  267. Vector2 vector = bounds.min;
  268. Vector2 vector2 = bounds.max;
  269. if (this.horizontalScrollBar != null && vector2.x > vector.x)
  270. {
  271. Vector4 finalClipRegion = this.mPanel.finalClipRegion;
  272. int num = Mathf.RoundToInt(finalClipRegion.z);
  273. if ((num & 1) != 0)
  274. {
  275. num--;
  276. }
  277. float num2 = (float)num * 0.5f;
  278. num2 = Mathf.Round(num2);
  279. if (this.mPanel.clipping == UIDrawCall.Clipping.SoftClip)
  280. {
  281. num2 -= this.mPanel.clipSoftness.x;
  282. }
  283. float contentSize = vector2.x - vector.x;
  284. float viewSize = num2 * 2f;
  285. float num3 = vector.x;
  286. float num4 = vector2.x;
  287. float num5 = finalClipRegion.x - num2;
  288. float num6 = finalClipRegion.x + num2;
  289. num3 = num5 - num3;
  290. num4 -= num6;
  291. this.UpdateScrollbars(this.horizontalScrollBar, num3, num4, contentSize, viewSize, false);
  292. }
  293. if (this.verticalScrollBar != null && vector2.y > vector.y)
  294. {
  295. Vector4 finalClipRegion2 = this.mPanel.finalClipRegion;
  296. int num7 = Mathf.RoundToInt(finalClipRegion2.w);
  297. if ((num7 & 1) != 0)
  298. {
  299. num7--;
  300. }
  301. float num8 = (float)num7 * 0.5f;
  302. num8 = Mathf.Round(num8);
  303. if (this.mPanel.clipping == UIDrawCall.Clipping.SoftClip)
  304. {
  305. num8 -= this.mPanel.clipSoftness.y;
  306. }
  307. float contentSize2 = vector2.y - vector.y;
  308. float viewSize2 = num8 * 2f;
  309. float num9 = vector.y;
  310. float num10 = vector2.y;
  311. float num11 = finalClipRegion2.y - num8;
  312. float num12 = finalClipRegion2.y + num8;
  313. num9 = num11 - num9;
  314. num10 -= num12;
  315. this.UpdateScrollbars(this.verticalScrollBar, num9, num10, contentSize2, viewSize2, true);
  316. }
  317. }
  318. else if (recalculateBounds)
  319. {
  320. this.mCalculatedBounds = false;
  321. }
  322. }
  323. protected void UpdateScrollbars(UIProgressBar slider, float contentMin, float contentMax, float contentSize, float viewSize, bool inverted)
  324. {
  325. if (slider == null)
  326. {
  327. return;
  328. }
  329. this.mIgnoreCallbacks = true;
  330. float num;
  331. if (viewSize < contentSize)
  332. {
  333. contentMin = Mathf.Clamp01(contentMin / contentSize);
  334. contentMax = Mathf.Clamp01(contentMax / contentSize);
  335. num = contentMin + contentMax;
  336. slider.value = ((!inverted) ? ((num <= 0.001f) ? 1f : (contentMin / num)) : ((num <= 0.001f) ? 0f : (1f - contentMin / num)));
  337. }
  338. else
  339. {
  340. contentMin = Mathf.Clamp01(-contentMin / contentSize);
  341. contentMax = Mathf.Clamp01(-contentMax / contentSize);
  342. num = contentMin + contentMax;
  343. slider.value = ((!inverted) ? ((num <= 0.001f) ? 1f : (contentMin / num)) : ((num <= 0.001f) ? 0f : (1f - contentMin / num)));
  344. if (contentSize > 0f)
  345. {
  346. contentMin = Mathf.Clamp01(contentMin / contentSize);
  347. contentMax = Mathf.Clamp01(contentMax / contentSize);
  348. num = contentMin + contentMax;
  349. }
  350. }
  351. UIScrollBar uiscrollBar = slider as UIScrollBar;
  352. if (uiscrollBar != null)
  353. {
  354. uiscrollBar.barSize = 1f - num;
  355. }
  356. this.mIgnoreCallbacks = false;
  357. }
  358. public virtual void SetDragAmount(float x, float y, bool updateScrollbars)
  359. {
  360. if (this.mPanel == null)
  361. {
  362. this.mPanel = base.GetComponent<UIPanel>();
  363. }
  364. this.DisableSpring();
  365. Bounds bounds = this.bounds;
  366. if (bounds.min.x == bounds.max.x || bounds.min.y == bounds.max.y)
  367. {
  368. return;
  369. }
  370. Vector4 finalClipRegion = this.mPanel.finalClipRegion;
  371. float num = finalClipRegion.z * 0.5f;
  372. float num2 = finalClipRegion.w * 0.5f;
  373. float num3 = bounds.min.x + num;
  374. float num4 = bounds.max.x - num;
  375. float num5 = bounds.min.y + num2;
  376. float num6 = bounds.max.y - num2;
  377. if (this.mPanel.clipping == UIDrawCall.Clipping.SoftClip)
  378. {
  379. num3 -= this.mPanel.clipSoftness.x;
  380. num4 += this.mPanel.clipSoftness.x;
  381. num5 -= this.mPanel.clipSoftness.y;
  382. num6 += this.mPanel.clipSoftness.y;
  383. }
  384. float num7 = Mathf.Lerp(num3, num4, x);
  385. float num8 = Mathf.Lerp(num6, num5, y);
  386. if (!updateScrollbars)
  387. {
  388. Vector3 localPosition = this.mTrans.localPosition;
  389. if (this.canMoveHorizontally)
  390. {
  391. localPosition.x += finalClipRegion.x - num7;
  392. }
  393. if (this.canMoveVertically)
  394. {
  395. localPosition.y += finalClipRegion.y - num8;
  396. }
  397. this.mTrans.localPosition = localPosition;
  398. }
  399. if (this.canMoveHorizontally)
  400. {
  401. finalClipRegion.x = num7;
  402. }
  403. if (this.canMoveVertically)
  404. {
  405. finalClipRegion.y = num8;
  406. }
  407. Vector4 baseClipRegion = this.mPanel.baseClipRegion;
  408. this.mPanel.clipOffset = new Vector2(finalClipRegion.x - baseClipRegion.x, finalClipRegion.y - baseClipRegion.y);
  409. if (updateScrollbars)
  410. {
  411. this.UpdateScrollbars(this.mDragID == -10);
  412. }
  413. }
  414. public void InvalidateBounds()
  415. {
  416. this.mCalculatedBounds = false;
  417. }
  418. [ContextMenu("Reset Clipping Position")]
  419. public void ResetPosition()
  420. {
  421. if (NGUITools.GetActive(this))
  422. {
  423. this.mCalculatedBounds = false;
  424. Vector2 pivotOffset = NGUIMath.GetPivotOffset(this.contentPivot);
  425. this.SetDragAmount(pivotOffset.x, 1f - pivotOffset.y, false);
  426. this.SetDragAmount(pivotOffset.x, 1f - pivotOffset.y, true);
  427. }
  428. }
  429. public void UpdatePosition()
  430. {
  431. if (!this.mIgnoreCallbacks && (this.horizontalScrollBar != null || this.verticalScrollBar != null))
  432. {
  433. this.mIgnoreCallbacks = true;
  434. this.mCalculatedBounds = false;
  435. Vector2 pivotOffset = NGUIMath.GetPivotOffset(this.contentPivot);
  436. float x = (!(this.horizontalScrollBar != null)) ? pivotOffset.x : this.horizontalScrollBar.value;
  437. float y = (!(this.verticalScrollBar != null)) ? (1f - pivotOffset.y) : this.verticalScrollBar.value;
  438. this.SetDragAmount(x, y, false);
  439. this.UpdateScrollbars(true);
  440. this.mIgnoreCallbacks = false;
  441. }
  442. }
  443. public void OnScrollBar()
  444. {
  445. if (!this.mIgnoreCallbacks)
  446. {
  447. this.mIgnoreCallbacks = true;
  448. float x = (!(this.horizontalScrollBar != null)) ? 0f : this.horizontalScrollBar.value;
  449. float y = (!(this.verticalScrollBar != null)) ? 0f : this.verticalScrollBar.value;
  450. this.SetDragAmount(x, y, false);
  451. this.mIgnoreCallbacks = false;
  452. }
  453. }
  454. public virtual void MoveRelative(Vector3 relative)
  455. {
  456. this.mTrans.localPosition += relative;
  457. Vector2 clipOffset = this.mPanel.clipOffset;
  458. clipOffset.x -= relative.x;
  459. clipOffset.y -= relative.y;
  460. this.mPanel.clipOffset = clipOffset;
  461. this.UpdateScrollbars(false);
  462. }
  463. public void MoveAbsolute(Vector3 absolute)
  464. {
  465. Vector3 a = this.mTrans.InverseTransformPoint(absolute);
  466. Vector3 b = this.mTrans.InverseTransformPoint(Vector3.zero);
  467. this.MoveRelative(a - b);
  468. }
  469. public void Press(bool pressed)
  470. {
  471. if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
  472. {
  473. return;
  474. }
  475. if (this.smoothDragStart && pressed)
  476. {
  477. this.mDragStarted = false;
  478. this.mDragStartOffset = Vector2.zero;
  479. }
  480. if (base.enabled && NGUITools.GetActive(base.gameObject))
  481. {
  482. if (!pressed && this.mDragID == UICamera.currentTouchID)
  483. {
  484. this.mDragID = -10;
  485. }
  486. this.mCalculatedBounds = false;
  487. this.mShouldMove = this.shouldMove;
  488. if (!this.mShouldMove)
  489. {
  490. return;
  491. }
  492. this.mPressed = pressed;
  493. if (pressed)
  494. {
  495. this.mMomentum = Vector3.zero;
  496. this.mScroll = 0f;
  497. this.DisableSpring();
  498. this.mLastPos = UICamera.lastWorldPosition;
  499. this.mPlane = new Plane(this.mTrans.rotation * Vector3.back, this.mLastPos);
  500. Vector2 clipOffset = this.mPanel.clipOffset;
  501. clipOffset.x = Mathf.Round(clipOffset.x);
  502. clipOffset.y = Mathf.Round(clipOffset.y);
  503. this.mPanel.clipOffset = clipOffset;
  504. Vector3 localPosition = this.mTrans.localPosition;
  505. localPosition.x = Mathf.Round(localPosition.x);
  506. localPosition.y = Mathf.Round(localPosition.y);
  507. this.mTrans.localPosition = localPosition;
  508. if (!this.smoothDragStart)
  509. {
  510. this.mDragStarted = true;
  511. this.mDragStartOffset = Vector2.zero;
  512. if (this.onDragStarted != null)
  513. {
  514. this.onDragStarted();
  515. }
  516. }
  517. }
  518. else if (this.centerOnChild != null)
  519. {
  520. this.centerOnChild.Recenter();
  521. }
  522. else
  523. {
  524. if (this.restrictWithinPanel && this.mPanel.clipping != UIDrawCall.Clipping.None)
  525. {
  526. this.RestrictWithinBounds(this.dragEffect == UIScrollView.DragEffect.None, this.canMoveHorizontally, this.canMoveVertically);
  527. }
  528. if (this.mDragStarted && this.onDragFinished != null)
  529. {
  530. this.onDragFinished();
  531. }
  532. if (!this.mShouldMove && this.onStoppedMoving != null)
  533. {
  534. this.onStoppedMoving();
  535. }
  536. }
  537. }
  538. }
  539. public void Drag()
  540. {
  541. if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
  542. {
  543. return;
  544. }
  545. if (base.enabled && NGUITools.GetActive(base.gameObject) && this.mShouldMove)
  546. {
  547. if (this.mDragID == -10)
  548. {
  549. this.mDragID = UICamera.currentTouchID;
  550. }
  551. UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
  552. if (this.smoothDragStart && !this.mDragStarted)
  553. {
  554. this.mDragStarted = true;
  555. this.mDragStartOffset = UICamera.currentTouch.totalDelta;
  556. if (this.onDragStarted != null)
  557. {
  558. this.onDragStarted();
  559. }
  560. }
  561. Ray ray = (!this.smoothDragStart) ? UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos) : UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos - this.mDragStartOffset);
  562. float distance = 0f;
  563. if (this.mPlane.Raycast(ray, out distance))
  564. {
  565. Vector3 point = ray.GetPoint(distance);
  566. Vector3 vector = point - this.mLastPos;
  567. this.mLastPos = point;
  568. if (vector.x != 0f || vector.y != 0f || vector.z != 0f)
  569. {
  570. vector = this.mTrans.InverseTransformDirection(vector);
  571. if (this.movement == UIScrollView.Movement.Horizontal)
  572. {
  573. vector.y = 0f;
  574. vector.z = 0f;
  575. }
  576. else if (this.movement == UIScrollView.Movement.Vertical)
  577. {
  578. vector.x = 0f;
  579. vector.z = 0f;
  580. }
  581. else if (this.movement == UIScrollView.Movement.Unrestricted)
  582. {
  583. vector.z = 0f;
  584. }
  585. else
  586. {
  587. vector.Scale(this.customMovement);
  588. }
  589. vector = this.mTrans.TransformDirection(vector);
  590. }
  591. if (this.dragEffect == UIScrollView.DragEffect.None)
  592. {
  593. this.mMomentum = Vector3.zero;
  594. }
  595. else
  596. {
  597. this.mMomentum = Vector3.Lerp(this.mMomentum, this.mMomentum + vector * (0.01f * this.momentumAmount), 0.67f);
  598. }
  599. if (!this.iOSDragEmulation || this.dragEffect != UIScrollView.DragEffect.MomentumAndSpring)
  600. {
  601. this.MoveAbsolute(vector);
  602. }
  603. else if (this.mPanel.CalculateConstrainOffset(this.bounds.min, this.bounds.max).magnitude > 1f)
  604. {
  605. this.MoveAbsolute(vector * 0.5f);
  606. this.mMomentum *= 0.5f;
  607. }
  608. else
  609. {
  610. this.MoveAbsolute(vector);
  611. }
  612. if (this.restrictWithinPanel && this.mPanel.clipping != UIDrawCall.Clipping.None && this.dragEffect != UIScrollView.DragEffect.MomentumAndSpring)
  613. {
  614. this.RestrictWithinBounds(true, this.canMoveHorizontally, this.canMoveVertically);
  615. }
  616. }
  617. }
  618. }
  619. public void Scroll(float delta)
  620. {
  621. if (base.enabled && NGUITools.GetActive(base.gameObject) && this.scrollWheelFactor != 0f)
  622. {
  623. this.DisableSpring();
  624. this.mShouldMove |= this.shouldMove;
  625. if (Mathf.Sign(this.mScroll) != Mathf.Sign(delta))
  626. {
  627. this.mScroll = 0f;
  628. }
  629. this.mScroll += delta * this.scrollWheelFactor;
  630. }
  631. }
  632. private void LateUpdate()
  633. {
  634. if (!Application.isPlaying)
  635. {
  636. return;
  637. }
  638. float deltaTime = RealTime.deltaTime;
  639. if (this.showScrollBars != UIScrollView.ShowCondition.Always && (this.verticalScrollBar || this.horizontalScrollBar))
  640. {
  641. bool flag = false;
  642. bool flag2 = false;
  643. if (this.showScrollBars != UIScrollView.ShowCondition.WhenDragging || this.mDragID != -10 || this.mMomentum.magnitude > 0.01f)
  644. {
  645. flag = this.shouldMoveVertically;
  646. flag2 = this.shouldMoveHorizontally;
  647. }
  648. if (this.verticalScrollBar)
  649. {
  650. float num = this.verticalScrollBar.alpha;
  651. num += ((!flag) ? (-deltaTime * 3f) : (deltaTime * 6f));
  652. num = Mathf.Clamp01(num);
  653. if (this.verticalScrollBar.alpha != num)
  654. {
  655. this.verticalScrollBar.alpha = num;
  656. }
  657. }
  658. if (this.horizontalScrollBar)
  659. {
  660. float num2 = this.horizontalScrollBar.alpha;
  661. num2 += ((!flag2) ? (-deltaTime * 3f) : (deltaTime * 6f));
  662. num2 = Mathf.Clamp01(num2);
  663. if (this.horizontalScrollBar.alpha != num2)
  664. {
  665. this.horizontalScrollBar.alpha = num2;
  666. }
  667. }
  668. }
  669. if (!this.mShouldMove)
  670. {
  671. return;
  672. }
  673. if (!this.mPressed)
  674. {
  675. if (this.mMomentum.magnitude > 0.0001f || this.mScroll != 0f)
  676. {
  677. if (this.movement == UIScrollView.Movement.Horizontal)
  678. {
  679. this.mMomentum -= this.mTrans.TransformDirection(new Vector3(this.mScroll * 0.05f, 0f, 0f));
  680. }
  681. else if (this.movement == UIScrollView.Movement.Vertical)
  682. {
  683. this.mMomentum -= this.mTrans.TransformDirection(new Vector3(0f, this.mScroll * 0.05f, 0f));
  684. }
  685. else if (this.movement == UIScrollView.Movement.Unrestricted)
  686. {
  687. this.mMomentum -= this.mTrans.TransformDirection(new Vector3(this.mScroll * 0.05f, this.mScroll * 0.05f, 0f));
  688. }
  689. else
  690. {
  691. this.mMomentum -= this.mTrans.TransformDirection(new Vector3(this.mScroll * this.customMovement.x * 0.05f, this.mScroll * this.customMovement.y * 0.05f, 0f));
  692. }
  693. this.mScroll = NGUIMath.SpringLerp(this.mScroll, 0f, 20f, deltaTime);
  694. Vector3 absolute = NGUIMath.SpringDampen(ref this.mMomentum, 9f, deltaTime);
  695. this.MoveAbsolute(absolute);
  696. if (this.restrictWithinPanel && this.mPanel.clipping != UIDrawCall.Clipping.None)
  697. {
  698. if (NGUITools.GetActive(this.centerOnChild))
  699. {
  700. if (this.centerOnChild.nextPageThreshold != 0f)
  701. {
  702. this.mMomentum = Vector3.zero;
  703. this.mScroll = 0f;
  704. }
  705. else
  706. {
  707. this.centerOnChild.Recenter();
  708. }
  709. }
  710. else
  711. {
  712. this.RestrictWithinBounds(false, this.canMoveHorizontally, this.canMoveVertically);
  713. }
  714. }
  715. if (this.onMomentumMove != null)
  716. {
  717. this.onMomentumMove();
  718. }
  719. }
  720. else
  721. {
  722. this.mScroll = 0f;
  723. this.mMomentum = Vector3.zero;
  724. SpringPanel component = base.GetComponent<SpringPanel>();
  725. if (component != null && component.enabled)
  726. {
  727. return;
  728. }
  729. this.mShouldMove = false;
  730. if (this.onStoppedMoving != null)
  731. {
  732. this.onStoppedMoving();
  733. }
  734. }
  735. }
  736. else
  737. {
  738. this.mScroll = 0f;
  739. NGUIMath.SpringDampen(ref this.mMomentum, 9f, deltaTime);
  740. }
  741. }
  742. public static BetterList<UIScrollView> list = new BetterList<UIScrollView>();
  743. public UIScrollView.Movement movement;
  744. public UIScrollView.DragEffect dragEffect = UIScrollView.DragEffect.MomentumAndSpring;
  745. public bool restrictWithinPanel = true;
  746. public bool disableDragIfFits;
  747. public bool smoothDragStart = true;
  748. public bool iOSDragEmulation = true;
  749. public float scrollWheelFactor = 0.25f;
  750. public float momentumAmount = 35f;
  751. public UIProgressBar horizontalScrollBar;
  752. public UIProgressBar verticalScrollBar;
  753. public UIScrollView.ShowCondition showScrollBars = UIScrollView.ShowCondition.OnlyIfNeeded;
  754. public Vector2 customMovement = new Vector2(1f, 0f);
  755. public UIWidget.Pivot contentPivot;
  756. public UIScrollView.OnDragNotification onDragStarted;
  757. public UIScrollView.OnDragNotification onDragFinished;
  758. public UIScrollView.OnDragNotification onMomentumMove;
  759. public UIScrollView.OnDragNotification onStoppedMoving;
  760. [HideInInspector]
  761. [SerializeField]
  762. private Vector3 scale = new Vector3(1f, 0f, 0f);
  763. [SerializeField]
  764. [HideInInspector]
  765. private Vector2 relativePositionOnReset = Vector2.zero;
  766. protected Transform mTrans;
  767. protected UIPanel mPanel;
  768. protected Plane mPlane;
  769. protected Vector3 mLastPos;
  770. protected bool mPressed;
  771. protected Vector3 mMomentum = Vector3.zero;
  772. protected float mScroll;
  773. protected Bounds mBounds;
  774. protected bool mCalculatedBounds;
  775. protected bool mShouldMove;
  776. protected bool mIgnoreCallbacks;
  777. protected int mDragID = -10;
  778. protected Vector2 mDragStartOffset = Vector2.zero;
  779. protected bool mDragStarted;
  780. [NonSerialized]
  781. private bool mStarted;
  782. [HideInInspector]
  783. public UICenterOnChild centerOnChild;
  784. public enum Movement
  785. {
  786. Horizontal,
  787. Vertical,
  788. Unrestricted,
  789. Custom
  790. }
  791. public enum DragEffect
  792. {
  793. None,
  794. Momentum,
  795. MomentumAndSpring
  796. }
  797. public enum ShowCondition
  798. {
  799. Always,
  800. OnlyIfNeeded,
  801. WhenDragging
  802. }
  803. public delegate void OnDragNotification();
  804. }