StandardDatePickerCell.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace Bitsplash.DatePicker
  10. {
  11. [ExecuteInEditMode]
  12. public class StandardDatePickerCell : DatePickerCell
  13. {
  14. public DatePickerText TextItem;
  15. public Image Mark;
  16. public Image Background;
  17. public int TextSize = 14;
  18. public FontStyle FontStyle;
  19. public Color EnabledTextColor;
  20. public Color SelectedTextColor;
  21. public Color DisabledTextColor;
  22. public Sprite MarkSprite;
  23. [NonSerialized]
  24. public Color MarkSelectedColor = new Color(0f,0f,0f,0f);
  25. public Color SelectedBackgroundColor;
  26. // public Color HoverBackgroundColor;
  27. public Color EnabledBackgroundColor;
  28. public Color DisabledBackgroundColor;
  29. public float SlideSpeed = 0.9f;
  30. bool mSelected = false;
  31. bool mEnabled = true;
  32. DateTime mDayValue = new DateTime();
  33. private IEnumerator mCoroutine;
  34. Color TargetColor;
  35. public override Color MarkerColor
  36. {
  37. get
  38. {
  39. return MarkSelectedColor;
  40. }
  41. set
  42. {
  43. MarkSelectedColor = value;
  44. AssignTextAndBackground();
  45. }
  46. }
  47. public override bool CellSelected
  48. {
  49. get
  50. {
  51. return mSelected;
  52. }
  53. set
  54. {
  55. mSelected = value;
  56. AssignTextAndBackground();
  57. }
  58. }
  59. public override bool CellEnabled
  60. {
  61. get
  62. {
  63. return mEnabled;
  64. }
  65. set
  66. {
  67. mEnabled = value;
  68. AssignTextAndBackground();
  69. }
  70. }
  71. public override DateTime DayValue
  72. {
  73. get
  74. {
  75. return mDayValue;
  76. }
  77. set
  78. {
  79. mDayValue = value;
  80. }
  81. }
  82. Vector4 ColorToVector4(Color c)
  83. {
  84. return new Vector4(c.r, c.g, c.b, c.a);
  85. }
  86. bool CompareColor(Color a, Color b, float margin)
  87. {
  88. Vector4 va = new Vector4(a.r, a.g, a.b, a.a);
  89. Vector4 vb = new Vector4(b.r, b.g, b.b, b.a);
  90. return (va - vb).sqrMagnitude < margin * margin;
  91. }
  92. void SlideColor(Color color)
  93. {
  94. if (Background == null)
  95. return;
  96. if (mCoroutine != null)
  97. {
  98. StopCoroutine(mCoroutine);
  99. mCoroutine = null;
  100. }
  101. if (CompareColor(Background.color, color, 0.01f))
  102. return;
  103. if (isActiveAndEnabled == false || SlideSpeed <0f)
  104. {
  105. Background.color = color;
  106. return;
  107. }
  108. mCoroutine = SlideToColor(color, SlideSpeed);
  109. StartCoroutine(mCoroutine);
  110. }
  111. private void OnDisable()
  112. {
  113. if (mCoroutine != null)
  114. {
  115. StopCoroutine(mCoroutine);
  116. if (Background != null)
  117. Background.color = TargetColor;
  118. mCoroutine = null;
  119. }
  120. }
  121. IEnumerator SlideToColor(Color color, float factor)
  122. {
  123. TargetColor = color;
  124. Vector4 from = ColorToVector4(Background.color);
  125. Vector4 to = ColorToVector4(color);
  126. Vector4 move = (to - from);
  127. float time = 0f;
  128. float magnitude = move.magnitude;
  129. Color start = Background.color;
  130. while (CompareColor(Background.color, color, 0.01f) == false)
  131. {
  132. Background.color = Color.Lerp(start, color, (time * factor) / magnitude);
  133. time += Time.deltaTime;
  134. yield return 0;
  135. }
  136. }
  137. public override void SetInitialSettings(bool enabled, bool selected)
  138. {
  139. mEnabled = enabled;
  140. mSelected = selected;
  141. AssignTextAndBackground(true);
  142. }
  143. void AssignTextAndBackground(bool dontSlide = false)
  144. {
  145. var color = SelectedTextColor;
  146. var backColor = SelectedBackgroundColor;
  147. if(mSelected == false)
  148. {
  149. if(mEnabled)
  150. {
  151. color = EnabledTextColor;
  152. backColor = EnabledBackgroundColor;
  153. }
  154. else
  155. {
  156. color = DisabledTextColor;
  157. backColor = DisabledBackgroundColor;
  158. }
  159. }
  160. if (Mark != null)
  161. Mark.color = MarkSelectedColor;
  162. if (Mark != null)
  163. Mark.sprite = MarkSprite;
  164. if (TextItem != null)
  165. {
  166. TextItem.Color = color;
  167. TextItem.TextSize = TextSize;
  168. TextItem.FontStyle = FontStyle;
  169. }
  170. if(dontSlide)
  171. {
  172. if (Background != null)
  173. Background.color = backColor;
  174. }
  175. else
  176. SlideColor(backColor);
  177. // if (Background != null)
  178. // Background.color = backColor;
  179. }
  180. void CopyFrom(StandardDatePickerCell cell)
  181. {
  182. TextSize = cell.TextSize;
  183. FontStyle = cell.FontStyle;
  184. EnabledTextColor = cell.EnabledTextColor;
  185. SelectedTextColor = cell.SelectedTextColor;
  186. DisabledTextColor = cell.DisabledTextColor;
  187. MarkSprite = cell.MarkSprite;
  188. MarkSelectedColor = cell.MarkSelectedColor;
  189. SelectedBackgroundColor = cell.SelectedBackgroundColor;
  190. // public Color HoverBackgroundColor;
  191. EnabledBackgroundColor = cell.EnabledBackgroundColor;
  192. DisabledBackgroundColor = cell.DisabledBackgroundColor;
  193. SlideSpeed = cell.SlideSpeed;
  194. }
  195. public void OnValidate()
  196. {
  197. if (GetComponent<DatePickerCellTemplate>() == null)
  198. {
  199. AssignTextAndBackground(true);
  200. return;
  201. }
  202. var settings = GetComponentInParent<DatePickerSettings>();
  203. if(settings != null)
  204. {
  205. foreach(var cell in settings.GetComponentsInChildren<StandardDatePickerCell>())
  206. {
  207. if (cell != this)
  208. {
  209. cell.CopyFrom(this);
  210. cell.OnValidate();
  211. }
  212. }
  213. }
  214. }
  215. public override void SetText(string text)
  216. {
  217. if (TextItem != null)
  218. TextItem.Text = text;// DateToString(mDayValue));
  219. }
  220. }
  221. }