Skip to content

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
merged 11 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#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 _bindingValuePath;
[SerializeField] private string _bindingChoicesPath;

private IProperty<string> _valueProperty;
private IReadOnlyProperty<ObservableCollection<string>> _itemsSource;

private PropertyBindingData _propertyBindingData;
private PropertyBindingData _itemsSourceBindingData;

public void SetBindingContext(IBindingContext context, IObjectProvider objectProvider)
{
if (string.IsNullOrWhiteSpace(_bindingChoicesPath))
{
return;
}

_itemsSourceBindingData ??= _bindingChoicesPath.ToPropertyBindingData();
_propertyBindingData ??= _bindingValuePath.ToPropertyBindingData();

_itemsSource = objectProvider
.RentReadOnlyProperty<ObservableCollection<string>>(context, _itemsSourceBindingData);
_itemsSource.Value.CollectionChanged += OnItemsCollectionChanged;

_valueProperty = objectProvider.RentProperty<string>(context, _propertyBindingData);
_valueProperty.ValueChanged += OnPropertyValueChanged;

UpdateControlValue(_dropdown.options.FindIndex(option => option.text == _valueProperty.Value));
_dropdown.onValueChanged.AddListener(OnControlValueChanged);

_dropdown.options = new List<TMP_Dropdown.OptionData>(_itemsSource.Value.Select(value => new TMP_Dropdown.OptionData(value)));
_valueProperty.Value = _dropdown.options[0].text;
}

private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:

if (e.NewItems.Count != 1)
{
throw new NotSupportedException("RangeActionsNotSupported");
}

if (e.NewItems?[0] is string newValue)
{
_dropdown.options.Add(new TMP_Dropdown.OptionData(newValue));
}
break;

case NotifyCollectionChangedAction.Remove:

if (e.OldItems.Count != 1)
{
throw new NotSupportedException("RangeActionsNotSupported");
}

if (e.OldStartingIndex < 0)
{
throw new InvalidOperationException("RemovedItemNotFound");
}


if (e.OldItems?[0] is string oldValue)
{
_dropdown.options.Remove(new TMP_Dropdown.OptionData(oldValue));
}
break;

case NotifyCollectionChangedAction.Replace:

if (e.NewItems.Count != 1 || e.OldItems.Count != 1)
{
throw new NotSupportedException("RangeActionsNotSupported");
}

if (e.NewItems?[0] is string replacingValue &&
e.OldItems?[0] is string replacedValue)
{
int indexReplacedValue = _dropdown.options.FindIndex(s => s.text == replacedValue);

if (indexReplacedValue != -1)
{
_dropdown.options[indexReplacedValue] = new TMP_Dropdown.OptionData(replacingValue);
}
}
break;

case NotifyCollectionChangedAction.Move:

if (e.NewItems.Count != 1)
{
throw new NotSupportedException("RangeActionsNotSupported");
}
if (e.NewStartingIndex < 0)
{
throw new InvalidOperationException("CannotMoveToUnknownPosition");
}

if (e.OldItems?[0] is string oldItemMoved)
{
_dropdown.options.Remove(new TMP_Dropdown.OptionData(oldItemMoved));
_dropdown.options.Insert(e.NewStartingIndex, new TMP_Dropdown.OptionData(oldItemMoved));
}
break;

case NotifyCollectionChangedAction.Reset:
_dropdown.options.Clear();
break;

default:
{
throw new NotSupportedException("UnexpectedCollectionChangeAction");
}
}
}

public virtual void ResetBindingContext(IObjectProvider objectProvider)
{
if (_valueProperty == null || _itemsSource == null)
{
return;
}

_valueProperty.ValueChanged -= OnPropertyValueChanged;
_itemsSource.Value.CollectionChanged -= OnItemsCollectionChanged;
_dropdown.options = new List<TMP_Dropdown.OptionData>();

objectProvider.ReturnProperty(_valueProperty);
objectProvider.ReturnReadOnlyProperty(_itemsSource);

_valueProperty = null;
_itemsSource = null;

_dropdown.onValueChanged.RemoveListener(OnControlValueChanged);
UpdateControlValue(default);
}

protected virtual void OnControlValueChanged(int index)
{
_valueProperty.Value = _dropdown.options[index].text;
}

