Skip to content

add bindable text path to BindableButton component #71

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -10,7 +10,9 @@ public partial class BindableButton : BaseButton, IBindableElement
private int? _buttonId;

private IBaseCommand _command;
private IReadOnlyProperty<string> _textProperty;
private CommandBindingData _commandBindingData;
private PropertyBindingData _propertyBindingData;

public virtual void SetBindingContext(IBindingContext context, IObjectProvider objectProvider)
{
Expand All @@ -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<string>(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;
Expand All @@ -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()
Expand All @@ -64,5 +89,10 @@ private void SetControlEnabled(bool isEnabled)
{
Enabled = isEnabled;
}

private void OnPropertyValueChanged(object sender, string newText)
{
UpdateControlText(newText);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ partial class BindableButton
{
public string Command { get; private set; }

public string BindingTextPath { get; private set; }

public new class UxmlFactory : UxmlFactory<BindableButton, UxmlTraits>
{
}
Expand All @@ -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<BindableButton>().Command = Command;
visualElement.As<BindableButton>().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<BindableButton>().Command = _commandAttribute.GetValueFromBag(bag, context);
visualElement.As<BindableButton>().BindingTextPath = _bindingTextAttribute.GetValueFromBag(bag, context);
}
}
#endif
Expand Down