-
Notifications
You must be signed in to change notification settings - Fork 4
/
controls.lua
79 lines (65 loc) · 2.06 KB
/
controls.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
local controls = {}
local l = require('lume')
controls.readTilt = function() return 0, 0, 0 end -- stub
controls.frozen = false -- ability to freeze tilt reading
local tiltP = {0,0,0}
local activeTouches = {}
function controls.load()
-- finding accelerometer
local joysticks = love.joystick.getJoysticks()
for _, joystick in ipairs(joysticks) do
if joystick:getName() == 'Android Accelerometer' then
controls.readTilt = function()
return joystick:getAxis(1), joystick:getAxis(2), joystick:getAxis(3)
end
break
end
end
end
local lastId = 0
local function nextId()
lastId = lastId + 1
return lastId
end
function controls.process(s)
local frameTouches = {} -- temp map for pruning non-active touches
s.touches = {}
local touches = love.touch.getTouches() -- returns array of 'light userdata' ids, guaranteed to be unique only for duration of touch
-- udid is user data id that Love2D provides, it is not Lua datatype and not serializable
-- seqid is integer id that is locally generated and sequential
for _,udid in ipairs(touches) do
local x, y = love.touch.getPosition(udid)
local seqid = activeTouches[udid] or nextId()
activeTouches[udid] = seqid
frameTouches[udid] = true
s.touches[seqid] = {x, y}
end
-- inject mouse as touch on left click
if love.system.getOS() ~= 'Android' and love.mouse.isDown(1) then
local id = #s.touches + 1
s.touches[id] = {love.mouse.getPosition()}
frameTouches[id] = true
end
-- prune active touches that dissapeared from this frame list of touches
for udid, seqid in pairs(activeTouches) do
if not frameTouches[udid] then
activeTouches[udid] = nil
end
end
if controls.frozen then
s.tilt = tiltP
s.tilt.lp = tiltP
else
s.tilt = {0,0,0}
s.tilt = {controls.readTilt()}
-- simple IIR low-pass filtering of tilt
local a0 = 0.05
s.tilt.lp = {}
for i,v in ipairs(s.tilt) do
s.tilt.lp[i] = s.tilt[i] * a0 + tiltP[i] * (1 - a0)
tiltP[i] = s.tilt.lp[i]
end
end
return s
end
return controls