GTweener.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. using System;
  2. using UnityEngine;
  3. #if FAIRYGUI_TOLUA
  4. using LuaInterface;
  5. #endif
  6. namespace FairyGUI
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. public delegate void GTweenCallback();
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. /// <param name="tweener"></param>
  16. public delegate void GTweenCallback1(GTweener tweener);
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public interface ITweenListener
  21. {
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. /// <param name="tweener"></param>
  26. void OnTweenStart(GTweener tweener);
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. /// <param name="tweener"></param>
  31. void OnTweenUpdate(GTweener tweener);
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. /// <param name="tweener"></param>
  36. void OnTweenComplete(GTweener tweener);
  37. }
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. public class GTweener
  42. {
  43. internal object _target;
  44. internal TweenPropType _propType;
  45. internal bool _killed;
  46. internal bool _paused;
  47. float _delay;
  48. float _duration;
  49. float _breakpoint;
  50. EaseType _easeType;
  51. float _easeOvershootOrAmplitude;
  52. float _easePeriod;
  53. int _repeat;
  54. bool _yoyo;
  55. float _timeScale;
  56. bool _ignoreEngineTimeScale;
  57. bool _snapping;
  58. object _userData;
  59. GPath _path;
  60. CustomEase _customEase;
  61. GTweenCallback _onUpdate;
  62. GTweenCallback _onStart;
  63. GTweenCallback _onComplete;
  64. GTweenCallback1 _onUpdate1;
  65. GTweenCallback1 _onStart1;
  66. GTweenCallback1 _onComplete1;
  67. ITweenListener _listener;
  68. TweenValue _startValue;
  69. TweenValue _endValue;
  70. TweenValue _value;
  71. TweenValue _deltaValue;
  72. int _valueSize;
  73. bool _started;
  74. int _ended;
  75. float _elapsedTime;
  76. float _normalizedTime;
  77. int _smoothStart;
  78. public GTweener()
  79. {
  80. _startValue = new TweenValue();
  81. _endValue = new TweenValue();
  82. _value = new TweenValue();
  83. _deltaValue = new TweenValue();
  84. }
  85. /// <summary>
  86. ///
  87. /// </summary>
  88. /// <param name="value"></param>
  89. /// <returns></returns>
  90. public GTweener SetDelay(float value)
  91. {
  92. _delay = value;
  93. return this;
  94. }
  95. /// <summary>
  96. ///
  97. /// </summary>
  98. public float delay
  99. {
  100. get { return _delay; }
  101. }
  102. /// <summary>
  103. ///
  104. /// </summary>
  105. /// <param name="value"></param>
  106. /// <returns></returns>
  107. public GTweener SetDuration(float value)
  108. {
  109. _duration = value;
  110. return this;
  111. }
  112. /// <summary>
  113. ///
  114. /// </summary>
  115. public float duration
  116. {
  117. get { return _duration; }
  118. }
  119. /// <summary>
  120. ///
  121. /// </summary>
  122. /// <param name="value"></param>
  123. /// <returns></returns>
  124. public GTweener SetBreakpoint(float value)
  125. {
  126. _breakpoint = value;
  127. return this;
  128. }
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. /// <param name="value"></param>
  133. /// <returns></returns>
  134. public GTweener SetEase(EaseType value)
  135. {
  136. _easeType = value;
  137. return this;
  138. }
  139. /// <summary>
  140. ///
  141. /// </summary>
  142. /// <param name="value"></param>
  143. /// <param name="customEase"></param>
  144. /// <returns></returns>
  145. public GTweener SetEase(EaseType value, CustomEase customEase)
  146. {
  147. _easeType = value;
  148. _customEase = customEase;
  149. return this;
  150. }
  151. /// <summary>
  152. ///
  153. /// </summary>
  154. /// <param name="value"></param>
  155. /// <returns></returns>
  156. public GTweener SetEasePeriod(float value)
  157. {
  158. _easePeriod = value;
  159. return this;
  160. }
  161. /// <summary>
  162. ///
  163. /// </summary>
  164. /// <param name="value"></param>
  165. /// <returns></returns>
  166. public GTweener SetEaseOvershootOrAmplitude(float value)
  167. {
  168. _easeOvershootOrAmplitude = value;
  169. return this;
  170. }
  171. /// <summary>
  172. ///
  173. /// </summary>
  174. /// <param name="times"></param>
  175. /// <param name="yoyo"></param>
  176. /// <returns></returns>
  177. public GTweener SetRepeat(int times, bool yoyo = false)
  178. {
  179. _repeat = times;
  180. _yoyo = yoyo;
  181. return this;
  182. }
  183. /// <summary>
  184. ///
  185. /// </summary>
  186. public int repeat
  187. {
  188. get { return _repeat; }
  189. }
  190. /// <summary>
  191. ///
  192. /// </summary>
  193. /// <param name="value"></param>
  194. /// <returns></returns>
  195. public GTweener SetTimeScale(float value)
  196. {
  197. _timeScale = value;
  198. return this;
  199. }
  200. /// <summary>
  201. ///
  202. /// </summary>
  203. /// <param name="value"></param>
  204. /// <returns></returns>
  205. public GTweener SetIgnoreEngineTimeScale(bool value)
  206. {
  207. _ignoreEngineTimeScale = value;
  208. return this;
  209. }
  210. /// <summary>
  211. ///
  212. /// </summary>
  213. /// <param name="value"></param>
  214. /// <returns></returns>
  215. public GTweener SetSnapping(bool value)
  216. {
  217. _snapping = value;
  218. return this;
  219. }
  220. /// <summary>
  221. ///
  222. /// </summary>
  223. /// <param name="value"></param>
  224. /// <returns></returns>
  225. public GTweener SetPath(GPath value)
  226. {
  227. _path = value;
  228. return this;
  229. }
  230. /// <summary>
  231. ///
  232. /// </summary>
  233. /// <param name="value"></param>
  234. /// <returns></returns>
  235. public GTweener SetTarget(object value)
  236. {
  237. _target = value;
  238. _propType = TweenPropType.None;
  239. return this;
  240. }
  241. /// <summary>
  242. ///
  243. /// </summary>
  244. /// <param name="value"></param>
  245. /// <param name="propType"></param>
  246. /// <returns></returns>
  247. public GTweener SetTarget(object value, TweenPropType propType)
  248. {
  249. _target = value;
  250. _propType = propType;
  251. return this;
  252. }
  253. /// <summary>
  254. ///
  255. /// </summary>
  256. public object target
  257. {
  258. get { return _target; }
  259. }
  260. /// <summary>
  261. ///
  262. /// </summary>
  263. /// <param name="value"></param>
  264. /// <returns></returns>
  265. public GTweener SetUserData(object value)
  266. {
  267. _userData = value;
  268. return this;
  269. }
  270. /// <summary>
  271. ///
  272. /// </summary>
  273. public object userData
  274. {
  275. get { return _userData; }
  276. }
  277. /// <summary>
  278. ///
  279. /// </summary>
  280. /// <param name="callback"></param>
  281. /// <returns></returns>
  282. #if FAIRYGUI_TOLUA
  283. [NoToLua]
  284. #endif
  285. public GTweener OnUpdate(GTweenCallback callback)
  286. {
  287. _onUpdate = callback;
  288. return this;
  289. }
  290. /// <summary>
  291. ///
  292. /// </summary>
  293. /// <param name="callback"></param>
  294. /// <returns></returns>
  295. #if FAIRYGUI_TOLUA
  296. [NoToLua]
  297. #endif
  298. public GTweener OnStart(GTweenCallback callback)
  299. {
  300. _onStart = callback;
  301. return this;
  302. }
  303. /// <summary>
  304. ///
  305. /// </summary>
  306. /// <param name="callback"></param>
  307. /// <returns></returns>
  308. #if FAIRYGUI_TOLUA
  309. [NoToLua]
  310. #endif
  311. public GTweener OnComplete(GTweenCallback callback)
  312. {
  313. _onComplete = callback;
  314. return this;
  315. }
  316. /// <summary>
  317. ///
  318. /// </summary>
  319. /// <param name="callback"></param>
  320. /// <returns></returns>
  321. public GTweener OnUpdate(GTweenCallback1 callback)
  322. {
  323. _onUpdate1 = callback;
  324. return this;
  325. }
  326. /// <summary>
  327. ///
  328. /// </summary>
  329. /// <param name="callback"></param>
  330. /// <returns></returns>
  331. public GTweener OnStart(GTweenCallback1 callback)
  332. {
  333. _onStart1 = callback;
  334. return this;
  335. }
  336. /// <summary>
  337. ///
  338. /// </summary>
  339. /// <param name="callback"></param>
  340. /// <returns></returns>
  341. public GTweener OnComplete(GTweenCallback1 callback)
  342. {
  343. _onComplete1 = callback;
  344. return this;
  345. }
  346. /// <summary>
  347. ///
  348. /// </summary>
  349. /// <param name="value"></param>
  350. /// <returns></returns>
  351. public GTweener SetListener(ITweenListener value)
  352. {
  353. _listener = value;
  354. return this;
  355. }
  356. /// <summary>
  357. ///
  358. /// </summary>
  359. public TweenValue startValue
  360. {
  361. get { return _startValue; }
  362. }
  363. /// <summary>
  364. ///
  365. /// </summary>
  366. public TweenValue endValue
  367. {
  368. get { return _endValue; }
  369. }
  370. /// <summary>
  371. ///
  372. /// </summary>
  373. public TweenValue value
  374. {
  375. get { return _value; }
  376. }
  377. /// <summary>
  378. ///
  379. /// </summary>
  380. public TweenValue deltaValue
  381. {
  382. get { return _deltaValue; }
  383. }
  384. /// <summary>
  385. ///
  386. /// </summary>
  387. public float normalizedTime
  388. {
  389. get { return _normalizedTime; }
  390. }
  391. /// <summary>
  392. ///
  393. /// </summary>
  394. public bool completed
  395. {
  396. get { return _ended != 0; }
  397. }
  398. /// <summary>
  399. ///
  400. /// </summary>
  401. public bool allCompleted
  402. {
  403. get { return _ended == 1; }
  404. }
  405. /// <summary>
  406. ///
  407. /// </summary>
  408. /// <param name="paused"></param>
  409. /// <returns></returns>
  410. public GTweener SetPaused(bool paused)
  411. {
  412. _paused = paused;
  413. if (_paused)
  414. _smoothStart = 0;
  415. return this;
  416. }
  417. /// <summary>
  418. ///
  419. /// </summary>
  420. /// <param name="time"></param>
  421. public void Seek(float time)
  422. {
  423. if (_killed)
  424. return;
  425. _elapsedTime = time;
  426. if (_elapsedTime < _delay)
  427. {
  428. if (_started)
  429. _elapsedTime = _delay;
  430. else
  431. return;
  432. }
  433. Update();
  434. }
  435. /// <summary>
  436. ///
  437. /// </summary>
  438. /// <param name="complete"></param>
  439. public void Kill(bool complete = false)
  440. {
  441. if (_killed)
  442. return;
  443. if (complete)
  444. {
  445. if (_ended == 0)
  446. {
  447. if (_breakpoint >= 0)
  448. _elapsedTime = _delay + _breakpoint;
  449. else if (_repeat >= 0)
  450. _elapsedTime = _delay + _duration * (_repeat + 1);
  451. else
  452. _elapsedTime = _delay + _duration * 2;
  453. Update();
  454. }
  455. CallCompleteCallback();
  456. }
  457. _killed = true;
  458. }
  459. internal GTweener _To(float start, float end, float duration)
  460. {
  461. _valueSize = 1;
  462. _startValue.x = start;
  463. _endValue.x = end;
  464. _value.x = start;
  465. _duration = duration;
  466. return this;
  467. }
  468. internal GTweener _To(Vector2 start, Vector2 end, float duration)
  469. {
  470. _valueSize = 2;
  471. _startValue.vec2 = start;
  472. _endValue.vec2 = end;
  473. _value.vec2 = start;
  474. _duration = duration;
  475. return this;
  476. }
  477. internal GTweener _To(Vector3 start, Vector3 end, float duration)
  478. {
  479. _valueSize = 3;
  480. _startValue.vec3 = start;
  481. _endValue.vec3 = end;
  482. _value.vec3 = start;
  483. _duration = duration;
  484. return this;
  485. }
  486. internal GTweener _To(Vector4 start, Vector4 end, float duration)
  487. {
  488. _valueSize = 4;
  489. _startValue.vec4 = start;
  490. _endValue.vec4 = end;
  491. _value.vec4 = start;
  492. _duration = duration;
  493. return this;
  494. }
  495. internal GTweener _To(Color start, Color end, float duration)
  496. {
  497. _valueSize = 4;
  498. _startValue.color = start;
  499. _endValue.color = end;
  500. _value.color = start;
  501. _duration = duration;
  502. return this;
  503. }
  504. internal GTweener _To(double start, double end, float duration)
  505. {
  506. _valueSize = 5;
  507. _startValue.d = start;
  508. _endValue.d = end;
  509. _value.d = start;
  510. _duration = duration;
  511. return this;
  512. }
  513. internal GTweener _Shake(Vector3 start, float amplitude, float duration)
  514. {
  515. _valueSize = 6;
  516. _startValue.vec3 = start;
  517. _startValue.w = amplitude;
  518. _duration = duration;
  519. _easeType = EaseType.Linear;
  520. return this;
  521. }
  522. internal void _Init()
  523. {
  524. _delay = 0;
  525. _duration = 0;
  526. _breakpoint = -1;
  527. _easeType = EaseType.QuadOut;
  528. _timeScale = 1;
  529. _ignoreEngineTimeScale = false;
  530. _easePeriod = 0;
  531. _easeOvershootOrAmplitude = 1.70158f;
  532. _snapping = false;
  533. _repeat = 0;
  534. _yoyo = false;
  535. _valueSize = 0;
  536. _started = false;
  537. _paused = false;
  538. _killed = false;
  539. _elapsedTime = 0;
  540. _normalizedTime = 0;
  541. _ended = 0;
  542. _path = null;
  543. _customEase = null;
  544. _smoothStart = Time.frameCount == 1 ? 3 : 1;//刚启动时会有多帧的超时
  545. }
  546. internal void _Reset()
  547. {
  548. _target = null;
  549. _listener = null;
  550. _userData = null;
  551. _onStart = _onUpdate = _onComplete = null;
  552. _onStart1 = _onUpdate1 = _onComplete1 = null;
  553. }
  554. internal void _Update()
  555. {
  556. if (_ended != 0) //Maybe completed by seek
  557. {
  558. CallCompleteCallback();
  559. _killed = true;
  560. return;
  561. }
  562. float dt;
  563. if (_smoothStart > 0)
  564. {
  565. _smoothStart--;
  566. dt = Mathf.Clamp(Time.unscaledDeltaTime, 0, Application.targetFrameRate > 0 ? (1.0f / Application.targetFrameRate) : 0.016f);
  567. if (!_ignoreEngineTimeScale)
  568. dt *= Time.timeScale;
  569. }
  570. else if (_ignoreEngineTimeScale)
  571. dt = Time.unscaledDeltaTime;
  572. else
  573. dt = Time.deltaTime;
  574. if (_timeScale != 1)
  575. dt *= _timeScale;
  576. if (dt == 0)
  577. return;
  578. _elapsedTime += dt;
  579. Update();
  580. if (_ended != 0)
  581. {
  582. if (!_killed)
  583. {
  584. CallCompleteCallback();
  585. _killed = true;
  586. }
  587. }
  588. }
  589. void Update()
  590. {
  591. _ended = 0;
  592. if (_valueSize == 0) //DelayedCall
  593. {
  594. if (_elapsedTime >= _delay + _duration)
  595. _ended = 1;
  596. return;
  597. }
  598. if (!_started)
  599. {
  600. if (_elapsedTime < _delay)
  601. return;
  602. _started = true;
  603. CallStartCallback();
  604. if (_killed)
  605. return;
  606. }
  607. bool reversed = false;
  608. float tt = _elapsedTime - _delay;
  609. if (_breakpoint >= 0 && tt >= _breakpoint)
  610. {
  611. tt = _breakpoint;
  612. _ended = 2;
  613. }
  614. if (_repeat != 0)
  615. {
  616. int round = Mathf.FloorToInt(tt / _duration);
  617. tt -= _duration * round;
  618. if (_yoyo)
  619. reversed = round % 2 == 1;
  620. if (_repeat > 0 && _repeat - round < 0)
  621. {
  622. if (_yoyo)
  623. reversed = _repeat % 2 == 1;
  624. tt = _duration;
  625. _ended = 1;
  626. }
  627. }
  628. else if (tt >= _duration)
  629. {
  630. tt = _duration;
  631. _ended = 1;
  632. }
  633. _normalizedTime = EaseManager.Evaluate(_easeType, reversed ? (_duration - tt) : tt, _duration,
  634. _easeOvershootOrAmplitude, _easePeriod, _customEase);
  635. _value.SetZero();
  636. _deltaValue.SetZero();
  637. if (_valueSize == 5)
  638. {
  639. double d = _startValue.d + (_endValue.d - _startValue.d) * _normalizedTime;
  640. if (_snapping)
  641. d = Math.Round(d);
  642. _deltaValue.d = d - _value.d;
  643. _value.d = d;
  644. _value.x = (float)d;
  645. }
  646. else if (_valueSize == 6)
  647. {
  648. if (_ended == 0)
  649. {
  650. Vector3 r = UnityEngine.Random.insideUnitSphere;
  651. r.x = r.x > 0 ? 1 : -1;
  652. r.y = r.y > 0 ? 1 : -1;
  653. r.z = r.z > 0 ? 1 : -1;
  654. r *= _startValue.w * (1 - _normalizedTime);
  655. _deltaValue.vec3 = r;
  656. _value.vec3 = _startValue.vec3 + r;
  657. }
  658. else
  659. _value.vec3 = _startValue.vec3;
  660. }
  661. else if (_path != null)
  662. {
  663. Vector3 vec3 = _path.GetPointAt(_normalizedTime);
  664. if (_snapping)
  665. {
  666. vec3.x = Mathf.Round(vec3.x);
  667. vec3.y = Mathf.Round(vec3.y);
  668. vec3.z = Mathf.Round(vec3.z);
  669. }
  670. _deltaValue.vec3 = vec3 - _value.vec3;
  671. _value.vec3 = vec3;
  672. }
  673. else
  674. {
  675. for (int i = 0; i < _valueSize; i++)
  676. {
  677. float n1 = _startValue[i];
  678. float n2 = _endValue[i];
  679. float f = n1 + (n2 - n1) * _normalizedTime;
  680. if (_snapping)
  681. f = Mathf.Round(f);
  682. _deltaValue[i] = f - _value[i];
  683. _value[i] = f;
  684. }
  685. _value.d = _value.x;
  686. }
  687. if (_target != null && _propType != TweenPropType.None)
  688. TweenPropTypeUtils.SetProps(_target, _propType, _value);
  689. CallUpdateCallback();
  690. }
  691. void CallStartCallback()
  692. {
  693. if (GTween.catchCallbackExceptions)
  694. {
  695. try
  696. {
  697. if (_onStart1 != null)
  698. _onStart1(this);
  699. if (_onStart != null)
  700. _onStart();
  701. if (_listener != null)
  702. _listener.OnTweenStart(this);
  703. }
  704. catch (Exception e)
  705. {
  706. Debug.LogWarning("FairyGUI: error in start callback > " + e.Message);
  707. }
  708. }
  709. else
  710. {
  711. if (_onStart1 != null)
  712. _onStart1(this);
  713. if (_onStart != null)
  714. _onStart();
  715. if (_listener != null)
  716. _listener.OnTweenStart(this);
  717. }
  718. }
  719. void CallUpdateCallback()
  720. {
  721. if (GTween.catchCallbackExceptions)
  722. {
  723. try
  724. {
  725. if (_onUpdate1 != null)
  726. _onUpdate1(this);
  727. if (_onUpdate != null)
  728. _onUpdate();
  729. if (_listener != null)
  730. _listener.OnTweenUpdate(this);
  731. }
  732. catch (Exception e)
  733. {
  734. Debug.LogWarning("FairyGUI: error in update callback > " + e.Message);
  735. }
  736. }
  737. else
  738. {
  739. if (_onUpdate1 != null)
  740. _onUpdate1(this);
  741. if (_onUpdate != null)
  742. _onUpdate();
  743. if (_listener != null)
  744. _listener.OnTweenUpdate(this);
  745. }
  746. }
  747. void CallCompleteCallback()
  748. {
  749. if (GTween.catchCallbackExceptions)
  750. {
  751. try
  752. {
  753. if (_onComplete1 != null)
  754. _onComplete1(this);
  755. if (_onComplete != null)
  756. _onComplete();
  757. if (_listener != null)
  758. _listener.OnTweenComplete(this);
  759. }
  760. catch (Exception e)
  761. {
  762. Debug.LogWarning("FairyGUI: error in complete callback > " + e.Message);
  763. }
  764. }
  765. else
  766. {
  767. if (_onComplete1 != null)
  768. _onComplete1(this);
  769. if (_onComplete != null)
  770. _onComplete();
  771. if (_listener != null)
  772. _listener.OnTweenComplete(this);
  773. }
  774. }
  775. }
  776. }