|
1 | 1 | --- Module to keep track of pressed and released states for all
|
2 | 2 | -- input that it receives.
|
| 3 | +-- |
3 | 4 | -- @usage
|
4 | 5 | --
|
| 6 | +-- -- Alternative with a global state tracker |
| 7 | +-- -- script1 |
5 | 8 | -- local input = require "ludobits.m.input.state"
|
6 | 9 | --
|
7 | 10 | -- function init(self)
|
|
24 | 27 | -- input.on_input(action_id, action)
|
25 | 28 | -- end
|
26 | 29 | --
|
| 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 | +-- |
27 | 55 |
|
| 56 | +local M = {} |
28 | 57 |
|
| 58 | +--- Create an instance of the input state tracker |
| 59 | +-- @return State instance |
| 60 | +function M.create() |
| 61 | + local instance = {} |
29 | 62 |
|
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 |
31 | 71 |
|
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() |
33 | 107 |
|
34 | 108 | --- Acquire input focus for the current script
|
35 | 109 | -- @param url
|
36 | 110 | function M.acquire(url)
|
37 |
| - msg.post(url or ".", "acquire_input_focus") |
38 |
| - action_map = {} |
| 111 | + return instance.acquire(url) |
39 | 112 | end
|
40 | 113 |
|
41 | 114 | --- Release input focus for the current script
|
42 | 115 | -- @param url
|
43 | 116 | function M.release(url)
|
44 |
| - msg.post(url or ".", "release_input_focus") |
45 |
| - action_map = {} |
| 117 | + return instance.release(url) |
46 | 118 | end
|
47 | 119 |
|
48 | 120 | --- Check if an action is pressed/active
|
49 | 121 | -- @param action_id
|
50 | 122 | -- @return true if pressed/active
|
51 | 123 | 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) |
55 | 125 | end
|
56 | 126 |
|
57 | 127 | --- Forward any calls to on_input from scripts using this module
|
58 | 128 | -- @param action_id
|
59 | 129 | -- @param action
|
60 | 130 | 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) |
70 | 132 | end
|
71 | 133 | 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) |
74 | 135 | end
|
75 | 136 |
|
76 | 137 | return M
|
0 commit comments