-
Notifications
You must be signed in to change notification settings - Fork 20
/
baton.lua
374 lines (329 loc) · 11.3 KB
/
baton.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
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
local baton = {
_VERSION = 'Baton v1.0.2',
_DESCRIPTION = 'Input library for LÖVE.',
_URL = 'https://github.com/tesselode/baton',
_LICENSE = [[
MIT License
Copyright (c) 2020 Andrew Minnich
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
}
-- string parsing functions --
-- splits a source definition into type and value
-- example: 'button:a' -> 'button', 'a'
local function parseSource(source)
return source:match '(.+):(.+)'
end
-- splits an axis value into axis and direction
-- example: 'leftx-' -> 'leftx', '-'
local function parseAxis(value)
return value:match '(.+)([%+%-])'
end
-- splits a joystick hat value into hat number and direction
-- example: '2rd' -> '2', 'rd'
local function parseHat(value)
return value:match '(%d)(.+)'
end
--[[
-- source functions --
each source function checks the state of one type of input
and returns a value from 0 to 1. for binary controls, such
as keyboard keys and gamepad buttons, they return 1 if the
input is held down and 0 if not. for analog controls, such
as "leftx+" (the left analog stick held to the right), they
return a number from 0 to 1.
source functions are split into keyboard/mouse functions
and joystick/gamepad functions. baton treats these two
categories slightly differently.
]]
local sourceFunction = {keyboardMouse = {}, joystick = {}}
-- checks whether a keyboard key is down or not
function sourceFunction.keyboardMouse.key(key)
return love.keyboard.isDown(key) and 1 or 0
end
-- checks whether a keyboard key is down or not,
-- but it takes a scancode as an input
function sourceFunction.keyboardMouse.sc(sc)
return love.keyboard.isScancodeDown(sc) and 1 or 0
end
-- checks whether a mouse buttons is down or not.
-- note that baton doesn't detect mouse movement, just the buttons
function sourceFunction.keyboardMouse.mouse(button)
return love.mouse.isDown(tonumber(button)) and 1 or 0
end
-- checks the position of a joystick axis
function sourceFunction.joystick.axis(joystick, value)
local axis, direction = parseAxis(value)
-- "a and b or c" is ok here because b will never be boolean
value = tonumber(axis) and joystick:getAxis(tonumber(axis))
or joystick:getGamepadAxis(axis)
if direction == '-' then value = -value end
return value > 0 and value or 0
end
-- checks whether a joystick button is held down or not
-- can take a number or a GamepadButton string
function sourceFunction.joystick.button(joystick, button)
-- i'm intentionally not using the "a and b or c" idiom here
-- because joystick.isDown returns a boolean
if tonumber(button) then
return joystick:isDown(tonumber(button)) and 1 or 0
else
return joystick:isGamepadDown(button) and 1 or 0
end
end
-- checks the direction of a joystick hat
function sourceFunction.joystick.hat(joystick, value)
local hat, direction = parseHat(value)
return joystick:getHat(hat) == direction and 1 or 0
end
--[[
-- player class --
the player object takes a configuration table and handles input
accordingly. it's called a "player" because it makes sense to use
multiple of these for each player in a multiplayer game, but
you can use separate player objects to organize inputs
however you want.
]]
local Player = {}
Player.__index = Player
-- internal functions --
-- sets the player's config to a user-defined config table
-- and sets some defaults if they're not already defined
function Player:_loadConfig(config)
if not config then
error('No config table provided', 4)
end
if not config.controls then
error('No controls specified', 4)
end
config.pairs = config.pairs or {}
config.deadzone = config.deadzone or .5
config.squareDeadzone = config.squareDeadzone or false
self.config = config
end
-- initializes a control object for each control defined in the config
function Player:_initControls()
self._controls = {}
for controlName, sources in pairs(self.config.controls) do
self._controls[controlName] = {
sources = sources,
rawValue = 0,
value = 0,
down = false,
downPrevious = false,
pressed = false,
released = false,
}
end
end
-- initializes an axis pair object for each axis pair defined in the config
function Player:_initPairs()
self._pairs = {}
for pairName, controls in pairs(self.config.pairs) do
self._pairs[pairName] = {
controls = controls,
rawX = 0,
rawY = 0,
x = 0,
y = 0,
down = false,
downPrevious = false,
pressed = false,
released = false,
}
end
end
function Player:_init(config)
self:_loadConfig(config)
self:_initControls()
self:_initPairs()
self._activeDevice = 'none'
end
--[[
detects the active device (keyboard/mouse or joystick).
if the keyboard or mouse is currently being used, joystick
inputs will be ignored. this is to prevent slight axis movements
from adding errant inputs when someone's using the keyboard.
the active device is saved to player._activeDevice, which is then
used throughout the rest of the update loop to check only
keyboard or joystick inputs.
]]
function Player:_setActiveDevice()
-- if the joystick is unset, then we should make sure _activeDevice
-- isn't "joy" anymore, otherwise there will be an error later
-- when we try to query a joystick that isn't there
if self._activeDevice == 'joy' and not self.config.joystick then
self._activeDevice = 'none'
end
for _, control in pairs(self._controls) do
for _, source in ipairs(control.sources) do
local type, value = parseSource(source)
if sourceFunction.keyboardMouse[type] then
if sourceFunction.keyboardMouse[type](value) > self.config.deadzone then
self._activeDevice = 'kbm'
return
end
elseif self.config.joystick and sourceFunction.joystick[type] then
if sourceFunction.joystick[type](self.config.joystick, value) > self.config.deadzone then
self._activeDevice = 'joy'
end
end
end
end
end
--[[
gets the value of a control by running the appropriate source functions
for all of its sources. does not apply deadzone.
]]
function Player:_getControlRawValue(control)
local rawValue = 0
for _, source in ipairs(control.sources) do
local type, value = parseSource(source)
if sourceFunction.keyboardMouse[type] and self._activeDevice == 'kbm' then
if sourceFunction.keyboardMouse[type](value) == 1 then
return 1
end
elseif sourceFunction.joystick[type] and self._activeDevice == 'joy' then
rawValue = rawValue + sourceFunction.joystick[type](self.config.joystick, value)
if rawValue >= 1 then
return 1
end
end
end
return rawValue
end
--[[
updates each control in a player. saves the value with and without deadzone
and the down/pressed/released state.
]]
function Player:_updateControls()
for _, control in pairs(self._controls) do
control.rawValue = self:_getControlRawValue(control)
control.value = control.rawValue >= self.config.deadzone and control.rawValue or 0
control.downPrevious = control.down
control.down = control.value > 0
control.pressed = control.down and not control.downPrevious
control.released = control.downPrevious and not control.down
end
end
--[[
updates each axis pair in a player. saves the value with and without deadzone
and the down/pressed/released state.
]]
function Player:_updatePairs()
for _, pair in pairs(self._pairs) do
-- get raw x and y
local l = self._controls[pair.controls[1]].rawValue
local r = self._controls[pair.controls[2]].rawValue
local u = self._controls[pair.controls[3]].rawValue
local d = self._controls[pair.controls[4]].rawValue
pair.rawX, pair.rawY = r - l, d - u
-- limit to 1
local len = math.sqrt(pair.rawX^2 + pair.rawY^2)
if len > 1 then
pair.rawX, pair.rawY = pair.rawX / len, pair.rawY / len
end
-- deadzone
if self.config.squareDeadzone then
pair.x = math.abs(pair.rawX) > self.config.deadzone and pair.rawX or 0
pair.y = math.abs(pair.rawY) > self.config.deadzone and pair.rawY or 0
else
pair.x = len > self.config.deadzone and pair.rawX or 0
pair.y = len > self.config.deadzone and pair.rawY or 0
end
-- down/pressed/released
pair.downPrevious = pair.down
pair.down = pair.x ~= 0 or pair.y ~= 0
pair.pressed = pair.down and not pair.downPrevious
pair.released = pair.downPrevious and not pair.down
end
end
-- public API --
-- checks for changes in inputs
function Player:update()
self:_setActiveDevice()
self:_updateControls()
self:_updatePairs()
end
-- gets the value of a control or axis pair without deadzone applied
function Player:getRaw(name)
if self._pairs[name] then
return self._pairs[name].rawX, self._pairs[name].rawY
elseif self._controls[name] then
return self._controls[name].rawValue
else
error('No control with name "' .. name .. '" defined', 3)
end
end
-- gets the value of a control or axis pair with deadzone applied
function Player:get(name)
if self._pairs[name] then
return self._pairs[name].x, self._pairs[name].y
elseif self._controls[name] then
return self._controls[name].value
else
error('No control with name "' .. name .. '" defined', 3)
end
end
-- gets whether a control or axis pair is "held down"
function Player:down(name)
if self._pairs[name] then
return self._pairs[name].down
elseif self._controls[name] then
return self._controls[name].down
else
error('No control with name "' .. name .. '" defined', 3)
end
end
-- gets whether a control or axis pair was pressed this frame
function Player:pressed(name)
if self._pairs[name] then
return self._pairs[name].pressed
elseif self._controls[name] then
return self._controls[name].pressed
else
error('No control with name "' .. name .. '" defined', 3)
end
end
-- gets whether a control or axis pair was released this frame
function Player:released(name)
if self._pairs[name] then
return self._pairs[name].released
elseif self._controls[name] then
return self._controls[name].released
else
error('No control with name "' .. name .. '" defined', 3)
end
end
--[[
gets the currently active device (either "kbm", "joy", or "none").
this is useful for displaying instructional text. you may have
a menu that says "press ENTER to confirm" or "press A to confirm"
depending on whether the player is using their keyboard or gamepad.
this function allows you to detect which they used most recently.
]]
function Player:getActiveDevice()
return self._activeDevice
end
-- main functions --
-- creates a new player with the user-provided config table
function baton.new(config)
local player = setmetatable({}, Player)
player:_init(config)
return player
end
return baton