InteractData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using UnityEngine;
  2. namespace XCharts.Runtime
  3. {
  4. public class InteractData
  5. {
  6. private float m_PreviousValue = 0;
  7. private float m_CurrentValue = float.NaN;
  8. private float m_TargetValue = float.NaN;
  9. private Vector3 m_PreviousPosition = Vector3.one;
  10. private Vector3 m_TargetPosition = Vector3.one;
  11. private Color32 m_PreviousColor = ColorUtil.clearColor32;
  12. private Color32 m_TargetColor = ColorUtil.clearColor32;
  13. private Color32 m_PreviousToColor = ColorUtil.clearColor32;
  14. private Color32 m_TargetToColor = ColorUtil.clearColor32;
  15. private float m_UpdateTime = 0;
  16. private bool m_UpdateFlag = false;
  17. private bool m_ValueEnable = false;
  18. internal float targetVaue { get { return m_TargetValue; } }
  19. internal float previousValue { get { return m_PreviousValue; } }
  20. internal bool valueEnable { get { return m_ValueEnable; } }
  21. internal bool updateFlag { get { return m_UpdateFlag; } }
  22. public override string ToString()
  23. {
  24. return string.Format("m_PreviousValue:{0},m_TargetValue:{1},m_UpdateTime:{2},m_UpdateFlag:{3},m_ValueEnable:{4},m_PreviousPosition:{5},m_TargetPosition:{6}",
  25. m_PreviousValue, m_TargetValue, m_UpdateTime, m_UpdateFlag, m_ValueEnable, m_PreviousPosition, m_TargetPosition);
  26. }
  27. public void SetValue(ref bool needInteract, float value, bool highlight, float rate = 1.3f)
  28. {
  29. value = highlight && rate != 0 ? value * rate : value;
  30. SetValue(ref needInteract, value);
  31. }
  32. public void SetValue(ref bool needInteract, float value, bool previousValueZero = false)
  33. {
  34. if (m_TargetValue != value)
  35. {
  36. needInteract = true;
  37. if (!m_ValueEnable)
  38. m_PreviousValue = previousValueZero ? 0 : value;
  39. else
  40. m_PreviousValue = m_CurrentValue;
  41. UpdateStart();
  42. m_TargetValue = value;
  43. }
  44. else if (m_UpdateFlag)
  45. {
  46. needInteract = true;
  47. }
  48. }
  49. public void SetPosition(ref bool needInteract, Vector3 pos)
  50. {
  51. if (m_TargetPosition != pos)
  52. {
  53. needInteract = true;
  54. UpdateStart();
  55. m_PreviousPosition = m_TargetPosition == Vector3.one ? pos : m_TargetPosition;
  56. m_TargetPosition = pos;
  57. }
  58. }
  59. public void SetColor(ref bool needInteract, Color32 color)
  60. {
  61. if (!ChartHelper.IsValueEqualsColor(color, m_TargetColor))
  62. {
  63. needInteract = true;
  64. UpdateStart();
  65. m_PreviousColor = ChartHelper.IsClearColor(m_TargetColor) ? color : m_TargetColor;
  66. m_TargetColor = color;
  67. }
  68. else if (m_UpdateFlag)
  69. {
  70. needInteract = true;
  71. }
  72. }
  73. public void SetColor(ref bool needInteract, Color32 color, Color32 toColor)
  74. {
  75. SetColor(ref needInteract, color);
  76. if (!ChartHelper.IsValueEqualsColor(toColor, m_TargetToColor))
  77. {
  78. needInteract = true;
  79. UpdateStart();
  80. m_PreviousToColor = ChartHelper.IsClearColor(m_TargetToColor) ? color : m_TargetToColor;
  81. m_TargetToColor = toColor;
  82. }
  83. }
  84. public void SetValueAndColor(ref bool needInteract, float value, Color32 color)
  85. {
  86. SetValue(ref needInteract, value);
  87. SetColor(ref needInteract, color);
  88. }
  89. public void SetValueAndColor(ref bool needInteract, float value, Color32 color, Color32 toColor)
  90. {
  91. SetValue(ref needInteract, value);
  92. SetColor(ref needInteract, color, toColor);
  93. }
  94. public bool TryGetValue(ref float value, ref bool interacting, float animationDuration = 250)
  95. {
  96. if (!IsValueEnable() || animationDuration == 0)
  97. return false;
  98. if (float.IsNaN(m_TargetValue))
  99. return false;
  100. if (m_UpdateFlag && !float.IsNaN(m_PreviousValue))
  101. {
  102. var rate = GetRate(animationDuration);
  103. if (rate < 1)
  104. {
  105. interacting = true;
  106. value = Mathf.Lerp(m_PreviousValue, m_TargetValue, rate);
  107. m_CurrentValue = value;
  108. return true;
  109. }
  110. else
  111. {
  112. UpdateEnd();
  113. }
  114. }
  115. value = m_TargetValue;
  116. return true;
  117. }
  118. public bool TryGetPosition(ref Vector3 pos, ref bool interacting, float animationDuration = 250)
  119. {
  120. if (!IsValueEnable() || animationDuration == 0)
  121. return false;
  122. if (m_TargetPosition == Vector3.one)
  123. {
  124. return false;
  125. }
  126. if (m_UpdateFlag && m_PreviousPosition != Vector3.one)
  127. {
  128. var rate = GetRate(animationDuration);
  129. if (rate < 1)
  130. {
  131. interacting = true;
  132. pos = Vector3.Lerp(m_PreviousPosition, m_TargetPosition, rate);
  133. return true;
  134. }
  135. else
  136. {
  137. UpdateEnd();
  138. }
  139. }
  140. pos = m_TargetPosition;
  141. return true;
  142. }
  143. public bool TryGetColor(ref Color32 color, ref bool interacting, float animationDuration = 250)
  144. {
  145. if (!IsValueEnable() || animationDuration == 0)
  146. return false;
  147. if (m_UpdateFlag)
  148. {
  149. var rate = GetRate(animationDuration);
  150. if (rate < 1)
  151. {
  152. interacting = true;
  153. color = Color32.Lerp(m_PreviousColor, m_TargetColor, rate);
  154. return true;
  155. }
  156. else
  157. {
  158. UpdateEnd();
  159. }
  160. }
  161. color = m_TargetColor;
  162. return true;
  163. }
  164. public bool TryGetColor(ref Color32 color, ref Color32 toColor, ref bool interacting, float animationDuration = 250)
  165. {
  166. if (!IsValueEnable() || animationDuration == 0)
  167. return false;
  168. if (m_UpdateFlag)
  169. {
  170. var rate = GetRate(animationDuration);
  171. if (rate < 1)
  172. {
  173. interacting = true;
  174. color = Color32.Lerp(m_PreviousColor, m_TargetColor, rate);
  175. toColor = Color32.Lerp(m_PreviousToColor, m_TargetToColor, rate);
  176. return true;
  177. }
  178. else
  179. {
  180. UpdateEnd();
  181. }
  182. }
  183. color = m_TargetColor;
  184. toColor = m_TargetToColor;
  185. return true;
  186. }
  187. public bool TryGetValueAndColor(ref float value, ref Color32 color, ref Color32 toColor, ref bool interacting, float animationDuration = 250)
  188. {
  189. if (!IsValueEnable() || animationDuration == 0)
  190. return false;
  191. if (float.IsNaN(m_TargetValue))
  192. return false;
  193. if (m_UpdateFlag && !float.IsNaN(m_PreviousValue))
  194. {
  195. var rate = GetRate(animationDuration);
  196. if (rate < 1)
  197. {
  198. interacting = true;
  199. value = Mathf.Lerp(m_PreviousValue, m_TargetValue, rate);
  200. color = Color32.Lerp(m_PreviousColor, m_TargetColor, rate);
  201. toColor = Color32.Lerp(m_PreviousToColor, m_TargetToColor, rate);
  202. m_CurrentValue = value;
  203. return true;
  204. }
  205. else
  206. {
  207. UpdateEnd();
  208. }
  209. }
  210. value = m_TargetValue;
  211. color = m_TargetColor;
  212. toColor = m_TargetToColor;
  213. return true;
  214. }
  215. private float GetRate(float animationDuration)
  216. {
  217. var time = Time.time - m_UpdateTime;
  218. var total = animationDuration / 1000;
  219. var rate = time / total;
  220. if (rate > 1) rate = 1;
  221. return rate;
  222. }
  223. private void UpdateStart()
  224. {
  225. m_ValueEnable = true;
  226. m_UpdateFlag = true;
  227. m_UpdateTime = Time.time;
  228. }
  229. private void UpdateEnd()
  230. {
  231. if (!m_UpdateFlag) return;
  232. m_UpdateFlag = false;
  233. m_PreviousColor = m_TargetColor;
  234. m_PreviousToColor = m_TargetToColor;
  235. m_PreviousValue = m_TargetValue;
  236. m_CurrentValue = m_TargetValue;
  237. m_PreviousPosition = m_TargetPosition;
  238. }
  239. public bool TryGetValueAndColor(ref float value, ref Vector3 pos, ref Color32 color, ref Color32 toColor, ref bool interacting, float animationDuration = 250)
  240. {
  241. var flag = TryGetValueAndColor(ref value, ref color, ref toColor, ref interacting, animationDuration);
  242. flag |= TryGetPosition(ref pos, ref interacting, animationDuration);
  243. return flag;
  244. }
  245. public bool TryGetValueAndColor(ref float value, ref Vector3 pos, ref bool interacting, float animationDuration = 250)
  246. {
  247. var flag = TryGetValue(ref value, ref interacting, animationDuration);
  248. flag |= TryGetPosition(ref pos, ref interacting, animationDuration);
  249. return flag;
  250. }
  251. public void Reset()
  252. {
  253. m_UpdateFlag = false;
  254. m_ValueEnable = false;
  255. m_TargetValue = float.NaN;
  256. m_PreviousValue = float.NaN;
  257. m_CurrentValue = float.NaN;
  258. m_PreviousPosition = Vector3.one;
  259. m_TargetPosition = Vector3.one;
  260. m_TargetColor = ColorUtil.clearColor32;
  261. m_TargetToColor = ColorUtil.clearColor32;
  262. m_PreviousColor = ColorUtil.clearColor32;
  263. m_PreviousToColor = ColorUtil.clearColor32;
  264. }
  265. private bool IsValueEnable()
  266. {
  267. #if UNITY_EDITOR
  268. if (!Application.isPlaying)
  269. return false;
  270. #endif
  271. return m_ValueEnable;
  272. }
  273. }
  274. }