Merged in dev/ComboBoxImprovements (pull request #92)

Dev/ComboBoxImprovements
pull/413/head
Simon Jackson 2020-09-24 21:03:30 +00:00
commit 12c01ae922
3 changed files with 94 additions and 38 deletions

View File

@ -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<DropDownListItem> ddItems = new List<DropDownListItem>();
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
/// </summary>
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
/// </summary>
/// <param name="directClick"> whether an item was directly clicked on</param>
public void ToggleDropdownPanel(bool directClick)
public void ToggleDropdownPanel(bool directClick = false)
{
_isPanelActive = !_isPanelActive;

View File

@ -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<string>
{
@ -178,7 +181,7 @@ namespace UnityEngine.UI.Extensions
{
_panelItems.Add(option.ToLower());
}
_panelItems.Sort();
if(_sortItems) _panelItems.Sort();
List<GameObject> itemObjs = new List<GameObject>(panelObjects.Values);
panelObjects.Clear();

View File

@ -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.
/// <summary>
/// Rebuilds the list from a new collection.
/// </summary>
/// <remarks>
/// NOTE, this will clear all existing items
/// </remarks>
/// <param name="list"></param>
public void RefreshItems(params object[] list)
{
Items.Clear();
List<DropDownListItem> ddItems = new List<DropDownListItem>();
foreach (var obj in list)
{
@ -157,10 +165,74 @@ namespace UnityEngine.UI.Extensions
}
}
Items.AddRange(ddItems);
Items = Items.Distinct().ToList();//remove any duplicates
RebuildPanel();
}
*/
/// <summary>
/// Adds an additional item to the drop down list (recommended)
/// </summary>
/// <param name="item">Item of type DropDownListItem</param>
public void AddItem(DropDownListItem item)
{
Items.Add(item);
RebuildPanel();
}
/// <summary>
/// Adds an additional drop down list item using a string name
/// </summary>
/// <param name="item">Item of type String</param>
public void AddItem(string item)
{
Items.Add(new DropDownListItem(caption: (string)item));
RebuildPanel();
}
/// <summary>
/// Adds an additional drop down list item using a sprite image
/// </summary>
/// <param name="item">Item of type UI Sprite</param>
public void AddItem(Sprite item)
{
Items.Add(new DropDownListItem(image: (Sprite)item));
RebuildPanel();
}
/// <summary>
/// Removes an item from the drop down list (recommended)
/// </summary>
/// <param name="item">Item of type DropDownListItem</param>
public void RemoveItem(DropDownListItem item)
{
Items.Remove(item);
RebuildPanel();
}
/// <summary>
/// Removes an item from the drop down list item using a string name
/// </summary>
/// <param name="item">Item of type String</param>
public void RemoveItem(string item)
{
Items.Remove(new DropDownListItem(caption: (string)item));
RebuildPanel();
}
/// <summary>
/// Removes an item from the drop down list item using a sprite image
/// </summary>
/// <param name="item">Item of type UI Sprite</param>
public void RemoveItem(Sprite item)
{
Items.Remove(new DropDownListItem(image: (Sprite)item));
RebuildPanel();
}
public void ResetItems()
{
Items.Clear();
RebuildPanel();
}
/// <summary>
/// Rebuilds the contents of the panel in response to items being added.