DayTitle.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using UnityEngine;
  6. namespace Bitsplash.DatePicker
  7. {
  8. /// <summary>
  9. /// shows the week day names on top the date picker
  10. /// </summary>
  11. [ExecuteInEditMode]
  12. public class DayTitle : DatePickerElement , IDatePickerSettingsItem
  13. {
  14. public string EditorTitle { get { return "Day Title"; } }
  15. public DatePickerCell CellPrefab;
  16. public string Format = "ddd";
  17. DatePickerContent mContent;
  18. bool mInvalid = true;
  19. public int Order { get { return 8; } }
  20. void GenerateCells()
  21. {
  22. Clear();
  23. if (CellPrefab == null || mContent == null)
  24. return;
  25. float ColumnSize = 1f / 7f;
  26. DateTime baseDate = DateTime.Today.Date;
  27. int monthDayOfWeek = (int)baseDate.DayOfWeek;
  28. int span = monthDayOfWeek - (int)mContent.FirstDayOfWeek;
  29. if (span < 0)
  30. span += 7;
  31. DateTime startFrom = (baseDate - TimeSpan.FromDays(span)).Date;
  32. for (int i = 0; i < 7; i++)
  33. {
  34. DateTime current = startFrom.Add(TimeSpan.FromDays(i)).Date;
  35. float startX = ((float)i) / 7f;
  36. if (mContent.RightToLeft)
  37. startX = 1f - startX - ColumnSize;
  38. float endX = startX + ColumnSize;
  39. GameObject newObj = GameObject.Instantiate(CellPrefab.gameObject, transform);
  40. CommonMethods.SafeDestroy(newObj.GetComponent<DatePickerCellTemplate>());
  41. CommonMethods.HideObject(newObj);
  42. newObj.name = String.Format("day_{0}", i);
  43. newObj.SetActive(true);
  44. CommonMethods.EnsureComponent<IDateTimeItem>(newObj);
  45. var rect = newObj.GetComponent<RectTransform>();
  46. rect.anchorMin = new Vector2(startX, 0f);
  47. rect.anchorMax = new Vector2(endX, 1f);
  48. rect.anchoredPosition = new Vector2(0f, 0f);
  49. rect.sizeDelta = new Vector2(0f, 0f);
  50. var cell = newObj.GetComponent<DatePickerCell>();
  51. cell.SetInitialSettings(true, false);
  52. cell.DayValue = current;
  53. try
  54. {
  55. cell.SetText(current.ToString(Format));
  56. }
  57. catch(Exception)
  58. {
  59. Debug.LogWarning("invalid format in day title");
  60. }
  61. }
  62. }
  63. public void Clear()
  64. {
  65. IDateTimeItem[] children = GetComponentsInChildren<IDateTimeItem>();
  66. for (int i = 0; i < children.Length; ++i)
  67. {
  68. if (children[i] != null)
  69. {
  70. if (children[i].gameObject.GetComponentInParent<DayTitle>() != this)
  71. continue;
  72. if (children[i].gameObject != gameObject)
  73. CommonMethods.SafeDestroy(children[i].gameObject);
  74. }
  75. }
  76. }
  77. public void Invalidate()
  78. {
  79. mInvalid = true;
  80. }
  81. public override void OnValidate()
  82. {
  83. base.OnValidate();
  84. Invalidate();
  85. }
  86. // Start is called before the first frame update
  87. protected override void Start()
  88. {
  89. base.Start();
  90. Invalidate();
  91. }
  92. protected override void OnSettingsChanged()
  93. {
  94. base.OnSettingsChanged();
  95. Invalidate();
  96. }
  97. // Update is called once per frame
  98. void Update()
  99. {
  100. if(mInvalid == true)
  101. {
  102. mInvalid = false;
  103. GenerateCells();
  104. }
  105. }
  106. protected override void SetContent(DatePickerContent content)
  107. {
  108. mContent = content;
  109. Invalidate();
  110. }
  111. protected override void SetMain(DatePickerSettings main)
  112. {
  113. }
  114. }
  115. }