-
Notifications
You must be signed in to change notification settings - Fork 1
/
touch.lua
99 lines (86 loc) · 3 KB
/
touch.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
-- Start / stop
function Client:startTouch()
self.touches = {} -- `touchId` -> touch
self.numTouches = 0 -- Number of current touches (including 'just released' ones)
self.maxNumTouches = 0 -- Max number of touches in the current gesture
self.allTouchesReleased = false -- Whether we are at the end of a gesture
self.gestureId = nil -- Unique id for this gesture
self.gestureStolen = false
end
-- Update
function Client:updateTouches()
-- Clear old released touches
for touchId, touch in pairs(self.touches) do
if touch.released then
self.touches[touchId] = nil
end
end
-- Track active touches
local activeTouches = {}
ui.setUpdatesPaused(false)
for _, touchId in ipairs(love.touch.getTouches()) do
activeTouches[touchId] = true
if not self.performing then
ui.setUpdatesPaused(true)
end
local screenX, screenY = love.touch.getPosition(touchId)
local x, y = self.viewTransform:inverseTransformPoint(screenX, screenY)
local touch = self.touches[touchId]
if not touch then -- Press
touch = {}
touch.initialX, touch.initialY = x, y
touch.x, touch.y = x, y
touch.dx, touch.dy = 0, 0
touch.screenX, touch.screenY = screenX, screenY
touch.initialScreenX, touch.initialScreenY = screenX, screenY
touch.screenDX, touch.screenDY = 0, 0
touch.pressTime = love.timer.getTime()
touch.pressed = true
touch.released = false
touch.moved = false
self.touches[touchId] = touch
else -- Move
touch.pressed = false
touch.dx, touch.dy = x - touch.x, y - touch.y
touch.x, touch.y = x, y
touch.screenDX, touch.screenDY = screenX - touch.screenX, screenY - touch.screenY
touch.screenX, touch.screenY = screenX, screenY
if not (touch.screenX == touch.initialScreenX and touch.screenY == touch.initialScreenY) then
touch.moved = true
end
end
end
-- Just released
for touchId, touch in pairs(self.touches) do
if not activeTouches[touchId] then
touch.released = true
end
end
-- End of gesture?
self.allTouchesReleased = false
for touchId, touch in pairs(self.touches) do
if touch.released then
self.allTouchesReleased = true
else
self.allTouchesReleased = false
break
end
end
-- Count
self.numTouches = 0
for touchId, touch in pairs(self.touches) do
self.numTouches = self.numTouches + 1
end
if self.numTouches == 0 then
self.maxNumTouches = 0
else
self.maxNumTouches = math.max(self.maxNumTouches, self.numTouches)
end
-- Gesture id
if self.numTouches > 0 then
self.gestureId = self.gestureId or util.uuid()
else
self.gestureId = nil
self.gestureStolen = false
end
end