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/Demultiplexer.lua | 69 --------------------------------------------------- not/Group.lua | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 69 deletions(-) delete mode 100644 not/Demultiplexer.lua create mode 100644 not/Group.lua (limited to 'not') diff --git a/not/Demultiplexer.lua b/not/Demultiplexer.lua deleted file mode 100644 index cbbbcef..0000000 --- a/not/Demultiplexer.lua +++ /dev/null @@ -1,69 +0,0 @@ ---- Element used for grouping elements and demultiplexing input of different controller sets. -Demultiplexer = require "not.Element":extends() - -function Demultiplexer:new (parent) - Demultiplexer.__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 Demultiplexer: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 Demultiplexer: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 Demultiplexer:focus () - self:callEach("focus") - self.focused = true - return true -end - -function Demultiplexer:blur () - self:callEach("blur") - self.focused = false -end - -function Demultiplexer:draw (scale) - self:callEach("draw", scale) -end - -function Demultiplexer:update (dt) - self:callEach("update", dt) -end - -function Demultiplexer:controlpressed (set, action, key) - if self.focused then - self:callWithSet(set, "controlpressed", set, action, key) - end -end - -function Demultiplexer:controlreleased (set, action, key) - if self.focused then - self:callWithSet(set, "controlreleased", set, action, key) - end -end - -return Demultiplexer 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