DatePickerDropDownBase.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.UI;
  7. namespace Bitsplash.DatePicker
  8. {
  9. public abstract class DatePickerDropDownBase : MonoBehaviour
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public string NoSelectionPrompt = "Select a date...";
  15. /// <summary>
  16. /// the date format of the label
  17. /// </summary>
  18. public string labelDateFormat = "d";
  19. /// <summary>
  20. /// the date picker settings object for the drop down
  21. /// </summary>
  22. public DatePickerSettings DropDownContent;
  23. /// <summary>
  24. /// the drop down button
  25. /// </summary>
  26. public Button DropDownButton;
  27. GameObject mBlocker;
  28. // Start is called before the first frame update
  29. void Start()
  30. {
  31. InitDropDown();
  32. }
  33. /// <summary>
  34. /// initializes the drop down events
  35. /// </summary>
  36. void InitDropDown()
  37. {
  38. if(DropDownButton == null)
  39. Debug.LogWarning("Drop Down Button Not Assigned"); // show warninig
  40. else
  41. DropDownButton.onClick.AddListener(ButtonClicked); // listen to drop down button clicks
  42. if (DropDownContent == null)
  43. Debug.LogWarning("Drop Down Content Not Assigned");// show warninig
  44. else
  45. {
  46. // set the selection mode to single.
  47. DropDownContent.Content.SelectionMode = SelectionType.Single;
  48. // listen to selection changed events on the date picker
  49. DropDownContent.Content.OnSelectionChanged.AddListener(SelectionChanged);
  50. // disable the drop down object
  51. DropDownContent.gameObject.SetActive(false);
  52. Canvas canvas = CommonMethods.EnsureComponent<Canvas>(DropDownContent.gameObject);
  53. CommonMethods.EnsureComponent<GraphicRaycaster>(DropDownContent.gameObject);
  54. }
  55. }
  56. protected abstract void SetText(string text);
  57. /// <summary>
  58. /// shows the drop down
  59. /// </summary>
  60. void Show()
  61. {
  62. var canvas = DropDownContent.GetComponent<Canvas>();
  63. if (canvas == null)
  64. return;
  65. DropDownContent.gameObject.SetActive(true);
  66. canvas.overrideSorting = true;
  67. canvas.sortingOrder = 30000;
  68. mBlocker = CreateBlocker();
  69. }
  70. /// <summary>
  71. /// returnes the selected date from the drop down , or null if non is selected
  72. /// </summary>
  73. /// <returns></returns>
  74. public System.DateTime? GetSelectedDate()
  75. {
  76. if (DropDownContent == null)
  77. return null;
  78. if (DropDownContent.Content.Selection.Count != 1)
  79. return null;
  80. return DropDownContent.Content.Selection.GetItem(0);
  81. }
  82. //hides the drop down
  83. void Hide()
  84. {
  85. DropDownContent.gameObject.SetActive(false);
  86. CommonMethods.SafeDestroy(mBlocker);
  87. }
  88. /// <summary>
  89. /// called when the date picker selection has changed
  90. /// </summary>
  91. void SelectionChanged()
  92. {
  93. var d = GetSelectedDate(); // get the selected date
  94. string t = NoSelectionPrompt;
  95. if (d != null)
  96. {
  97. t = d.Value.ToString("yyyy-MM-dd");
  98. }
  99. // try
  100. // {
  101. // if (d.HasValue)
  102. // t = d.Value.ToString("labelDateFormat"); // find the correct string to show for the selected date
  103. // }
  104. // catch(Exception)
  105. // {
  106. // Debug.LogWarning("the format specified for the drop down is not valid");
  107. // }
  108. SetText(t); // show the selected date
  109. Hide();
  110. }
  111. protected virtual GameObject CreateBlocker()
  112. {
  113. var canvasItems = GetComponentsInParent<Canvas>();
  114. if (canvasItems.Length == 0)
  115. return null;
  116. Canvas rootCanvas = canvasItems[0];
  117. GameObject gameObject = new GameObject("Blocker");
  118. RectTransform rectTransform = gameObject.AddComponent<RectTransform>();
  119. rectTransform.SetParent(rootCanvas.transform, false);
  120. rectTransform.anchorMin = (Vector2)Vector3.zero;
  121. rectTransform.anchorMax = (Vector2)Vector3.one;
  122. rectTransform.sizeDelta = Vector2.zero;
  123. Canvas canvas = gameObject.AddComponent<Canvas>();
  124. canvas.overrideSorting = true;
  125. Canvas component = DropDownContent.GetComponent<Canvas>();
  126. canvas.sortingLayerID = component.sortingLayerID;
  127. canvas.sortingOrder = component.sortingOrder - 1;
  128. gameObject.AddComponent<GraphicRaycaster>();
  129. gameObject.AddComponent<Image>().color = Color.clear;
  130. gameObject.AddComponent<Button>().onClick.AddListener(new UnityAction(this.Hide));
  131. return gameObject;
  132. }
  133. /// <summary>
  134. /// handle the drop down button click
  135. /// </summary>
  136. void ButtonClicked()
  137. {
  138. if (DropDownContent != null)
  139. {
  140. if (DropDownContent.gameObject.activeSelf)
  141. Hide();
  142. else
  143. Show();
  144. }
  145. }
  146. // Update is called once per frame
  147. void Update()
  148. {
  149. }
  150. }
  151. }