-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamepad.lua
262 lines (233 loc) · 6.42 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
local abs, sqrt = math.abs, math.sqrt
local cos, sin, atan2 = math.cos, math.sin, math.atan2
local PI, TURN = math.pi, 2 * math.pi
local floor = math.floor
local min, max = math.min, math.max
local function mergeAxes(a, b)
return max(a, b, 0) + min(a, b, 0)
end
local function mergeSticks(ax, ay, bx, by)
local x, y = mergeAxes(ax, bx), mergeAxes(ay, by)
local d2 = x * x + y * y
local s = d2 > 1 and 1 / sqrt(d2) or 1
return x * s, y * s
end
local function axis(device, axis, dir)
local x
if type(axis) == 'number' then
x = device:getAxis(axis)
elseif type(axis) == 'string' then
x = device:getGamepadAxis(axis)
else
error("Axis must be a number (joystick axis) or string (gamepad axis).")
end
return x * (dir or 1)
end
local function stick(device, xAxis, yAxis, xDir, yDir)
local x = axis(device, xAxis, xDir)
local y = axis(device, yAxis, yDir)
local r2 = x * x + y * y
local s = (r2 > 1) and 1 / sqrt(r2) or 1
return x * s, y * s
end
local function button(device, button)
local x
if type(button) == 'number' then
x = device:isDown(button)
elseif type(button) == 'string' then
x = device:isGamepadDown(button)
else
error("Button must be a number (joystick button) or string (gamepad button).")
end
return x
end
local function condition(old, dt, x, y)
-- Apply threshold and square result.
local r0, s = 0.15, 0
local r = sqrt(x * x + y * y)
if r > r0 then
s = max(0, r - r0) / (1 - r0)
s = s * s / r
end
x, y = x * s, y * s
-- Smooth over time.
local ct = 0.1 -- seconds of smoothing
local k = 1 - (1 - 0.9)^(dt/ct)
x = old.x + k * (x - old.x)
y = old.y + k * (y - old.y)
old.x, old.y = x, y
return x, y
end
local function roundTo(x, unit)
return floor(0.5 + x / unit) * unit
end
local function inCones(x, y, cx, cy, w, posDir, negDir)
local rc2 = 1 / (cx * cx + cy * cy) -- reciprocal of |c|^2
local along = (x * cx + y * cy) * rc2
if abs(along) < 1 then return false end
local xCross, yCross = x - along * cx, y - along * cy
local rr2 = 1 / (x * x + y * y)
local across2 = (xCross * xCross + yCross * yCross) * rr2
if across2 > w * w then return false end
return along < 0 and negDir or posDir
end
local sector = TURN / 6
local hexAxes = {
{ cos(0.5 * sector), sin(0.5 * sector), 1, 4 },
{ cos(1.5 * sector), sin(1.5 * sector), 2, 5 },
{ cos(2.5 * sector), sin(2.5 * sector), 3, 6 }
}
-- return direction or false (released) or nil (ambiguous).
local function toHex(x, y, r, w)
local released = true
for _,axis in ipairs(hexAxes) do
local cx, cy, posDir, negDir = unpack(axis)
local d = inCones(x, y, r * cx, r * cy, w, posDir, negDir)
if d then return d, false end
local r2, w2 = r * 0.8, w * 1.1
d = inCones(x, y, r2 * cx, r2 * cy, w2, posDir, negDir)
released = released and not d
end
return false, released
end
local function addStick(self, device, xAxis, yAxis, xDir, yDir)
table.insert(self.sticks, {
device = device,
xAxis = xAxis, yAxis = yAxis,
xDir = xDir, yDir = yDir,
})
end
local function removeStick(self, device, xAxis, yAxis)
for n,i in ipairs(self.sticks) do
if i.device == device and i.xAxis == xAxis and i.yAxis == yAxis then
table.remove(self.sticks, n)
return true
end
end
return false
end
local function addButton(self, device, button, name)
if not self.buttons[name] then
self.buttons[name] = { down = false }
end
table.insert(self.buttons[name], {
device = device, button = button
})
end
local function removeButton(self, device, button, name)
for n,b in ipairs(self.buttons[name] or {}) do
if b.device == device and b.button == button then
table.remove(self.buttons, n)
return true
end
end
return false
end
local function removeDevice(self, device)
for i,s in ipairs(self.sticks) do
if s.device == device then
table.remove(self.sticks, i)
end
end
for _,button in pairs(self.buttons) do
for i,b in ipairs(button) do
if b.device == device then
table.remove(b, i)
end
end
end
end
local function update(self, dt)
local x, y = 0, 0
for _,i in ipairs(self.sticks) do
local x2, y2 = stick(i.device, i.xAxis, i.yAxis, i.xDir, i.yDir)
x, y = mergeSticks(x, y, x2, y2)
end
x, y = condition(self, dt, x, y)
local dir, released = toHex(x, y, self.rt, self.wt)
self.length = sqrt(x * x + y * y)
local inputSent = false
if dir then
if dir ~= self.direction then
self.direction, inputSent = dir, true
self.pressed(self.aimControls[dir])
end
elseif released then
if self.direction then
self.direction, inputSent = false, true
self.released(self.aimControls[dir])
end
end
for name,b in pairs(self.buttons) do
local down = false
for _,i in ipairs(b) do
down = button(i.device, i.button) or down
end
if down ~= b.down then
b.down, inputSent = down, true
if down then self.pressed(name) else self.released(name) end
end
end
if x*x + y*y > 0.01 and not inputSent then
self.pressed('stick-moved')
end
end
local function draw(self, x, y, r, dr, alpha)
alpha = alpha or 0.6
love.graphics.push()
love.graphics.translate(x, y)
local lw = love.graphics.getLineWidth()
local c = { love.graphics.getColor() }
love.graphics.setColor(0.5, 0.5, 0.5, alpha)
love.graphics.polygon('fill', {
self.x * r, self.y * r,
(-self.x - self.y) * 0.2 * r, (-self.y + self.x) * 0.2 * r,
0, 0,
(-self.x + self.y) * 0.2 * r, (-self.y - self.x) * 0.2 * r,
})
for i=0,5 do
local rt = self.rt * r
local wt = self.wt * rt
local angle = (i + 0.5) * sector
local ux, uy = cos(angle), sin(angle)
local vx, vy = -uy * wt, ux * wt
ux, uy = ux * rt, uy * rt
if i + 1 == self.direction then
love.graphics.setLineWidth(5)
love.graphics.setColor(0.8, 0.8, 0.8, alpha)
else
love.graphics.setLineWidth(2)
love.graphics.setColor(0.5, 0.5, 0.5, alpha)
end
love.graphics.line(ux - vx, uy - vy, ux + vx, uy + vy)
end
love.graphics.setColor(c)
love.graphics.setLineWidth(lw)
love.graphics.pop()
end
local methods = {
addStick = addStick,
removeStick = removeStick,
addButton = addButton,
removeButton = removeButton,
removeDevice = removeDevice,
update = update,
draw = draw
}
local class = { __index = methods }
local function new()
return setmetatable({
sticks = {},
buttons = {},
aimControls = {
'downright', 'down', 'downleft',
'upleft', 'up', 'upright'
},
x = 0, y = 0,
rt = 0.8, wt = sin(0.8 * sector/2),
direction = 0, length = 0, down = false,
pressed = function(self, name) end,
released = function(self, name) end
}, class)
end
return { new = new, class = class, methods = methods }