summaryrefslogtreecommitdiffhomepage
path: root/menu.lua
blob: 206c45e51b2a3711ae437b0cde55507858a7ce4f (plain)
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
-- `Menu` (Scene)
-- It creates single screen of a menu
-- I do know that model I used here and in `World` loading configuration files is not flawless but I did not want to rewrite `World`s one but wanted to keep things similar at least in project scope.

require "music"

-- Here it begins
Menu = {
	scale = getScale(),
	elements, --table
	active = 1,
	music
}
function Menu:new(name)
	local o = {}
	setmetatable(o, self)
	self.__index = self
	o.elements = {}
	o:load(name)
	o.music = Music:new("menu.ogg")
	return o
end
function Menu:delete() end

-- Load menu from file
function Menu:load(name)
	local name = "config/" .. (name or "menumain") .. ".lua"
	local menu = love.filesystem.load(name)
	self.active = 1
	self.elements = menu(self)
	self.elements[self.active]:focus()
end

-- Cycle elements
function Menu:next()
	self.elements[self.active]:blur()
	self.active = (self.active%#self.elements)+1
	if not self.elements[self.active]:focus() then
		self:next()
	end
end
function Menu:previous()
	self.elements[self.active]:blur()
	if self.active == 1 then
		self.active = #self.elements
	else
		self.active = self.active - 1
	end
	if not self.elements[self.active]:focus() then
		self:previous()
	end
end

-- LÖVE2D callbacks
function Menu:update(dt)
	for _,element in pairs(self.elements) do
		element:update(dt)
	end
end
function Menu:draw()
	local scale = self.scale
	love.graphics.setFont(Font)
	for _,element in pairs(self.elements) do
		element:draw(scale)
	end
end

-- Controller callbacks
function Menu:controlpressed(set, action, key)
	if action == "down" then
		self:next()
	end
	if action == "up" then
		self:previous()
	end
	for _,element in pairs(self.elements) do
		element:controlpressed(set, action, key)
	end
end
function Menu:controlreleased(set, action, key) end