-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenuhandler.lua
More file actions
142 lines (120 loc) · 4.07 KB
/
menuhandler.lua
File metadata and controls
142 lines (120 loc) · 4.07 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
module(..., package.seeall)
--
-- Simple Menu Handler
-- I was going to do an XML menu reader, but I'd have to assign functions to it anyway
--
require 'utils'
function MenuHandler(opts)
assert(type(menu) == "table" or not menu, "Passed in menu must be a table of menu items, or nothing at all")
self = {}
self.menu = {}
self.current = nil
self.opts = opts
if not self.opts.pos then self.opts.pos = {x=0,y=0} end
if not self.opts.spacing then self.opts.spacing = 20 end
--
-- Adds an item to the menu
-- Display is what will be shown on the menu. Should either be a string or a function that returns a string.
-- Action is the function that will be run when they choose this. Doesn't make sense to add to a parent element.
-- Parent is the Display name of the parent element. If the Display is a function, it will be run before comparison
--
self.addItem = function(self, display, action, parent)
if parent then
assert(#self.menu > 0, "Menu cannot add children because there are no parent elements")
end
local tempItem = {display = display, action = action, children = {}, parent = nil, empty = true,
getDisplay = function() return type(display) == 'function' and display() or display end
}
if parent then
parent = self:findNode(parent)
if parent then
tempItem.parent = parent
table.insert(parent.children, tempItem)
parent.empty = false
else error("Cannot find parent element")
end
else
table.insert(self.menu, tempItem)
end
if not self.current then
self.current = tempItem
end
return tempItem
end
self.findNode = function(self, display, startNode)
assert(#self.menu > 0, "Menu has no nodes to search")
startNode = startNode or self.menu
for _,v in ipairs(startNode) do
if v.getDisplay() == display then return v end
if #v.children > 0 then self:findNode(display, v.children) end
end
return nil
end
self.nextNode = function(self)
local list = self.current.parent and self.current.parent.children or self.menu
for i,v in ipairs(list) do
if v == self.current then
if i == #list then self.current = list[1]
else self.current = list[i+1] end
break
end
end
end
self.prevNode = function(self)
local list = self.current.parent and self.current.parent.children or self.menu
for i,v in ipairs(list) do
if v == self.current then
if i == 1 then self.current = list[#list]
else self.current = list[i-1] end
break
end
end
end
self.parent = function(self)
if self.current.parent then
self.current = self.current.parent
end
end
self.child = function(self)
if self.current.action then
self.current.action()
return
end
if not self.current.empty then
self.current = self.current.children[1]
end
end
local outerSelf = self
self.screen = {
name = "menu",
enter = function(self) love.graphics.setColor(255, 255, 255, 255) end,
draw = function(self)
if outerSelf.opts.backImage then
love.graphics.draw(outerSelf.opts.backImage, 0, 0)
end
if outerSelf.opts.font then
love.graphics.setFont(outerSelf.opts.font)
end
local list = outerSelf.current.parent and outerSelf.current.parent.children or outerSelf.menu
for i,menu_item in ipairs(list) do
local y = outerSelf.opts.spacing * (i-1) + 10
love.graphics.print(menu_item.getDisplay(), outerSelf.opts.pos.x, outerSelf.opts.pos.y + y)
if menu_item == outerSelf.current then
love.graphics.print("-", outerSelf.opts.pos.x - 25, outerSelf.opts.pos.y + y)
end
end
end,
update = function(self, dt)
if self.keyhandler:handle('menuup') then
outerSelf:prevNode()
elseif self.keyhandler:handle('menudown') then
outerSelf:nextNode()
elseif self.keyhandler:handle('menuexit') then
outerSelf:parent()
elseif self.keyhandler:handle('menuenter') then
outerSelf:child()
end
end
}
return self
end