using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; namespace Bitsplash.DatePicker { public abstract class DatePickerDropDownBase : MonoBehaviour { /// /// /// public string NoSelectionPrompt = "Select a date..."; /// /// the date format of the label /// public string labelDateFormat = "d"; /// /// the date picker settings object for the drop down /// public DatePickerSettings DropDownContent; /// /// the drop down button /// public Button DropDownButton; GameObject mBlocker; // Start is called before the first frame update void Start() { InitDropDown(); } /// /// initializes the drop down events /// void InitDropDown() { if(DropDownButton == null) Debug.LogWarning("Drop Down Button Not Assigned"); // show warninig else DropDownButton.onClick.AddListener(ButtonClicked); // listen to drop down button clicks if (DropDownContent == null) Debug.LogWarning("Drop Down Content Not Assigned");// show warninig else { // set the selection mode to single. DropDownContent.Content.SelectionMode = SelectionType.Single; // listen to selection changed events on the date picker DropDownContent.Content.OnSelectionChanged.AddListener(SelectionChanged); // disable the drop down object DropDownContent.gameObject.SetActive(false); Canvas canvas = CommonMethods.EnsureComponent(DropDownContent.gameObject); CommonMethods.EnsureComponent(DropDownContent.gameObject); } } protected abstract void SetText(string text); /// /// shows the drop down /// void Show() { var canvas = DropDownContent.GetComponent(); if (canvas == null) return; DropDownContent.gameObject.SetActive(true); canvas.overrideSorting = true; canvas.sortingOrder = 30000; mBlocker = CreateBlocker(); } /// /// returnes the selected date from the drop down , or null if non is selected /// /// public System.DateTime? GetSelectedDate() { if (DropDownContent == null) return null; if (DropDownContent.Content.Selection.Count != 1) return null; return DropDownContent.Content.Selection.GetItem(0); } //hides the drop down void Hide() { DropDownContent.gameObject.SetActive(false); CommonMethods.SafeDestroy(mBlocker); } /// /// called when the date picker selection has changed /// void SelectionChanged() { var d = GetSelectedDate(); // get the selected date string t = NoSelectionPrompt; try { if (d.HasValue) t = d.Value.ToString(labelDateFormat); // find the correct string to show for the selected date } catch(Exception) { Debug.LogWarning("the format specified for the drop down is not valid"); } SetText(t); // show the selected date Hide(); } protected virtual GameObject CreateBlocker() { var canvasItems = GetComponentsInParent(); if (canvasItems.Length == 0) return null; Canvas rootCanvas = canvasItems[0]; GameObject gameObject = new GameObject("Blocker"); RectTransform rectTransform = gameObject.AddComponent(); rectTransform.SetParent(rootCanvas.transform, false); rectTransform.anchorMin = (Vector2)Vector3.zero; rectTransform.anchorMax = (Vector2)Vector3.one; rectTransform.sizeDelta = Vector2.zero; Canvas canvas = gameObject.AddComponent(); canvas.overrideSorting = true; Canvas component = DropDownContent.GetComponent(); canvas.sortingLayerID = component.sortingLayerID; canvas.sortingOrder = component.sortingOrder - 1; gameObject.AddComponent(); gameObject.AddComponent().color = Color.clear; gameObject.AddComponent