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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
using System;
using Microsoft.Xna.Framework;
namespace Celesteia.UI {
public struct Rect {
public static Rect AbsoluteZero = new Rect(AbsoluteUnit.WithValue(0f));
public static Rect AbsoluteOne = new Rect(AbsoluteUnit.WithValue(1f));
public static Rect ScreenFull = new Rect(
new ScreenSpaceUnit(0f, ScreenSpaceUnit.ScreenSpaceOrientation.Horizontal),
new ScreenSpaceUnit(0f, ScreenSpaceUnit.ScreenSpaceOrientation.Vertical),
new ScreenSpaceUnit(1f, ScreenSpaceUnit.ScreenSpaceOrientation.Horizontal),
new ScreenSpaceUnit(1f, ScreenSpaceUnit.ScreenSpaceOrientation.Vertical)
);
public static Rect RelativeFull(Rect parent) => new Rect(
new RelativeUnit(0f, parent, RelativeUnit.Orientation.Horizontal),
new RelativeUnit(0f, parent, RelativeUnit.Orientation.Vertical),
new RelativeUnit(1f, parent, RelativeUnit.Orientation.Horizontal),
new RelativeUnit(1f, parent, RelativeUnit.Orientation.Vertical)
);
public IInterfaceUnit X;
public IInterfaceUnit Y;
public IInterfaceUnit Width;
public IInterfaceUnit Height;
public Rect(IInterfaceUnit uniform) : this (uniform, uniform, uniform, uniform) { }
public Rect(IInterfaceUnit x, IInterfaceUnit y, IInterfaceUnit width, IInterfaceUnit height) {
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
}
public Rect SetX(IInterfaceUnit x) {
X = x;
return this;
}
public Rect SetY(IInterfaceUnit y) {
Y = y;
return this;
}
public Rect SetWidth(IInterfaceUnit w) {
Width = w;
return this;
}
public Rect SetHeight(IInterfaceUnit h) {
Height = h;
return this;
}
public float[] Resolve() {
return new float[] { X.Resolve(), Y.Resolve(), Width.Resolve(), Height.Resolve() };
}
public bool Contains(Point point) {
return (
point.X >= this.X.Resolve() &&
point.Y >= this.Y.Resolve() &&
point.X <= this.X.Resolve() + this.Width.Resolve() &&
point.Y <= this.Y.Resolve() + this.Height.Resolve()
);
}
public bool Contains(int x, int y) {
return Contains(new Point(x, y));
}
public override string ToString() {
return $"{X.Resolve().ToString("0.00")} {Y.Resolve().ToString("0.00")} {Width.Resolve().ToString("0.00")} {Height.Resolve().ToString("0.00")}";
}
public Rectangle ResolveRectangle()
{
float[] resolved = this.Resolve();
return new Rectangle(
(int) MathF.Floor(resolved[0]),
(int) MathF.Floor(resolved[1]),
(int) MathF.Floor(resolved[2]),
(int) MathF.Floor(resolved[3])
);
}
}
public interface IInterfaceUnit {
public float Resolve();
public void SetValue(float value);
}
public struct AbsoluteUnit : IInterfaceUnit
{
public float value { get; private set; }
public AbsoluteUnit(float value) {
this.value = value;
}
public static AbsoluteUnit WithValue(float value) {
return new AbsoluteUnit(value);
}
public void SetValue(float value) {
this.value = value;
}
public float Resolve()
{
return value;
}
}
public struct ScreenSpaceUnit : IInterfaceUnit
{
public float value { get; private set; }
public ScreenSpaceOrientation orientation { get; private set; }
public ScreenSpaceUnit(float value, ScreenSpaceOrientation orientation) {
this.value = value;
this.orientation = orientation;
}
public void SetValue(float value) {
this.value = value;
}
public void SetOrientation(ScreenSpaceOrientation orientation) {
this.orientation = orientation;
}
public float Resolve()
{
if (UIReferences.gameWindow != null) {
switch (orientation) {
case ScreenSpaceOrientation.Horizontal:
return value * UIReferences.gameWindow.ClientBounds.Width;
case ScreenSpaceOrientation.Vertical:
return value * UIReferences.gameWindow.ClientBounds.Height;
}
}
return 0f;
}
public enum ScreenSpaceOrientation {
Horizontal, Vertical
}
}
public struct RelativeUnit : IInterfaceUnit
{
public float value { get; private set; }
public Rect parent { get; private set; }
public Orientation orientation { get; private set; }
public RelativeUnit(float value, Rect parent, Orientation orientation) {
this.value = value;
this.parent = parent;
this.orientation = orientation;
}
public void SetValue(float value) {
this.value = value;
}
public void SetOrientation(Orientation orientation) {
this.orientation = orientation;
}
public float Resolve()
{
switch (orientation) {
case Orientation.Horizontal:
return value * parent.Resolve()[2];
case Orientation.Vertical:
return value * parent.Resolve()[3];
default:
return 0f;
}
}
public enum Orientation {
Horizontal, Vertical
}
}
}
|