forked from Lydzje/lynput
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLynput.lua
More file actions
640 lines (534 loc) · 18.3 KB
/
Lynput.lua
File metadata and controls
640 lines (534 loc) · 18.3 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
--
-- Lynput
--
-- Copyright (c) 2019, Lydzje
--
-- This module is free software; you can redistribute it and/or modify it under
-- the terms of the MIT license. See LICENSE for details.
--
local Lynput = {}
Lynput.__index = Lynput
setmetatable(
Lynput,
{
__call = function (cls, ...)
return cls.new(...)
end,
}
)
Lynput.s_lynputs = {}
Lynput.s_idCount = 0
Lynput.s_count = 0
Lynput.s_reservedNames = {
-- Reserved by Lua
"and", "break", "do", "else", "elseif", "end", "false", "for",
"function", "if", "in", "local", "nil", "not", "or", "repeat",
"return", "then", "true", "until", "while",
-- Reserved by Lynput
"inputsSet", "gpad", "gpadDeadZone", "id", "remove", "attachGamepad",
"bind", "unbind", "unbindAll", "removeAction", "update"
}
Lynput.s_reservedCharacters = {
"+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=", "<", ">",
"=", "(", ")", "{", "}", "[", "]", ";", ":", ",", ".", "..", "..."
}
----------------
-- DICTIONARIES
----------------
Lynput.s_mouseButtons = {
["1"]="LMB", ["2"]="RMB", ["3"]="MMB", ["x1"]="MB4", ["x2"]="MB5"
}
Lynput.s_gamepadButtons = {
["a"]="G_A", ["b"]="G_B", ["x"]="G_X", ["y"]="G_Y",
["back"]="G_BACK", ["guide"]="G_GUIDE", ["start"]="G_START",
["leftstick"]="G_LEFTSTICK", ["rightstick"]="G_RIGHTSTICK",
["leftshoulder"]="G_LB", ["rightshoulder"]="G_RB",
["dpup"]="G_DPAD_UP", ["dpdown"]="G_DPAD_DOWN",
["dpleft"]="G_DPAD_LEFT", ["dpright"]="G_DPAD_RIGHT"
}
Lynput.s_gamepadAxes = {
["leftx"]="G_LEFTSTICK_X", ["lefty"]="G_LEFTSTICK_Y",
["rightx"]="G_RIGHTSTICK_X", ["righty"]="G_RIGHTSTICK_Y",
["triggerleft"]="G_LT", ["triggerright"]="G_RT"
}
-----------------------
-- FUTURE DICTIONARIES
-----------------------
-- Lynput.s_mouse_axes = {
-- "wd", "wu"
-- }
function Lynput.new()
local self = setmetatable({}, Lynput)
-- Maps Lynput inputs and states to actions, inputsSet[state][action]
self.inputsSet = {}
self.gpad = nil
-- TODO: Different deadzones for joysticks and triggers
self.gpadDeadZone = 30
self.id = tostring(Lynput.s_idCount)
Lynput.s_lynputs[self.id] = self
Lynput.s_idCount = Lynput.s_idCount + 1
Lynput.s_count = Lynput.s_count + 1
return self
end
function Lynput.load_key_callbacks()
function love.keypressed(key) Lynput.onkeypressed(key) end
function love.keyreleased(key) Lynput.onkeyreleased(key) end
end
function Lynput.load_mouse_callbacks()
function love.mousepressed(x, y, button, istouch) Lynput.onmousepressed(button) end
function love.mousereleased(x, y, button, istouch) Lynput.onmousereleased(button) end
end
function Lynput.load_gamepad_callbacks()
function love.gamepadpressed(joystick, button) Lynput.ongamepadpressed(joystick:getID(), button) end
function love.gamepadreleased(joystick, button) Lynput.ongamepadreleased(joystick:getID(), button) end
function love.joystickadded(joystick) Lynput.ongamepadadded(joystick) end
end
function Lynput.update_(dt)
for _, lynput in pairs(Lynput.s_lynputs) do
lynput:update(dt)
end -- for each lynput
end
local function _isActionValid(action)
if type(action) ~= "string" then
return false
end -- if not string
for _, reservedName in ipairs(Lynput.s_reservedNames) do
if reservedName == action then
return false
end -- if action name is reserved
end -- for each reserved name
for _, reservedChar in ipairs(Lynput.s_reservedCharacters) do
if string.find(action, "%" .. reservedChar) then
return false
end -- if action name contains reserved characters
end -- for each reserved character
return true
end
local function _isCommandValid(command)
local stateValid, inputValid = false, false
local state, input = string.match(command, "(.+)%s(.+)")
if not state or not input then
goto exit
end -- if state or input are nil
-- Process state
stateValid =
state == "release" or
state == "press" or
state == "hold"
if not stateValid then
local min, max = string.match(state, "(.+)%:(.+)")
min = tonumber(min)
max = tonumber(max)
if not min or not max then
goto exit
end -- if min or max are nil
stateValid =
min < max and
min >= -100 and
max <= 100
end -- if state is not meant for buttons
-- Process input
if input == "any" then
inputValid = true
goto exit
end -- if input == any
for _, button in pairs(Lynput.s_mouseButtons) do
if button == input then
inputValid = true
goto exit
end -- if button == input
end -- for each Lynput mouse button
for _, button in pairs(Lynput.s_gamepadButtons) do
if button == input then
inputValid = true
goto exit
end -- if button == input
end -- for each Lynput gamepad button
for _, axis in pairs(Lynput.s_gamepadAxes) do
if axis == input then
inputValid = true
goto exit
end -- if axis == input
end -- for each Lynput gamepad axis
-- TODO: Touch screen
inputValid = pcall(love.keyboard.getScancodeFromKey, input)
::exit::
return stateValid and inputValid, state, input
end
function Lynput:remove()
Lynput.s_lynputs[self.id] = nil
Lynput.s_count = Lynput.s_count - 1
end
-- @param gamepad is a Lynput gamepad string name (e.g., "GPAD_1", "GPAD_2", ..., "GPAD_N")
function Lynput:attachGamepad(gamepad)
-- TODO: More code, this needs to check if the parameter given is like expected
self.gpad = gamepad
end
function Lynput:getAxis(axis)
for loveAxis, lynputAxis in pairs(Lynput.s_gamepadAxes) do
if axis == loveAxis or axis == lynputAxis then
return Lynput[self.gpad]:getGamepadAxis(loveAxis) * 100
end
end
error(
"Axis->" .. axis .. " is not a valid name. Use LÖVE or Lynput names for axes."
)
end
function Lynput:bind(action, commands)
-- Type checking for argument #1
assert(
type(action) == "string",
"bad argument #1 to 'Lynput:bind' (string expected, got " .. type(action) .. ")" ..
"\nCheck the stack traceback to know where the invalid arguments have been passed"
)
-- Transforms 1 command to a table
if type(commands) ~= "table" then
local command = commands
commands = {}
commands[1] = command
end -- if only one command was given
-- Type checking for argument #2
for i, command in ipairs(commands) do
assert(
type(command) == "string",
"bad argument #2 to 'Lynput:bind' (string or table of strings expected, got " ..
type(command) .. " in element #" .. i .. ")" ..
"\nCheck the stack traceback to know where the invalid arguments have been passed"
)
end -- for each element in commands table
-- Process command
for _, command in ipairs(commands) do
-- Is action valid?
assert(
_isActionValid(action),
"Could not bind command->" .. command ..
-- TODO: Check documentation for valid actions message
" to action->" .. action .. ", the action is not valid."
)
-- Is command valid?
local commandValid, state, input = _isCommandValid(command)
-- TODO: Use a "check the manual for commands format" instead of explaining it here
assert(
commandValid,
"Could not bind command->" .. command ..
" to action->" .. action .. ", the command is not valid." ..
"\n\nCommands should be formated as follows: \n\n" ..
" COMMAND = STATE INPUT (with a space in between)\n" ..
" STATE =\n" ..
" for buttons -> press, release or hold\n" ..
" for analog inputs -> x:y (with a colon in between) where x and y are numbers\n" ..
" INPUT = check the manual for all availible inputs\n\n"
)
if self[action] == nil then
self[action] = false
end -- if action not set
if not self.inputsSet[input] then
self.inputsSet[input] = {}
end -- if input hasn't already been set
self.inputsSet[input][state] = action
end -- for each command
end
function Lynput:unbind(action, commands)
-- Type checking for argument #1
assert(
type(action) == "string",
"bad argument #1 to 'Lynput:unbind' (string expected, got " .. type(action) .. ")" ..
"\nCheck the stack traceback to know where the invalid arguments have been passed"
)
-- Transforms 1 command to a table
if type(commands) ~= "table" then
local command = commands
commands = {}
commands[1] = command
end -- if only one command was given
-- Type checking for argument #2
for i, command in ipairs(commands) do
assert(
type(command) == "string",
"bad argument #2 to 'Lynput:unbind' (string or table of strings expected, got " ..
type(command) .. " in element #" .. i .. ")" ..
"\nCheck the stack traceback to know where the invalid arguments have been passed"
)
end -- for each element in commands table
-- Process command
for _, command in ipairs(commands) do
-- Is action set?
-- FIXME: Exception when indexing nil values are not being handled, fix everywhere
assert(
self[action] ~= nil,
"Could not unbind command->" .. command ..
" to action->" .. action .. ", the action is not set."
)
-- Is command set?
local state, input = string.match(command, "(.+)%s(.+)")
assert(
self.inputsSet[input][state],
"Could not unbind command->" .. command ..
" to action->" .. action .. ", the command is not set."
)
self.inputsSet[input][state] = nil
local inputStates = self.inputsSet[input]
local statesNum = 0
for state, _ in pairs(self.inputsSet[input]) do
statesNum = statesNum + 1
end -- for each state
if statesNum == 0 then
self.inputsSet[input] = nil
end -- if there are no more states set
self[action] = false
end -- for each command
end
function Lynput:unbindAll(action)
-- Type checking for argument #1
assert(
type(action) == "string",
"bad argument #1 to 'Lynput:unbindAll' (string expected, got " .. type(action) .. ")" ..
"\nCheck the stack traceback to know where the invalid arguments have been passed"
)
-- Is action set?
assert(
self[action] ~= nil,
"Could not unbind all commands in action->" .. action ..
", the action is not set."
)
for inputSet, states in pairs(self.inputsSet) do
for state, actionSet in pairs(states) do
if actionSet == action then
self.inputsSet[inputSet][state] = nil
end -- if actionSet == action
end -- for each inputSet state
end -- for each input set
self[action] = false
end
function Lynput:removeAction(action)
-- Type checking for argument #1
assert(
type(action) == "string",
"bad argument #1 to 'Lynput:removeAction' (string expected, got " .. type(action) .. ")" ..
"\nCheck the stack traceback to know where the invalid arguments have been passed"
)
-- Is action set?
assert(
self[action] ~= nil,
"Could not remove action->" .. action ..
", this action does not exist."
)
self:unbindAll(action)
self[action] = nil
end
function Lynput:update(dt)
-- It's not possible to iterate actions through "self" because it
-- also contains the inputsSet table and other data
for _, states in pairs(self.inputsSet) do
for state, actionSet in pairs(states) do
if state == "press" or state == "release" then
self[actionSet] = false
end -- if state ~= hold
end -- for each state
end -- for each input set
if Lynput[self.gpad] then
for loveAxis, lynputAxis in pairs(Lynput.s_gamepadAxes) do
if self.inputsSet[lynputAxis] then
local val = Lynput[self.gpad]:getGamepadAxis(loveAxis) * 100
for interval, action in pairs(self.inputsSet[lynputAxis]) do
local min, max = string.match(interval, "(.+)%:(.+)")
min = tonumber(min)
max = tonumber(max)
if val >= min and (math.abs(val) > self.gpadDeadZone) and val <= max then
self[action] = true
else
self[action] = false
end -- if val is in interval
end -- for each interval
end -- if the axis is set
end -- for each axis
end -- if the gamepad has been added
end
---------------------------------
-- KEYBOARD CALLBACKS
---------------------------------
function Lynput.onkeypressed(key)
for _, lynput in pairs(Lynput.s_lynputs) do
if lynput.inputsSet["any"] then
if lynput.inputsSet["any"]["press"] then
local action = lynput.inputsSet["any"]["press"]
lynput[action] = true
end -- if press_any is set
if lynput.inputsSet["any"]["hold"] then
local action = lynput.inputsSet["any"]["hold"]
lynput[action] = true
end -- if hold_any is set
end -- if "any" is set
if lynput.inputsSet[key] then
if lynput.inputsSet[key]["press"] then
local action = lynput.inputsSet[key]["press"]
lynput[action] = true
end -- if press_key is set
if lynput.inputsSet[key]["hold"] then
local action = lynput.inputsSet[key]["hold"]
lynput[action] = true
end -- if hold_key is set
end -- if key set
end -- for each lynput
end
function Lynput.onkeyreleased(key)
for _, lynput in pairs(Lynput.s_lynputs) do
if lynput.inputsSet["any"] then
if lynput.inputsSet["any"]["release"] then
local action = lynput.inputsSet["any"]["release"]
lynput[action] = true
end -- if release_any is set
if lynput.inputsSet["any"]["hold"] then
local action = lynput.inputsSet["any"]["hold"]
lynput[action] = false
end -- if hold_any is set
end -- if "any" is set
if lynput.inputsSet[key] then
if lynput.inputsSet[key]["release"] then
local action = lynput.inputsSet[key]["release"]
lynput[action] = true
end -- if release_key is set
if lynput.inputsSet[key]["hold"] then
local action = lynput.inputsSet[key]["hold"]
lynput[action] = false
end -- if hold_key is set
end -- if key is set
end -- for each lynput
end
--------------------------------------
-- MOUSE CALLBACKS
--------------------------------------
function Lynput.onmousepressed(button)
-- Translate LÖVE button to Lynput button
button = Lynput.s_mouseButtons[tostring(button)]
-- Process button
for _, lynput in pairs(Lynput.s_lynputs) do
if lynput.inputsSet["any"] then
if lynput.inputsSet["any"]["press"] then
local action = lynput.inputsSet["any"]["press"]
lynput[action] = true
end -- if press_any is set
if lynput.inputsSet["any"]["hold"] then
local action = lynput.inputsSet["any"]["hold"]
lynput[action] = true
end -- if hold_any is set
end -- if "any" is set
if lynput.inputsSet[button] then
if lynput.inputsSet[button]["press"] then
local action = lynput.inputsSet[button]["press"]
lynput[action] = true
end -- if press_button is set
if lynput.inputsSet[button]["hold"] then
local action = lynput.inputsSet[button]["hold"]
lynput[action] = true
end -- if hold_button is set
end -- if button is set
end -- for each lynput
end
function Lynput.onmousereleased(button)
-- Translate LÖVE button to Lynput button
button = Lynput.s_mouseButtons[tostring(button)]
-- Process button
for _, lynput in pairs(Lynput.s_lynputs) do
if lynput.inputsSet["any"] then
if lynput.inputsSet["any"]["release"] then
local action = lynput.inputsSet["any"]["release"]
lynput[action] = true
end -- if release_any is set
if lynput.inputsSet["any"]["hold"] then
local action = lynput.inputsSet["any"]["hold"]
lynput[action] = false
end -- if hold_any is set
end -- if "any" is set
if lynput.inputsSet[button] then
if lynput.inputsSet[button]["release"] then
local action = lynput.inputsSet[button]["release"]
lynput[action] = true
end -- if press_button is set
if lynput.inputsSet[button]["hold"] then
local action = lynput.inputsSet[button]["hold"]
lynput[action] = false
end -- if hold_button is set
end -- if button is set
end -- for each lynput
end
---------------------------------------------------
-- GAMEPAD CALLBACKS
---------------------------------------------------
function Lynput.ongamepadpressed(gamepadID, button)
-- Translate LÖVE button to Lynput button
button = Lynput.s_gamepadButtons[button]
-- Process Lynput button
for _, lynput in pairs(Lynput.s_lynputs) do
if lynput.gpad then
if Lynput[lynput.gpad]:getID() == gamepadID then
if lynput.inputsSet["any"] then
if lynput.inputsSet["any"]["press"] then
local action = lynput.inputsSet["any"]["press"]
lynput[action] = true
end -- if press_any is set
if lynput.inputsSet["any"]["hold"] then
local action = lynput.inputsSet["any"]["hold"]
lynput[action] = true
end -- if hold_any is set
end -- if "any" is set
if lynput.inputsSet[button] then
if lynput.inputsSet[button]["press"] then
local action = lynput.inputsSet[button]["press"]
lynput[action] = true
end -- if press_button is set
if lynput.inputsSet[button]["hold"] then
local action = lynput.inputsSet[button]["hold"]
lynput[action] = true
end -- if hold_button is set
end -- if button is set
end -- if gamepad is set
end -- if lynput has a gamepad attached
end -- for each lynput
end
function Lynput.ongamepadreleased(gamepadID, button)
-- Translate LÖVE button to Lynput button
button = Lynput.s_gamepadButtons[button]
-- Process Lynput button
for _, lynput in pairs(Lynput.s_lynputs) do
if lynput.gpad then
if Lynput[lynput.gpad]:getID() == gamepadID then
if lynput.inputsSet["any"] then
if lynput.inputsSet["any"]["release"] then
local action = lynput.inputsSet["any"]["release"]
lynput[action] = true
end -- if release_any is set
if lynput.inputsSet["any"]["hold"] then
local action = lynput.inputsSet["any"]["hold"]
lynput[action] = false
end -- if hold_any is set
end -- if "any" is set
if lynput.inputsSet[button] then
if lynput.inputsSet[button]["release"] then
local action = lynput.inputsSet[button]["release"]
lynput[action] = true
end -- if release_button is set
if lynput.inputsSet[button]["hold"] then
local action = lynput.inputsSet[button]["hold"]
lynput[action] = false
end -- if hold_button is set
end -- if button is set
end -- if gamepad is set
end -- if lynput has a gamepad attached
end -- for each lynput
end
function Lynput.ongamepadadded(gamepad)
local gamepadID = gamepad:getID()
local i = 1
local gpad = "GPAD_1"
while Lynput[gpad] do
if Lynput[gpad]:getID() == gamepadID then
return
end -- if gamepadID is already assigned
i = i +1
gpad = "GPAD_" .. i
end -- while gpad exists
-- gpad does no exists, so we assign the new gamepad to it
Lynput[gpad] = gamepad
end
return Lynput