Repository navigation
Expand file tree
/
Copy pathinput.lua
More file actions
104 lines (87 loc) · 2.42 KB
/
Copy pathinput.lua
File metadata and controls
104 lines (87 loc) · 2.42 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
--centralised input
local path = (...):gsub(".input", "")
local function relative_require(module)
return require(path .. "." .. module)
end
local input = {
keyboard = relative_require("keyboard")(),
mouse = relative_require("mouse")(),
gamepad = relative_require("gamepad")(1),
}
input.mode = "desktop"
--init the cursor stuff
input._cursor = vec2()
input._first_cursor = true
input._cursor_pressed = {false, false, false}
input._cursor_old_pressed = {false, false, false}
--if the whole input should be cleared if we swap modes (saves mistaken clicks on desktop, causes problems on deck)
input.clear_on_mode_change = true
function input:update(dt)
self.keyboard:update(dt)
self.mouse:update(dt)
self.gamepad:update(dt)
local old_mode = self.mode
if
self.keyboard:any_just_pressed()
or self.mouse:any_just_pressed()
or self.mouse.delta:length_squared() > 10
then
self.mode = "desktop"
if self._first_cursor then
self._first_cursor = false
end
elseif self.gamepad:any_just_pressed_even_axes() then
self.mode = "gamepad"
if self._first_cursor then
--init cursor to the middle of the screen
input._cursor:sset(display:dimensions()):smuli(0.5):vaddi(display.offset)
self._first_cursor = false
end
end
--clear on first press
if old_mode ~= self.mode and self.clear_on_mode_change then
self:clear()
end
--update cursor
if self.mode == "gamepad" then
--gamepad mouse has to manage writing it in somehow
else
self._cursor:vset(self.mouse.pos)
end
for i, v in ipairs(self._cursor_pressed) do
self._cursor_old_pressed[i] = v
end
if input.mode == "gamepad" then
for i, v in ipairs({"a", "b", "leftstick"}) do
self._cursor_pressed[i] = self.gamepad:pressed(v)
end
else
for i = 1, 3 do
self._cursor_pressed[i] = self.mouse:pressed(i)
end
end
end
function input:clear()
self.keyboard:clear()
self.mouse:clear()
self.gamepad:clear()
end
function input:cursor()
return self._cursor
end
function input:cursor_pressed(button)
return self._cursor_pressed[button]
end
function input:cursor_released(button)
return not self._cursor_pressed[button]
end
function input:cursor_old_pressed(button)
return self._cursor_old_pressed[button]
end
function input:cursor_just_pressed(button)
return not self:cursor_old_pressed(button) and self:cursor_pressed(button)
end
function input:cursor_just_released(button)
return self:cursor_old_pressed(button) and not self:cursor_pressed(button)
end
return input