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;
5using System.Globalization;
6using osu.Framework.Graphics;
7
8namespace osu.Framework.Bindables
9{
10 public class BindableMarginPadding : RangeConstrainedBindable<MarginPadding>
11 {
12 protected override MarginPadding DefaultMinValue => new MarginPadding(float.MinValue);
13 protected override MarginPadding DefaultMaxValue => new MarginPadding(float.MaxValue);
14
15 public BindableMarginPadding(MarginPadding defaultValue = default)
16 : base(defaultValue)
17 {
18 }
19
20 public override string ToString() => Value.ToString();
21
22 public override void Parse(object input)
23 {
24 switch (input)
25 {
26 case string str:
27 string[] split = str.Trim("() ".ToCharArray()).Split(',');
28
29 if (split.Length != 4)
30 throw new ArgumentException($"Input string was in wrong format! (expected: '(<top>, <left>, <bottom>, <right>)', actual: '{str}')");
31
32 Value = new MarginPadding
33 {
34 Top = float.Parse(split[0], CultureInfo.InvariantCulture),
35 Left = float.Parse(split[1], CultureInfo.InvariantCulture),
36 Bottom = float.Parse(split[2], CultureInfo.InvariantCulture),
37 Right = float.Parse(split[3], CultureInfo.InvariantCulture),
38 };
39 break;
40
41 default:
42 base.Parse(input);
43 break;
44 }
45 }
46
47 protected sealed override MarginPadding ClampValue(MarginPadding value, MarginPadding minValue, MarginPadding maxValue)
48 {
49 return new MarginPadding
50 {
51 Top = Math.Clamp(value.Top, minValue.Top, maxValue.Top),
52 Left = Math.Clamp(value.Left, minValue.Left, maxValue.Left),
53 Bottom = Math.Clamp(value.Bottom, minValue.Bottom, maxValue.Bottom),
54 Right = Math.Clamp(value.Right, minValue.Right, maxValue.Right)
55 };
56 }
57
58 protected sealed override bool IsValidRange(MarginPadding min, MarginPadding max)
59 {
60 return min.Top <= max.Top &&
61 min.Left <= max.Left &&
62 min.Bottom <= max.Bottom &&
63 min.Right <= max.Right;
64 }
65
66 protected override Bindable<MarginPadding> CreateInstance() => new BindableMarginPadding();
67 }
68}