-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu-main.tl
346 lines (303 loc) · 11.4 KB
/
menu-main.tl
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
print('hello from top of main menu module.')
require "background"
require "cmn"
require "common"
require "love"
local inspect = require "inspect"
local g = love.graphics
global record MenuObject
draw: function(MenuObject)
enter: function(MenuObject)
keypressed: function(MenuObject, string)
keyreleased: function(MenuObject, string)
leave: function(MenuObject)
mousemoved: function(MenuObject, number, number, number, number, boolean)
mousepressed: function(MenuObject, number, number, number, boolean)
mousereleased: function(MenuObject, number, number, number, boolean)
touchmoved: function(MenuObject, id: number, x: number, y: number, dx: number, dy: number, pressure: number)
touchpressed: function(MenuObject, id: number, x: number, y: number, dx: number, dy: number, pressure: number)
touchreleased: function(MenuObject, id: number, x: number, y: number, dx: number, dy: number, pressure: number)
update: function(MenuObject, number)
end
global type MenuFunction = function()
global type Menu = record
record Item
name: string
obj: MenuObject
end
record ItemRect
x: number
y: number
w: number
h: number
end
items: {Item}
back: Background
active_item: integer
w: number
h : number
active: boolean
font: love.graphics.Font
maxWidth: number
alpha: number
canvas: love.graphics.Canvas
y_pos: number
items_rects: {ItemRect}
--drawCursor: function(Menu)
addItem: function(Menu, name: string, object: MenuObject|MenuFunction)
clear: function(Menu)
compute_rects: function(Menu)
cursorDraw: function(Menu)
draw: function(Menu)
drawList: function(Menu)
goBack: function(Menu)
init: function(Menu)
keypressed: function(Menu, string)
keyreleased: function(Menu, string)
mousemoved: function(Menu, number, number, number, number, boolean)
mousepressed: function(Menu, number, number, number, boolean)
mousereleased: function(Menu, number, number, number, boolean)
moveDown: function(Menu)
moveUp: function(Menu)
process_menu_selection: function(Menu, number, number, number, number, boolean)
resize: function(Menu, number, number)
searchWidestText: function(Menu)
touchmoved: function(Menu, id: number, x: number, y: number, dx: number, dy: number, pressure: number)
touchpressed: function(Menu, id: number, x: number, y: number, dx: number, dy: number, pressure: number)
touchreleased: function(Menu, id: number, x: number, y: number, dx: number, dy: number, pressure: number)
update: function(Menu, number)
end
local Menu_mt: metatable<Menu> = {
__index = Menu
}
function Menu.new(): Menu
local self: Menu = {
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: number, newh: number)
self.w = neww
self.h = newh
self:compute_rects()
self.back:resize(neww, newh)
self.canvas = g.newCanvas(neww, newh, {msaa = 4})
--print(string.format("menu:resize() %d*%d -> %d*%d!", w, h, neww, newh))
end
-- как лучше хранить актиный элемент из списка меню? По индексу?
-- Важен порядок элементов. Значит добавлять в массив таблички по индексу.
-- TODO перенести в конструктор
function Menu:init()
self:resize(g.getDimensions())
self.alpha = 1
end
-- ищет наиболее длинный текст в списке пунктов меню и устанавливает
-- внутреннюю переменную menu.maxWidth по найденному значению(в знаках).
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
-- добавить следующий пункт с заголовком name и обработчиком object.
-- object - таблица с методами update, draw, keypressed и другими.
-- Или object может быть функцией(пока не реализовано).
function Menu:addItem(name: string, object: MenuObject)
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 as MenuObject}
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: string)
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: string)
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 as function)()
else
self.active = true
if obj.enter then obj:enter() end
end
end
end
end
function Menu:update(dt: number)
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: number, y: number, _: number, _: number, _: boolean)
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: number, y: number, dx: number, dy: number, istouch: boolean)
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: number, y: number, btn: number, istouch: boolean)
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: number, y: number, button: number, istouch: boolean)
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 as function)() end
end
end
end
-- печать вертикального списка меню
function Menu:drawList()
local y = self.y_pos
g.setFont(self.font)
--print('Menu:drawList()', inspect(self.items))
--os.exit()
for i, k in ipairs(self.items) do
--local q = self.active_item == i and pallete.active or pallete.inactive
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
--print("Menu:drawList() self", inspect(self))
end
function Menu:draw()
--print("Menu:draw() self", inspect(self))
g.clear(0, 0, 0, 0)
if self.active then
local obj = self.items[self.active_item].obj
if obj.draw then
--g.setCanvas{self.canvas, stencil = true}
--g.setColor{1, 1, 1, 1}
--g.clear{1, 1, 1, 1}
obj:draw()
--g.setCanvas()
--g.setColor{1, 1, 1, 0.2}
--g.clear(pallete.background)
--g.setColor{1, 1, 1, 1}
--g.draw(self.canvas)
end
else
g.push("all")
self.back:draw()
self:drawList()
--self:drawCursor()
self:cursorDraw()
g.pop()
end
end
--function Menu:drawCursor()
function Menu:cursorDraw()
--print("Menu:drawCursor() self", self.items_rects[self.active_item])
--print(debug.traceback())
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: number, x: number, y: number, dx: number, dy: number, pressure: number)
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: number, x: number, y: number, dx: number, dy: number, pressure: number)
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: number, x: number, y: number, dx: number, dy: number, pressure: number)
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
global mainMenu: Menu = Menu.new()
print('hello from bottom of main menu module.')