-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinput.lua
More file actions
106 lines (87 loc) · 1.87 KB
/
input.lua
File metadata and controls
106 lines (87 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
-- input.lua
local M = {
context = {}
}
local contexts = {}
local raw = {
key = {
down = {},
pressed = {},
released = {}
},
joystick = {
down = {},
pressed = {},
released = {},
axis = {}
}
}
M.context.new = function (arg)
local self = {
active = arg.active or false,
priority = arg.priority or 0
}
local object = {}
object.active = function (active)
self.active = active or self.active
return self.active
end
object.priority = function (priority)
self.priority = priority or self.priority
return self.priority
end
object.map = function (raw_input, map_input)
return raw_input, map_input
end
table.insert(contexts, object)
table.sort(contexts,
function (a, b)
return a.priority() > b.priority()
end
)
return object
end
M.keyPressed = function(key)
raw.key.down[key] = true
raw.key.pressed[key] = true
end
M.keyReleased = function(key)
raw.key.down[key] = nil
raw.key.released[key] = true
end
M.joystickPressed = function(joystick, button)
raw.joystick.down[button] = true
raw.joystick.pressed[button] = true
end
M.joystickReleased = function(joystick, button)
raw.joystick.down[button] = nill
raw.joystick.released[button] = true
end
M.update = function (dt)
local map = {
actions = {},
states = {},
ranges = {}
}
if love.joystick.getNumJoysticks() > 0 then
for i=1,love.joystick.getNumAxes(1) do
raw.joystick.axis[i] = love.joystick.getAxis(1, i)
end
end
for _,context in ipairs(contexts) do
if context.active() then
map = context.map(raw, map)
end
end
raw.key.pressed = {}
raw.key.released = {}
raw.joystick.pressed = {}
raw.joystick.released = {}
if next(map.actions) == nil and
next(map.states) == nil and
next(map.ranges) == nil then
return nil
end
return map
end
return M