From 8549c3ba6a2dffc7b3b5f07f3d98f00f21546f86 Mon Sep 17 00:00:00 2001 From: "LARS-LAPTOP2\\larsme" Date: Mon, 27 Mar 2017 14:33:08 +0200 Subject: [PATCH] Replaced OnSelectionChanged with event(s) accessible from the inspector (DropDownList & AutoCompleteComboBox) --- .../Controls/ComboBox/AutoCompleteComboBox.cs | 31 +++++++++++++++++-- Scripts/Controls/ComboBox/DropDownList.cs | 12 +++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs b/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs index 9528a9c..a406129 100644 --- a/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs +++ b/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs @@ -15,8 +15,6 @@ namespace UnityEngine.UI.Extensions public List AvailableOptions; - public System.Action OnSelectionChanged; // fires when selection is changed; - //private bool isInitialized = false; private bool _isPanelActive = false; private bool _hasDrawnOnce = false; @@ -134,6 +132,27 @@ namespace UnityEngine.UI.Extensions private bool _selectionIsValid = false; + [System.Serializable] + public class SelectionChangedEvent : UnityEngine.Events.UnityEvent { + } + + [System.Serializable] + public class SelectinTextChangedEvent : UnityEngine.Events.UnityEvent { + } + + [System.Serializable] + public class SelectionValidityChangedEvent : UnityEngine.Events.UnityEvent { + } + + // fires when input text is changed; + public SelectinTextChangedEvent OnSelectinTextChanged; + // fires when when an Item gets selected / deselected (including when items are added/removed once this is possible) + public SelectionValidityChangedEvent OnSelectionValidityChanged; + // fires in both cases + public SelectionChangedEvent OnSelectionChanged; + + + public void Awake() { Initialize(); @@ -369,7 +388,15 @@ namespace UnityEngine.UI.Extensions { ToggleDropdownPanel(false); } + + bool validity_changed = (_panelItems.Contains (Text) == _selectionIsValid); _selectionIsValid = _panelItems.Contains (Text); + OnSelectionChanged.Invoke (Text, _selectionIsValid); + OnSelectinTextChanged.Invoke (Text); + if(validity_changed){ + OnSelectionValidityChanged.Invoke (_selectionIsValid); + } + SetInputTextColor (); } diff --git a/Scripts/Controls/ComboBox/DropDownList.cs b/Scripts/Controls/ComboBox/DropDownList.cs index 7c5a0f7..a93680e 100644 --- a/Scripts/Controls/ComboBox/DropDownList.cs +++ b/Scripts/Controls/ComboBox/DropDownList.cs @@ -17,7 +17,6 @@ namespace UnityEngine.UI.Extensions public DropDownListItem SelectedItem { get; private set; } //outside world gets to get this, not set it public List Items; - public System.Action OnSelectionChanged; // fires when selection is changed; public bool OverrideHighlighted = true; //private bool isInitialized = false; @@ -109,6 +108,15 @@ namespace UnityEngine.UI.Extensions public bool SelectFirstItemOnStart = false; + [System.Serializable] + public class SelectionChangedEvent : UnityEngine.Events.UnityEvent { + } + // fires when item is changed; + public SelectionChangedEvent OnSelectionChanged; + + + + public void Start() { Initialize(); @@ -239,7 +247,7 @@ namespace UnityEngine.UI.Extensions private void OnItemClicked(int indx) { Debug.Log("item " + indx + " clicked"); - if (indx != _selectedIndex && OnSelectionChanged != null) OnSelectionChanged(indx); + if (indx != _selectedIndex && OnSelectionChanged != null) OnSelectionChanged.Invoke(indx); _selectedIndex = indx; ToggleDropdownPanel(true);