summaryrefslogtreecommitdiffhomepage
path: root/not/Group.lua
blob: 970d3bc0a2fc3e07f784bdfb60e7b89654bd3995 (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
--- Element used for grouping elements and passing input to selected child based on controller set.
Group = require "not.Element":extends()

function Group:new (parent)
	Group.__super.new(self, parent)
	self.children = {}
	self.margin = 0
end

function Group:addChild (element)
	table.insert(self.children, element)
	return element
end

-- TODO: Missing semi-important docs on Group's setPosition.
function Group:setPosition (x, y)
	local dx = 0
	for _,child in ipairs(self.children) do
		child:setPosition(x + dx, y)
		dx = dx + child:getSize() + self.margin
	end
	return Group.__super.setPosition(self, x, y)
end

function Group:getSize ()
	local twidth = -self.margin
	local theight = 0
	for _,child in ipairs(self.children) do
		local cwidth, cheight = child:getSize()
		twidth = twidth + child:getSize() + self.margin
		if theight < cheight then
			theight = cheight
		end
	end
	return twidth, theight
end

--- Calls function with parameters for each child.
-- @param func key of function to call
-- @param ... parameters passed to function
-- @return table with calls' results
function Group:callEach (func, ...)
	local results = {}
	for _,child in ipairs(self.children) do
		if type(child[func]) == "function" then
			table.insert(results, child[func](child, ...) or nil)
		end
	end
	return results
end

--- Calls function with parameters for each but one child.
-- @param avoid child to avoid calling
-- @param func key of function to call
-- @param ... parameters passed to function
-- @return table with calls' results
function Group:callEachBut (avoid, func, ...)
	local results = {}
	for _,child in ipairs(self.children) do
		if child ~= avoid then
			if type(child[func]) == "function" then
				table.insert(results, child[func](child, ...) or nil)
			end
		end
	end
	return results
end

--- Calls function with parameters for one child based on controller set.
-- @param set controller set
-- @param func key of function to call
-- @param ... parameters passed to function
-- @return results of called function
function Group:callWithSet (set, func, ...)
	for i,test in ipairs(Controller.getSets()) do
		if test == set then
			local child = self.children[i]
			if child then
				return child[func](child, ...)
			end
		end
	end
end

function Group:focus ()
	self:callEach("focus")
	self.focused = true
	return true
end 

function Group:blur ()
	self:callEach("blur")
	self.focused = false
end 

function Group:draw (scale)
	self:callEach("draw", scale)
end

function Group:update (dt)
	self:callEach("update", dt)
end

function Group:controlpressed (set, action, key)
	if self.focused then
		self:callWithSet(set, "controlpressed", set, action, key)
	end
end

function Group:controlreleased (set, action, key)
	if self.focused then
		self:callWithSet(set, "controlreleased", set, action, key)
	end
end

return Group