Skip to content

Commit 8e58607

Browse files
committed
Added support for local input state trackers
1 parent ba39ee5 commit 8e58607

File tree

2 files changed

+112
-20
lines changed

2 files changed

+112
-20
lines changed

ludobits/m/input/state.lua

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
--- Module to keep track of pressed and released states for all
22
-- input that it receives.
3+
--
34
-- @usage
45
--
6+
-- -- Alternative with a global state tracker
7+
-- -- script1
58
-- local input = require "ludobits.m.input.state"
69
--
710
-- function init(self)
@@ -24,53 +27,111 @@
2427
-- input.on_input(action_id, action)
2528
-- end
2629
--
30+
-- -- Alternative with a script local state tracker
31+
-- -- script2
32+
-- local input = require "ludobits.m.input.state"
33+
--
34+
-- function init(self)
35+
-- self.input = input.create()
36+
-- self.input.acquire()
37+
-- end
38+
--
39+
-- function final(self)
40+
-- self.input.release()
41+
-- end
42+
--
43+
-- function update(self, dt)
44+
-- if self.input.is_pressed(hash("left")) then
45+
-- go.set_position(go.get_position() - vmath.vector3(50, 0, 0) * dt)
46+
-- elseif self.input.is_pressed(hash("right")) then
47+
-- go.set_position(go.get_position() + vmath.vector3(50, 0, 0) * dt)
48+
-- end
49+
-- end
50+
--
51+
-- function on_input(self, action_id, action)
52+
-- self.input.on_input(action_id, action)
53+
-- end
54+
--
2755

56+
local M = {}
2857

58+
--- Create an instance of the input state tracker
59+
-- @return State instance
60+
function M.create()
61+
local instance = {}
2962

30-
local M = {}
63+
local action_map = {}
64+
65+
--- Acquire input focus for the current script
66+
-- @param url
67+
function instance.acquire(url)
68+
msg.post(url or ".", "acquire_input_focus")
69+
action_map = {}
70+
end
3171

32-
local action_map = {}
72+
--- Release input focus for the current script
73+
-- @param url
74+
function instance.release(url)
75+
msg.post(url or ".", "release_input_focus")
76+
action_map = {}
77+
end
78+
79+
--- Check if an action is currently pressed or not
80+
-- @param action_id
81+
-- @return true if action_id is pressed
82+
function instance.is_pressed(action_id)
83+
assert(action_id, "You must provide an action_id")
84+
action_id = type(action_id) == "string" and hash(action_id) or action_id
85+
return action_map[action_id]
86+
end
87+
88+
--- Forward any calls to on_input from scripts using this module
89+
-- @param action_id
90+
-- @param action
91+
function instance.on_input(action_id, action)
92+
assert(action, "You must provide an action")
93+
if action_id then
94+
action_id = type(action_id) == "string" and hash(action_id) or action_id
95+
if action.pressed then
96+
action_map[action_id] = true
97+
elseif action.released then
98+
action_map[action_id] = false
99+
end
100+
end
101+
end
102+
103+
return instance
104+
end
105+
106+
local instance = M.create()
33107

34108
--- Acquire input focus for the current script
35109
-- @param url
36110
function M.acquire(url)
37-
msg.post(url or ".", "acquire_input_focus")
38-
action_map = {}
111+
return instance.acquire(url)
39112
end
40113

41114
--- Release input focus for the current script
42115
-- @param url
43116
function M.release(url)
44-
msg.post(url or ".", "release_input_focus")
45-
action_map = {}
117+
return instance.release(url)
46118
end
47119

48120
--- Check if an action is pressed/active
49121
-- @param action_id
50122
-- @return true if pressed/active
51123
function M.is_pressed(action_id)
52-
assert(action_id, "You must provide an action_id")
53-
action_id = type(action_id) == "string" and hash(action_id) or action_id
54-
return action_map[action_id]
124+
return instance.is_pressed(action_id)
55125
end
56126

57127
--- Forward any calls to on_input from scripts using this module
58128
-- @param action_id
59129
-- @param action
60130
function M.update(action_id, action)
61-
assert(action, "You must provide an action")
62-
if action_id then
63-
action_id = type(action_id) == "string" and hash(action_id) or action_id
64-
if action.pressed then
65-
action_map[action_id] = true
66-
elseif action.released then
67-
action_map[action_id] = false
68-
end
69-
end
131+
return instance.on_input(action_id, action)
70132
end
71133
function M.on_input(action_id, action)
72-
-- I can't decide on which I like best, on_input() or update()
73-
M.update(action_id, action)
134+
return instance.on_input(action_id, action)
74135
end
75136

76137
return M

test/test_input.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,36 @@ return function()
9393
assert(input.is_pressed(action_id_string))
9494
assert(input.is_pressed(action_id_hash))
9595
end)
96+
97+
98+
it("should be able to create multiple instances", function()
99+
local action_id1 = hash("action1")
100+
local action_id2 = hash("action2")
101+
local pressed = { pressed = true }
102+
local released = { released = true }
103+
104+
local input1 = input.create()
105+
local input2 = input.create()
106+
107+
assert(not input1.is_pressed(action_id1))
108+
assert(not input1.is_pressed(action_id2))
109+
assert(not input2.is_pressed(action_id1))
110+
assert(not input2.is_pressed(action_id2))
111+
112+
input1.on_input(action_id1, pressed)
113+
assert(input1.is_pressed(action_id1))
114+
assert(not input2.is_pressed(action_id1))
115+
116+
input2.on_input(action_id2, pressed)
117+
assert(input1.is_pressed(action_id1))
118+
assert(not input2.is_pressed(action_id1))
119+
assert(not input1.is_pressed(action_id2))
120+
assert(input2.is_pressed(action_id2))
121+
122+
input1.on_input(action_id1, released)
123+
assert(not input1.is_pressed(action_id1))
124+
input2.on_input(action_id2, released)
125+
assert(not input2.is_pressed(action_id2))
126+
end)
96127
end)
97128
end

0 commit comments

Comments
 (0)