DatePickerDropDownBase.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. try
  96. {
  97. if (d.HasValue)
  98. t = d.Value.ToString(labelDateFormat); // find the correct string to show for the selected date
  99. }
  100. catch(Exception)
  101. {
  102. Debug.LogWarning("the format specified for the drop down is not valid");
  103. }
  104. SetText(t); // show the selected date
  105. Hide();
  106. }
  107. protected virtual GameObject CreateBlocker()
  108. {
  109. var canvasItems = GetComponentsInParent<Canvas>();
  110. if (canvasItems.Length == 0)
  111. return null;
  112. Canvas rootCanvas = canvasItems[0];
  113. GameObject gameObject = new GameObject("Blocker");
  114. RectTransform rectTransform = gameObject.AddComponent<RectTransform>();
  115. rectTransform.SetParent(rootCanvas.transform, false);
  116. rectTransform.anchorMin = (Vector2)Vector3.zero;
  117. rectTransform.anchorMax = (Vector2)Vector3.one;
  118. rectTransform.sizeDelta = Vector2.zero;
  119. Canvas canvas = gameObject.AddComponent<Canvas>();
  120. canvas.overrideSorting = true;
  121. Canvas component = DropDownContent.GetComponent<Canvas>();
  122. canvas.sortingLayerID = component.sortingLayerID;
  123. canvas.sortingOrder = component.sortingOrder - 1;
  124. gameObject.AddComponent<GraphicRaycaster>();
  125. gameObject.AddComponent<Image>().color = Color.clear;
  126. gameObject.AddComponent<Button>().onClick.AddListener(new UnityAction(this.Hide));
  127. return gameObject;
  128. }
  129. /// <summary>
  130. /// handle the drop down button click
  131. /// </summary>
  132. void ButtonClicked()
  133. {
  134. if (DropDownContent != null)
  135. {
  136. if (DropDownContent.gameObject.activeSelf)
  137. Hide();
  138. else
  139. Show();
  140. }
  141. }
  142. // Update is called once per frame
  143. void Update()
  144. {
  145. }
  146. }
  147. }