-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGamepad.lua
162 lines (135 loc) · 4.44 KB
/
Gamepad.lua
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-- Import
local math = math
local ipairs = ipairs
local Class = require "Class"
local RingBuffer = require "RingBuffer"
local Gamepad = Class{
id = -1,
bufferSize = 0,
inputBuffer = nil
}
setfenv(1, Gamepad)
local gamepadButtons = {
"a",
"b",
"x",
"y",
"rightshoulder",
"leftshoulder",
"rightstick",
"leftstick",
"back",
"guide",
"start",
"dpup",
"dpdown",
"dpright",
"dpleft"
}
local gamepadAxes = {
"leftx",
"lefty",
"rightx",
"righty",
"triggerleft",
"triggerright"
}
-- Button state data structure
local ButtonState = Class{pressed = false}
-- Axis state data structure
local AxisState = Class{value = 0}
-- Gamepad state data structure
local GamepadState = Class{}
function GamepadState:init()
for i, value in ipairs(gamepadButtons) do
self[value] = ButtonState()
end
for i, value in ipairs(gamepadAxes) do
self[value] = AxisState()
end
end
function Gamepad:init(bufferSize, id)
self.id = id
self.inputBuffer = RingBuffer(bufferSize)
self.inputBuffer:insertElement(GamepadState())
self.inputBuffer:insertElement(GamepadState())
end
function Gamepad:update()
self.inputBuffer:insertElement(self.inputBuffer:headElement():clone())
end
function Gamepad:updateButton(button, buttonState)
self.inputBuffer:headElement()[button].pressed = buttonState
end
function Gamepad:updateAxis(axis, axisValue)
self.inputBuffer:headElement()[axis].value = axisValue
end
local function axisGT(a, b)
if math.abs(a) >= math.abs(b) and a * b >= 0 then
return true
end
return false
end
function Gamepad:axis(axis)
return self.inputBuffer:headElement()[axis].value
end
function Gamepad:axisMoved(axis, axisValue, frameTolerance)
if not frameTolerance then frameTolerance = 0
elseif frameTolerance > self.inputBuffer:size() - 1 then frameTolerance = self.inputBuffer:size() - 1 end
for i, state in RingBuffer.reverseIterator(self.inputBuffer, frameTolerance) do
if axisGT (state[axis].value, axisValue) then
return true
end
end
return false
end
function Gamepad:axisJustMoved(axis, axisValue, frameTolerance)
if not frameTolerance then frameTolerance = 0
elseif frameTolerance > self.inputBuffer:size() - 2 then frameTolerance = self.inputBuffer:size() - 2 end
for i, state in RingBuffer.reverseIterator(self.inputBuffer, frameTolerance) do
if axisGT(state[axis].value, axisValue) and not axisGT(self.inputBuffer:getElement(i - 1)[axis].value, axisValue) then
return true
end
end
return false
end
function Gamepad:axisJustReleased(axis, axisValue, frameTolerance)
if not frameTolerance then frameTolerance = 0
elseif frameTolerance > self.inputBuffer:size() - 2 then frameTolerance = self.inputBuffer:size() - 2 end
for i, state in RingBuffer.reverseIterator(self.inputBuffer, frameTolerance) do
if not axisGT(state[axis].value, axisValue) and axisGT(self.inputBuffer:getElement(i - 1)[axis].value, axisValue) then
return true
end
end
return false
end
function Gamepad:buttonPressed(button, frameTolerance)
if not frameTolerance then frameTolerance = 0
elseif frameTolerance > self.inputBuffer:size() - 1 then frameTolerance = self.inputBuffer:size() - 1 end
for i, state in RingBuffer.reverseIterator(self.inputBuffer, frameTolerance) do
if state[button].pressed then
return true
end
end
return false
end
function Gamepad:buttonJustPressed(button, frameTolerance)
if not frameTolerance then frameTolerance = 0
elseif frameTolerance > self.inputBuffer:size() - 2 then frameTolerance = self.inputBuffer:size() - 2 end
for i, state in RingBuffer.reverseIterator(self.inputBuffer, frameTolerance) do
if state[button].pressed and not self.inputBuffer:getElement(i - 1)[button].pressed then
return true
end
end
return false
end
function Gamepad:buttonJustReleased(button, frameTolerance)
if not frameTolerance then frameTolerance = 0
elseif frameTolerance > self.inputBuffer:size() - 2 then frameTolerance = self.inputBuffer:size() - 2 end
for i, state in RingBuffer.reverseIterator(self.inputBuffer, frameTolerance) do
if not state[button].pressed and self.inputBuffer:getElement(i - 1)[button].pressed then
return true
end
end
return false
end
return Gamepad