summaryrefslogtreecommitdiffhomepage
path: root/selector.lua
blob: f07b9e78c7e2cda68ebe3006a325d067e1a32c93 (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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
-- `Selector` (Element)
-- Used in Menu for selecting various things from list. Works for each Controller set or globally.
--[[
How to use `Selector` in `Menu` config file?
selector:new(menu)
	:setPosition(x, y)
	:setSpacing(42, 0)
	:set("list", require "nautslist")
	:set("sprite", love.graphics.newImage("assets/portraits.png"))
	:set("quads", require "portraits")
	:set("global", false) -- true: single selector; false: selector for each controller set present
	:init()
]]

Selector = {
	parent,
	x = 0,
	y = 0,
	horizontal = 0,
	height = 0,
	focused = false,
	global = false,
	list,
	sets,
	locks,
	selections,
	sprite,
	quads
}

-- Constructor
function Selector:new(parent)
	local o = {}
	setmetatable(o, self)
	self.__index = self
	o.parent = parent
	return o
end

-- Position
function Selector:getPosition()
	return self.x, self.y
end
function Selector:setPosition(x,y)
	self.x, self.y = x, y
	return self
end

-- Spacing between positions of two blocks
function Selector:getSpacing()
	return self.horizontal, self.vertical
end
function Selector:setSpacing(horizontal, vertical)
	self.horizontal, self.vertical = horizontal, vertical
	return self
end

-- General setter for Menu configuration files
function Selector:set(name, func)
	if type(name) == "string" and func ~= nil then
		self[name] = func
	end
	return self
end

-- Initialize Selector with current settings.
function Selector:init()
	-- Make sure that there is list present
	if self.list == nil then
		self.list = {}
	end
	-- Initialize global Selector
	if self.global then
		self.locks = {false}
		self.selections = {1}
	-- Initialize Selector for Controllers
	else
		self.sets = Controller.getSets()
		self.locks = {}
		self.selections = {}
		for n=1,#self.sets do
			self.locks[n] = false
			self.selections[n] = 1
		end
	end
	return self
end

-- Cycle through list on given number
function Selector:next(n)
	local total = #self.list
	local current = self.selections[n]
	local locked = self:isLocked(n)
	if not locked then
		self.selections[n] = (current % total) + 1
	end
end
function Selector:previous(n)
	local total = #self.list
	local current = self.selections[n]
	local locked = self:isLocked(n)
	if not locked then
		if current == 1 then
			self.selections[n] = total
		else
			self.selections[n] = current - 1
		end
	end
end

-- Get number associated with a given set
function Selector:checkNumber(set)
	if self.global then return 1 end -- For global Selector
	for n,check in pairs(self.sets) do
		if check == set then return n end
	end
end

-- Check if given number is locked
function Selector:isLocked(n)
	return self.locks[n]
end

-- Get value of selection of given number
function Selector:getSelection(n)
	return self.selections[n]
end

-- Get value from list by selection
function Selector:getListValue(i)
	return self.list[i]
end

-- Draw single block of Selector
function Selector:drawBlock(n, x, y, scale)
	local x, y = x or 0, y or 0
	if self.quads == nil or self.sprite == nil then return end
	love.graphics.setFont(Font)
	love.graphics.setColor(255, 255, 255, 255)
	local name = self:getListValue(self:getSelection(n))
	local locked = self:isLocked(n)
	local sprite = self.sprite
	local quad = self.quads[name]
	local arrowl = self.quads.arrow_left
	local arrowr = self.quads.arrow_right
	if not locked then
		love.graphics.draw(sprite, quad.normal, x*scale, y*scale, 0, scale, scale)
		if self.focused then
			love.graphics.draw(sprite, arrowl, (x-2)* scale, (y+13)*scale, 0, scale, scale)
			love.graphics.draw(sprite, arrowr, (x+30)*scale, (y+13)*scale, 0, scale, scale)
		end
	else
		love.graphics.draw(sprite, quad.active, x*scale, y*scale, 0, scale, scale)
	end
	if self:getSelection(n) ~= 1 then
		love.graphics.printf(string.upper(name), (x-8)*scale, (y+33)*scale, 48, "center", 0, scale, scale)
	end
end

-- Menu callbacks
function Selector:focus() -- Called when Element gains focus
	self.focused = true
	return true
end 
function Selector:blur() -- Called when Element loses focus
	self.focused = false
end 

-- LÖVE2D callbacks
function Selector:draw(scale)
	local x,y = self:getPosition()
	local h,v = self:getSpacing()
	for n=1,#self.selections do
		self:drawBlock(n, x+h*n, y+v*n, scale)
	end
end
function Selector:update(dt) end

-- Controller callbacks
function Selector:controlpressed(set, action, key)
	if set and self.focused then
		local n = self:checkNumber(set)
		local locked = self:isLocked(n)
		if action == "left" and not locked then self:previous(n) end
		if action == "right" and not locked then self:next(n) end
	end
end
function Selector:controlreleased(set, action, key) end

return Selector