DayTitle.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. DateTimeFormatInfo dtfi = new CultureInfo("zh-CN", false).DateTimeFormat;
  35. DateTime current = startFrom.Add(TimeSpan.FromDays(i)).Date;
  36. float startX = ((float)i) / 7f;
  37. if (mContent.RightToLeft)
  38. startX = 1f - startX - ColumnSize;
  39. float endX = startX + ColumnSize;
  40. GameObject newObj = GameObject.Instantiate(CellPrefab.gameObject, transform);
  41. CommonMethods.SafeDestroy(newObj.GetComponent<DatePickerCellTemplate>());
  42. CommonMethods.HideObject(newObj);
  43. newObj.name = String.Format("day_{0}", i);
  44. newObj.SetActive(true);
  45. CommonMethods.EnsureComponent<IDateTimeItem>(newObj);
  46. var rect = newObj.GetComponent<RectTransform>();
  47. rect.anchorMin = new Vector2(startX, 0f);
  48. rect.anchorMax = new Vector2(endX, 1f);
  49. rect.anchoredPosition = new Vector2(0f, 0f);
  50. rect.sizeDelta = new Vector2(0f, 0f);
  51. var cell = newObj.GetComponent<DatePickerCell>();
  52. cell.SetInitialSettings(true, false);
  53. cell.DayValue = current;
  54. try
  55. {
  56. cell.SetText(current.ToString(Format,dtfi));
  57. }
  58. catch(Exception)
  59. {
  60. Debug.LogWarning("invalid format in day title");
  61. }
  62. }
  63. }
  64. public void Clear()
  65. {
  66. IDateTimeItem[] children = GetComponentsInChildren<IDateTimeItem>();
  67. for (int i = 0; i < children.Length; ++i)
  68. {
  69. if (children[i] != null)
  70. {
  71. if (children[i].gameObject.GetComponentInParent<DayTitle>() != this)
  72. continue;
  73. if (children[i].gameObject != gameObject)
  74. CommonMethods.SafeDestroy(children[i].gameObject);
  75. }
  76. }
  77. }
  78. public void Invalidate()
  79. {
  80. mInvalid = true;
  81. }
  82. public override void OnValidate()
  83. {
  84. base.OnValidate();
  85. Invalidate();
  86. }
  87. // Start is called before the first frame update
  88. protected override void Start()
  89. {
  90. base.Start();
  91. Invalidate();
  92. }
  93. protected override void OnSettingsChanged()
  94. {
  95. base.OnSettingsChanged();
  96. Invalidate();
  97. }
  98. // Update is called once per frame
  99. void Update()
  100. {
  101. if(mInvalid == true)
  102. {
  103. mInvalid = false;
  104. GenerateCells();
  105. }
  106. }
  107. protected override void SetContent(DatePickerContent content)
  108. {
  109. mContent = content;
  110. Invalidate();
  111. }
  112. protected override void SetMain(DatePickerSettings main)
  113. {
  114. }
  115. }
  116. }