diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ebb7c08..375b60f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 更新日志 +* (2020.06.07) 增加`Animation`的`customFadeInDelay`等自定义数据项延时和时长回调函数 * (2020.06.07) 优化`PieChart`在数据全为`0`时的显示为等份的效果 * (2020.06.04) 增加`SerieLabel`的`autoOffset`参数设置是否自动判断上下偏移 * (2020.06.04) 增加`Tooltip`的`alwayShow`参数设置触发后一直显示 diff --git a/Documentation/XCharts配置项手册.md b/Documentation/XCharts配置项手册.md index 6976f476..8187c7f4 100644 --- a/Documentation/XCharts配置项手册.md +++ b/Documentation/XCharts配置项手册.md @@ -686,9 +686,10 @@ * `enable`:是否开启动画系统。 * ~~`threshold`:是否开启动画的阈值,当单个系列显示的图形数量大于这个阈值时会关闭动画。~~ -* `fadeInDelay`:设定的渐入动画延时,单位毫秒。 -* `fadeInDuration`:设定的渐入动画时长,单位毫秒。 -* `fadeOutDuration`:设定的渐出动画时长,单位毫秒。 +* `fadeInDelay`:设定的渐入动画延时,单位毫秒。如果要设置单个数据项的延时,可以用代码定制:`customFadeInDelay`。 +* `fadeInDuration`:设定的渐入动画时长,单位毫秒。如果要设置单个数据项的渐入时长,可以用代码定制:`customFadeInDuration`。 +* `fadeOutDelay`:设定的渐出动画延时,单位毫秒。如果要设置单个数据项的延时,可以用代码定制:`customFadeOutDelay`。 +* `fadeOutDuration`:设定的渐出动画时长,单位毫秒。如果要设置单个数据项的渐出时长,可以用代码定制:`customFadeOutDuration`。 * `dataChangeEnable`:是否开启数据变更动画。 * `dataChangeDuration`:数据变更动画时长,单位毫秒。 diff --git a/Editor/PropertyDrawers/AnimationDrawer.cs b/Editor/PropertyDrawers/AnimationDrawer.cs index d7f66ef3..c33117c7 100644 --- a/Editor/PropertyDrawers/AnimationDrawer.cs +++ b/Editor/PropertyDrawers/AnimationDrawer.cs @@ -30,6 +30,7 @@ namespace XCharts SerializedProperty m_FadeInDelay = prop.FindPropertyRelative("m_FadeInDelay"); SerializedProperty m_Threshold = prop.FindPropertyRelative("m_Threshold"); SerializedProperty m_ActualDuration = prop.FindPropertyRelative("m_ActualDuration"); + SerializedProperty m_FadeOutDelay = prop.FindPropertyRelative("m_FadeOutDelay"); // SerializedProperty m_CurrDetailProgress = prop.FindPropertyRelative("m_CurrDetailProgress"); // SerializedProperty m_DestDetailProgress = prop.FindPropertyRelative("m_DestDetailProgress"); @@ -40,15 +41,18 @@ namespace XCharts ++EditorGUI.indentLevel; //EditorGUI.PropertyField(drawRect, m_Easting); //drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; - EditorGUI.PropertyField(drawRect, m_Threshold); - if (m_Threshold.intValue < 0) m_Threshold.intValue = 0; - drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + // EditorGUI.PropertyField(drawRect, m_Threshold); + // if (m_Threshold.intValue < 0) m_Threshold.intValue = 0; + // drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; EditorGUI.PropertyField(drawRect, m_FadeInDelay); if (m_FadeInDelay.floatValue < 0) m_FadeInDelay.floatValue = 0; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; EditorGUI.PropertyField(drawRect, m_FadeInDuration); if (m_FadeInDuration.floatValue < 0) m_FadeInDuration.floatValue = 0; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + EditorGUI.PropertyField(drawRect, m_FadeOutDelay); + if (m_FadeOutDelay.floatValue < 0) m_FadeOutDelay.floatValue = 0; + drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; EditorGUI.PropertyField(drawRect, m_FadeOutDuration); if (m_FadeOutDuration.floatValue < 0) m_FadeOutDuration.floatValue = 0; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; diff --git a/Examples/Runtime/Example03_ChartAnimation.cs b/Examples/Runtime/Example03_ChartAnimation.cs new file mode 100644 index 00000000..0c261be3 --- /dev/null +++ b/Examples/Runtime/Example03_ChartAnimation.cs @@ -0,0 +1,43 @@ +/******************************************/ +/* */ +/* Copyright (c) 2018 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ + +using UnityEngine; + +namespace XCharts.Examples +{ + [DisallowMultipleComponent] + [ExecuteInEditMode] + public class Example03_ChartAnimation : MonoBehaviour + { + BaseChart chart; + + void Awake() + { + chart = gameObject.GetComponent(); + if (chart == null) + { + chart = gameObject.AddComponent(); + } + var serie = chart.series.GetSerie(0); + serie.animation.enable = true; + //自定义每个数据项的渐入延时 + serie.animation.customFadeInDelay = CustomFadeInDelay; + //自定义每个数据项的渐入时长 + serie.animation.customFadeInDuration = CustomFadeInDuration; + } + + float CustomFadeInDelay(int dataIndex) + { + return dataIndex * 1000; + } + + float CustomFadeInDuration(int dataIndex) + { + return dataIndex * 1000 + 1000; + } + } +} \ No newline at end of file diff --git a/Examples/Runtime/Example03_ChartAnimation.cs.meta b/Examples/Runtime/Example03_ChartAnimation.cs.meta new file mode 100644 index 00000000..93e6d6bd --- /dev/null +++ b/Examples/Runtime/Example03_ChartAnimation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6258ca3b055714eac92804f501011b53 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Component/Sub/SerieAnimation.cs b/Runtime/Component/Sub/SerieAnimation.cs index ca8ac60f..150d6be9 100644 --- a/Runtime/Component/Sub/SerieAnimation.cs +++ b/Runtime/Component/Sub/SerieAnimation.cs @@ -1,5 +1,4 @@ -using System.Threading; -/******************************************/ +/******************************************/ /* */ /* Copyright (c) 2018 monitor1394 */ /* https://github.com/monitor1394 */ @@ -8,9 +7,13 @@ using System.Collections.Generic; using UnityEngine; +using System; namespace XCharts { + public delegate float CustomAnimationDelay(int dataIndex); + public delegate float CustomAnimationDuration(int dataIndex); + /// /// the animation of serie. /// 动画表现。 @@ -28,9 +31,26 @@ namespace XCharts [SerializeField] private float m_FadeInDuration = 1000; [SerializeField] private float m_FadeInDelay = 0; [SerializeField] private float m_FadeOutDuration = 1000f; + [SerializeField] private float m_FadeOutDelay = 0; [SerializeField] private bool m_DataChangeEnable = true; [SerializeField] private float m_DataChangeDuration = 500; [SerializeField] private float m_ActualDuration; + /// + /// 自定义渐入动画延时函数。返回ms值。 + /// + public CustomAnimationDelay customFadeInDelay; + /// + /// 自定义渐入动画时长函数。返回ms值。 + /// + public CustomAnimationDuration customFadeInDuration; + /// + /// 自定义渐出动画延时函数。返回ms值。 + /// + public CustomAnimationDelay customFadeOutDelay; + /// + /// 自定义渐出动画时长函数。返回ms值。 + /// + public CustomAnimationDuration customFadeOutDuration; /// /// Whether to enable animation. @@ -44,12 +64,12 @@ namespace XCharts //public Easing easing { get { return m_Easting; } set { m_Easting = value; } } /// /// The milliseconds duration of the fadeIn animation. - /// 设定的渐入动画时长(毫秒)。 + /// 设定的渐入动画时长(毫秒)。如果要设置单个数据项的渐入时长,可以用代码定制:customFadeInDuration。 /// public float fadeInDuration { get { return m_FadeInDuration; } set { m_FadeInDuration = value < 0 ? 0 : value; } } /// /// The milliseconds duration of the fadeOut animation. - /// 设定的渐出动画时长(毫秒)。 + /// 设定的渐出动画时长(毫秒)。如果要设置单个数据项的渐出时长,可以用代码定制:customFadeOutDuration。 /// public float fadeOutDuration { get { return m_FadeOutDuration; } set { m_FadeOutDuration = value < 0 ? 0 : value; } } /// @@ -64,9 +84,13 @@ namespace XCharts public int threshold { get { return m_Threshold; } set { m_Threshold = value; } } /// /// The milliseconds delay before updating the first animation. - /// 动画延时(毫秒)。 + /// 渐入动画延时(毫秒)。如果要设置单个数据项的延时,可以用代码定制:customFadeInDelay。 /// - public float delay { get { return m_FadeInDelay; } set { m_FadeInDelay = value < 0 ? 0 : value; } } + public float fadeInDelay { get { return m_FadeInDelay; } set { m_FadeInDelay = value < 0 ? 0 : value; } } + /// + /// 渐出动画延时(毫秒)。如果要设置单个数据项的延时,可以用代码定制:customFadeOutDelay。 + /// + public float fadeOutDelay { get { return m_FadeOutDelay; } set { m_FadeInDelay = value < 0 ? 0 : value; } } /// /// 是否开启数据变更动画。 /// @@ -77,7 +101,8 @@ namespace XCharts /// public float dataChangeDuration { get { return m_DataChangeDuration; } set { m_DataChangeDuration = value < 0 ? 0 : value; } } - private Dictionary m_DataAnimationState = new Dictionary(); + private Dictionary m_DataCurrProgress = new Dictionary(); + private Dictionary m_DataDestProgress = new Dictionary(); private bool m_FadeIn = false; private bool m_IsEnd = true; private bool m_IsPause = false; @@ -112,7 +137,8 @@ namespace XCharts m_CurrDetailProgress = 0; m_DestDetailProgress = 1; m_CurrSymbolProgress = 0; - m_DataAnimationState.Clear(); + m_DataCurrProgress.Clear(); + m_DataDestProgress.Clear(); } public void Restart() @@ -139,7 +165,8 @@ namespace XCharts m_CurrDetailProgress = 0; m_DestDetailProgress = 1; m_CurrSymbolProgress = 0; - m_DataAnimationState.Clear(); + m_DataCurrProgress.Clear(); + m_DataDestProgress.Clear(); } public void Pause() @@ -161,7 +188,7 @@ namespace XCharts private void End() { if (m_IsEnd) return; - m_ActualDuration = (int)((Time.time - startTime) * 1000) - delay; + m_ActualDuration = (int)((Time.time - startTime) * 1000) - (m_FadeOut ? fadeOutDelay : fadeInDelay); m_CurrDataProgress = m_DestDataProgress + (m_FadeOut ? -1 : 1); m_FadeIn = false; m_IsEnd = true; @@ -181,7 +208,7 @@ namespace XCharts m_IsPause = false; m_FadeOut = false; m_FadeOuted = false; - m_DataAnimationState.Clear(); + m_DataCurrProgress.Clear(); } public void InitProgress(int data, float curr, float dest) @@ -209,32 +236,73 @@ namespace XCharts m_CurrDataProgress = dataIndex + (m_FadeOut ? -1 : 1); } - private void SetDataState(int index, float state) + private void SetDataCurrProgress(int index, float state) { - m_DataAnimationState[index] = state; + m_DataCurrProgress[index] = state; } - private float GetDataState(int index, float dest) + private float GetDataCurrProgress(int index, float initValue, float destValue) { - if (IsInDelay()) return dest; - if (!m_DataAnimationState.ContainsKey(index)) + if (IsInDelay()) return initValue; + if (!m_DataCurrProgress.ContainsKey(index)) { - m_DataAnimationState.Add(index, dest); + m_DataCurrProgress.Add(index, initValue); + m_DataDestProgress.Add(index, destValue); } - return m_DataAnimationState[index]; + return m_DataCurrProgress[index]; } - public bool IsFinish() + public bool IsFinish(int dataCount = -1) { #if UNITY_EDITOR if (!Application.isPlaying) return true; #endif + if (dataCount > 0 && (!IsAllOutDelay(dataCount) || !IsAllDataFinishProgress(dataCount))) return false; return !m_Enable || m_IsEnd || (m_CurrDataProgress > m_DestDataProgress && m_CurrDetailProgress > m_DestDetailProgress); } public bool IsInDelay() { - return (delay > 0 && Time.time - startTime < delay / 1000); + if (m_FadeOut) return (fadeOutDelay > 0 && Time.time - startTime < fadeOutDelay / 1000); + return (fadeInDelay > 0 && Time.time - startTime < fadeInDelay / 1000); + } + + public float GetDataDelay(int dataIndex) + { + if (m_FadeOut && customFadeOutDelay != null) return customFadeOutDelay(dataIndex); + if (m_FadeIn && customFadeInDelay != null) return customFadeInDelay(dataIndex); + return 0; + } + + public bool IsInDataDelay(int dataIndex) + { + return Time.time - startTime < GetDataDelay(dataIndex) / 1000f; + } + + public bool IsAllOutDelay(int dataCount) + { + var nowTime = Time.time - startTime; + for (int i = 0; i < dataCount; i++) + { + if (nowTime < GetDataDelay(i) / 1000) return false; + } + return true; + } + + public bool IsAllDataFinishProgress(int dataCount) + { + for (int i = 0; i < dataCount; i++) + { + if (m_DataDestProgress.ContainsKey(i) && m_DataCurrProgress.ContainsKey(i)) + { + if (m_DataCurrProgress[i] != m_DataDestProgress[i]) return false; + } + else + { + return false; + } + } + return true; } public bool CheckDetailBreak(float detail) @@ -262,7 +330,7 @@ namespace XCharts if (IsFinish()) return; if (!m_IsInit || m_IsPause || m_IsEnd) return; if (IsInDelay()) return; - m_ActualDuration = (int)((Time.time - startTime) * 1000) - delay; + m_ActualDuration = (int)((Time.time - startTime) * 1000) - fadeInDelay; var duration = GetCurrAnimationDuration(); var delta = total / duration * Time.deltaTime; if (m_FadeOut) @@ -285,43 +353,54 @@ namespace XCharts } } - internal float GetCurrAnimationDuration() + internal float GetCurrAnimationDuration(int dataIndex = -1) { - if (m_FadeOut) return m_FadeOutDuration > 0 ? m_FadeOutDuration / 1000 : 1; - else return m_FadeInDuration > 0 ? m_FadeInDuration / 1000 : 1; + if (dataIndex >= 0) + { + if (m_FadeOut && customFadeOutDuration != null) return customFadeOutDuration(dataIndex) / 1000f; + if (m_FadeIn && customFadeInDuration != null) return customFadeInDuration(dataIndex) / 1000f; + } + if (m_FadeOut) return m_FadeOutDuration > 0 ? m_FadeOutDuration / 1000 : 1f; + else return m_FadeInDuration > 0 ? m_FadeInDuration / 1000 : 1f; } - internal float CheckBarProgress(int dataIndex, float barHig) + internal float CheckBarProgress(int dataIndex, float barHig, int dataCount) { - //if (!m_IsInit) return barHig; - var destHig = m_FadeOut ? barHig : 0; - if (IsInDelay() || IsFinish() || m_IsEnd) + var initHig = m_FadeOut ? barHig : 0; + var destHig = m_FadeOut ? 0 : barHig; + if (IsFinish() || m_IsEnd) { return m_FadeOuted ? 0 : barHig; } + else if (IsInDelay() || IsInDataDelay(dataIndex)) + { + return m_FadeOut ? barHig : 0; + } else if (m_IsPause) { - return GetDataState(dataIndex, destHig); + return GetDataCurrProgress(dataIndex, initHig, destHig); } else { - var duration = GetCurrAnimationDuration(); + var duration = GetCurrAnimationDuration(dataIndex); var delta = barHig / duration * Time.deltaTime; - var currHig = GetDataState(dataIndex, destHig) + (m_FadeOut ? -delta : delta); - SetDataState(dataIndex, currHig); + var currHig = GetDataCurrProgress(dataIndex, initHig, destHig) + (m_FadeOut ? -delta : delta); if (m_FadeOut) { - if ((destHig > 0 && currHig <= 0) || (destHig < 0 && currHig >= 0)) + if ((initHig > 0 && currHig <= 0) || (initHig < 0 && currHig >= 0)) { - End(); currHig = 0; } } else if (Mathf.Abs(currHig) >= Mathf.Abs(barHig)) { - End(); currHig = barHig; } + SetDataCurrProgress(dataIndex, currHig); + if (IsAllOutDelay(dataCount) && IsAllDataFinishProgress(dataCount)) + { + End(); + } return currHig; } } diff --git a/Runtime/Internal/CoordinateChart_DrawBar.cs b/Runtime/Internal/CoordinateChart_DrawBar.cs index d3f6844a..083adcd6 100644 --- a/Runtime/Internal/CoordinateChart_DrawBar.cs +++ b/Runtime/Internal/CoordinateChart_DrawBar.cs @@ -147,7 +147,7 @@ namespace XCharts private float CheckAnimation(Serie serie, int dataIndex, float barHig) { - float currHig = serie.animation.CheckBarProgress(dataIndex, barHig); + float currHig = serie.animation.CheckBarProgress(dataIndex, barHig,serie.dataCount); if (!serie.animation.IsFinish()) { RefreshChart();