DatePickerDropDownBase.cs 5.4 KB

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