From 9faae09581d9c4aa24175ed78197720fd983e40e Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 22 Nov 2022 13:36:36 +0900 Subject: [PATCH] Add `Bindable.CopyTo` and use in `GetUnboundCopy` implementation --- osu.Framework/Bindables/Bindable.cs | 21 +++++++++++++------ osu.Framework/Bindables/BindableNumber.cs | 6 +++--- .../Bindables/RangeConstrainedBindable.cs | 14 ++++++++++--- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/osu.Framework/Bindables/Bindable.cs b/osu.Framework/Bindables/Bindable.cs index 58d2fa567e..266a9f3e5f 100644 --- a/osu.Framework/Bindables/Bindable.cs +++ b/osu.Framework/Bindables/Bindable.cs @@ -180,6 +180,17 @@ public IBindable BindTarget set => ((IBindable)this).BindTo(value); } + /// + /// Copies all values and value limitations of this bindable to another such. + /// + /// The target to copy to + public virtual void CopyTo(Bindable them) + { + them.Value = Value; + them.Default = Default; + them.Disabled = Disabled; + } + /// /// 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. @@ -191,9 +202,7 @@ public virtual void BindTo(Bindable 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); @@ -389,9 +398,9 @@ public virtual void UnbindFrom(IUnbindable them) /// public Bindable GetUnboundCopy() { - var clone = GetBoundCopy(); - clone.UnbindAll(); - return clone; + var newBindable = CreateInstance(); + CopyTo(newBindable); + return newBindable; } IBindable IBindable.CreateInstance() => CreateInstance(); diff --git a/osu.Framework/Bindables/BindableNumber.cs b/osu.Framework/Bindables/BindableNumber.cs index 6d605667fe..430da79d05 100644 --- a/osu.Framework/Bindables/BindableNumber.cs +++ b/osu.Framework/Bindables/BindableNumber.cs @@ -199,12 +199,12 @@ protected void TriggerPrecisionChange(BindableNumber source = null, bool prop PrecisionChanged?.Invoke(precision); } - public override void BindTo(Bindable them) + public override void CopyTo(Bindable them) { if (them is BindableNumber other) - Precision = other.Precision; + other.Precision = Precision; - base.BindTo(them); + base.CopyTo(them); } public override void UnbindEvents() diff --git a/osu.Framework/Bindables/RangeConstrainedBindable.cs b/osu.Framework/Bindables/RangeConstrainedBindable.cs index cef619065c..e710cef884 100644 --- a/osu.Framework/Bindables/RangeConstrainedBindable.cs +++ b/osu.Framework/Bindables/RangeConstrainedBindable.cs @@ -158,6 +158,17 @@ protected void TriggerMaxValueChange(RangeConstrainedBindable source = null, MaxValueChanged?.Invoke(maxValue); } + public override void CopyTo(Bindable them) + { + base.CopyTo(them); + + if (them is RangeConstrainedBindable other) + { + other.MinValue = MinValue; + other.MaxValue = MaxValue; + } + } + public override void BindTo(Bindable them) { if (them is RangeConstrainedBindable other) @@ -167,9 +178,6 @@ public override void BindTo(Bindable 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);