-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.lua
115 lines (102 loc) · 2.43 KB
/
menu.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
--[[
Squaricles
Copyright (C) 2017 ITR
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
]]--
Menu = {
elements = {},
maxWidth = 0.,
spacing = 4,
selectedOption = 1
}
function Menu:new(canvas, elements)
local o = clone(self)
o.elements = elements or o.elements
o.canvas = canvas
for i = 1,#o.elements do
local w = font:getWidth(o.elements[i])
if w > o.maxWidth then
o.maxWidth = w
end
end
return o
end
function Menu:draw()
local w, h = self.canvas:getDimensions()
local dy = #self.elements*(font:getHeight()+self.spacing)
local y = h/2 - dy/2
love.graphics.setColor(0.247,0.247,0.247,1)
love.graphics.rectangle(
"fill",
w/2 - self.maxWidth/2 - 16,
h/2 - dy/2 - 8,
self.maxWidth + 32,
dy + 16,
20,
20
)
love.graphics.setLineWidth(4)
love.graphics.setColor(1,1,1,1)
for i = 1,#self.elements do
local dx, dy = font:getWidth(self.elements[i]), font:getHeight()
love.graphics.print(
self.elements[i],
w/2, y,
0,
1, 1,
dx/2, 0
)
if i == self.selectedOption then
love.graphics.rectangle(
"line",
w/2 - dx/2 - 8,
y + self.spacing/2,
dx + 16, dy+self.spacing,
2, 2
)
end
y = y + font:getHeight() + self.spacing
end
love.graphics.setLineWidth(1)
end
function Menu:run(input)
local run = false
local offset = 0
local stop = false
local actions = {
[LEFT ] = function() end,
[RIGHT] = function() end,
[UP ] = function()
offset = offset - 1
end,
[DOWN ] = function()
offset = offset + 1
end,
[A ] = function()
run = true
end,
[B ] = function()
stop = true
end,
[START] = function()
run = true
end,
}
input:useInput(function(key) actions[key]() end)
self.selectedOption = (self.selectedOption+offset-1)%#self.elements + 1
if run then
return self.selectedOption
elseif stop then
return #self.elements
end
return 0
end