-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
286 lines (247 loc) · 7.64 KB
/
init.lua
File metadata and controls
286 lines (247 loc) · 7.64 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
local min, max = math.min, math.max
---The rectangle class that implements the algorithm.
---Can be called to instantiate a Rect.
---@class Rect
---@field public minX number
---@field public minY number
---@field public maxX number
---@field public maxY number
---@overload fun(minX: number, minY: number, maxX: number, maxY: number): Rect
---@overload fun(r: {minX: number, minY: number, maxX: number, maxY: number}): Rect
local Rect = { _version = "2.0" }
Rect.__index = Rect
---Creates a new Rect. Equivalent to calling the class: `Rect(...)`.
---@param minX number
---@param minY number
---@param maxX number
---@param maxY number
---@return Rect
---@overload fun(r: {minX: number, minY: number, maxX: number, maxY: number}): Rect
function Rect.new(minX, minY, maxX, maxY)
if type(minX) == "table" then
minX, minY, maxX, maxY = minX.minX, minX.minY, minX.maxX, minY.maxY
end
return setmetatable({
minX = minX,
minY = minY,
maxX = maxX,
maxY = maxY,
}, Rect --[[@as table]])
end
---Creates a new Rect from XYWH parameters.
---@param x number
---@param y number
---@param width number
---@param height number
---@return Rect
---@overload fun(r: {x: number, y: number, width: number, height: number}): Rect
function Rect.fromXYWH(x, y, width, height)
if type(x) == "table" then
x, y, width, height = x.x, x.y, x.width, x.height
end
return setmetatable({
minX = x,
minY = y,
maxX = x + width,
maxY = y + height,
}, Rect --[[@as table]])
end
---Returns all components of this Rect.
---@return number minX, number minY, number maxX, number maxY
function Rect:unpack() return self.minX, self.minY, self.maxX, self.maxY end
---Returns a copy of this Rect.
---@return Rect
function Rect:copy() return Rect.new(self:unpack()) end
---Returns the components of this Rect, in XYWH format.
---@return number x, number y, number width, number height
function Rect:xywh()
return self.minX, self.minY,
self.minX + (self.maxX - self.minX),
self.minY + (self.maxY - self.minY)
end
---A variant of the `get_*` methods that takes in the side to get from.
---@param side "left"|"right"|"top"|"bottom"
---@param a number
---@return Rect
function Rect:get(side, a)
local f = self["get_" .. side]
assert(f, "rectcut: no such side '" .. side .. "'")
return f(self, a)
end
---Same as `cut_left`, except it keeps the Rect intact.
---@param a number
---@return Rect
function Rect:get_left(a)
return Rect.new(
self.minX, self.minY,
min(self.maxX, self.minX + a), self.maxY
)
end
---Alias for `get_left`.
---@param a number
---@return Rect
---@see Rect.get_left
function Rect:getLeft(a) return Rect:get_left(a) end
---Same as `cut_right`, expect it keeps the Rect intact.
---@param a number
---@return Rect
function Rect:get_right(a)
return Rect.new(
max(self.minX, self.maxX - a), self.minY,
self.maxX, self.maxY
)
end
---Alias for `get_right`.
---@param a number
---@return Rect
---@see Rect.get_right
function Rect:getRight(a) return Rect:get_right(a) end
---Same as `cut_top`, expect it keeps the Rect intact.
---@param a number
---@return Rect
function Rect:get_top(a)
return Rect.new(
self.minX, self.minY,
self.maxX, min(self.maxY, self.minY + a)
)
end
---Alias for `get_top`.
---@param a number
---@return Rect
---@see Rect.get_top
function Rect:getTop(a) return Rect:get_top(a) end
---Same as `cut_bottom`, expect it keeps the Rect intact.
---@param a number
---@return Rect
function Rect:get_bottom(a)
return Rect.new(
self.minX, max(self.minY, self.maxY - a),
self.maxX, self.maxY
)
end
---Alias for `get_bottom`.
---@param a number
---@return Rect
---@see Rect.get_bottom
function Rect:getBottom(a) return Rect:get_bottom(a) end
---A variant of the `cut_*` methods that takes in the side to cut from.
---@param side "left"|"right"|"top"|"bottom"
---@param a number
---@return Rect
function Rect:cut(side, a)
local f = self["cut_" .. side]
assert(f, "rectcut: no such side '" .. side .. "'")
return f(self, a)
end
---Cuts `a` from the left side of the Rect, and returns the cut part.
---@param a number
---@return Rect
function Rect:cut_left(a)
local left = self:get_left(a)
self.minX = left.maxX
return left
end
---Alias for `cut_left`.
---@param a number
---@return Rect
---@see Rect.cut_left
function Rect:cutLeft(a) return Rect:cut_left(a) end
---Cuts `a` from the right side of the Rect, and returns the cut part.
---@param a number
---@return Rect
function Rect:cut_right(a)
local right = self:get_right(a)
self.maxX = right.minX
return right
end
---Alias for `cut_right`.
---@param a number
---@return Rect
---@see Rect.cut_right
function Rect:cutRight(a) return Rect:cut_right(a) end
---Cuts `a` from the top side of the Rect, and returns the cut part.
---@param a number
---@return Rect
function Rect:cut_top(a)
local top = self:get_top(a)
self.minY = top.maxY
return top
end
---Alias for `cut_top`.
---@param a number
---@return Rect
---@see Rect.cut_top
function Rect:cutTop(a) return Rect:cut_top(a) end
---Cuts `a` from the bottom side of the Rect, and returns the cut part.
---@param a number
---@return Rect
function Rect:cut_bottom(a)
local bottom = self:get_bottom(a)
self.maxY = bottom.minY
return bottom
end
---Alias for `cut_bottom`.
---@param a number
---@return Rect
---@see Rect.cut_bottom
function Rect:cutBottom(a) return Rect:cut_bottom(a) end
---Returns a copy of this rect, with `a` added to its left side.
---@param a number
---@return Rect
function Rect:add_left(a) return Rect.new(self.minX - a, self.minY, self.maxX, self.maxY) end
---Alias for `add_left`.
---@param a number
---@return Rect
---@see Rect.add_left
function Rect:addLeft(a) return Rect:add_left(a) end
---Returns a copy of this rect, with `a` added to its right side.
---@param a number
---@return Rect
function Rect:add_right(a) return Rect.new(self.minX, self.minY, self.maxX + a, self.maxY) end
---Alias for `add_right`.
---@param a number
---@return Rect
---@see Rect.add_right
function Rect:addRight(a) return Rect:add_right(a) end
---Returns a copy of this rect, with `a` added to its top side.
---@param a number
---@return Rect
function Rect:add_top(a) return Rect.new(self.minX, self.minY - a, self.maxX, self.maxY) end
---Alias for `add_top`.
---@param a number
---@return Rect
---@see Rect.add_top
function Rect:addTop(a) return Rect:add_top(a) end
---Returns a copy of this rect, with `a` added to its bottom side.
---@param a number
---@return Rect
function Rect:add_bottom(a) return Rect.new(self.minX, self.minY, self.maxX, self.maxY + a) end
---Alias for `add_bottom`.
---@param a number
---@return Rect
---@see Rect.add_bottom
function Rect:addBottom(a) return Rect:add_bottom(a) end
---A variant of the `add_*` methods that takes in the side to add from.
---@param side "left"|"right"|"top"|"bottom"
---@param a number
---@return Rect
function Rect:add(side, a)
local f = self["add_" .. side]
assert(f, "rectcut: no such side '" .. side .. "'")
return f(self, a)
end
---Returns an extended version of this Rect.
---@param a number
---@return Rect
function Rect:extend(a) return Rect.new(self.minX - a, self.minY - a, self.maxX + a, self.maxY + a) end
---Returns a contracted version of this Rect.
---@param a number
---@return Rect
function Rect:contract(a) return Rect.new(self.minX + a, self.minY + a, self.maxX - a, self.maxY - a) end
---@return string
function Rect:__tostring()
return string.format("Rect: min(%g,%g), max(%g,%g)", self:unpack())
end
return setmetatable(Rect --[[@as table]], {
__call = function(cls, ...) return cls.new(...) end,
}) --[[@as Rect]]