Skip to content

Commit

Permalink
Add Bindable<T>.CopyTo and use in GetUnboundCopy implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Nov 22, 2022
1 parent 72c1cc2 commit 9faae09
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
21 changes: 15 additions & 6 deletions osu.Framework/Bindables/Bindable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ public IBindable<T> BindTarget
set => ((IBindable<T>)this).BindTo(value);
}

/// <summary>
/// Copies all values and value limitations of this bindable to another such.
/// </summary>
/// <param name="them">The target to copy to</param>
public virtual void CopyTo(Bindable<T> them)
{
them.Value = Value;
them.Default = Default;
them.Disabled = Disabled;
}

/// <summary>
/// Binds this bindable to another such that bi-directional updates are propagated.
/// This will adopt any values and value limitations of the bindable bound to.
Expand All @@ -191,9 +202,7 @@ public virtual void BindTo(Bindable<T> them)
if (Bindings?.Contains(them) == true)
throw new InvalidOperationException($"This bindable is already bound to the requested bindable ({them}).");

Value = them.Value;
Default = them.Default;
Disabled = them.Disabled;
them.CopyTo(this);

addWeakReference(them.weakReference);
them.addWeakReference(weakReference);
Expand Down Expand Up @@ -389,9 +398,9 @@ public virtual void UnbindFrom(IUnbindable them)
/// </summary>
public Bindable<T> GetUnboundCopy()
{
var clone = GetBoundCopy();
clone.UnbindAll();
return clone;
var newBindable = CreateInstance();
CopyTo(newBindable);
return newBindable;
}

IBindable IBindable.CreateInstance() => CreateInstance();
Expand Down
6 changes: 3 additions & 3 deletions osu.Framework/Bindables/BindableNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ protected void TriggerPrecisionChange(BindableNumber<T> source = null, bool prop
PrecisionChanged?.Invoke(precision);
}

public override void BindTo(Bindable<T> them)
public override void CopyTo(Bindable<T> them)
{
if (them is BindableNumber<T> other)
Precision = other.Precision;
other.Precision = Precision;

base.BindTo(them);
base.CopyTo(them);
}

public override void UnbindEvents()
Expand Down
14 changes: 11 additions & 3 deletions osu.Framework/Bindables/RangeConstrainedBindable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ protected void TriggerMaxValueChange(RangeConstrainedBindable<T> source = null,
MaxValueChanged?.Invoke(maxValue);
}

public override void CopyTo(Bindable<T> them)
{
base.CopyTo(them);

if (them is RangeConstrainedBindable<T> other)
{
other.MinValue = MinValue;
other.MaxValue = MaxValue;
}
}

public override void BindTo(Bindable<T> them)
{
if (them is RangeConstrainedBindable<T> other)
Expand All @@ -167,9 +178,6 @@ public override void BindTo(Bindable<T> them)
throw new ArgumentOutOfRangeException(
nameof(them), $"The target bindable has specified an invalid range of [{other.MinValue} - {other.MaxValue}].");
}

MinValue = other.MinValue;
MaxValue = other.MaxValue;
}

base.BindTo(them);
Expand Down

0 comments on commit 9faae09

Please sign in to comment.