Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnimationStyle.cs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace XCharts.Runtime
  5. {
  6. public enum AnimationType
  7. {
  8. /// <summary>
  9. /// he default. An animation playback mode will be selected according to the actual situation.
  10. /// |默认。内部会根据实际情况选择一种动画播放方式。
  11. /// </summary>
  12. Default,
  13. /// <summary>
  14. /// Play the animation from left to right.
  15. /// |从左往右播放动画。
  16. /// </summary>
  17. LeftToRight,
  18. /// <summary>
  19. /// Play the animation from bottom to top.
  20. /// |从下往上播放动画。
  21. /// </summary>
  22. BottomToTop,
  23. /// <summary>
  24. /// Play animations from the inside out.
  25. /// |由内到外播放动画。
  26. /// </summary>
  27. InsideOut,
  28. /// <summary>
  29. /// Play the animation along the path.
  30. /// |沿着路径播放动画。当折线图从左到右无序或有折返时,可以使用该模式。
  31. /// </summary>
  32. AlongPath,
  33. /// <summary>
  34. /// Play the animation clockwise.
  35. /// |顺时针播放动画。
  36. /// </summary>
  37. Clockwise,
  38. }
  39. public enum AnimationEasing
  40. {
  41. Linear,
  42. }
  43. /// <summary>
  44. /// the animation of serie.
  45. /// |动画表现。
  46. /// </summary>
  47. [System.Serializable]
  48. public class AnimationStyle : ChildComponent
  49. {
  50. [SerializeField] private bool m_Enable = true;
  51. [SerializeField] private AnimationType m_Type;
  52. [SerializeField] private AnimationEasing m_Easting;
  53. [SerializeField] private int m_Threshold = 2000;
  54. [SerializeField] private float m_FadeInDuration = 1000;
  55. [SerializeField] private float m_FadeInDelay = 0;
  56. [SerializeField] private float m_FadeOutDuration = 1000f;
  57. [SerializeField] private float m_FadeOutDelay = 0;
  58. [SerializeField] private bool m_DataChangeEnable = true;
  59. [SerializeField] private float m_DataChangeDuration = 500;
  60. [SerializeField] private float m_ActualDuration;
  61. [SerializeField][Since("v3.4.0")] private bool m_UnscaledTime;
  62. /// <summary>
  63. /// 自定义渐入动画延时函数。返回ms值。
  64. /// </summary>
  65. public AnimationDelayFunction fadeInDelayFunction;
  66. /// <summary>
  67. /// 自定义渐入动画时长函数。返回ms值。
  68. /// </summary>
  69. public AnimationDurationFunction fadeInDurationFunction;
  70. /// <summary>
  71. /// 自定义渐出动画延时函数。返回ms值。
  72. /// </summary>
  73. public AnimationDelayFunction fadeOutDelayFunction;
  74. /// <summary>
  75. /// 自定义渐出动画时长函数。返回ms值。
  76. /// </summary>
  77. public AnimationDurationFunction fadeOutDurationFunction;
  78. public AnimationStyleContext context = new AnimationStyleContext();
  79. /// <summary>
  80. /// Whether to enable animation.
  81. /// |是否开启动画效果。
  82. /// </summary>
  83. public bool enable { get { return m_Enable; } set { m_Enable = value; } }
  84. /// <summary>
  85. /// The type of animation.
  86. /// |动画类型。
  87. /// </summary>
  88. public AnimationType type { get { return m_Type; } set { m_Type = value; } }
  89. /// <summary>
  90. /// Easing method used for the first animation.
  91. /// |动画的缓动效果。
  92. /// </summary>
  93. //public Easing easting { get { return m_Easting; } set { m_Easting = value; } }
  94. /// <summary>
  95. /// The milliseconds duration of the fadeIn animation.
  96. /// |设定的渐入动画时长(毫秒)。如果要设置单个数据项的渐入时长,可以用代码定制:customFadeInDuration。
  97. /// </summary>
  98. public float fadeInDuration { get { return m_FadeInDuration; } set { m_FadeInDuration = value < 0 ? 0 : value; } }
  99. /// <summary>
  100. /// The milliseconds duration of the fadeOut animation.
  101. /// |设定的渐出动画时长(毫秒)。如果要设置单个数据项的渐出时长,可以用代码定制:customFadeOutDuration。
  102. /// </summary>
  103. public float fadeOutDuration { get { return m_FadeOutDuration; } set { m_FadeOutDuration = value < 0 ? 0 : value; } }
  104. /// <summary>
  105. /// The milliseconds actual duration of the first animation.
  106. /// |实际的动画时长(毫秒)。
  107. /// </summary>
  108. public float actualDuration { get { return m_ActualDuration; } }
  109. /// <summary>
  110. /// Whether to set graphic number threshold to animation. Animation will be disabled when graphic number is larger than threshold.
  111. /// |是否开启动画的阈值,当单个系列显示的图形数量大于这个阈值时会关闭动画。
  112. /// </summary>
  113. public int threshold { get { return m_Threshold; } set { m_Threshold = value; } }
  114. /// <summary>
  115. /// The milliseconds delay before updating the first animation.
  116. /// |渐入动画延时(毫秒)。如果要设置单个数据项的延时,可以用代码定制:customFadeInDelay。
  117. /// </summary>
  118. public float fadeInDelay { get { return m_FadeInDelay; } set { m_FadeInDelay = value < 0 ? 0 : value; } }
  119. /// <summary>
  120. /// 渐出动画延时(毫秒)。如果要设置单个数据项的延时,可以用代码定制:customFadeOutDelay。
  121. /// </summary>
  122. public float fadeOutDelay { get { return m_FadeOutDelay; } set { m_FadeInDelay = value < 0 ? 0 : value; } }
  123. /// <summary>
  124. /// 是否开启数据变更动画。
  125. /// </summary>
  126. public bool dataChangeEnable { get { return m_DataChangeEnable; } set { m_DataChangeEnable = value; } }
  127. /// <summary>
  128. /// The milliseconds duration of the data change animation.
  129. /// |数据变更的动画时长(毫秒)。
  130. /// </summary>
  131. public float dataChangeDuration { get { return m_DataChangeDuration; } set { m_DataChangeDuration = value < 0 ? 0 : value; } }
  132. /// <summary>
  133. /// Animation updates independently of Time.timeScale.
  134. /// |动画是否受TimeScaled的影响。默认为 false 受TimeScaled的影响。
  135. /// </summary>
  136. public bool unscaledTime { get { return m_UnscaledTime; } set { m_UnscaledTime = value; } }
  137. /// <summary>
  138. /// 渐入动画完成回调
  139. /// </summary>
  140. public Action fadeInFinishCallback { get; set; }
  141. /// <summary>
  142. /// 渐出动画完成回调
  143. /// </summary>
  144. public Action fadeOutFinishCallback { get; set; }
  145. private Dictionary<int, float> m_ItemCurrProgress = new Dictionary<int, float>();
  146. private Dictionary<int, float> m_ItemDestProgress = new Dictionary<int, float>();
  147. private bool m_FadeIn = false;
  148. private bool m_IsEnd = true;
  149. private bool m_IsPause = false;
  150. private bool m_FadeOut = false;
  151. private bool m_FadeOuted = false;
  152. private bool m_IsInit = false;
  153. private float startTime { get; set; }
  154. private float m_CurrDetailProgress;
  155. private float m_DestDetailProgress;
  156. private float m_TotalDetailProgress;
  157. private float m_CurrSymbolProgress;
  158. private Vector3 m_LinePathLastPos;
  159. public void FadeIn()
  160. {
  161. if (m_FadeOut)
  162. return;
  163. if (m_IsPause)
  164. {
  165. m_IsPause = false;
  166. return;
  167. }
  168. if (m_FadeIn)
  169. return;
  170. startTime = Time.time;
  171. m_FadeIn = true;
  172. m_IsEnd = false;
  173. m_IsInit = false;
  174. m_IsPause = false;
  175. m_FadeOuted = false;
  176. m_CurrDetailProgress = 0;
  177. m_DestDetailProgress = 1;
  178. m_CurrSymbolProgress = 0;
  179. m_ItemCurrProgress.Clear();
  180. m_ItemDestProgress.Clear();
  181. }
  182. public void Restart()
  183. {
  184. Reset();
  185. FadeIn();
  186. }
  187. public void FadeOut()
  188. {
  189. if (m_IsPause)
  190. {
  191. m_IsPause = false;
  192. return;
  193. }
  194. m_FadeOut = true;
  195. startTime = Time.time;
  196. m_FadeIn = true;
  197. m_IsEnd = false;
  198. m_IsInit = false;
  199. m_IsPause = false;
  200. m_CurrDetailProgress = 0;
  201. m_DestDetailProgress = 1;
  202. m_CurrSymbolProgress = 0;
  203. m_ItemCurrProgress.Clear();
  204. m_ItemDestProgress.Clear();
  205. }
  206. public void Pause()
  207. {
  208. if (!m_IsPause)
  209. {
  210. m_IsPause = true;
  211. }
  212. }
  213. public void Resume()
  214. {
  215. if (m_IsPause)
  216. {
  217. m_IsPause = false;
  218. }
  219. }
  220. private void End()
  221. {
  222. if (m_IsEnd)
  223. return;
  224. m_ActualDuration = (int) ((Time.time - startTime) * 1000) - (m_FadeOut ? fadeOutDelay : fadeInDelay);
  225. m_IsEnd = true;
  226. m_IsInit = false;
  227. if (m_FadeIn)
  228. {
  229. m_FadeIn = false;
  230. if (fadeInFinishCallback != null)
  231. {
  232. fadeInFinishCallback();
  233. }
  234. }
  235. if (m_FadeOut)
  236. {
  237. m_FadeOut = false;
  238. m_FadeOuted = true;
  239. if (fadeOutFinishCallback != null)
  240. {
  241. fadeOutFinishCallback();
  242. }
  243. }
  244. }
  245. public void Reset()
  246. {
  247. m_FadeIn = false;
  248. m_IsEnd = true;
  249. m_IsInit = false;
  250. m_IsPause = false;
  251. m_FadeOut = false;
  252. m_FadeOuted = false;
  253. m_ItemCurrProgress.Clear();
  254. }
  255. public void InitProgress(float curr, float dest)
  256. {
  257. if (m_IsInit || m_IsEnd)
  258. return;
  259. m_IsInit = true;
  260. m_TotalDetailProgress = dest - curr;
  261. if (m_FadeOut)
  262. {
  263. m_CurrDetailProgress = dest;
  264. m_DestDetailProgress = curr;
  265. }
  266. else
  267. {
  268. m_CurrDetailProgress = curr;
  269. m_DestDetailProgress = dest;
  270. }
  271. }
  272. public void InitProgress(List<Vector3> paths, bool isY)
  273. {
  274. if (paths.Count < 1) return;
  275. var sp = paths[0];
  276. var ep = paths[paths.Count - 1];
  277. var currDetailProgress = isY ? sp.y : sp.x;
  278. var totalDetailProgress = isY ? ep.y : ep.x;
  279. if (context.type == AnimationType.AlongPath)
  280. {
  281. currDetailProgress = 0;
  282. totalDetailProgress = 0;
  283. var lp = sp;
  284. for (int i = 1; i < paths.Count; i++)
  285. {
  286. var np = paths[i];
  287. totalDetailProgress += Vector3.Distance(np, lp);
  288. lp = np;
  289. }
  290. m_LinePathLastPos = sp;
  291. context.currentPathDistance = 0;
  292. }
  293. InitProgress(currDetailProgress, totalDetailProgress);
  294. }
  295. private void SetDataCurrProgress(int index, float state)
  296. {
  297. m_ItemCurrProgress[index] = state;
  298. }
  299. private float GetDataCurrProgress(int index, float initValue, float destValue, ref bool isBarEnd)
  300. {
  301. if (IsInDelay())
  302. {
  303. isBarEnd = false;
  304. return initValue;
  305. }
  306. var c1 = !m_ItemCurrProgress.ContainsKey(index);
  307. var c2 = !m_ItemDestProgress.ContainsKey(index);
  308. if (c1 || c2)
  309. {
  310. if (c1)
  311. m_ItemCurrProgress.Add(index, initValue);
  312. if (c2)
  313. m_ItemDestProgress.Add(index, destValue);
  314. isBarEnd = false;
  315. }
  316. else
  317. {
  318. isBarEnd = m_ItemCurrProgress[index] == m_ItemDestProgress[index];
  319. }
  320. return m_ItemCurrProgress[index];
  321. }
  322. public bool IsFinish()
  323. {
  324. #if UNITY_EDITOR
  325. if (!Application.isPlaying)
  326. return true;
  327. #endif
  328. if (!m_Enable || m_IsEnd)
  329. return true;
  330. if (IsIndexAnimation())
  331. {
  332. if (m_FadeOut) return m_CurrDetailProgress <= m_DestDetailProgress;
  333. else return m_CurrDetailProgress > m_DestDetailProgress;
  334. }
  335. if (IsItemAnimation())
  336. return false;
  337. return true;
  338. }
  339. public bool IsInFadeOut()
  340. {
  341. return m_FadeOut;
  342. }
  343. public bool IsInDelay()
  344. {
  345. if (m_FadeOut)
  346. return (fadeOutDelay > 0 && Time.time - startTime < fadeOutDelay / 1000);
  347. else
  348. return (fadeInDelay > 0 && Time.time - startTime < fadeInDelay / 1000);
  349. }
  350. public bool IsItemAnimation()
  351. {
  352. return context.type == AnimationType.BottomToTop || context.type == AnimationType.InsideOut;
  353. }
  354. public bool IsIndexAnimation()
  355. {
  356. return context.type == AnimationType.LeftToRight ||
  357. context.type == AnimationType.Clockwise ||
  358. context.type == AnimationType.AlongPath;
  359. }
  360. public float GetIndexDelay(int dataIndex)
  361. {
  362. if (m_FadeOut && fadeOutDelayFunction != null)
  363. return fadeOutDelayFunction(dataIndex);
  364. else if (m_FadeIn && fadeInDelayFunction != null)
  365. return fadeInDelayFunction(dataIndex);
  366. else
  367. return 0;
  368. }
  369. public bool IsInIndexDelay(int dataIndex)
  370. {
  371. return Time.time - startTime < GetIndexDelay(dataIndex) / 1000f;
  372. }
  373. public bool IsAllOutDelay(int dataCount)
  374. {
  375. var nowTime = Time.time - startTime;
  376. for (int i = 0; i < dataCount; i++)
  377. {
  378. if (nowTime < GetIndexDelay(i) / 1000)
  379. return false;
  380. }
  381. return true;
  382. }
  383. public bool CheckDetailBreak(float detail)
  384. {
  385. if (!IsIndexAnimation())
  386. return false;
  387. return !IsFinish() && detail > m_CurrDetailProgress;
  388. }
  389. public bool CheckDetailBreak(Vector3 pos, bool isYAxis)
  390. {
  391. if (!IsIndexAnimation())
  392. return false;
  393. if (IsFinish())
  394. return false;
  395. if (context.type == AnimationType.AlongPath)
  396. {
  397. context.currentPathDistance += Vector3.Distance(pos, m_LinePathLastPos);
  398. m_LinePathLastPos = pos;
  399. return CheckDetailBreak(context.currentPathDistance);
  400. }
  401. else
  402. {
  403. if (isYAxis)
  404. return pos.y > m_CurrDetailProgress;
  405. else
  406. return pos.x > m_CurrDetailProgress;
  407. }
  408. }
  409. public void CheckProgress()
  410. {
  411. if (IsItemAnimation() && context.isAllItemAnimationEnd)
  412. {
  413. End();
  414. return;
  415. }
  416. CheckProgress(m_TotalDetailProgress);
  417. }
  418. public void CheckProgress(double total)
  419. {
  420. if (IsFinish())
  421. return;
  422. if (!m_IsInit || m_IsPause || m_IsEnd)
  423. return;
  424. if (IsInDelay())
  425. return;
  426. m_ActualDuration = (int) ((Time.time - startTime) * 1000) - fadeInDelay;
  427. var duration = GetCurrAnimationDuration();
  428. var delta = (float) (total / duration * (m_UnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime));
  429. if (m_FadeOut)
  430. {
  431. m_CurrDetailProgress -= delta;
  432. if (m_CurrDetailProgress <= m_DestDetailProgress)
  433. {
  434. m_CurrDetailProgress = m_DestDetailProgress;
  435. End();
  436. }
  437. }
  438. else
  439. {
  440. m_CurrDetailProgress += delta;
  441. if (m_CurrDetailProgress >= m_DestDetailProgress)
  442. {
  443. m_CurrDetailProgress = m_DestDetailProgress;
  444. End();
  445. }
  446. }
  447. }
  448. internal float GetCurrAnimationDuration(int dataIndex = -1)
  449. {
  450. if (dataIndex >= 0)
  451. {
  452. if (m_FadeOut && fadeOutDurationFunction != null)
  453. return fadeOutDurationFunction(dataIndex) / 1000f;
  454. if (m_FadeIn && fadeInDurationFunction != null)
  455. return fadeInDurationFunction(dataIndex) / 1000f;
  456. }
  457. if (m_FadeOut)
  458. return m_FadeOutDuration > 0 ? m_FadeOutDuration / 1000 : 1f;
  459. else
  460. return m_FadeInDuration > 0 ? m_FadeInDuration / 1000 : 1f;
  461. }
  462. internal float CheckItemProgress(int dataIndex, float destProgress, ref bool isEnd, float startProgress = 0)
  463. {
  464. isEnd = false;
  465. var initHig = m_FadeOut ? destProgress : startProgress;
  466. var destHig = m_FadeOut ? startProgress : destProgress;
  467. var currHig = GetDataCurrProgress(dataIndex, initHig, destHig, ref isEnd);
  468. if (isEnd || IsFinish())
  469. {
  470. return m_FadeOuted ? startProgress : destProgress;
  471. }
  472. else if (IsInDelay() || IsInIndexDelay(dataIndex))
  473. {
  474. return m_FadeOut ? destProgress : startProgress;
  475. }
  476. else if (m_IsPause)
  477. {
  478. return currHig;
  479. }
  480. else
  481. {
  482. var duration = GetCurrAnimationDuration(dataIndex);
  483. var delta = (destProgress - startProgress) / duration * (m_UnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime);
  484. currHig = currHig + (m_FadeOut ? -delta : delta);
  485. if (m_FadeOut)
  486. {
  487. if ((initHig > 0 && currHig <= 0) || (initHig < 0 && currHig >= 0))
  488. {
  489. currHig = 0;
  490. isEnd = true;
  491. }
  492. }
  493. else
  494. {
  495. if ((destProgress - startProgress > 0 && currHig > destProgress) ||
  496. (destProgress - startProgress < 0 && currHig < destProgress))
  497. {
  498. currHig = destProgress;
  499. isEnd = true;
  500. }
  501. }
  502. SetDataCurrProgress(dataIndex, currHig);
  503. return currHig;
  504. }
  505. }
  506. public void CheckSymbol(float dest)
  507. {
  508. if (!enable || m_IsEnd || m_IsPause || !m_IsInit)
  509. return;
  510. if (IsInDelay())
  511. return;
  512. var duration = GetCurrAnimationDuration();
  513. var delta = dest / duration * (m_UnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime);
  514. if (m_FadeOut)
  515. {
  516. m_CurrSymbolProgress -= delta;
  517. if (m_CurrSymbolProgress < 0)
  518. m_CurrSymbolProgress = 0;
  519. }
  520. else
  521. {
  522. m_CurrSymbolProgress += delta;
  523. if (m_CurrSymbolProgress > dest)
  524. m_CurrSymbolProgress = dest;
  525. }
  526. }
  527. public float GetSysmbolSize(float dest)
  528. {
  529. #if UNITY_EDITOR
  530. if (!Application.isPlaying)
  531. return dest;
  532. #endif
  533. if (!enable)
  534. return dest;
  535. if (m_IsEnd)
  536. return m_FadeOut ? 0 : dest;
  537. return m_CurrSymbolProgress;
  538. }
  539. public float GetCurrDetail()
  540. {
  541. #if UNITY_EDITOR
  542. if (!Application.isPlaying)
  543. return m_DestDetailProgress;
  544. #endif
  545. return m_CurrDetailProgress;
  546. }
  547. public float GetCurrRate()
  548. {
  549. #if UNITY_EDITOR
  550. if (!Application.isPlaying)
  551. return 1;
  552. #endif
  553. if (!enable || m_IsEnd)
  554. return 1;
  555. return m_CurrDetailProgress;
  556. }
  557. public int GetCurrIndex()
  558. {
  559. #if UNITY_EDITOR
  560. if (!Application.isPlaying)
  561. return -1;
  562. #endif
  563. if (!enable || m_IsEnd)
  564. return -1;
  565. return (int) m_CurrDetailProgress;
  566. }
  567. public float GetUpdateAnimationDuration()
  568. {
  569. if (m_Enable && m_DataChangeEnable && IsFinish())
  570. return m_DataChangeDuration;
  571. else
  572. return 0;
  573. }
  574. public bool HasFadeOut()
  575. {
  576. return enable && m_FadeOuted && m_IsEnd;
  577. }
  578. }
  579. }