Skip to content

Commit

Permalink
Added support for local input state trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Dec 30, 2017
1 parent ba39ee5 commit 8e58607
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 20 deletions.
101 changes: 81 additions & 20 deletions ludobits/m/input/state.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
--- Module to keep track of pressed and released states for all
-- input that it receives.
--
-- @usage
--
-- -- Alternative with a global state tracker
-- -- script1
-- local input = require "ludobits.m.input.state"
--
-- function init(self)
Expand All @@ -24,53 +27,111 @@
-- input.on_input(action_id, action)
-- end
--
-- -- Alternative with a script local state tracker
-- -- script2
-- local input = require "ludobits.m.input.state"
--
-- function init(self)
-- self.input = input.create()
-- self.input.acquire()
-- end
--
-- function final(self)
-- self.input.release()
-- end
--
-- function update(self, dt)
-- if self.input.is_pressed(hash("left")) then
-- go.set_position(go.get_position() - vmath.vector3(50, 0, 0) * dt)
-- elseif self.input.is_pressed(hash("right")) then
-- go.set_position(go.get_position() + vmath.vector3(50, 0, 0) * dt)
-- end
-- end
--
-- function on_input(self, action_id, action)
-- self.input.on_input(action_id, action)
-- end
--

local M = {}

--- Create an instance of the input state tracker
-- @return State instance
function M.create()
local instance = {}

local M = {}
local action_map = {}

--- Acquire input focus for the current script
-- @param url
function instance.acquire(url)
msg.post(url or ".", "acquire_input_focus")
action_map = {}
end

local action_map = {}
--- Release input focus for the current script
-- @param url
function instance.release(url)
msg.post(url or ".", "release_input_focus")
action_map = {}
end

--- Check if an action is currently pressed or not
-- @param action_id
-- @return true if action_id is pressed
function instance.is_pressed(action_id)
assert(action_id, "You must provide an action_id")
action_id = type(action_id) == "string" and hash(action_id) or action_id
return action_map[action_id]
end

--- Forward any calls to on_input from scripts using this module
-- @param action_id
-- @param action
function instance.on_input(action_id, action)
assert(action, "You must provide an action")
if action_id then
action_id = type(action_id) == "string" and hash(action_id) or action_id
if action.pressed then
action_map[action_id] = true
elseif action.released then
action_map[action_id] = false
end
end
end

return instance
end

local instance = M.create()

--- Acquire input focus for the current script
-- @param url
function M.acquire(url)
msg.post(url or ".", "acquire_input_focus")
action_map = {}
return instance.acquire(url)
end

--- Release input focus for the current script
-- @param url
function M.release(url)
msg.post(url or ".", "release_input_focus")
action_map = {}
return instance.release(url)
end

--- Check if an action is pressed/active
-- @param action_id
-- @return true if pressed/active
function M.is_pressed(action_id)
assert(action_id, "You must provide an action_id")
action_id = type(action_id) == "string" and hash(action_id) or action_id
return action_map[action_id]
return instance.is_pressed(action_id)
end

--- Forward any calls to on_input from scripts using this module
-- @param action_id
-- @param action
function M.update(action_id, action)
assert(action, "You must provide an action")
if action_id then
action_id = type(action_id) == "string" and hash(action_id) or action_id
if action.pressed then
action_map[action_id] = true
elseif action.released then
action_map[action_id] = false
end
end
return instance.on_input(action_id, action)
end
function M.on_input(action_id, action)
-- I can't decide on which I like best, on_input() or update()
M.update(action_id, action)
return instance.on_input(action_id, action)
end

return M
31 changes: 31 additions & 0 deletions test/test_input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,36 @@ return function()
assert(input.is_pressed(action_id_string))
assert(input.is_pressed(action_id_hash))
end)


it("should be able to create multiple instances", function()
local action_id1 = hash("action1")
local action_id2 = hash("action2")
local pressed = { pressed = true }
local released = { released = true }

local input1 = input.create()
local input2 = input.create()

assert(not input1.is_pressed(action_id1))
assert(not input1.is_pressed(action_id2))
assert(not input2.is_pressed(action_id1))
assert(not input2.is_pressed(action_id2))

input1.on_input(action_id1, pressed)
assert(input1.is_pressed(action_id1))
assert(not input2.is_pressed(action_id1))

input2.on_input(action_id2, pressed)
assert(input1.is_pressed(action_id1))
assert(not input2.is_pressed(action_id1))
assert(not input1.is_pressed(action_id2))
assert(input2.is_pressed(action_id2))

input1.on_input(action_id1, released)
assert(not input1.is_pressed(action_id1))
input2.on_input(action_id2, released)
assert(not input2.is_pressed(action_id2))
end)
end)
end

0 comments on commit 8e58607

Please sign in to comment.