diff --git a/src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/BindableButton.cs b/src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/BindableButton.cs index 69702a8..ca792fc 100644 --- a/src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/BindableButton.cs +++ b/src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/BindableButton.cs @@ -10,7 +10,9 @@ public partial class BindableButton : BaseButton, IBindableElement private int? _buttonId; private IBaseCommand _command; + private IReadOnlyProperty _textProperty; private CommandBindingData _commandBindingData; + private PropertyBindingData _propertyBindingData; public virtual void SetBindingContext(IBindingContext context, IObjectProvider objectProvider) { @@ -30,10 +32,26 @@ public virtual void SetBindingContext(IBindingContext context, IObjectProvider o clicked += OnButtonClicked; SetControlEnabled(_command.CanExecute()); + + if (!string.IsNullOrWhiteSpace(BindingTextPath)) + { + _propertyBindingData ??= BindingTextPath.ToPropertyBindingData(); + _textProperty = objectProvider.RentReadOnlyProperty(context, _propertyBindingData); + _textProperty.ValueChanged += OnPropertyValueChanged; + UpdateControlText(_textProperty.Value); + } } public virtual void ResetBindingContext(IObjectProvider objectProvider) { + if (_textProperty != null) + { + _textProperty.ValueChanged -= OnPropertyValueChanged; + objectProvider.ReturnReadOnlyProperty(_textProperty); + _textProperty = null; + UpdateControlText(null); + } + if (_command is null) { return; @@ -47,6 +65,13 @@ public virtual void ResetBindingContext(IObjectProvider objectProvider) clicked -= OnButtonClicked; SetControlEnabled(true); + + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected virtual void UpdateControlText(string newText) + { + text = newText; } private void OnButtonClicked() @@ -64,5 +89,10 @@ private void SetControlEnabled(bool isEnabled) { Enabled = isEnabled; } + + private void OnPropertyValueChanged(object sender, string newText) + { + UpdateControlText(newText); + } } } \ No newline at end of file diff --git a/src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/Uxmls/BindableButton.Uxml.cs b/src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/Uxmls/BindableButton.Uxml.cs index 92477cd..b4ecc56 100644 --- a/src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/Uxmls/BindableButton.Uxml.cs +++ b/src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/Uxmls/BindableButton.Uxml.cs @@ -7,6 +7,8 @@ partial class BindableButton { public string Command { get; private set; } + public string BindingTextPath { get; private set; } + public new class UxmlFactory : UxmlFactory { } @@ -18,25 +20,37 @@ partial class BindableButton // ReSharper disable once InconsistentNaming #pragma warning disable 649 [UnityEngine.SerializeField] private string Command; - #pragma warning restore 649 +#pragma warning restore 649 + + [UnityEngine.SerializeField] private string BindingTextPath; public override object CreateInstance() => new BindableButton(); + public override void Deserialize(object visualElement) { base.Deserialize(visualElement); visualElement.As().Command = Command; + visualElement.As().BindingTextPath = BindingTextPath; } } #else public new class UxmlTraits : BaseButton.UxmlTraits { private readonly UxmlStringAttributeDescription _commandAttribute = new() - { name = "command", defaultValue = "" }; + { + name = "command", defaultValue = "" + }; + + private readonly UxmlStringAttributeDescription _bindingTextAttribute = new() + { + name = "binding-text-path", defaultValue = "" + }; public override void Init(VisualElement visualElement, IUxmlAttributes bag, CreationContext context) { base.Init(visualElement, bag, context); visualElement.As().Command = _commandAttribute.GetValueFromBag(bag, context); + visualElement.As().BindingTextPath = _bindingTextAttribute.GetValueFromBag(bag, context); } } #endif