diff --git a/Runtime/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs b/Runtime/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs index a59f39e..1350f87 100644 --- a/Runtime/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs +++ b/Runtime/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs @@ -95,8 +95,10 @@ namespace UnityEngine.UI.Extensions } } - //TODO design as foldout for Inspector - public Color ValidSelectionTextColor = Color.green; + public float DropdownOffset = 10f; + + //TODO design as foldout for Inspector + public Color ValidSelectionTextColor = Color.green; public Color MatchingItemsRemainingTextColor = Color.black; public Color NoItemsRemainingTextColor = Color.red; @@ -184,35 +186,6 @@ namespace UnityEngine.UI.Extensions return success; } - /* currently just using items in the list instead of being able to add to it. - public void AddItems(params object[] list) - { - List ddItems = new List(); - foreach (var obj in list) - { - if (obj is DropDownListItem) - { - ddItems.Add((DropDownListItem)obj); - } - else if (obj is string) - { - ddItems.Add(new DropDownListItem(caption: (string)obj)); - } - else if (obj is Sprite) - { - ddItems.Add(new DropDownListItem(image: (Sprite)obj)); - } - else - { - throw new System.Exception("Only ComboBoxItems, Strings, and Sprite types are allowed"); - } - } - Items.AddRange(ddItems); - Items = Items.Distinct().ToList();//remove any duplicates - RebuildPanel(); - } - */ - public void AddItem(string item) { AvailableOptions.Add(item); @@ -255,11 +228,19 @@ namespace UnityEngine.UI.Extensions /// private void RebuildPanel() { + if (_isPanelActive) ToggleDropdownPanel(); + //panel starts with all options _panelItems.Clear(); _prunedPanelItems.Clear(); panelObjects.Clear(); + //clear Autocomplete children in scene + foreach (Transform child in _itemsPanelRT.transform) + { + Destroy(child.gameObject); + } + foreach (string option in AvailableOptions) { _panelItems.Add(option.ToLower()); @@ -363,7 +344,7 @@ namespace UnityEngine.UI.Extensions if (_panelItems.Count < 1) return; - float dropdownHeight = _rectTransform.sizeDelta.y * Mathf.Min(_itemsToDisplay, _panelItems.Count); + float dropdownHeight = _rectTransform.sizeDelta.y * Mathf.Min(_itemsToDisplay, _panelItems.Count) + DropdownOffset; _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, dropdownHeight); _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _rectTransform.sizeDelta.x); @@ -424,7 +405,7 @@ namespace UnityEngine.UI.Extensions /// Toggle the drop down list /// /// whether an item was directly clicked on - public void ToggleDropdownPanel(bool directClick) + public void ToggleDropdownPanel(bool directClick = false) { _isPanelActive = !_isPanelActive; diff --git a/Runtime/Scripts/Controls/ComboBox/ComboBox.cs b/Runtime/Scripts/Controls/ComboBox/ComboBox.cs index 02da2ff..f0b7bbe 100644 --- a/Runtime/Scripts/Controls/ComboBox/ComboBox.cs +++ b/Runtime/Scripts/Controls/ComboBox/ComboBox.cs @@ -21,6 +21,9 @@ namespace UnityEngine.UI.Extensions [SerializeField] private int _itemsToDisplay; + [SerializeField] + private bool _sortItems = true; + [System.Serializable] public class SelectionChangedEvent : UnityEngine.Events.UnityEvent { @@ -178,7 +181,7 @@ namespace UnityEngine.UI.Extensions { _panelItems.Add(option.ToLower()); } - _panelItems.Sort(); + if(_sortItems) _panelItems.Sort(); List itemObjs = new List(panelObjects.Values); panelObjects.Clear(); diff --git a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs index b702d46..959c8fe 100644 --- a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs +++ b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs @@ -133,9 +133,17 @@ namespace UnityEngine.UI.Extensions return success; } - /* currently just using items in the list instead of being able to add to it. - public void AddItems(params object[] list) + // currently just using items in the list instead of being able to add to it. + /// + /// Rebuilds the list from a new collection. + /// + /// + /// NOTE, this will clear all existing items + /// + /// + public void RefreshItems(params object[] list) { + Items.Clear(); List ddItems = new List(); foreach (var obj in list) { @@ -157,10 +165,74 @@ namespace UnityEngine.UI.Extensions } } Items.AddRange(ddItems); - Items = Items.Distinct().ToList();//remove any duplicates RebuildPanel(); } - */ + + /// + /// Adds an additional item to the drop down list (recommended) + /// + /// Item of type DropDownListItem + public void AddItem(DropDownListItem item) + { + Items.Add(item); + RebuildPanel(); + } + + /// + /// Adds an additional drop down list item using a string name + /// + /// Item of type String + public void AddItem(string item) + { + Items.Add(new DropDownListItem(caption: (string)item)); + RebuildPanel(); + } + + /// + /// Adds an additional drop down list item using a sprite image + /// + /// Item of type UI Sprite + public void AddItem(Sprite item) + { + Items.Add(new DropDownListItem(image: (Sprite)item)); + RebuildPanel(); + } + + /// + /// Removes an item from the drop down list (recommended) + /// + /// Item of type DropDownListItem + public void RemoveItem(DropDownListItem item) + { + Items.Remove(item); + RebuildPanel(); + } + + /// + /// Removes an item from the drop down list item using a string name + /// + /// Item of type String + public void RemoveItem(string item) + { + Items.Remove(new DropDownListItem(caption: (string)item)); + RebuildPanel(); + } + + /// + /// Removes an item from the drop down list item using a sprite image + /// + /// Item of type UI Sprite + public void RemoveItem(Sprite item) + { + Items.Remove(new DropDownListItem(image: (Sprite)item)); + RebuildPanel(); + } + + public void ResetItems() + { + Items.Clear(); + RebuildPanel(); + } /// /// Rebuilds the contents of the panel in response to items being added.