-
-
Notifications
You must be signed in to change notification settings - Fork 30
Bindable dropdown #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ChebanovDD
merged 11 commits into
LibraStack:develop
from
VasilevMaxim:feature/bindable_dropdown
Jun 26, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
173222d
Written readme for dropdown
VasilevMaxim e3d47f8
BindableDropdownField and BindableDropdown TMP
VasilevMaxim f85cf86
Cleaunp
VasilevMaxim 93a5c2b
Cleanup
VasilevMaxim bf6f564
Cleanup
VasilevMaxim c2967f8
Cleanup
VasilevMaxim 1ac27af
Cleanup
VasilevMaxim 8322ae8
Cleanup
VasilevMaxim f7806f2
Cleanup
VasilevMaxim d0237e7
Changed NotifyCollectionChangedEventArgs
VasilevMaxim 208cd20
Add Reset
VasilevMaxim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
...age/Assets/Plugins/UnityMvvmToolkit/Runtime/UGUI/BindableUGUIElements/BindableDropdown.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#if UNITYMVVMTOOLKIT_TEXTMESHPRO_SUPPORT | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Collections.Specialized; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using TMPro; | ||
using UnityEngine; | ||
using UnityMvvmToolkit.Core; | ||
using UnityMvvmToolkit.Core.Extensions; | ||
using UnityMvvmToolkit.Core.Interfaces; | ||
|
||
namespace UnityMvvmToolkit.UGUI.BindableUGUIElements | ||
{ | ||
[RequireComponent(typeof(TMP_Dropdown))] | ||
public class BindableDropdown : MonoBehaviour, IBindableElement | ||
{ | ||
[SerializeField] private TMP_Dropdown _dropdown; | ||
[SerializeField] private string _bindingSelectedItemPath; | ||
[SerializeField] private string _bindingItemsSourcePath; | ||
|
||
private IProperty<string> _selectedItemProperty; | ||
private IReadOnlyProperty<ObservableCollection<string>> _itemsSource; | ||
|
||
private PropertyBindingData _selectedItemBindingData; | ||
private PropertyBindingData _itemsSourceBindingData; | ||
|
||
public void SetBindingContext(IBindingContext context, IObjectProvider objectProvider) | ||
{ | ||
if (string.IsNullOrWhiteSpace(_bindingItemsSourcePath) == false) | ||
{ | ||
_itemsSourceBindingData ??= _bindingItemsSourcePath.ToPropertyBindingData(); | ||
_itemsSource = objectProvider | ||
.RentReadOnlyProperty<ObservableCollection<string>>(context, _itemsSourceBindingData); | ||
_itemsSource.Value.CollectionChanged += OnItemsCollectionChanged; | ||
_dropdown.options = new List<TMP_Dropdown.OptionData>(_itemsSource.Value.Select(value => new TMP_Dropdown.OptionData(value))); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(_bindingSelectedItemPath) == false) | ||
{ | ||
_selectedItemBindingData ??= _bindingSelectedItemPath.ToPropertyBindingData(); | ||
_selectedItemProperty = objectProvider.RentProperty<string>(context, _selectedItemBindingData); | ||
_selectedItemProperty.ValueChanged += OnPropertySelectedItemChanged; | ||
|
||
var foundIndex = _dropdown.options.FindIndex(option => option.text == _selectedItemProperty.Value); | ||
if (foundIndex != -1) | ||
{ | ||
UpdateControlValue(foundIndex); | ||
} | ||
|
||
_dropdown.onValueChanged.AddListener(OnControlValueChanged); | ||
_selectedItemProperty.Value = _dropdown.options.Count > 0 ? _dropdown.options[0].text : default; | ||
} | ||
} | ||
|
||
private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) | ||
{ | ||
if (e.Action == NotifyCollectionChangedAction.Add) | ||
{ | ||
foreach (string newItem in e.NewItems) | ||
{ | ||
_dropdown.options.Add(new TMP_Dropdown.OptionData(newItem)); | ||
} | ||
} | ||
|
||
if (e.Action == NotifyCollectionChangedAction.Remove) | ||
{ | ||
foreach (string oldItem in e.OldItems) | ||
{ | ||
_dropdown.options.Remove(new TMP_Dropdown.OptionData(oldItem)); | ||
} | ||
} | ||
|
||
if (e.Action == NotifyCollectionChangedAction.Reset) | ||
{ | ||
_dropdown.options.Clear(); | ||
} | ||
} | ||
|
||
public virtual void ResetBindingContext(IObjectProvider objectProvider) | ||
{ | ||
if (_itemsSource != null) | ||
{ | ||
_itemsSource.Value.CollectionChanged -= OnItemsCollectionChanged; | ||
objectProvider.ReturnReadOnlyProperty(_itemsSource); | ||
_itemsSource = null; | ||
_dropdown.options = new List<TMP_Dropdown.OptionData>(); | ||
} | ||
|
||
if (_selectedItemProperty != null) | ||
{ | ||
_selectedItemProperty.ValueChanged -= OnPropertySelectedItemChanged; | ||
objectProvider.ReturnProperty(_selectedItemProperty); | ||
_selectedItemProperty = null; | ||
_dropdown.onValueChanged.RemoveListener(OnControlValueChanged); | ||
} | ||
|
||
UpdateControlValue(default); | ||
} | ||
|
||
protected virtual void OnControlValueChanged(int index) | ||
{ | ||
_selectedItemProperty.Value = _dropdown.options[index].text; | ||
} | ||
|
||
private void OnPropertySelectedItemChanged(object sender, string newValue) | ||
{ | ||
var foundIndex = _dropdown.options.FindIndex(option => option.text == newValue); | ||
if (foundIndex == -1) | ||
{ | ||
return; | ||
} | ||
|
||
UpdateControlValue(foundIndex); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
protected virtual void UpdateControlValue(int newValue) | ||
{ | ||
_dropdown.SetValueWithoutNotify(newValue); | ||
} | ||
} | ||
} | ||
|
||
#endif |
11 changes: 11 additions & 0 deletions
...ssets/Plugins/UnityMvvmToolkit/Runtime/UGUI/BindableUGUIElements/BindableDropdown.cs.meta
Oops, something went wrong.
128 changes: 128 additions & 0 deletions
.../Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/BindableDropdownField.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Collections.Specialized; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using UnityEngine.UIElements; | ||
using UnityMvvmToolkit.Common.Interfaces; | ||
using UnityMvvmToolkit.Core; | ||
using UnityMvvmToolkit.Core.Extensions; | ||
using UnityMvvmToolkit.Core.Interfaces; | ||
|
||
namespace UnityMvvmToolkit.UITK.BindableUIElements | ||
{ | ||
public partial class BindableDropdownField : DropdownField, IBindableCollection, IInitializable, IDisposable | ||
{ | ||
private IProperty<string> _selectedItemProperty; | ||
private IReadOnlyProperty<ObservableCollection<string>> _itemsSource; | ||
|
||
private PropertyBindingData _selectedItemBindingData; | ||
private PropertyBindingData _itemsSourceBindingData; | ||
|
||
public void Initialize() | ||
{ | ||
choices = new List<string>(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
choices.Clear(); | ||
} | ||
|
||
public void SetBindingContext(IBindingContext context, IObjectProvider objectProvider) | ||
{ | ||
if (string.IsNullOrWhiteSpace(BindingItemsSourcePath) == false) | ||
{ | ||
_itemsSourceBindingData ??= BindingItemsSourcePath.ToPropertyBindingData(); | ||
_itemsSource = objectProvider | ||
.RentReadOnlyProperty<ObservableCollection<string>>(context, _itemsSourceBindingData); | ||
_itemsSource.Value.CollectionChanged += OnItemsCollectionChanged; | ||
choices = new List<string>(_itemsSource.Value); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(BindingSelectedItemPath) == false) | ||
{ | ||
_selectedItemBindingData ??= BindingSelectedItemPath.ToPropertyBindingData(); | ||
_selectedItemProperty = objectProvider.RentProperty<string>(context, _selectedItemBindingData); | ||
_selectedItemProperty.ValueChanged += OnSelectedItemValueChanged; | ||
|
||
var isContains = choices.Contains(_selectedItemProperty.Value); | ||
if (isContains == true) | ||
{ | ||
UpdateControlValue(_selectedItemProperty.Value); | ||
} | ||
|
||
this.RegisterValueChangedCallback(OnControlValueChanged); | ||
_selectedItemProperty.Value = choices.Count > 0 ? choices[0] : default; | ||
} | ||
} | ||
|
||
protected virtual void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) | ||
{ | ||
if (e.Action == NotifyCollectionChangedAction.Add) | ||
{ | ||
foreach (string newItem in e.NewItems) | ||
{ | ||
choices.Add(newItem); | ||
} | ||
} | ||
|
||
if (e.Action == NotifyCollectionChangedAction.Remove) | ||
{ | ||
foreach (string oldItem in e.OldItems) | ||
{ | ||
choices.Remove(oldItem); | ||
} | ||
} | ||
|
||
if (e.Action == NotifyCollectionChangedAction.Reset) | ||
{ | ||
choices.Clear(); | ||
} | ||
} | ||
|
||
public virtual void ResetBindingContext(IObjectProvider objectProvider) | ||
{ | ||
if (_selectedItemProperty != null) | ||
{ | ||
_selectedItemProperty.ValueChanged -= OnSelectedItemValueChanged; | ||
objectProvider.ReturnProperty(_selectedItemProperty); | ||
_selectedItemProperty = null; | ||
this.UnregisterValueChangedCallback(OnControlValueChanged); | ||
} | ||
|
||
if (_itemsSource != null) | ||
{ | ||
_itemsSource.Value.CollectionChanged -= OnItemsCollectionChanged; | ||
choices = new List<string>(); | ||
objectProvider.ReturnReadOnlyProperty(_itemsSource); | ||
_itemsSource = null; | ||
} | ||
|
||
UpdateControlValue(default); | ||
} | ||
|
||
protected virtual void OnControlValueChanged(ChangeEvent<string> e) | ||
{ | ||
_selectedItemProperty.Value = e.newValue; | ||
} | ||
|
||
private void OnSelectedItemValueChanged(object sender, string newValue) | ||
{ | ||
var isContains = choices.Contains(newValue); | ||
if (isContains == false) | ||
{ | ||
return; | ||
} | ||
|
||
UpdateControlValue(newValue); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
protected virtual void UpdateControlValue(string newValue) | ||
{ | ||
SetValueWithoutNotify(newValue); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
...ts/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/BindableDropdownField.cs.meta
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
...gins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/Uxmls/BindableDropdownField.Uxml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using UnityEngine.UIElements; | ||
using UnityMvvmToolkit.UITK.Extensions; | ||
|
||
namespace UnityMvvmToolkit.UITK.BindableUIElements | ||
{ | ||
partial class BindableDropdownField | ||
{ | ||
public string BindingSelectedItemPath { get; private set; } | ||
|
||
public string BindingItemsSourcePath { get; private set; } | ||
|
||
public new class UxmlFactory : UxmlFactory<BindableDropdownField, UxmlTraits> | ||
{ | ||
} | ||
|
||
#if UNITY_2023_2_OR_NEWER | ||
[System.Serializable] | ||
public new class UxmlSerializedData : DropdownField.UxmlSerializedData | ||
{ | ||
// ReSharper disable once InconsistentNaming | ||
#pragma warning disable 649 | ||
[UnityEngine.SerializeField] private string BindingSelectedItemPath; | ||
[UnityEngine.SerializeField] private string BindingItemsSourcePath; | ||
#pragma warning restore 649 | ||
|
||
public override object CreateInstance() => new BindableDropdownField(); | ||
public override void Deserialize(object visualElement) | ||
{ | ||
base.Deserialize(visualElement); | ||
|
||
var bindableDropdownField = visualElement.As<BindableDropdownField>(); | ||
bindableDropdownField.BindingSelectedItemPath = BindingSelectedItemPath; | ||
bindableDropdownField.BindingItemsSourcePath = BindingItemsSourcePath; | ||
} | ||
} | ||
#else | ||
public new class UxmlTraits : DropdownField.UxmlTraits | ||
{ | ||
private readonly UxmlStringAttributeDescription _bindingSelectedItemPath = new() | ||
{ name = "binding-selected-item-path", defaultValue = "" }; | ||
|
||
private readonly UxmlStringAttributeDescription _bindingItemsSourcePath = new() | ||
{ name = "binding-items-source-path", defaultValue = "" }; | ||
|
||
public override void Init(VisualElement visualElement, IUxmlAttributes bag, CreationContext context) | ||
{ | ||
base.Init(visualElement, bag, context); | ||
|
||
var bindableDropdownField = visualElement.As<BindableDropdownField>(); | ||
|
||
bindableDropdownField.BindingSelectedItemPath = _bindingSelectedItemPath.GetValueFromBag(bag, context); | ||
bindableDropdownField.BindingItemsSourcePath = _bindingItemsSourcePath.GetValueFromBag(bag, context); | ||
} | ||
} | ||
#endif | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
...UnityMvvmToolkit/Runtime/UITK/BindableUIElements/Uxmls/BindableDropdownField.Uxml.cs.meta
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.