-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout.lua
212 lines (131 loc) · 4.24 KB
/
layout.lua
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
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local math = _tl_compat and _tl_compat.math or math; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local _tl_table_unpack = unpack or table.unpack; require("love")
local lg = love.graphics
local inspect = require("inspect")
Layout = {}
local Layout_mt = {
__index = Layout,
}
function makeScreenTable()
return { x = 0, y = 0, w = lg.getWidth(), h = lg.getHeight() }
end
function assertHelper(tbl)
assert(tbl.x and tbl.y and tbl.w and tbl.h,
"not all fields are correct " .. inspect(tbl))
end
function checkHelper(tbl)
return (tbl.x and tbl.y and tbl.w and tbl.h) ~= nil
end
function Layout.new(x, y, w, h)
local self
if not x and not y and not w and not h then
self = makeScreenTable()
else
self = { x = x, y = y, w = w, h = h }
end
if type(x) == "table" then
assertHelper(x)
local tbl = x
self = { x = tbl.x, y = tbl.y, w = tbl.w, h = tbl.h }
end
return setmetatable(self, Layout_mt)
end
function assertVariadic(...)
local sum = 0.
for i = 1, select("#", ...) do
sum = sum + select(i, ...)
end
assert(math.abs(sum - 1) < 0.01)
end
function splith(tbl, ...)
assertVariadic(...)
assertHelper(tbl)
local subTbls = {}
local lasty = tbl.y
for i = 1, select("#", ...) do
local currenth = tbl.h * math.floor(select(i, ...))
table.insert(subTbls, { x = tbl.x, y = lasty, w = tbl.w, h = currenth })
lasty = lasty + currenth
end
return _tl_table_unpack(subTbls)
end
function assertAlign(alignMode)
assert(type(alignMode) == "string")
assert(alignMode == "left" or alignMode == "right" or
alignMode == "center")
end
function splitv(tbl, ...)
assertVariadic(...)
assertHelper(tbl)
local subTbls = {}
local lastx = tbl.x
for i = 1, select("#", ...) do
local currentw = tbl.w * math.floor(select(i, ...))
table.insert(subTbls, { x = lastx, y = tbl.y, w = currentw, h = tbl.h })
lastx = lastx + currentw
end
return _tl_table_unpack(subTbls)
end
function splitvAlign(tbl, alignMode)
assert(false, "Not yet implemented")
assertHelper(tbl)
assertAlign(alignMode)
assert(false, "Not implemented")
end
function splithByNum(tbl, piecesNum)
assertHelper(tbl)
local subTbls = {}
local prevy, h = tbl.y, math.floor(tbl.h / piecesNum)
for _ = 1, piecesNum do
table.insert(subTbls, { x = tbl.x, y = prevy, w = tbl.w, h = h })
prevy = prevy + h
end
return _tl_table_unpack(subTbls)
end
function splitvByNum(tbl, piecesNum)
assertHelper(tbl)
local subTbls = {}
local prevx, w = tbl.x, math.floor(tbl.w / piecesNum)
for _ = 1, piecesNum do
table.insert(subTbls, { x = prevx, y = tbl.y, w = w, h = tbl.h })
prevx = prevx + w
end
return _tl_table_unpack(subTbls)
end
function shrink(tbl, value)
assertHelper(tbl)
assert(type(value) == "number", string.format("number expected, but %s is", type(value)))
return { x = tbl.x + value, y = tbl.y + value,
w = tbl.w - value * 2, h = tbl.h - value * 2, }
end
function areaGrowByPixel(tbl, delta)
assertHelper(tbl)
assert(type(delta) == "number")
return { x = tbl.x + delta, y = tbl.y + delta,
w = tbl.w - delta * 2, h = tbl.h - delta * 2, }
end
function drawHelper(...)
for i = 1, select("#", ...) do
local tbl = select(i, ...)
assertHelper(tbl)
if checkHelper(tbl) then
lg.rectangle("line", tbl.x, tbl.y, tbl.w, tbl.h)
lg.rectangle("line", tbl.x + 1, tbl.y + 1, tbl.w - 1, tbl.h - 1)
elseif type(tbl) == "table" then
for _, v in pairs(tbl) do
if checkHelper(v) then
drawHelper(v)
end
end
end
end
end
function drawHierachy(rootTbl)
if checkHelper(rootTbl) then
drawHelper(rootTbl)
end
for _, v in pairs(rootTbl) do
if type(v) == "table" and checkHelper(v) then
drawHierachy(v)
end
end
end