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.Drawing;
6
7namespace osu.Framework.Bindables
8{
9 /// <summary>
10 /// Represents a <see cref="Size"/> bindable with defined component-wise constraints applied to it.
11 /// </summary>
12 public class BindableSize : RangeConstrainedBindable<Size>
13 {
14 protected override Size DefaultMinValue => new Size(int.MinValue, int.MinValue);
15 protected override Size DefaultMaxValue => new Size(int.MaxValue, int.MaxValue);
16
17 public BindableSize(Size defaultValue = default)
18 : base(defaultValue)
19 {
20 }
21
22 public override string ToString() => $"{Value.Width}x{Value.Height}";
23
24 public override void Parse(object input)
25 {
26 switch (input)
27 {
28 case string str:
29 string[] split = str.Split('x');
30
31 if (split.Length != 2)
32 throw new ArgumentException($"Input string was in wrong format! (expected: '<width>x<height>', actual: '{str}')");
33
34 Value = new Size(int.Parse(split[0]), int.Parse(split[1]));
35 break;
36
37 default:
38 base.Parse(input);
39 break;
40 }
41 }
42
43 protected override Bindable<Size> CreateInstance() => new BindableSize();
44
45 protected sealed override Size ClampValue(Size value, Size minValue, Size maxValue)
46 {
47 return new Size
48 {
49 Width = Math.Clamp(value.Width, minValue.Width, maxValue.Width),
50 Height = Math.Clamp(value.Height, minValue.Height, maxValue.Height)
51 };
52 }
53
54 protected sealed override bool IsValidRange(Size min, Size max) => min.Width <= max.Width && min.Height <= max.Height;
55 }
56}