-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu-main.lua
346 lines (211 loc) · 6.47 KB
/
menu-main.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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 ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local string = _tl_compat and _tl_compat.string or string; print('hello from top of main menu module.')
require("background")
require("cmn")
require("common")
require("love")
local inspect = require("inspect")
local g = love.graphics
MenuObject = {}
MenuFunction = {}
Menu = {Item = {}, ItemRect = {}, }
local Menu_mt = {
__index = Menu,
}
function Menu.new()
local self = {
items = {},
active_item = 1,
active = false,
font = require("fonts").menu,
back = require("background").new(),
}
return setmetatable(self, Menu_mt)
end
function Menu:goBack()
local obj = self.items[self.active_item].obj
if obj.leave then obj:leave() end
self.active = false
end
function Menu:compute_rects()
self.y_pos = (self.h - #self.items * self.font:getHeight()) / 2
self.items_rects = {}
local y = self.y_pos
local rect_width = self.maxWidth
for _, _ in ipairs(self.items) do
self.items_rects[#self.items_rects + 1] = {
x = (self.w - rect_width) / 2,
y = y,
w = rect_width,
h = self.font:getHeight(),
}
y = y + self.font:getHeight()
end
end
function Menu:resize(neww, newh)
self.w = neww
self.h = newh
self:compute_rects()
self.back:resize(neww, newh)
self.canvas = g.newCanvas(neww, newh, { msaa = 4 })
end
function Menu:init()
self:resize(g.getDimensions())
self.alpha = 1
end
function Menu:searchWidestText()
local maxWidth = 0.
for _, v in ipairs(self.items) do
local w = self.font:getWidth(v.name)
if w > maxWidth then maxWidth = w end
end
self.maxWidth = maxWidth
end
function Menu:clear()
self.items = {}
self:searchWidestText()
self:resize(g.getDimensions())
end
function Menu:addItem(name, object)
print("menu:addItem()", name, inspect(object))
assert(type(name) == "string", string.format('Type should be string, not %s', type(name)))
self.items[#self.items + 1] = { name = name, obj = object }
self:searchWidestText()
self:resize(g.getDimensions())
end
function Menu:moveDown()
if self.active_item + 1 <= #self.items then
self.active_item = self.active_item + 1
else
self.active_item = 1
end
end
function Menu:moveUp()
if self.active_item - 1 >= 1 then
self.active_item = self.active_item - 1
else
self.active_item = #self.items
end
end
function Menu:keyreleased(key)
if self.active then
local obj = self.items[self.active_item].obj
if obj.keyreleased then obj:keyreleased(key) end
end
end
function Menu:keypressed(key)
if self.active then
local obj = self.items[self.active_item].obj
assert(obj)
if obj.keypressed then obj:keypressed(key) end
else
if key == "up" or key == "k" then self:moveUp()
elseif key == "down" or key == "j" then self:moveDown()
elseif key == "escape" then love.event.quit()
elseif key == "return" or key == "space" then
local obj = self.items[self.active_item].obj
if type(obj) == "function" then
(obj)()
else
self.active = true
if obj.enter then obj:enter() end
end
end
end
end
function Menu:update(dt)
if self.active then
local obj = self.items[self.active_item].obj
if obj.update then obj:update(dt) end
else
self.back:update(dt)
end
end
function Menu:process_menu_selection(x, y, _, _, _)
for k, v in ipairs(self.items_rects) do
if pointInRect(x, y, v.x, v.y, v.w, v.h) then
self.active_item = k
end
end
end
function Menu:mousemoved(x, y, dx, dy, istouch)
if self.active then
local obj = self.items[self.active_item].obj
if obj.mousemoved then obj:mousemoved(x, y, dx, dy, istouch) end
else
self:process_menu_selection(x, y, dx, dy, istouch)
end
end
function Menu:mousereleased(x, y, btn, istouch)
if self.active then
local obj = self.items[self.active_item].obj
if obj.mousereleased then obj:mousereleased(x, y, btn, istouch) end
end
end
function Menu:mousepressed(x, y, button, istouch)
if self.active then
local obj = self.items[self.active_item].obj
assert(obj)
if obj.mousepressed then obj:mousepressed(x, y, button, istouch) end
else
local active_rect = self.items_rects[self.active_item]
if button == 1 and active_rect and pointInRect(x, y, active_rect.x,
active_rect.y, active_rect.w, active_rect.h) then
self.active = true
local obj = self.items[self.active_item].obj
if type(obj) == "table" and obj.enter then obj:enter()
elseif type(obj) == "function" then (obj)() end
end
end
end
function Menu:drawList()
local y = self.y_pos
g.setFont(self.font)
for i, k in ipairs(self.items) do
local q = self.active_item == i and { 1., 1., 1., 1. } or { 0., 0., 0., 1. }
q[4] = self.alpha
g.setColor(q)
g.printf(k.name, 0, y, self.w, "center")
y = y + self.font:getHeight()
end
end
function Menu:draw()
g.clear(0, 0, 0, 0)
if self.active then
local obj = self.items[self.active_item].obj
if obj.draw then
obj:draw()
end
else
g.push("all")
self.back:draw()
self:drawList()
self:cursorDraw()
g.pop()
end
end
function Menu:cursorDraw()
local v = self.items_rects[self.active_item]
g.setLineWidth(3)
g.setColor({ 1, 0, 0 })
g.rectangle("line", v.x, v.y, v.w, v.h)
end
function Menu:touchpressed(id, x, y, dx, dy, pressure)
if self.active then
local obj = self.items[self.active_item].obj
if obj.touchpressed then obj:touchpressed(id, x, y, dx, dy, pressure) end
end
end
function Menu:touchreleased(id, x, y, dx, dy, pressure)
if self.active then
local obj = self.items[self.active_item].obj
if obj.touchreleased then obj:touchreleased(id, x, y, dx, dy, pressure) end
end
end
function Menu:touchmoved(id, x, y, dx, dy, pressure)
if self.active then
local obj = self.items[self.active_item].obj
if obj.touchmoved then obj:touchmoved(id, x, y, dx, dy, pressure) end
end
end
mainMenu = Menu.new()
print('hello from bottom of main menu module.')