summaryrefslogtreecommitdiffhomepage
path: root/not/Menu.lua
blob: f6da35419dcb44b1f28f65266daae6b6640fb7c0 (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
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
--- `Menu`
-- 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.
Menu = require "not.Scene":extends()

Menu.elements = --[[{not.Element}]]nil
Menu.active = 1
Menu.music = --[[not.Music]]nil
Menu.sprite = --[[love.graphics.newImage]]nil
Menu.allowMove = true
Menu.quads = { -- TODO: Could be moved to config file or perhaps QuadManager to manage all quads for animations etc.
	button = {
		normal = love.graphics.newQuad(0, 0, 58,15, 80,130),
		active = love.graphics.newQuad(0, 0, 58,15, 80,130)
	},
	portrait = {
		normal = love.graphics.newQuad( 0, 15, 32,32, 80,130),
		active = love.graphics.newQuad(32, 15, 32,32, 80,130)
	},
	panorama = {
		normal = love.graphics.newQuad(0,47, 80,42, 80,130),
		active = love.graphics.newQuad(0,88, 80,42, 80,130)
	},
	arrow_l = love.graphics.newQuad(68, 0, 6, 6, 80,130),
	arrow_r = love.graphics.newQuad(74, 0, 6, 6, 80,130),
}

function Menu:new (name)
	-- Load statically.
	if Menu.sprite == nil then
		Menu.sprite = love.graphics.newImage("assets/menu.png")
	end
	self.elements = {}
	self:open(name)
end

function Menu:delete () end

function Menu:open (name)
	local name = name or "main"
	self.active = Menu.active --Menu.active is initial
	self.elements = love.filesystem.load(string.format("config/menus/%s.lua", name))(self, self.elements[1])
	-- Common with `next` method.
	if not self.elements[self.active]:focus() then
		self:next()
	end
end

-- Return reference to quads table and menu sprite
function Menu:getSheet ()
	return self.sprite, self.quads
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

-- @Override
function Menu:isInputDisabled ()
	if self.inputBreakTimer then
		return self.inputDisabled or self.inputBreakTimer > 0
	end
	return self.inputDisabled
end

-- LÖVE2D callbacks
function Menu:update (dt)
	for _,element in pairs(self.elements) do
		element:update(dt)
	end
	if self.inputBreakTimer and self.inputBreakTimer > 0 then
		self.inputBreakTimer = self.inputBreakTimer - dt
	end
end
function Menu:draw ()
	local scale = getScale()
	local scaler = getRealScale()
	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 self.allowMove then
		if action == "down" then
			self:next()
		end
		if action == "up" then
			self:previous()
		end
	end
	for _,element in pairs(self.elements) do
		element:controlpressed(set, action, key)
	end
end
function Menu:controlreleased (set, action, key)
	for _,element in pairs(self.elements) do
		element:controlreleased(set, action, key)
	end
end

return Menu