-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
151 lines (129 loc) · 3.39 KB
/
utils.lua
File metadata and controls
151 lines (129 loc) · 3.39 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
local currentPath = (...):match('(.-)[^%./]+$')
--- @class IUILib
local iui = require(currentPath .. "iui")
--- @class IUIUtils
local utils = {}
--- Returns 1 if `n` is positive, -1 if negative, and 0 if zero.
--- @param n number
--- @return number
function utils.sign(n)
if n > 0 then
return 1
elseif n == 0 then
return 0
else
return -1
end
end
--- Returns `n` rounded to the nearest integer, with 0.5 rounding up.
--- @param n number
--- @return number
function utils.round(n)
return math.floor(n + 0.5)
end
--- Returns the closest value to `n` that is neither below `low` nor above
--- `high`.
--- @param n number
--- @param low number
--- @param high number
--- @return number
function utils.clamp(n, low, high)
if n < low then
return low
elseif n > high then
return high
end
return n
end
--- @param rx number
--- @param ry number
--- @param rw number
--- @param rh number
--- @param px number
--- @param py number
--- @return boolean isInside
function utils.rectContains(rx, ry, rw, rh, px, py)
if px < rx or py < ry then
return false
end
if px >= rx + rw or py >= ry + rh then
return false
end
return true
end
--- @param iw number
--- @param ih number
--- @param x number
--- @param y number
--- @param w number
--- @param h number
--- @return number x, number y, number w, number h
function utils.aspectFit(iw, ih, x, y, w, h)
local aspectBounds = w / h
local aspectImage = iw / ih
local ox, oy, ow, oh = x, y, w, h
if aspectImage > aspectBounds then
oh = iui.utils.round(w / aspectImage)
oy = y + iui.utils.round((h - oh) / 2)
else
ow = iui.utils.round(h * aspectImage)
ox = x + iui.utils.round((w - ow) / 2)
end
return ox, oy, ow, oh
end
--- @param iw number
--- @param ih number
--- @param x number
--- @param y number
--- @param w number
--- @param h number
--- @return number x, number y, number w, number h
function utils.aspectFill(iw, ih, x, y, w, h)
local aspectBounds = w / h
local aspectImage = iw / ih
local ox, oy, ow, oh = x, y, w, h
if aspectImage < aspectBounds then
oh = iui.utils.round(ow / aspectImage)
oy = y + iui.utils.round((h - oh) / 2)
else
ow = iui.utils.round(oh * aspectImage)
ox = x + iui.utils.round((w - ow) / 2)
end
return ox, oy, ow, oh
end
--- @param iw number
--- @param ih number
--- @param x number
--- @param y number
--- @param w number
--- @param h number
--- @return number x, number y, number w, number h
function utils.center(iw, ih, x, y, w, h)
local ox, oy, ow, oh = x, y, w, h
ow, oh = iw, ih
ox = x + iui.utils.round((w - ow) / 2)
oy = y + iui.utils.round((h - oh) / 2)
return ox, oy, ow, oh
end
--- @param mode IUIImageMode
--- @param iw number
--- @param ih number
--- @param x number
--- @param y number
--- @param w number
--- @param h number
--- @return number x, number y, number w, number h
function utils.fill(mode, iw, ih, x, y, w, h)
if mode == "fill" then
return x, y, w, h
elseif mode == "aspectFit" then
return iui.utils.aspectFit(iw, ih, x, y, w, h)
elseif mode == "aspectFill" then
return iui.utils.aspectFill(iw, ih, x, y, w, h)
elseif mode == "center" then
return iui.utils.center(iw, ih, x, y, w, h)
else
error("unrecognized fillmode")
end
end
iui.utils = utils