A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5
6namespace osu.Framework.Bindables
7{
8 /// <summary>
9 /// A bindable which holds a reference to a bound target, allowing switching between targets and handling unbind/rebind.
10 /// </summary>
11 /// <typeparam name="T">The type of our stored <see cref="Bindable{T}.Value"/>.</typeparam>
12 public class BindableNumberWithCurrent<T> : BindableNumber<T>, IBindableWithCurrent<T>
13 where T : struct, IComparable<T>, IConvertible, IEquatable<T>
14 {
15 private BindableNumber<T> currentBound;
16
17 public Bindable<T> Current
18 {
19 get => this;
20 set
21 {
22 if (value == null)
23 throw new ArgumentNullException(nameof(value));
24
25 if (currentBound != null) UnbindFrom(currentBound);
26 BindTo(currentBound = (BindableNumber<T>)value);
27 }
28 }
29
30 public BindableNumberWithCurrent(T defaultValue = default)
31 : base(defaultValue)
32 {
33 }
34
35 protected override Bindable<T> CreateInstance() => new BindableNumberWithCurrent<T>();
36 }
37}