forked from ppy/osu-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BindableSize.cs
58 lines (46 loc) · 2.11 KB
/
BindableSize.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Drawing;
namespace osu.Framework.Bindables
{
/// <summary>
/// Represents a <see cref="Size"/> bindable with defined component-wise constraints applied to it.
/// </summary>
public class BindableSize : RangeConstrainedBindable<Size>
{
protected override Size DefaultMinValue => new Size(int.MinValue, int.MinValue);
protected override Size DefaultMaxValue => new Size(int.MaxValue, int.MaxValue);
public BindableSize(Size defaultValue = default)
: base(defaultValue)
{
}
public override string ToString(string? format, IFormatProvider? formatProvider) => ((FormattableString)$"{Value.Width}x{Value.Height}").ToString(formatProvider);
public override void Parse(object? input, IFormatProvider provider)
{
ArgumentNullException.ThrowIfNull(input);
switch (input)
{
case string str:
string[] split = str.Split('x');
if (split.Length != 2)
throw new ArgumentException($"Input string was in wrong format! (expected: '<width>x<height>', actual: '{str}')");
Value = new Size(int.Parse(split[0], provider), int.Parse(split[1], provider));
break;
default:
base.Parse(input, provider);
break;
}
}
protected override Bindable<Size> CreateInstance() => new BindableSize();
protected sealed override Size ClampValue(Size value, Size minValue, Size maxValue)
{
return new Size
{
Width = Math.Clamp(value.Width, minValue.Width, maxValue.Width),
Height = Math.Clamp(value.Height, minValue.Height, maxValue.Height)
};
}
protected sealed override bool IsValidRange(Size min, Size max) => min.Width <= max.Width && min.Height <= max.Height;
}
}