private void OnPropertyValueChanged(object sender, string newValue)
{
UpdateControlValue(_dropdown.options.FindIndex(option => option.text == newValue));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual void UpdateControlValue(int newValue)
{
_dropdown.SetValueWithoutNotify(newValue);
}
}
}

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Runtime.CompilerServices;
using UnityEngine.UIElements;
using UnityMvvmToolkit.Core;
using UnityMvvmToolkit.Core.Extensions;
using UnityMvvmToolkit.Core.Interfaces;

namespace UnityMvvmToolkit.UITK.BindableUIElements
{
public partial class BindableDropdownField : DropdownField, IBindableElement
{
private IProperty<string> _valueProperty;
private IReadOnlyProperty<ObservableCollection<string>> _itemsSource;

private PropertyBindingData _propertyBindingData;
private PropertyBindingData _itemsSourceBindingData;

public void SetBindingContext(IBindingContext context, IObjectProvider objectProvider)
{
if (string.IsNullOrWhiteSpace(BindingChoicesPath))
{
return;
}

_itemsSourceBindingData ??= BindingChoicesPath.ToPropertyBindingData();
_propertyBindingData ??= BindingValuePath.ToPropertyBindingData();

_itemsSource = objectProvider
.RentReadOnlyProperty<ObservableCollection<string>>(context, _itemsSourceBindingData);
_itemsSource.Value.CollectionChanged += OnItemsCollectionChanged;

_valueProperty = objectProvider.RentProperty<string>(context, _propertyBindingData);
_valueProperty.ValueChanged += OnPropertyValueChanged;

UpdateControlValue(_valueProperty.Value);
this.RegisterValueChangedCallback(OnControlValueChanged);

choices = new List<string>(_itemsSource.Value);
_valueProperty.Value = choices[0];
}

private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:

if (e.NewItems.Count != 1)
{
throw new NotSupportedException("RangeActionsNotSupported");
}

if (e.NewItems?[0] is string newValue)
{
choices.Add(newValue);
}
break;

case NotifyCollectionChangedAction.Remove:

if (e.OldItems.Count != 1)
{
throw new NotSupportedException("RangeActionsNotSupported");
}

if (e.OldStartingIndex < 0)
{
throw new InvalidOperationException("RemovedItemNotFound");
}


if (e.OldItems?[0] is string oldValue)
{
choices.Add(oldValue);
}
break;

case NotifyCollectionChangedAction.Replace:

if (e.NewItems.Count != 1 || e.OldItems.Count != 1)
{
throw new NotSupportedException("RangeActionsNotSupported");
}

if (e.NewItems?[0] is string replacingValue &&
e.OldItems?[0] is string replacedValue)
{
int indexReplacedValue = choices.FindIndex(s => s == replacedValue);

if (indexReplacedValue != -1)
{
choices[indexReplacedValue] = replacingValue;
}
}
break;

case NotifyCollectionChangedAction.Move:

if (e.NewItems.Count != 1)
{
throw new NotSupportedException("RangeActionsNotSupported");
}
if (e.NewStartingIndex < 0)
{
throw new InvalidOperationException("CannotMoveToUnknownPosition");
}

if (e.OldItems?[0] is string oldItemMoved)
{
choices.Remove(oldItemMoved);
choices.Insert(e.NewStartingIndex, oldItemMoved);
}
break;

case NotifyCollectionChangedAction.Reset:
choices.Clear();
break;

default:
{
throw new NotSupportedException("UnexpectedCollectionChangeAction");
}
}
}

public virtual void ResetBindingContext(IObjectProvider objectProvider)
{
if (_valueProperty == null || _itemsSource == null)
{
return;
}

_valueProperty.ValueChanged -= OnPropertyValueChanged;
_itemsSource.Value.CollectionChanged -= OnItemsCollectionChanged;
choices = new List<string>();

objectProvider.ReturnProperty(_valueProperty);
objectProvider.ReturnReadOnlyProperty(_itemsSource);

_valueProperty = null;
_itemsSource = null;

this.UnregisterValueChangedCallback(OnControlValueChanged);
UpdateControlValue(default);
}

protected virtual void OnControlValueChanged(ChangeEvent<string> e)
{
_valueProperty.Value = e.newValue;
}

private void OnPropertyValueChanged(object sender, string newValue)
{
UpdateControlValue(newValue);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual void UpdateControlValue(string newValue)
{
SetValueWithoutNotify(newValue);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading