AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
SerieAnimation.cs
1 /************************************************/
2 /* */
3 /* Copyright (c) 2018 - 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /************************************************/
7 
8 using System.Collections.Generic;
9 using UnityEngine;
10 using System;
11 
12 namespace XCharts
13 {
14  public delegate float CustomAnimationDelay(int dataIndex);
15  public delegate float CustomAnimationDuration(int dataIndex);
16 
21  [System.Serializable]
23  {
24  public enum Easing
25  {
26  Linear,
27  }
28  [SerializeField] private bool m_Enable = true;
29  [SerializeField] private Easing m_Easting;
30  [SerializeField] private int m_Threshold = 2000;
31  [SerializeField] private float m_FadeInDuration = 1000;
32  [SerializeField] private float m_FadeInDelay = 0;
33  [SerializeField] private float m_FadeOutDuration = 1000f;
34  [SerializeField] private float m_FadeOutDelay = 0;
35  [SerializeField] private bool m_DataChangeEnable = true;
36  [SerializeField] private float m_DataChangeDuration = 500;
37  [SerializeField] private float m_ActualDuration;
38  [SerializeField] private bool m_AlongWithLinePath;
42  public CustomAnimationDelay customFadeInDelay;
46  public CustomAnimationDuration customFadeInDuration;
50  public CustomAnimationDelay customFadeOutDelay;
54  public CustomAnimationDuration customFadeOutDuration;
55 
60  public bool enable { get { return m_Enable; } set { m_Enable = value; } }
65  //public Easing easing { get { return m_Easting; } set { m_Easting = value; } }
70  public float fadeInDuration { get { return m_FadeInDuration; } set { m_FadeInDuration = value < 0 ? 0 : value; } }
75  public float fadeOutDuration { get { return m_FadeOutDuration; } set { m_FadeOutDuration = value < 0 ? 0 : value; } }
80  public float actualDuration { get { return m_ActualDuration; } }
85  public int threshold { get { return m_Threshold; } set { m_Threshold = value; } }
90  public float fadeInDelay { get { return m_FadeInDelay; } set { m_FadeInDelay = value < 0 ? 0 : value; } }
94  public float fadeOutDelay { get { return m_FadeOutDelay; } set { m_FadeInDelay = value < 0 ? 0 : value; } }
98  public bool dataChangeEnable { get { return m_DataChangeEnable; } set { m_DataChangeEnable = value; } }
103  public float dataChangeDuration { get { return m_DataChangeDuration; } set { m_DataChangeDuration = value < 0 ? 0 : value; } }
107  public bool alongWithLinePath { get { return m_AlongWithLinePath; } set { m_AlongWithLinePath = value; } }
111  public Action fadeInFinishCallback { get; set; }
115  public Action fadeOutFinishCallback { get; set; }
116  private Dictionary<int, float> m_DataCurrProgress = new Dictionary<int, float>();
117  private Dictionary<int, float> m_DataDestProgress = new Dictionary<int, float>();
118  private bool m_FadeIn = false;
119  private bool m_IsEnd = true;
120  private bool m_IsPause = false;
121  private bool m_FadeOut = false;
122  private bool m_FadeOuted = false;
123  private bool m_IsInit = false;
124 
125  private float startTime { get; set; }
126  private int m_CurrDataProgress { get; set; }
127  private int m_DestDataProgress { get; set; }
128  [SerializeField] private float m_CurrDetailProgress;
129  [SerializeField] private float m_DestDetailProgress;
130  private float m_CurrSymbolProgress;
131  private Vector3 m_LinePathLastPos;
132  private float m_LinePathCurrTotalDist = 0f;
133 
134  public void FadeIn()
135  {
136  if (m_FadeOut) return;
137  if (m_IsPause)
138  {
139  m_IsPause = false;
140  return;
141  }
142  if (m_FadeIn) return;
143  startTime = Time.time;
144  m_FadeIn = true;
145  m_IsEnd = false;
146  m_IsInit = false;
147  m_IsPause = false;
148  m_FadeOuted = false;
149  m_CurrDataProgress = 1;
150  m_DestDataProgress = 1;
151  m_CurrDetailProgress = 0;
152  m_DestDetailProgress = 1;
153  m_CurrSymbolProgress = 0;
154  m_DataCurrProgress.Clear();
155  m_DataDestProgress.Clear();
156  }
157 
158  public void Restart()
159  {
160  Reset();
161  FadeIn();
162  }
163 
164  public void FadeOut()
165  {
166  if (m_IsPause)
167  {
168  m_IsPause = false;
169  return;
170  }
171  m_FadeOut = true;
172  startTime = Time.time;
173  m_FadeIn = true;
174  m_IsEnd = false;
175  m_IsInit = false;
176  m_IsPause = false;
177  m_CurrDataProgress = 0;
178  m_DestDataProgress = 0;
179  m_CurrDetailProgress = 0;
180  m_DestDetailProgress = 1;
181  m_CurrSymbolProgress = 0;
182  m_DataCurrProgress.Clear();
183  m_DataDestProgress.Clear();
184  }
185 
186  public void Pause()
187  {
188  if (!m_IsPause)
189  {
190  m_IsPause = true;
191  }
192  }
193 
194  public void Resume()
195  {
196  if (m_IsPause)
197  {
198  m_IsPause = false;
199  }
200  }
201 
202  private void End()
203  {
204  if (m_IsEnd) return;
205  m_ActualDuration = (int)((Time.time - startTime) * 1000) - (m_FadeOut ? fadeOutDelay : fadeInDelay);
206  m_CurrDataProgress = m_DestDataProgress + (m_FadeOut ? -1 : 1);
207  m_IsEnd = true;
208  m_IsInit = false;
209  if (m_FadeIn)
210  {
211  m_FadeIn = false;
212  if (fadeInFinishCallback != null)
213  {
215  }
216  }
217  if (m_FadeOut)
218  {
219  m_FadeOut = false;
220  m_FadeOuted = true;
221  if (fadeOutFinishCallback != null)
222  {
224  }
225  }
226  }
227 
228  public void Reset()
229  {
230  m_FadeIn = false;
231  m_IsEnd = true;
232  m_IsInit = false;
233  m_IsPause = false;
234  m_FadeOut = false;
235  m_FadeOuted = false;
236  m_DataCurrProgress.Clear();
237  }
238 
239  public void InitProgress(int data, float curr, float dest)
240  {
241  if (m_IsInit || m_IsEnd) return;
242  if (curr > dest) return;
243  m_IsInit = true;
244  m_DestDataProgress = data;
245 
246  if (m_FadeOut)
247  {
248  m_CurrDetailProgress = dest;
249  m_DestDetailProgress = curr;
250  }
251  else
252  {
253  m_CurrDetailProgress = curr;
254  m_DestDetailProgress = dest;
255  }
256  }
257 
258  public void SetDataFinish(int dataIndex)
259  {
260  if (m_IsEnd) return;
261  m_CurrDataProgress = dataIndex + (m_FadeOut ? -1 : 1);
262  }
263 
264  private void SetDataCurrProgress(int index, float state)
265  {
266  m_DataCurrProgress[index] = state;
267  }
268 
269  private float GetDataCurrProgress(int index, float initValue, float destValue, out bool isBarEnd)
270  {
271  if (IsInDelay())
272  {
273  isBarEnd = false;
274  return initValue;
275  }
276  var c1 = !m_DataCurrProgress.ContainsKey(index);
277  var c2 = !m_DataDestProgress.ContainsKey(index);
278  if (c1 || c2)
279  {
280  if (c1) m_DataCurrProgress.Add(index, initValue);
281  if (c2) m_DataDestProgress.Add(index, destValue);
282  isBarEnd = false;
283  }
284  else
285  {
286  isBarEnd = m_DataCurrProgress[index] == m_DataDestProgress[index];
287  }
288  return m_DataCurrProgress[index];
289  }
290 
291  public bool IsFinish()
292  {
293 #if UNITY_EDITOR
294  if (!Application.isPlaying) return true;
295 #endif
296  return !m_Enable || m_IsEnd || (m_CurrDataProgress > m_DestDataProgress && m_CurrDetailProgress > m_DestDetailProgress);
297  }
298 
299  public bool IsInFadeOut()
300  {
301  return m_FadeOut;
302  }
303 
304  public bool IsInDelay()
305  {
306  if (m_FadeOut) return (fadeOutDelay > 0 && Time.time - startTime < fadeOutDelay / 1000);
307  return (fadeInDelay > 0 && Time.time - startTime < fadeInDelay / 1000);
308  }
309 
310  public float GetDataDelay(int dataIndex)
311  {
312  if (m_FadeOut && customFadeOutDelay != null) return customFadeOutDelay(dataIndex);
313  if (m_FadeIn && customFadeInDelay != null) return customFadeInDelay(dataIndex);
314  return 0;
315  }
316 
317  public bool IsInDataDelay(int dataIndex)
318  {
319  return Time.time - startTime < GetDataDelay(dataIndex) / 1000f;
320  }
321 
322  public bool IsAllOutDelay(int dataCount)
323  {
324  var nowTime = Time.time - startTime;
325  for (int i = 0; i < dataCount; i++)
326  {
327  if (nowTime < GetDataDelay(i) / 1000) return false;
328  }
329  return true;
330  }
331 
332  public bool IsAllDataFinishProgress(int dataCount)
333  {
334  for (int i = 0; i < dataCount; i++)
335  {
336  if (m_DataDestProgress.ContainsKey(i) && m_DataCurrProgress.ContainsKey(i))
337  {
338  if (m_DataCurrProgress[i] != m_DataDestProgress[i]) return false;
339  }
340  else
341  {
342  return false;
343  }
344  }
345  return true;
346  }
347 
348  public bool CheckDetailBreak(float detail)
349  {
350  return !IsFinish() && detail > m_CurrDetailProgress;
351  }
352 
353  public void SetLinePathStartPos(Vector3 pos)
354  {
355  if (m_AlongWithLinePath)
356  {
357  m_LinePathLastPos = pos;
358  m_LinePathCurrTotalDist = 0;
359  }
360  }
361 
362  public bool CheckDetailBreak(Vector3 pos, bool isYAxis)
363  {
364  if (IsFinish()) return false;
365  if (m_AlongWithLinePath)
366  {
367  m_LinePathCurrTotalDist += Vector3.Distance(pos, m_LinePathLastPos);
368  m_LinePathLastPos = pos;
369  return CheckDetailBreak(m_LinePathCurrTotalDist);
370  }
371  else
372  {
373  if (isYAxis) return pos.y > m_CurrDetailProgress;
374  else return pos.x > m_CurrDetailProgress;
375  }
376  }
377 
378  public bool NeedAnimation(int dataIndex)
379  {
380  if (!m_Enable || m_IsEnd) return true;
381  if (IsInDelay()) return false;
382  if (m_FadeOut) return dataIndex > 0;
383  else return dataIndex <= m_CurrDataProgress;
384  }
385 
386  internal void CheckProgress(double total)
387  {
388  if (IsFinish()) return;
389  if (!m_IsInit || m_IsPause || m_IsEnd) return;
390  if (IsInDelay()) return;
391  m_ActualDuration = (int)((Time.time - startTime) * 1000) - fadeInDelay;
392  var duration = GetCurrAnimationDuration();
393  var delta = (float)(total / duration * Time.deltaTime);
394  if (m_FadeOut)
395  {
396  m_CurrDetailProgress -= delta;
397  if (m_CurrDetailProgress <= m_DestDetailProgress)
398  {
399  m_CurrDetailProgress = m_DestDetailProgress;
400  End();
401  }
402  }
403  else
404  {
405  m_CurrDetailProgress += delta;
406  if (m_CurrDetailProgress >= m_DestDetailProgress)
407  {
408  m_CurrDetailProgress = m_DestDetailProgress;
409  End();
410  }
411  }
412  }
413 
414  internal float GetCurrAnimationDuration(int dataIndex = -1)
415  {
416  if (dataIndex >= 0)
417  {
418  if (m_FadeOut && customFadeOutDuration != null) return customFadeOutDuration(dataIndex) / 1000f;
419  if (m_FadeIn && customFadeInDuration != null) return customFadeInDuration(dataIndex) / 1000f;
420  }
421  if (m_FadeOut) return m_FadeOutDuration > 0 ? m_FadeOutDuration / 1000 : 1f;
422  else return m_FadeInDuration > 0 ? m_FadeInDuration / 1000 : 1f;
423  }
424 
425  internal float CheckBarProgress(int dataIndex, float barHig, int dataCount, out bool isBarEnd)
426  {
427  isBarEnd = false;
428  var initHig = m_FadeOut ? barHig : 0;
429  var destHig = m_FadeOut ? 0 : barHig;
430  var currHig = GetDataCurrProgress(dataIndex, initHig, destHig, out isBarEnd);
431  if (isBarEnd || IsFinish())
432  {
433  return m_FadeOuted ? 0 : barHig;
434  }
435  else if (IsInDelay() || IsInDataDelay(dataIndex))
436  {
437  return m_FadeOut ? barHig : 0;
438  }
439  else if (m_IsPause)
440  {
441  return currHig;
442  }
443  else
444  {
445  var duration = GetCurrAnimationDuration(dataIndex);
446  var delta = barHig / duration * Time.deltaTime;
447  currHig = currHig + (m_FadeOut ? -delta : delta);
448  if (m_FadeOut)
449  {
450  if ((initHig > 0 && currHig <= 0) || (initHig < 0 && currHig >= 0))
451  {
452  currHig = 0;
453  isBarEnd = true;
454  }
455  }
456  else if (Mathf.Abs(currHig) >= Mathf.Abs(barHig))
457  {
458  currHig = barHig;
459  isBarEnd = true;
460  }
461  SetDataCurrProgress(dataIndex, currHig);
462  return currHig;
463  }
464  }
465 
466  public void AllBarEnd()
467  {
468  End();
469  }
470 
471  internal void CheckSymbol(float dest)
472  {
473  if (!enable || m_IsEnd || m_IsPause || !m_IsInit) return;
474  if (IsInDelay()) return;
475  var duration = GetCurrAnimationDuration();
476  var delta = dest / duration * Time.deltaTime;
477  if (m_FadeOut)
478  {
479  m_CurrSymbolProgress -= delta;
480  if (m_CurrSymbolProgress < 0) m_CurrSymbolProgress = 0;
481  }
482  else
483  {
484  m_CurrSymbolProgress += delta;
485  if (m_CurrSymbolProgress > dest) m_CurrSymbolProgress = dest;
486  }
487  }
488 
489  public float GetSysmbolSize(float dest)
490  {
491 #if UNITY_EDITOR
492  if (!Application.isPlaying) return dest;
493 #endif
494  if (!enable) return dest;
495  if (m_IsEnd) return m_FadeOut ? 0 : dest;
496  return m_CurrSymbolProgress;
497  }
498 
499  public float GetCurrDetail()
500  {
501  return m_CurrDetailProgress;
502  }
503 
504  public float GetCurrRate()
505  {
506 #if UNITY_EDITOR
507  if (!Application.isPlaying) return 1;
508 #endif
509  if (!enable || m_IsEnd) return 1;
510  return m_CurrDetailProgress;
511  }
512 
513  public int GetCurrIndex()
514  {
515 #if UNITY_EDITOR
516  if (!Application.isPlaying) return -1;
517 #endif
518  if (!enable || m_IsEnd) return -1;
519  return (int)m_CurrDetailProgress;
520  }
521 
522  public float GetCurrData()
523  {
524  return m_CurrDataProgress;
525  }
526 
527  public float GetUpdateAnimationDuration()
528  {
529  if (m_Enable && m_DataChangeEnable && IsFinish()) return m_DataChangeDuration;
530  else return 0;
531  }
532 
533  public bool HasFadeOut()
534  {
535  return enable && m_FadeOuted && m_IsEnd;
536  }
537  }
538 }
XCharts.SerieAnimation.customFadeOutDelay
CustomAnimationDelay customFadeOutDelay
自定义渐出动画延时函数。返回ms值。
Definition: SerieAnimation.cs:50
XCharts.SerieAnimation.fadeOutDelay
float? fadeOutDelay
渐出动画延时(毫秒)。如果要设置单个数据项的延时,可以用代码定制:customFadeOutDelay。
Definition: SerieAnimation.cs:94
XCharts.SerieAnimation.customFadeOutDuration
CustomAnimationDuration customFadeOutDuration
自定义渐出动画时长函数。返回ms值。
Definition: SerieAnimation.cs:54
XCharts.SerieAnimation.dataChangeEnable
bool dataChangeEnable
是否开启数据变更动画。
Definition: SerieAnimation.cs:98
XCharts.SubComponent
Definition: ChartComponent.cs:71
XCharts.SerieAnimation.customFadeInDelay
CustomAnimationDelay customFadeInDelay
自定义渐入动画延时函数。返回ms值。
Definition: SerieAnimation.cs:42
XCharts.SerieAnimation.alongWithLinePath
bool alongWithLinePath
是否沿着线的轨迹进行匀速动画。
Definition: SerieAnimation.cs:107
XCharts.SerieAnimation.fadeInDelay
float? fadeInDelay
The milliseconds delay before updating the first animation. 渐入动画延时(毫秒)。如果要设置单个数据项的延时,可以用代码定制:customFa...
Definition: SerieAnimation.cs:90
Pause
Definition: Pause.cs:6
XCharts
Definition: RewardChart.cs:14
XCharts.SerieAnimation.fadeInDuration
float? fadeInDuration
Easing method used for the first animation. 动画的缓动效果。
Definition: SerieAnimation.cs:70
XCharts.SerieAnimation.threshold
int threshold
Whether to set graphic number threshold to animation. Animation will be disabled when graphic number ...
Definition: SerieAnimation.cs:85
XCharts.SerieAnimation.fadeOutDuration
float? fadeOutDuration
The milliseconds duration of the fadeOut animation. 设定的渐出动画时长(毫秒)。如果要设置单个数据项的渐出时长,可以用代码定制:customFadeO...
Definition: SerieAnimation.cs:75
XCharts.SerieAnimation.fadeInFinishCallback
Action fadeInFinishCallback
渐入动画完成回调
Definition: SerieAnimation.cs:111
XCharts.SerieAnimation.customFadeInDuration
CustomAnimationDuration customFadeInDuration
自定义渐入动画时长函数。返回ms值。
Definition: SerieAnimation.cs:46
XCharts.SerieAnimation.enable
bool enable
Whether to enable animation. 是否开启动画效果。
Definition: SerieAnimation.cs:60
XCharts.SerieAnimation.actualDuration
float actualDuration
The milliseconds actual duration of the first animation. 实际的动画时长(毫秒)。
Definition: SerieAnimation.cs:80
XCharts.SerieAnimation.dataChangeDuration
float? dataChangeDuration
The milliseconds duration of the data change animation. 数据变更的动画时长(毫秒)。
Definition: SerieAnimation.cs:103
XCharts.SerieAnimation
the animation of serie. 动画表现。
Definition: SerieAnimation.cs:22
XCharts.SerieAnimation.fadeOutFinishCallback
Action fadeOutFinishCallback
渐出动画完成回调
Definition: SerieAnimation.cs:115