AnimationStyle.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. support animation type: fadeIn, fadeOut, change, addition.
  45. /// ||动画组件,用于控制图表的动画播放。支持配置五种动画表现:FadeIn(渐入动画),FadeOut(渐出动画),Change(变更动画),Addition(新增动画),Interaction(交互动画)。
  46. /// 按作用的对象可以分为两类:SerieAnimation(系列动画)和DataAnimation(数据动画)。
  47. /// </summary>
  48. [System.Serializable]
  49. public class AnimationStyle : ChildComponent
  50. {
  51. [SerializeField] private bool m_Enable = true;
  52. [SerializeField] private AnimationType m_Type;
  53. [SerializeField] private AnimationEasing m_Easting;
  54. [SerializeField] private int m_Threshold = 2000;
  55. [SerializeField][Since("v3.4.0")] private bool m_UnscaledTime;
  56. [SerializeField][Since("v3.8.0")] private AnimationFadeIn m_FadeIn = new AnimationFadeIn();
  57. [SerializeField][Since("v3.8.0")] private AnimationFadeOut m_FadeOut = new AnimationFadeOut() { reverse = true };
  58. [SerializeField][Since("v3.8.0")] private AnimationChange m_Change = new AnimationChange() { duration = 500 };
  59. [SerializeField][Since("v3.8.0")] private AnimationAddition m_Addition = new AnimationAddition() { duration = 500 };
  60. [SerializeField][Since("v3.8.0")] private AnimationHiding m_Hiding = new AnimationHiding() { duration = 500 };
  61. [SerializeField][Since("v3.8.0")] private AnimationInteraction m_Interaction = new AnimationInteraction() { duration = 250 };
  62. [Obsolete("Use animation.fadeIn.delayFunction instead.", true)]
  63. public AnimationDelayFunction fadeInDelayFunction;
  64. [Obsolete("Use animation.fadeIn.durationFunction instead.", true)]
  65. public AnimationDurationFunction fadeInDurationFunction;
  66. [Obsolete("Use animation.fadeOut.delayFunction instead.", true)]
  67. public AnimationDelayFunction fadeOutDelayFunction;
  68. [Obsolete("Use animation.fadeOut.durationFunction instead.", true)]
  69. public AnimationDurationFunction fadeOutDurationFunction;
  70. [Obsolete("Use animation.fadeIn.OnAnimationEnd() instead.", true)]
  71. public Action fadeInFinishCallback { get; set; }
  72. [Obsolete("Use animation.fadeOut.OnAnimationEnd() instead.", true)]
  73. public Action fadeOutFinishCallback { get; set; }
  74. public AnimationStyleContext context = new AnimationStyleContext();
  75. /// <summary>
  76. /// Whether to enable animation.
  77. /// ||是否开启动画效果。
  78. /// </summary>
  79. public bool enable { get { return m_Enable; } set { m_Enable = value; } }
  80. /// <summary>
  81. /// The type of animation.
  82. /// ||动画类型。
  83. /// </summary>
  84. public AnimationType type
  85. {
  86. get { return m_Type; }
  87. set
  88. {
  89. m_Type = value;
  90. if (m_Type != AnimationType.Default)
  91. {
  92. context.type = m_Type;
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// Whether to set graphic number threshold to animation. Animation will be disabled when graphic number is larger than threshold.
  98. /// ||是否开启动画的阈值,当单个系列显示的图形数量大于这个阈值时会关闭动画。
  99. /// </summary>
  100. public int threshold { get { return m_Threshold; } set { m_Threshold = value; } }
  101. /// <summary>
  102. /// Animation updates independently of Time.timeScale.
  103. /// ||动画是否受TimeScaled的影响。默认为 false 受TimeScaled的影响。
  104. /// </summary>
  105. public bool unscaledTime { get { return m_UnscaledTime; } set { m_UnscaledTime = value; } }
  106. /// <summary>
  107. /// Fade in animation configuration.
  108. /// ||渐入动画配置。
  109. /// </summary>
  110. public AnimationFadeIn fadeIn { get { return m_FadeIn; } }
  111. /// <summary>
  112. /// Fade out animation configuration.
  113. /// ||渐出动画配置。
  114. /// </summary>
  115. public AnimationFadeOut fadeOut { get { return m_FadeOut; } }
  116. /// <summary>
  117. /// Update data animation configuration.
  118. /// ||数据变更动画配置。
  119. /// </summary>
  120. public AnimationChange change { get { return m_Change; } }
  121. /// <summary>
  122. /// Add data animation configuration.
  123. /// ||数据新增动画配置。
  124. /// </summary>
  125. public AnimationAddition addition { get { return m_Addition; } }
  126. /// <summary>
  127. /// Data hiding animation configuration.
  128. /// ||数据隐藏动画配置。
  129. /// </summary>
  130. public AnimationHiding hiding { get { return m_Hiding; } }
  131. /// <summary>
  132. /// Interaction animation configuration.
  133. /// ||交互动画配置。
  134. /// </summary>
  135. public AnimationInteraction interaction { get { return m_Interaction; } }
  136. private Vector3 m_LinePathLastPos;
  137. private List<AnimationInfo> m_Animations;
  138. private List<AnimationInfo> animations
  139. {
  140. get
  141. {
  142. if (m_Animations == null)
  143. {
  144. m_Animations = new List<AnimationInfo>();
  145. m_Animations.Add(m_FadeIn);
  146. m_Animations.Add(m_FadeOut);
  147. m_Animations.Add(m_Change);
  148. m_Animations.Add(m_Addition);
  149. m_Animations.Add(m_Hiding);
  150. }
  151. return m_Animations;
  152. }
  153. }
  154. /// <summary>
  155. /// The actived animation.
  156. /// ||当前激活的动画。
  157. /// </summary>
  158. public AnimationInfo activedAnimation
  159. {
  160. get
  161. {
  162. foreach (var anim in animations)
  163. {
  164. if (anim.context.start) return anim;
  165. }
  166. return null;
  167. }
  168. }
  169. /// <summary>
  170. /// Start fadein animation.
  171. /// ||开始渐入动画。
  172. /// </summary>
  173. public void FadeIn()
  174. {
  175. if (m_FadeOut.context.start) return;
  176. m_FadeIn.Start();
  177. }
  178. /// <summary>
  179. /// Restart the actived animation.
  180. /// ||重启当前激活的动画。
  181. /// </summary>
  182. public void Restart()
  183. {
  184. var anim = activedAnimation;
  185. Reset();
  186. if (anim != null)
  187. {
  188. anim.Start();
  189. }
  190. }
  191. /// <summary>
  192. /// Start fadeout animation.
  193. /// ||开始渐出动画。
  194. /// </summary>
  195. public void FadeOut()
  196. {
  197. m_FadeOut.Start();
  198. }
  199. /// <summary>
  200. /// Start additon animation.
  201. /// ||开始数据新增动画。
  202. /// </summary>
  203. public void Addition()
  204. {
  205. if (!enable) return;
  206. if (!m_FadeIn.context.start && !m_FadeOut.context.start)
  207. {
  208. m_Addition.Start(false);
  209. }
  210. }
  211. /// <summary>
  212. /// Pause all animations.
  213. /// ||暂停所有动画。
  214. /// </summary>
  215. public void Pause()
  216. {
  217. foreach (var anim in animations)
  218. {
  219. anim.Pause();
  220. }
  221. }
  222. /// <summary>
  223. /// Resume all animations.
  224. /// ||恢复所有动画。
  225. /// </summary>
  226. public void Resume()
  227. {
  228. foreach (var anim in animations)
  229. {
  230. anim.Resume();
  231. }
  232. }
  233. /// <summary>
  234. /// Reset all animations.
  235. /// </summary>
  236. public void Reset()
  237. {
  238. foreach (var anim in animations)
  239. {
  240. anim.Reset();
  241. }
  242. }
  243. /// <summary>
  244. /// Initialize animation configuration.
  245. /// ||初始化动画配置。
  246. /// </summary>
  247. /// <param name="curr">当前进度</param>
  248. /// <param name="dest">目标进度</param>
  249. public void InitProgress(float curr, float dest)
  250. {
  251. var anim = activedAnimation;
  252. if (anim == null) return;
  253. var isAddedAnim = anim is AnimationAddition;
  254. if (IsSerieAnimation())
  255. {
  256. if (isAddedAnim)
  257. {
  258. anim.Init(anim.context.currPointIndex, dest, (int)dest - 1);
  259. }
  260. else
  261. {
  262. m_Addition.context.currPointIndex = (int)dest - 1;
  263. anim.Init(curr, dest, (int)dest - 1);
  264. }
  265. }
  266. else
  267. {
  268. anim.Init(curr, dest, 0);
  269. }
  270. }
  271. /// <summary>
  272. /// Initialize animation configuration.
  273. /// ||初始化动画配置。
  274. /// </summary>
  275. /// <param name="paths">路径坐标点列表</param>
  276. /// <param name="isY">是Y轴还是X轴</param>
  277. public void InitProgress(List<Vector3> paths, bool isY)
  278. {
  279. if (paths.Count < 1) return;
  280. var anim = activedAnimation;
  281. if (anim == null)
  282. {
  283. m_Addition.context.currPointIndex = paths.Count - 1;
  284. return;
  285. }
  286. var isAddedAnim = anim is AnimationAddition;
  287. var startIndex = 0;
  288. if (isAddedAnim)
  289. {
  290. startIndex = anim.context.currPointIndex == paths.Count - 1 ?
  291. paths.Count - 2 :
  292. anim.context.currPointIndex;
  293. if (startIndex < 0 || startIndex > paths.Count - 2) startIndex = 0;
  294. }
  295. else
  296. {
  297. m_Addition.context.currPointIndex = paths.Count - 1;
  298. }
  299. var sp = paths[startIndex];
  300. var ep = paths[paths.Count - 1];
  301. var currDetailProgress = isY ? sp.y : sp.x;
  302. var totalDetailProgress = isY ? ep.y : ep.x;
  303. if (context.type == AnimationType.AlongPath)
  304. {
  305. currDetailProgress = 0;
  306. totalDetailProgress = 0;
  307. var lp = sp;
  308. for (int i = 1; i < paths.Count; i++)
  309. {
  310. var np = paths[i];
  311. totalDetailProgress += Vector3.Distance(np, lp);
  312. lp = np;
  313. if (startIndex > 0 && i == startIndex)
  314. currDetailProgress = totalDetailProgress;
  315. }
  316. m_LinePathLastPos = sp;
  317. context.currentPathDistance = 0;
  318. }
  319. if (sp == anim.context.currPoint && ep == anim.context.destPoint)
  320. {
  321. return;
  322. }
  323. anim.context.currPoint = sp;
  324. anim.context.destPoint = ep;
  325. anim.Init(currDetailProgress, totalDetailProgress, paths.Count - 1);
  326. }
  327. public bool IsEnd()
  328. {
  329. foreach (var animation in animations)
  330. {
  331. if (animation.context.start)
  332. return animation.context.end;
  333. }
  334. return m_FadeIn.context.end;
  335. }
  336. public bool IsFinish()
  337. {
  338. #if UNITY_EDITOR
  339. if (!Application.isPlaying)
  340. return true;
  341. #endif
  342. if (!m_Enable)
  343. return true;
  344. var animation = activedAnimation;
  345. if (animation != null && animation.context.end)
  346. return true;
  347. if (IsSerieAnimation())
  348. {
  349. if (m_FadeOut.context.start) return m_FadeOut.context.currProgress <= m_FadeOut.context.destProgress;
  350. else if (m_Addition.context.start) return m_Addition.context.currProgress >= m_Addition.context.destProgress;
  351. else return m_FadeIn.context.currProgress >= m_FadeIn.context.destProgress;
  352. }
  353. else if (IsDataAnimation())
  354. {
  355. if (animation == null) return true;
  356. else return animation.context.end;
  357. }
  358. return true;
  359. }
  360. public bool IsInDelay()
  361. {
  362. var anim = activedAnimation;
  363. if (anim != null)
  364. return anim.IsInDelay();
  365. return false;
  366. }
  367. /// <summary>
  368. /// whther animaiton is data animation. BottomToTop and InsideOut are data animation.
  369. /// ||是否为数据动画。BottomToTop和InsideOut类型的为数据动画。
  370. /// </summary>
  371. public bool IsDataAnimation()
  372. {
  373. return context.type == AnimationType.BottomToTop || context.type == AnimationType.InsideOut;
  374. }
  375. /// <summary>
  376. /// whther animaiton is serie animation. LeftToRight, AlongPath and Clockwise are serie animation.
  377. /// ||是否为系列动画。LeftToRight、AlongPath和Clockwise类型的为系列动画。
  378. /// </summary>
  379. public bool IsSerieAnimation()
  380. {
  381. return context.type == AnimationType.LeftToRight ||
  382. context.type == AnimationType.AlongPath || context.type == AnimationType.Clockwise;
  383. }
  384. public bool CheckDetailBreak(float detail)
  385. {
  386. if (!IsSerieAnimation())
  387. return false;
  388. foreach (var animation in animations)
  389. {
  390. if (animation.context.start)
  391. return !IsFinish() && detail > animation.context.currProgress;
  392. }
  393. return false;
  394. }
  395. public bool CheckDetailBreak(Vector3 pos, bool isYAxis)
  396. {
  397. if (!IsSerieAnimation())
  398. return false;
  399. if (IsFinish())
  400. return false;
  401. if (context.type == AnimationType.AlongPath)
  402. {
  403. context.currentPathDistance += Vector3.Distance(pos, m_LinePathLastPos);
  404. m_LinePathLastPos = pos;
  405. return CheckDetailBreak(context.currentPathDistance);
  406. }
  407. else
  408. {
  409. if (isYAxis)
  410. return pos.y > GetCurrDetail();
  411. else
  412. return pos.x > GetCurrDetail();
  413. }
  414. }
  415. public void CheckProgress()
  416. {
  417. if (IsDataAnimation() && context.isAllItemAnimationEnd)
  418. {
  419. foreach (var animation in animations)
  420. {
  421. animation.End();
  422. }
  423. return;
  424. }
  425. foreach (var animation in animations)
  426. {
  427. animation.CheckProgress(animation.context.totalProgress, m_UnscaledTime);
  428. }
  429. }
  430. public void CheckProgress(double total)
  431. {
  432. if (IsFinish())
  433. return;
  434. foreach (var animation in animations)
  435. {
  436. animation.CheckProgress(total, m_UnscaledTime);
  437. }
  438. }
  439. internal float CheckItemProgress(int dataIndex, float destProgress, ref bool isEnd, float startProgress = 0)
  440. {
  441. isEnd = false;
  442. var anim = activedAnimation;
  443. if (anim == null)
  444. {
  445. isEnd = true;
  446. return destProgress;
  447. }
  448. return anim.CheckItemProgress(dataIndex, destProgress, ref isEnd, startProgress, m_UnscaledTime);
  449. }
  450. public void CheckSymbol(float dest)
  451. {
  452. m_FadeIn.CheckSymbol(dest, m_UnscaledTime);
  453. m_FadeOut.CheckSymbol(dest, m_UnscaledTime);
  454. }
  455. public float GetSysmbolSize(float dest)
  456. {
  457. #if UNITY_EDITOR
  458. if (!Application.isPlaying)
  459. return dest;
  460. #endif
  461. if (!enable)
  462. return dest;
  463. if (IsEnd())
  464. return m_FadeOut.context.start ? 0 : dest;
  465. return m_FadeOut.context.start ? m_FadeOut.context.sizeProgress : m_FadeIn.context.sizeProgress;
  466. }
  467. public float GetCurrDetail()
  468. {
  469. #if UNITY_EDITOR
  470. if (!Application.isPlaying)
  471. {
  472. foreach (var animation in animations)
  473. {
  474. if (animation.context.start)
  475. return animation.context.destProgress;
  476. }
  477. }
  478. #endif
  479. foreach (var animation in animations)
  480. {
  481. if (animation.context.start)
  482. return animation.context.currProgress;
  483. }
  484. return m_FadeIn.context.currProgress;
  485. }
  486. public float GetCurrRate()
  487. {
  488. #if UNITY_EDITOR
  489. if (!Application.isPlaying)
  490. return 1;
  491. #endif
  492. if (!enable || IsEnd())
  493. return 1;
  494. return m_FadeOut.context.start ? m_FadeOut.context.currProgress : m_FadeIn.context.currProgress;
  495. }
  496. public int GetCurrIndex()
  497. {
  498. #if UNITY_EDITOR
  499. if (!Application.isPlaying)
  500. return -1;
  501. #endif
  502. if (!enable)
  503. return -1;
  504. var anim = activedAnimation;
  505. if (anim == null)
  506. return -1;
  507. return (int)anim.context.currProgress;
  508. }
  509. public float GetChangeDuration()
  510. {
  511. if (m_Enable && m_Change.enable)
  512. return m_Change.duration;
  513. else
  514. return 0;
  515. }
  516. public float GetAdditionDuration()
  517. {
  518. if (m_Enable && m_Addition.enable)
  519. return m_Addition.duration;
  520. else
  521. return 0;
  522. }
  523. public float GetInteractionDuration()
  524. {
  525. if (m_Enable && m_Interaction.enable)
  526. return m_Interaction.duration;
  527. else
  528. return 0;
  529. }
  530. public float GetInteractionRadius(float radius)
  531. {
  532. if (m_Enable && m_Interaction.enable)
  533. return m_Interaction.GetRadius(radius);
  534. else
  535. return radius;
  536. }
  537. public bool HasFadeOut()
  538. {
  539. return enable && m_FadeOut.context.end;
  540. }
  541. public bool IsFadeIn()
  542. {
  543. return enable && m_FadeIn.context.start;
  544. }
  545. public bool IsFadeOut()
  546. {
  547. return enable && m_FadeOut.context.start;
  548. }
  549. public bool CanCheckInteract()
  550. {
  551. return enable && interaction.enable
  552. && !IsFadeIn() && !IsFadeOut();
  553. }
  554. }
  555. }