summaryrefslogtreecommitdiffhomepage
path: root/not/Demultiplexer.lua
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2017-09-04 03:47:49 +0200
committerAki <nthirtyone@gmail.com>2017-09-04 03:47:49 +0200
commit7afcf7efbead72263d97da376de802e553bfb7eb (patch)
tree4b89d43fc5f0365e230396d47462a549d1126a85 /not/Demultiplexer.lua
parentddd632254394a8ebfa3151e91978daa5455829a6 (diff)
downloadroflnauts-7afcf7efbead72263d97da376de802e553bfb7eb.zip
roflnauts-7afcf7efbead72263d97da376de802e553bfb7eb.tar.gz
roflnauts-7afcf7efbead72263d97da376de802e553bfb7eb.tar.bz2
New menu element Demultiplexer
Diffstat (limited to 'not/Demultiplexer.lua')
-rw-r--r--not/Demultiplexer.lua63
1 files changed, 63 insertions, 0 deletions
diff --git a/not/Demultiplexer.lua b/not/Demultiplexer.lua
new file mode 100644
index 0000000..468f007
--- /dev/null
+++ b/not/Demultiplexer.lua
@@ -0,0 +1,63 @@
+--- 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:callOne (set, func, ...)
+ for i,test in ipairs(Controller.getSets()) do
+ if test == set then
+ self.children[i][func](...)
+ return nil
+ 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)
+ self:callOne(set, "controlpressed", set, action, key)
+end
+
+function Demultiplexer:controlreleased (set, action, key)
+ self:callOne(set, "controlreleased", set, action, key)
+end
+
+return Demultiplexer