-
Notifications
You must be signed in to change notification settings - Fork 0
/
RectExtension.cs
205 lines (168 loc) · 5.49 KB
/
RectExtension.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using UnityEngine;
namespace Aya.Extension
{
public static partial class RectExtension
{
#region Deconstruct
public static void Deconstruct(this Rect rect, out float x, out float y, out float width, out float height)
{
x = rect.x;
y = rect.y;
width = rect.width;
height = rect.height;
}
public static void Deconstruct(this Rect rect, out Vector2 position, out Vector2 size)
{
position = rect.position;
size = rect.size;
}
#endregion
#region With
public static Rect With(this Rect rect, float? x = null, float? y = null, float? width = null, float? height = null)
{
var result = new Rect(x ?? rect.x, y ?? rect.y, width ?? rect.width, height ?? rect.height);
return result;
}
public static Rect With(this Rect rect, Vector2? position = null, Vector2? size = null)
{
var result = new Rect(position ?? rect.position, size ?? rect.size);
return result;
}
#endregion
#region Width / Height / Size
public static Rect SetWidth(this ref Rect rect, float width)
{
rect.width = width;
return rect;
}
public static Rect SetHeight(this ref Rect rect, float height)
{
rect.height = height;
return rect;
}
public static Rect SetSize(this ref Rect rect, float width, float height)
{
rect.width = width;
rect.height = height;
return rect;
}
public static Rect SetSize(this ref Rect rect, Vector2 size)
{
rect.size = size;
return rect;
}
#endregion
#region Center
public static Rect SetCenterX(this ref Rect rect, float x)
{
rect.center = new Vector2(x, rect.center.y);
return rect;
}
public static Rect SetCenterY(this ref Rect rect, float y)
{
rect.center = new Vector2(rect.center.x, y);
return rect;
}
public static Rect SetCenter(this ref Rect rect, float x, float y)
{
rect.center = new Vector2(x, y);
return rect;
}
public static Rect SetCenter(this ref Rect rect, Vector2 center)
{
rect.center = center;
return rect;
}
#endregion
#region X / Y / Position
public static Rect SetX(this ref Rect rect, float x)
{
rect.x = x;
return rect;
}
public static Rect SetY(this ref Rect rect, float y)
{
rect.y = y;
return rect;
}
public static Rect SetPosition(this ref Rect rect, Vector2 position)
{
rect.position = position;
return rect;
}
#endregion
#region Expand / Reduce
public static Rect Expand(this Rect rect, float expandSize)
{
rect.x -= expandSize;
rect.y -= expandSize;
rect.width += expandSize * 2f;
rect.height += expandSize * 2f;
return rect;
}
public static Rect Expand(this Rect rect, Rect expandRect)
{
rect.x -= expandRect.x;
rect.y -= expandRect.y;
rect.width += expandRect.width;
rect.height += expandRect.height;
return rect;
}
public static Rect Reduce(this Rect rect, float reduceSize)
{
rect.x += reduceSize;
rect.y += reduceSize;
rect.width -= reduceSize * 2f;
rect.height -= reduceSize * 2f;
return rect;
}
public static Rect Reduce(this Rect rect, Rect reduceRect)
{
rect.x += reduceRect.x;
rect.y += reduceRect.y;
rect.width -= reduceRect.width;
rect.height -= reduceRect.height;
return rect;
}
#endregion
#region Split
public static Rect SplitHorizontal(this Rect rect, int index, int count)
{
var num = rect.width / (float)count;
rect.width = num;
rect.x += num * (float)index;
return rect;
}
public static Rect SplitVertical(this Rect rect, int index, int count)
{
var num = rect.height / (float)count;
rect.height = num;
rect.y += num * (float)index;
return rect;
}
public static Rect SplitGrid(this Rect rect, float width, float height, int index)
{
var num1 = (int)((double)rect.width / (double)width);
var num2 = num1 > 0 ? num1 : 1;
var num3 = index % num2;
var num4 = index / num2;
rect.x += (float)num3 * width;
rect.y += (float)num4 * height;
rect.width = width;
rect.height = height;
return rect;
}
public static Rect SplitTableGrid(this Rect rect, int columnCount, float rowHeight, int index)
{
var num1 = index % columnCount;
var num2 = index / columnCount;
var num3 = rect.width / (float)columnCount;
rect.x += (float)num1 * num3;
rect.y += (float)num2 * rowHeight;
rect.width = num3;
rect.height = rowHeight;
return rect;
}
#endregion
}
}