No Description
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.

InteractData.cs 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using UnityEngine;
  2. namespace XCharts.Runtime
  3. {
  4. public class InteractData
  5. {
  6. private float m_PreviousValue = 0;
  7. private float m_TargetValue = 0;
  8. private Color32 m_PreviousColor;
  9. private Color32 m_TargetColor;
  10. private Color32 m_PreviousToColor;
  11. private Color32 m_TargetToColor;
  12. private float m_UpdateTime = 0;
  13. private bool m_UpdateFlag = false;
  14. private bool m_ValueEnable = false;
  15. internal float targetVaue { get { return m_TargetValue; } }
  16. public void SetValue(ref bool needInteract, float size, bool highlight, float rate = 1.3f)
  17. {
  18. size = highlight ? size * rate : size;
  19. SetValue(ref needInteract, size);
  20. }
  21. public void SetValue(ref bool needInteract, float size)
  22. {
  23. if (m_TargetValue != size)
  24. {
  25. needInteract = true;
  26. m_UpdateFlag = true;
  27. m_ValueEnable = true;
  28. m_UpdateTime = Time.time;
  29. m_PreviousValue = m_TargetValue;
  30. m_TargetValue = size;
  31. }
  32. }
  33. public void SetColor(ref bool needInteract, Color32 color)
  34. {
  35. if (!ChartHelper.IsValueEqualsColor(color, m_TargetColor))
  36. {
  37. if (!ChartHelper.IsClearColor(m_TargetColor))
  38. {
  39. needInteract = true;
  40. m_UpdateFlag = true;
  41. m_ValueEnable = true;
  42. m_UpdateTime = Time.time;
  43. m_PreviousColor = m_TargetColor;
  44. }
  45. m_TargetColor = color;
  46. }
  47. }
  48. public void SetColor(ref bool needInteract, Color32 color, Color32 toColor)
  49. {
  50. SetColor(ref needInteract, color);
  51. if (!ChartHelper.IsValueEqualsColor(toColor, m_TargetToColor))
  52. {
  53. if (!ChartHelper.IsClearColor(m_TargetToColor))
  54. {
  55. needInteract = true;
  56. m_UpdateFlag = true;
  57. m_ValueEnable = true;
  58. m_UpdateTime = Time.time;
  59. m_PreviousToColor = m_TargetToColor;
  60. }
  61. m_TargetToColor = toColor;
  62. }
  63. }
  64. public void SetValueAndColor(ref bool needInteract, float value, Color32 color)
  65. {
  66. SetValue(ref needInteract, value);
  67. SetColor(ref needInteract, color);
  68. }
  69. public void SetValueAndColor(ref bool needInteract, float value, Color32 color, Color32 toColor)
  70. {
  71. SetValue(ref needInteract, value);
  72. SetColor(ref needInteract, color, toColor);
  73. }
  74. public bool TryGetValue(ref float value, ref bool interacting, float animationDuration = 250)
  75. {
  76. if (!IsValueEnable() || m_PreviousValue == 0)
  77. return false;
  78. if (m_UpdateFlag)
  79. {
  80. var time = Time.time - m_UpdateTime;
  81. var total = animationDuration / 1000;
  82. var rate = time / total;
  83. if (rate > 1) rate = 1;
  84. if (rate < 1)
  85. {
  86. interacting = true;
  87. value = Mathf.Lerp(m_PreviousValue, m_TargetValue, rate);
  88. return true;
  89. }
  90. else
  91. {
  92. m_UpdateFlag = false;
  93. }
  94. }
  95. value = m_TargetValue;
  96. return true;
  97. }
  98. public bool TryGetColor(ref Color32 color, ref bool interacting, float animationDuration = 250)
  99. {
  100. if (!IsValueEnable())
  101. return false;
  102. if (m_UpdateFlag)
  103. {
  104. var time = Time.time - m_UpdateTime;
  105. var total = animationDuration / 1000;
  106. var rate = time / total;
  107. if (rate > 1) rate = 1;
  108. if (rate < 1)
  109. {
  110. interacting = true;
  111. color = Color32.Lerp(m_PreviousColor, m_TargetColor, rate);
  112. return true;
  113. }
  114. else
  115. {
  116. m_UpdateFlag = false;
  117. }
  118. }
  119. color = m_TargetColor;
  120. return true;
  121. }
  122. public bool TryGetColor(ref Color32 color, ref Color32 toColor, ref bool interacting, float animationDuration = 250)
  123. {
  124. if (!IsValueEnable())
  125. return false;
  126. if (m_UpdateFlag)
  127. {
  128. var time = Time.time - m_UpdateTime;
  129. var total = animationDuration / 1000;
  130. var rate = time / total;
  131. if (rate > 1) rate = 1;
  132. if (rate < 1)
  133. {
  134. interacting = true;
  135. color = Color32.Lerp(m_PreviousColor, m_TargetColor, rate);
  136. toColor = Color32.Lerp(m_PreviousToColor, m_TargetToColor, rate);
  137. return true;
  138. }
  139. else
  140. {
  141. m_UpdateFlag = false;
  142. }
  143. }
  144. color = m_TargetColor;
  145. toColor = m_TargetToColor;
  146. return true;
  147. }
  148. public bool TryGetValueAndColor(ref float value, ref Color32 color, ref Color32 toColor, ref bool interacting, float animationDuration = 250)
  149. {
  150. if (!IsValueEnable())
  151. return false;
  152. if (m_UpdateFlag)
  153. {
  154. var time = Time.time - m_UpdateTime;
  155. var total = animationDuration / 1000;
  156. var rate = time / total;
  157. if (rate > 1) rate = 1;
  158. if (rate < 1)
  159. {
  160. interacting = true;
  161. value = Mathf.Lerp(m_PreviousValue, m_TargetValue, rate);
  162. color = Color32.Lerp(m_PreviousColor, m_TargetColor, rate);
  163. toColor = Color32.Lerp(m_PreviousToColor, m_TargetToColor, rate);
  164. return true;
  165. }
  166. else
  167. {
  168. m_UpdateFlag = false;
  169. }
  170. }
  171. value = m_TargetValue;
  172. color = m_TargetColor;
  173. toColor = m_TargetToColor;
  174. return true;
  175. }
  176. public void Reset()
  177. {
  178. m_UpdateFlag = false;
  179. m_ValueEnable = false;
  180. m_PreviousValue = float.NaN;
  181. m_TargetColor = ColorUtil.clearColor32;
  182. m_TargetToColor = ColorUtil.clearColor32;
  183. m_PreviousColor = ColorUtil.clearColor32;
  184. m_PreviousToColor = ColorUtil.clearColor32;
  185. }
  186. private bool IsValueEnable()
  187. {
  188. #if UNITY_EDITOR
  189. if (!Application.isPlaying)
  190. return false;
  191. #endif
  192. return m_ValueEnable;
  193. }
  194. }
  195. }