-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmenu.lua
More file actions
175 lines (159 loc) · 5.3 KB
/
menu.lua
File metadata and controls
175 lines (159 loc) · 5.3 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
Menu = Object:extend()
function Menu:new(x, width, height, needsConfirm)
self.offset = 20
self.x = x
self.y = love.graphics.getHeight() * 0.05
self.width = width
self.height = height
self.backgroundColor = {0, 0, 0, 0.2}
self.confirmBackgroundColor = {0, 0, 0, 0.8}
self.unselectedColor = {10, 10, 10, 0.4}
self.selectedColor = {1, 1, 1, 1}
self.optionSpacing = 20
self.options = {}
self.currentSelected = 0
self.down = false
self.up = false
self.length = 0
self.confirmPopped = false
self.needsConfirm = needsConfirm
self.confirmTextSize = 40
self.confirmed = false
end
function Menu:center(x, y)
if x then
self.x = love.graphics.getWidth() / 2 - self.width / 2
end
if y then
self.y = love.graphics.getHeight() / 2 - self.height / 2
end
end
function Menu:drawBackground()
local r, g, b, a = love.graphics.getColor()
love.graphics.setColor(self.backgroundColor)
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
love.graphics.setColor(r, g, b, a)
end
-- options is a table of strings
function Menu:setOptions(options, fixHeight)
self.options = options
self.length = self:len()
if fixHeight then
self.height = self.offset + self.length * self.optionSpacing + self.offset
end
end
function Menu:drawOptions()
local r, g, b, a = love.graphics.getColor()
love.graphics.setColor(self.unselectedColor)
local x, y = self.x + self.offset, self.y + self.offset
for key, option in pairs(self.options) do
if self:isSelected(option) then
love.graphics.setColor(self.selectedColor)
love.graphics.print(option, x, y)
love.graphics.setColor(self.unselectedColor)
else
love.graphics.print(option, x, y)
end
y = y + self.optionSpacing
end
love.graphics.setColor(r, g, b, a)
end
function Menu:draw()
self:drawBackground()
self:drawOptions()
self:drawConfirmWindow()
if self.confirmPopped and self.confirmed then
self.confirmPopped = false
self.confirmed = false
end
end
function Menu:drawConfirmWindow()
if self.confirmPopped and self.currentSelected ~= 0 then
self:drawBackgroundConfirmWindow()
local request = self.options[self.currentSelected]
local myFont = love.graphics.getFont()
love.graphics.setFont(love.graphics.newFont(self.confirmTextSize))
local font = love.graphics.getFont()
local fontWidth = font:getWidth(request .. "?")
local x = (love.graphics.getWidth() - fontWidth) / 2
local y = love.graphics.getHeight() / 2 - font:getHeight(request .. "?")
love.graphics.print(request .. "?", x, y)
love.graphics.setFont(love.graphics.newFont(self.confirmTextSize / 2))
local font = love.graphics.getFont()
local fontWidth = font:getWidth(request .. "?")
local fontWidth = font:getWidth("Y / N")
local x = (love.graphics.getWidth() - fontWidth) / 2
love.graphics.print("Y / N", x, y + self.confirmTextSize * 1.3)
love.graphics.setFont(myFont)
end
end
function Menu:drawBackgroundConfirmWindow()
local windowWidth = love.graphics.getWidth()
local windowHeight = love.graphics.getHeight()
local spaceWidth = math.floor(windowWidth / 3.4)
local spaceHeight = math.floor(windowHeight / 3)
local width = windowWidth - 2 * spaceWidth
local height = windowHeight - 2 * spaceHeight
local r, g, b, a = love.graphics.getColor()
love.graphics.setColor(self.confirmBackgroundColor)
love.graphics.rectangle("fill", spaceWidth, spaceHeight, width, height)
love.graphics.setColor(r, g, b, a)
end
function Menu:isSelected(option)
if self.currentSelected == 0 then return false end
return self.options[self.currentSelected] == option
end
function Menu:updateSelection()
local i = self.currentSelected
if self.confirmPopped then
if love.keyboard.isDown('y') then
self.confirmed = true
elseif love.keyboard.isDown('n') then
self.confirmPopped = false
end
else
if self:downPressed() and i < self.length then
self.currentSelected = i + 1
elseif self:upPressed() and i > 0 then
self.currentSelected = i - 1
end
end
end
function Menu:downPressed()
local lastDown = self.down
self.down = love.keyboard.isDown('down')
return (not lastDown) and self.down
end
function Menu:upPressed()
local lastUp = self.up
self.up = love.keyboard.isDown('up')
return (not lastUp) and self.up
end
function Menu:len()
local length = 0
for key, option in pairs(self.options) do
length = length + 1
end
return length
end
function Menu:optionRequested()
if self.confirmPopped then
if self.confirmed then
return self.options[self.currentSelected]
end
else
if love.keyboard.isDown('return') and self.currentSelected ~= 0 then
self.confirmPopped = true and self.needsConfirm
end
end
return nil
end
function Menu:updateOnResize(x, y)
self.x = x
self.y = y
end
function Menu:getMenuEnd()
local x = self.x + self.offset / 2
local y = self.y + self.length * self.optionSpacing + 2.5 * self.offset
return x, y
end