From de2ee3b44373da6e3c110d00644d04c78f2efa24 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 20:07:12 +0200 Subject: No more dank name: Demux => Group --- not/Group.lua | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 not/Group.lua (limited to 'not/Group.lua') diff --git a/not/Group.lua b/not/Group.lua new file mode 100644 index 0000000..5dedc9a --- /dev/null +++ b/not/Group.lua @@ -0,0 +1,69 @@ +--- 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 = {} +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, ...)) + 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 +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 -- cgit v1.1 From 994768edaf706c890bcad6d6cdecedaf0f4bdec1 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 21:00:16 +0200 Subject: Added callEachBut method --- not/Group.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'not/Group.lua') diff --git a/not/Group.lua b/not/Group.lua index 5dedc9a..d9c0b9c 100644 --- a/not/Group.lua +++ b/not/Group.lua @@ -20,10 +20,28 @@ function Group:callEach (func, ...) 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, ...)) + 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 -- cgit v1.1 From cc96f0f2fe9af1ef9de0846f9939f3d477e86849 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 23:40:37 +0200 Subject: Fix for empty second parameter in table.insert --- not/Group.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'not/Group.lua') diff --git a/not/Group.lua b/not/Group.lua index d9c0b9c..e644e5e 100644 --- a/not/Group.lua +++ b/not/Group.lua @@ -14,7 +14,7 @@ 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, ...)) + table.insert(results, child[func](child, ...) or nil) end end return results -- cgit v1.1 From 168123c5a499d2d103d55bbf403b3a49bc3c28ec Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 23:43:02 +0200 Subject: Fix also in this line --- not/Group.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'not/Group.lua') diff --git a/not/Group.lua b/not/Group.lua index e644e5e..45dc474 100644 --- a/not/Group.lua +++ b/not/Group.lua @@ -30,7 +30,7 @@ function Group:callEachBut (avoid, func, ...) for _,child in ipairs(self.children) do if child ~= avoid then if type(child[func]) == "function" then - table.insert(results, child[func](child, ...)) + table.insert(results, child[func](child, ...) or nil) end end end -- cgit v1.1 From a866dc8e7d1ec941b96acb7dd12c1636cc3f1a80 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 5 Sep 2017 01:55:14 +0200 Subject: Added addChild, setPosition and getSize to Group --- not/Group.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'not/Group.lua') diff --git a/not/Group.lua b/not/Group.lua index 45dc474..898d7f3 100644 --- a/not/Group.lua +++ b/not/Group.lua @@ -4,6 +4,35 @@ Group = require "not.Element":extends() function Group:new (parent) Group.__super.new(self, parent) self.children = {} + self.marign = 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.marigin + end + return Group.__super.setPosition(self, x, y) +end + +function Group:getSize () + local twidth = -self.marigin + local theight = 0 + for _,child in ipairs(self.children) do + local cwidth, cheight = child:getSize() + twidth = twidth + child:getSize() + self.marigin + if theight < cheight then + theight = cheight + end + end + return twidth, theight end --- Calls function with parameters for each child. -- cgit v1.1 From 0406d5d9617d20b528d2c5135a2474c19e23e9fa Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 5 Sep 2017 02:03:12 +0200 Subject: Fixed typo in Group --- not/Group.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'not/Group.lua') diff --git a/not/Group.lua b/not/Group.lua index 898d7f3..970d3bc 100644 --- a/not/Group.lua +++ b/not/Group.lua @@ -4,7 +4,7 @@ Group = require "not.Element":extends() function Group:new (parent) Group.__super.new(self, parent) self.children = {} - self.marign = 0 + self.margin = 0 end function Group:addChild (element) @@ -17,17 +17,17 @@ 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.marigin + dx = dx + child:getSize() + self.margin end return Group.__super.setPosition(self, x, y) end function Group:getSize () - local twidth = -self.marigin + local twidth = -self.margin local theight = 0 for _,child in ipairs(self.children) do local cwidth, cheight = child:getSize() - twidth = twidth + child:getSize() + self.marigin + twidth = twidth + child:getSize() + self.margin if theight < cheight then theight = cheight end -- cgit v1.1