forked from ppy/osu-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BindableMarginPadding.cs
68 lines (57 loc) · 2.61 KB
/
BindableMarginPadding.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
59
60
61
62
63
64
65
66
67
68
// 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.Globalization;
using osu.Framework.Graphics;
namespace osu.Framework.Bindables
{
public class BindableMarginPadding : RangeConstrainedBindable<MarginPadding>
{
protected override MarginPadding DefaultMinValue => new MarginPadding(float.MinValue);
protected override MarginPadding DefaultMaxValue => new MarginPadding(float.MaxValue);
public BindableMarginPadding(MarginPadding defaultValue = default)
: base(defaultValue)
{
}
public override void Parse(object? input, IFormatProvider provider)
{
ArgumentNullException.ThrowIfNull(input);
switch (input)
{
case string str:
string[] split = str.Trim("() ".ToCharArray()).Split(',');
if (split.Length != 4)
throw new ArgumentException($"Input string was in wrong format! (expected: '(<top>, <left>, <bottom>, <right>)', actual: '{str}')");
Value = new MarginPadding
{
Top = float.Parse(split[0], CultureInfo.InvariantCulture),
Left = float.Parse(split[1], CultureInfo.InvariantCulture),
Bottom = float.Parse(split[2], CultureInfo.InvariantCulture),
Right = float.Parse(split[3], CultureInfo.InvariantCulture),
};
break;
default:
base.Parse(input, provider);
break;
}
}
protected sealed override MarginPadding ClampValue(MarginPadding value, MarginPadding minValue, MarginPadding maxValue)
{
return new MarginPadding
{
Top = Math.Clamp(value.Top, minValue.Top, maxValue.Top),
Left = Math.Clamp(value.Left, minValue.Left, maxValue.Left),
Bottom = Math.Clamp(value.Bottom, minValue.Bottom, maxValue.Bottom),
Right = Math.Clamp(value.Right, minValue.Right, maxValue.Right)
};
}
protected sealed override bool IsValidRange(MarginPadding min, MarginPadding max)
{
return min.Top <= max.Top &&
min.Left <= max.Left &&
min.Bottom <= max.Bottom &&
min.Right <= max.Right;
}
protected override Bindable<MarginPadding> CreateInstance() => new BindableMarginPadding();
}
}