From c06cd38081d576f8de1fec961e358949191a23f6 Mon Sep 17 00:00:00 2001 From: Robert Wartenberg Date: Wed, 16 Aug 2023 12:02:41 +0200 Subject: [PATCH 1/4] add: public accessable reset function --- .../Scripts/Controls/ComboBox/DropDownList.cs | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs index 7de1b07..46ea9d3 100644 --- a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs +++ b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; namespace UnityEngine.UI.Extensions { /// - /// Extension to the UI class which creates a dropdown list + /// Extension to the UI class which creates a dropdown list /// [RequireComponent(typeof(RectTransform))] [AddComponentMenu("UI/Extensions/ComboBox/Dropdown List")] @@ -49,6 +49,9 @@ namespace UnityEngine.UI.Extensions private GameObject _itemTemplate; private bool _initialized; + private string _defaultMainButtonCaption = null; + private Color _defaultNormalColor; + [SerializeField] private float _scrollBarWidth = 20.0f; public float ScrollBarWidth @@ -121,6 +124,9 @@ namespace UnityEngine.UI.Extensions _rectTransform = GetComponent(); _mainButton = new DropDownListButton(_rectTransform.Find("MainButton").gameObject); + _defaultMainButtonCaption = _mainButton.txt.text; + _defaultNormalColor = _mainButton.btn.colors.normalColor; + _overlayRT = _rectTransform.Find("Overlay").GetComponent(); _overlayRT.gameObject.SetActive(false); _scrollPanelRT = _overlayRT.Find("ScrollPanel").GetComponent(); @@ -212,7 +218,7 @@ namespace UnityEngine.UI.Extensions } /// - /// Adds an additional drop down list item using a string name + /// Adds an additional drop down list item using a string name /// /// Item of type String public void AddItem(string item) @@ -223,7 +229,7 @@ namespace UnityEngine.UI.Extensions } /// - /// Adds an additional drop down list item using a sprite image + /// Adds an additional drop down list item using a sprite image /// /// Item of type UI Sprite public void AddItem(Sprite item) @@ -245,7 +251,7 @@ namespace UnityEngine.UI.Extensions } /// - /// Removes an item from the drop down list item using a string name + /// Removes an item from the drop down list item using a string name /// /// Item of type String public void RemoveItem(string item) @@ -256,7 +262,7 @@ namespace UnityEngine.UI.Extensions } /// - /// Removes an item from the drop down list item using a sprite image + /// Removes an item from the drop down list item using a sprite image /// /// Item of type UI Sprite public void RemoveItem(Sprite item) @@ -266,7 +272,21 @@ namespace UnityEngine.UI.Extensions RedrawPanel(); } - public void ResetItems() + public void ResetDropDown() + { + if (!_initialized) + return; + + _mainButton.txt.text = _defaultMainButtonCaption; + for (int i = 0; i < _itemsPanelRT.childCount; i++) + _panelItems[i].btnImg.color = _defaultNormalColor; + + _selectedIndex = -1; + _initialized = false; + Initialize(); + } + + public void ResetItems() { Items.Clear(); RebuildPanel(); @@ -304,7 +324,7 @@ namespace UnityEngine.UI.Extensions _panelItems[i].txt.text = item.Caption; if (item.IsDisabled) _panelItems[i].txt.color = disabledTextColor; - if (_panelItems[i].btnImg != null) _panelItems[i].btnImg.sprite = null;//hide the button image + if (_panelItems[i].btnImg != null) _panelItems[i].btnImg.sprite = null;//hide the button image _panelItems[i].img.sprite = item.Image; _panelItems[i].img.color = (item.Image == null) ? new Color(1, 1, 1, 0) : item.IsDisabled ? new Color(1, 1, 1, .5f) @@ -413,7 +433,7 @@ namespace UnityEngine.UI.Extensions public void ToggleDropdownPanel(bool directClick) { if (!isActive) return; - + _overlayRT.transform.localScale = new Vector3(1, 1, 1); _scrollBarRT.transform.localScale = new Vector3(1, 1, 1); _isPanelActive = !_isPanelActive; @@ -424,7 +444,7 @@ namespace UnityEngine.UI.Extensions } else if (directClick) { - // scrollOffset = Mathf.RoundToInt(itemsPanelRT.anchoredPosition.y / _rectTransform.sizeDelta.y); + // scrollOffset = Mathf.RoundToInt(itemsPanelRT.anchoredPosition.y / _rectTransform.sizeDelta.y); } } From 2a59f37653052cf331cc363fd099c90dd6e72121 Mon Sep 17 00:00:00 2001 From: Robert Wartenberg Date: Wed, 16 Aug 2023 12:03:59 +0200 Subject: [PATCH 2/4] fix: tabs vs. spaces in DropDownList { } --- .../Scripts/Controls/ComboBox/DropDownList.cs | 262 +++++++++--------- 1 file changed, 131 insertions(+), 131 deletions(-) diff --git a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs index 46ea9d3..40e90c4 100644 --- a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs +++ b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs @@ -5,25 +5,25 @@ using System.Collections.Generic; namespace UnityEngine.UI.Extensions { - /// - /// Extension to the UI class which creates a dropdown list - /// - [RequireComponent(typeof(RectTransform))] + /// + /// Extension to the UI class which creates a dropdown list + /// + [RequireComponent(typeof(RectTransform))] [AddComponentMenu("UI/Extensions/ComboBox/Dropdown List")] public class DropDownList : MonoBehaviour { public Color disabledTextColor; public DropDownListItem SelectedItem { get; private set; } //outside world gets to get this, not set it - [Header("Dropdown List Items")] - public List Items; + [Header("Dropdown List Items")] + public List Items; - [Header("Properties")] + [Header("Properties")] - [SerializeField] - private bool isActive = true; + [SerializeField] + private bool isActive = true; - public bool OverrideHighlighted = true; + public bool OverrideHighlighted = true; //private bool isInitialized = false; private bool _isPanelActive = false; @@ -46,13 +46,13 @@ namespace UnityEngine.UI.Extensions private List _panelItems = new List(); - private GameObject _itemTemplate; + private GameObject _itemTemplate; private bool _initialized; private string _defaultMainButtonCaption = null; private Color _defaultNormalColor; - [SerializeField] + [SerializeField] private float _scrollBarWidth = 20.0f; public float ScrollBarWidth { @@ -64,7 +64,7 @@ namespace UnityEngine.UI.Extensions } } - // private int scrollOffset; //offset of the selected item + // private int scrollOffset; //offset of the selected item private int _selectedIndex = -1; [SerializeField] @@ -79,42 +79,42 @@ namespace UnityEngine.UI.Extensions } } - [SerializeField] - private float dropdownOffset; + [SerializeField] + private float dropdownOffset; - [SerializeField] - private bool _displayPanelAbove = false; + [SerializeField] + private bool _displayPanelAbove = false; - public bool SelectFirstItemOnStart = false; + public bool SelectFirstItemOnStart = false; - [SerializeField] - private int selectItemIndexOnStart = 0; - private bool shouldSelectItemOnStart => SelectFirstItemOnStart || selectItemIndexOnStart > 0; + [SerializeField] + private int selectItemIndexOnStart = 0; + private bool shouldSelectItemOnStart => SelectFirstItemOnStart || selectItemIndexOnStart > 0; - [System.Serializable] + [System.Serializable] public class SelectionChangedEvent : Events.UnityEvent { } - // fires when item is changed; - [Header("Events")] - public SelectionChangedEvent OnSelectionChanged; + // fires when item is changed; + [Header("Events")] + public SelectionChangedEvent OnSelectionChanged; - [System.Serializable] - public class ControlDisabledEvent : Events.UnityEvent { } + [System.Serializable] + public class ControlDisabledEvent : Events.UnityEvent { } - // fires when item is changed; - public ControlDisabledEvent OnControlDisabled; + // fires when item is changed; + public ControlDisabledEvent OnControlDisabled; - public void Start() - { - Initialize(); - if (shouldSelectItemOnStart && Items.Count > 0) - { - SelectItemIndex(SelectFirstItemOnStart ? 0 : selectItemIndexOnStart); - } - RedrawPanel(); - } + public void Start() + { + Initialize(); + if (shouldSelectItemOnStart && Items.Count > 0) + { + SelectItemIndex(SelectFirstItemOnStart ? 0 : selectItemIndexOnStart); + } + RedrawPanel(); + } - private bool Initialize() + private bool Initialize() { if (_initialized) return true; @@ -153,32 +153,32 @@ namespace UnityEngine.UI.Extensions Debug.LogError("Something is setup incorrectly with the dropdownlist component causing a Null Reference Exception"); success = false; } - _initialized = true; + _initialized = true; - RebuildPanel(); + RebuildPanel(); RedrawPanel(); return success; } - /// + /// /// Update the drop down selection to a specific index /// /// public void SelectItemIndex(int index) - { - ToggleDropdownPanel(false); - OnItemClicked(index); - } + { + ToggleDropdownPanel(false); + OnItemClicked(index); + } - // 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) + // 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(); @@ -203,74 +203,74 @@ namespace UnityEngine.UI.Extensions } Items.AddRange(ddItems); RebuildPanel(); - RedrawPanel(); - } + RedrawPanel(); + } - /// - /// Adds an additional item to the drop down list (recommended) - /// - /// Item of type DropDownListItem - public void AddItem(DropDownListItem item) - { + /// + /// Adds an additional item to the drop down list (recommended) + /// + /// Item of type DropDownListItem + public void AddItem(DropDownListItem item) + { Items.Add(item); RebuildPanel(); RedrawPanel(); - } + } - /// - /// Adds an additional drop down list item using a string name - /// - /// Item of type String - public void AddItem(string item) + /// + /// 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(); - RedrawPanel(); - } + RedrawPanel(); + } - /// - /// Adds an additional drop down list item using a sprite image - /// - /// Item of type UI Sprite - public void AddItem(Sprite item) + /// + /// 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(); - RedrawPanel(); - } + RedrawPanel(); + } - /// - /// Removes an item from the drop down list (recommended) - /// - /// Item of type DropDownListItem - public void RemoveItem(DropDownListItem item) + /// + /// Removes an item from the drop down list (recommended) + /// + /// Item of type DropDownListItem + public void RemoveItem(DropDownListItem item) { Items.Remove(item); RebuildPanel(); - RedrawPanel(); - } + RedrawPanel(); + } - /// - /// Removes an item from the drop down list item using a string name - /// - /// Item of type String - public void RemoveItem(string item) + /// + /// 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(); - RedrawPanel(); - } + RedrawPanel(); + } - /// - /// Removes an item from the drop down list item using a sprite image - /// - /// Item of type UI Sprite - public void RemoveItem(Sprite item) + /// + /// 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(); - RedrawPanel(); - } + RedrawPanel(); + } public void ResetDropDown() { @@ -369,25 +369,25 @@ namespace UnityEngine.UI.Extensions _mainButton.txt.text = SelectedItem.Caption; - //update selected index color - if (OverrideHighlighted) - { - for (int i = 0; i < _itemsPanelRT.childCount; i++) - { - _panelItems[i].btnImg.color = (_selectedIndex == i) ? _mainButton.btn.colors.highlightedColor : new Color(0, 0, 0, 0); - } - } - } + //update selected index color + if (OverrideHighlighted) + { + for (int i = 0; i < _itemsPanelRT.childCount; i++) + { + _panelItems[i].btnImg.color = (_selectedIndex == i) ? _mainButton.btn.colors.highlightedColor : new Color(0, 0, 0, 0); + } + } + } private void RedrawPanel() { float scrollbarWidth = _panelItems.Count > ItemsToDisplay ? _scrollBarWidth : 0f;//hide the scrollbar if there's not enough items _scrollBarRT.gameObject.SetActive(_panelItems.Count > ItemsToDisplay); - float dropdownHeight = _itemsToDisplay > 0 ? _rectTransform.sizeDelta.y * Mathf.Min(_itemsToDisplay, _panelItems.Count) : _rectTransform.sizeDelta.y * _panelItems.Count; - dropdownHeight += dropdownOffset; + float dropdownHeight = _itemsToDisplay > 0 ? _rectTransform.sizeDelta.y * Mathf.Min(_itemsToDisplay, _panelItems.Count) : _rectTransform.sizeDelta.y * _panelItems.Count; + dropdownHeight += dropdownOffset; - if (!_hasDrawnOnce || _rectTransform.sizeDelta != _mainButton.rectTransform.sizeDelta) + if (!_hasDrawnOnce || _rectTransform.sizeDelta != _mainButton.rectTransform.sizeDelta) { _hasDrawnOnce = true; _mainButton.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _rectTransform.sizeDelta.x); @@ -397,12 +397,12 @@ namespace UnityEngine.UI.Extensions itemsRemaining = itemsRemaining < 0 ? 0 : itemsRemaining; _scrollPanelRT.SetParent(transform, true); - _scrollPanelRT.anchoredPosition = _displayPanelAbove ? - new Vector2(0, dropdownOffset + dropdownHeight) : - new Vector2(0, -(dropdownOffset + _rectTransform.sizeDelta.y)); + _scrollPanelRT.anchoredPosition = _displayPanelAbove ? + new Vector2(0, dropdownOffset + dropdownHeight) : + new Vector2(0, -(dropdownOffset + _rectTransform.sizeDelta.y)); - //make the overlay fill the screen - _overlayRT.SetParent(_canvas.transform, false); + //make the overlay fill the screen + _overlayRT.SetParent(_canvas.transform, false); _overlayRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _canvasRT.sizeDelta.x); _overlayRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, _canvasRT.sizeDelta.y); @@ -412,7 +412,7 @@ namespace UnityEngine.UI.Extensions if (_panelItems.Count < 1) return; - _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, dropdownHeight); + _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, dropdownHeight); _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _rectTransform.sizeDelta.x); _itemsPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _scrollPanelRT.sizeDelta.x - scrollbarWidth - 5); @@ -432,7 +432,7 @@ namespace UnityEngine.UI.Extensions /// whether an item was directly clicked on public void ToggleDropdownPanel(bool directClick) { - if (!isActive) return; + if (!isActive) return; _overlayRT.transform.localScale = new Vector3(1, 1, 1); _scrollBarRT.transform.localScale = new Vector3(1, 1, 1); @@ -448,17 +448,17 @@ namespace UnityEngine.UI.Extensions } } - /// - /// Updates the control and sets its active status, determines whether the dropdown will open ot not - /// - /// - public void SetActive(bool status) - { - if (status != isActive) - { - OnControlDisabled?.Invoke(status); - } - isActive = status; - } - } + /// + /// Updates the control and sets its active status, determines whether the dropdown will open ot not + /// + /// + public void SetActive(bool status) + { + if (status != isActive) + { + OnControlDisabled?.Invoke(status); + } + isActive = status; + } + } } \ No newline at end of file From 95f1956f48179662f0286173681ad460348123d7 Mon Sep 17 00:00:00 2001 From: Robert Wartenberg Date: Wed, 16 Aug 2023 14:47:35 +0200 Subject: [PATCH 3/4] fix: SetActive() takes care of btn. Add: ded. Hide funct --- .../Scripts/Controls/ComboBox/DropDownList.cs | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs index 40e90c4..2bbbf02 100644 --- a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs +++ b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs @@ -101,7 +101,7 @@ namespace UnityEngine.UI.Extensions [System.Serializable] public class ControlDisabledEvent : Events.UnityEvent { } - // fires when item is changed; + // fires when item changed between enabled and disabled; public ControlDisabledEvent OnControlDisabled; public void Start() @@ -427,38 +427,45 @@ namespace UnityEngine.UI.Extensions } /// - /// Toggle the drop down list + /// Toggle the drop down list if it's active /// - /// whether an item was directly clicked on + /// makes no difference, only for backwards compatibility reasons public void ToggleDropdownPanel(bool directClick) { - if (!isActive) return; + if (!isActive) + return; _overlayRT.transform.localScale = new Vector3(1, 1, 1); _scrollBarRT.transform.localScale = new Vector3(1, 1, 1); _isPanelActive = !_isPanelActive; _overlayRT.gameObject.SetActive(_isPanelActive); + if (_isPanelActive) - { transform.SetAsLastSibling(); - } - else if (directClick) - { - // scrollOffset = Mathf.RoundToInt(itemsPanelRT.anchoredPosition.y / _rectTransform.sizeDelta.y); - } + } + + /// + /// Hides the drop down panel if its visible at the moment + /// + public void HideDropDownPanel() + { + if (!_isPanelActive) + return; + ToggleDropdownPanel(false); } /// /// Updates the control and sets its active status, determines whether the dropdown will open ot not + /// and takes care of the underlying button to follow the status. /// /// public void SetActive(bool status) { - if (status != isActive) - { - OnControlDisabled?.Invoke(status); - } + if (status == isActive) + return; isActive = status; + OnControlDisabled?.Invoke(isActive); + _mainButton.btn.enabled = isActive; } } } \ No newline at end of file From 289a50d7966efb415976d72496217c868c23d436 Mon Sep 17 00:00:00 2001 From: "Simon (Darkside) Jackson" Date: Thu, 5 Oct 2023 16:53:04 +0100 Subject: [PATCH 4/4] Minor review corrections --- .../Scripts/Controls/ComboBox/DropDownList.cs | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs index 2bbbf02..00ff714 100644 --- a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs +++ b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs @@ -64,7 +64,6 @@ namespace UnityEngine.UI.Extensions } } - // private int scrollOffset; //offset of the selected item private int _selectedIndex = -1; [SerializeField] @@ -101,7 +100,7 @@ namespace UnityEngine.UI.Extensions [System.Serializable] public class ControlDisabledEvent : Events.UnityEvent { } - // fires when item changed between enabled and disabled; + // fires when item changes between enabled and disabled; public ControlDisabledEvent OnControlDisabled; public void Start() @@ -275,11 +274,15 @@ namespace UnityEngine.UI.Extensions public void ResetDropDown() { if (!_initialized) + { return; + } _mainButton.txt.text = _defaultMainButtonCaption; for (int i = 0; i < _itemsPanelRT.childCount; i++) + { _panelItems[i].btnImg.color = _defaultNormalColor; + } _selectedIndex = -1; _initialized = false; @@ -427,13 +430,24 @@ namespace UnityEngine.UI.Extensions } /// - /// Toggle the drop down list if it's active + /// Toggle the drop down list if it is active /// - /// makes no difference, only for backwards compatibility reasons - public void ToggleDropdownPanel(bool directClick) + /// Retained for backwards compatibility only. + [Obsolete("DirectClick Parameter is no longer required")] + public void ToggleDropdownPanel(bool directClick = false) + { + ToggleDropdownPanel(); + } + + /// + /// Toggle the drop down list if it is active + /// + public void ToggleDropdownPanel() { if (!isActive) + { return; + } _overlayRT.transform.localScale = new Vector3(1, 1, 1); _scrollBarRT.transform.localScale = new Vector3(1, 1, 1); @@ -441,7 +455,9 @@ namespace UnityEngine.UI.Extensions _overlayRT.gameObject.SetActive(_isPanelActive); if (_isPanelActive) + { transform.SetAsLastSibling(); + } } /// @@ -450,7 +466,10 @@ namespace UnityEngine.UI.Extensions public void HideDropDownPanel() { if (!_isPanelActive) + { return; + } + ToggleDropdownPanel(false); } @@ -462,7 +481,9 @@ namespace UnityEngine.UI.Extensions public void SetActive(bool status) { if (status == isActive) + { return; + } isActive = status; OnControlDisabled?.Invoke(isActive); _mainButton.btn.enabled = isActive;