-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainDisplay.lua
208 lines (180 loc) · 6.6 KB
/
mainDisplay.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
module(..., package.seeall)
gas = require "gas"
mainDisplay = display.newGroup()
function init_scale_values()
xScaleMin = display.contentHeight/levels.background.contentWidth
yScaleMin = display.contentHeight/levels.background.contentHeight
if xScaleMin > yScaleMin then
xScaleMin = yScaleMin
else
yScaleMin = xScaleMin
end
end
--[[
This function calculates the distance between the two fingers
--]]
local function calculateDelta( previousTouches, event )
local id,touch = next( previousTouches )
if event.id == id then
id,touch = next( previousTouches, id )
assert( id ~= event.id )
end
local dx = touch.x - event.x
local dy = touch.y - event.y
return dx, dy
end
-- Code to Handle zoom on the mainDisplay
-- create a table listener object for the background image
function mainDisplay:touch( event )
local phase = event.phase
local previousTouches = self.previousTouches
-- The number of fingers on the screen
local numTotalTouches = 1
local last_time = 0
--[[ Debugging code to test misclassigied touch ended events.
local prevTouches = 1
local box = display.newRect( 0, 0, 200, 200)
local debug2 = display.newText(prevTouches, 60, 30, "Times New Roman", 48)
local debug3 = display.newText(prevTouches, 0, 30, "Times New Roman", 48)
debug3:setTextColor(0, 0, 0)
debug2:setTextColor(0, 0, 0)
--]]
if ( previousTouches ) then
-- add in total from previousTouches, subtract one if event is already in the array
numTotalTouches = numTotalTouches + self.numPreviousTouches
if previousTouches[event.id] then
numTotalTouches = numTotalTouches - 1
end
end
if "began" == phase then
-- Don't add gas unless you have a single finger on the screen.
-- Need to send gas the
if numTotalTouches == 1 then
levels.scrollTouch(event)
end
-- Very first "began" event
if ( not self.isFocus ) then
-- Subsequent touch events will target button even if they are outside the stageBounds of button
display.getCurrentStage():setFocus( self )
self.isFocus = true
previousTouches = {}
self.previousTouches = previousTouches
self.numPreviousTouches = 0
elseif ( not self.distance ) then
local dx,dy
if previousTouches and ( numTotalTouches ) >= 2 then
dx,dy = calculateDelta( previousTouches, event )
end
-- initialize to distance between two touches
if ( dx and dy ) then
local d = math.sqrt( dx*dx + dy*dy )
if ( d > 0 ) then
self.distance = d
self.xScaleOriginal = self.xScale
self.yScaleOriginal = self.yScale
--print( "distance = " .. self.distance )
end
end
end
if not previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches + 1
end
previousTouches[event.id] = event
elseif "moved" == phase then
if self.isFocus then
if numTotalTouches == 1 then
levels.scrollTouch(event)
end
if ( self.distance ) then
local dx,dy
if previousTouches and ( numTotalTouches ) >= 2 then
dx,dy = calculateDelta( previousTouches, event )
end
if ( dx and dy ) then
local newDistance = math.sqrt( dx*dx + dy*dy )
local scale = newDistance / self.distance
--print( "newDistance(" ..newDistance .. ") / distance(" .. self.distance .. ") = scale(".. scale ..")" )
if ( scale > 0 ) then
local tempXScale = self.xScaleOriginal * scale
local tempYScale = self.yScaleOriginal * scale
---[[
if xScaleMin > tempXScale then
self.xScale = xScaleMin
elseif tempXScale > 1 then
self.xScale = 1
else
self.xScale = tempXScale
end
if yScaleMin > tempYScale then
self.yScale = yScaleMin
elseif tempYScale > 1 then
self.yScale = 1
else
self.yScale = tempYScale
end
--]]
end
end
end
if not previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches + 1
end
previousTouches[event.id] = event
end
elseif "ended" == phase or "cancelled" == phase then
--[[ The idea here is to see if this touch ended signal is generated after an event that had 2 fingers on the screen.
-- If it was then it was the extraneous event, generated when the second of two fingers stopped touching the screen.
-- This however didn't seem to fix the issue.
if numTotalTouches >= 2 then
prevTouches = numTotalTouches
end
if numTotalTouches == 1 then
if prevTouches == 1 then
box:setFillColor(255,0,0)
levels.scrollTouch(event)
else
prevTouches = 1
end
end--]]
--[[
Ugly hack, if a single-touch touch ended event is generated less than 200ms after a multitouch touch ended event,
ignore it as it's caused by the second finger leaving the screen.
--]]
if numTotalTouches >= 2 then
touch_time = system.getTimer()
elseif numTotalTouches == 1 and system.getTimer() - touch_time > 200 then
levels.scrollTouch(event)
end
if self.isFocus then
if previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches - 1
previousTouches[event.id] = nil
end
if ( #previousTouches > 0 ) then
-- must be at least 2 touches remaining to pinch/zoom
self.distance = nil
else
-- previousTouches is empty so no more fingers are touching the screen
-- Allow touch events to be sent normally to the objects they "hit"
display.getCurrentStage():setFocus( nil )
self.isFocus = false
self.distance = nil
self.xScaleOriginal = nil
self.yScaleOriginal = nil
-- reset array
self.previousTouches = nil
self.numPreviousTouches = nil
end
end
end
--
--[[ Debugging code to test a possible way to detect misclassified touch ended events
-- A single finger touch ended event is generated after a multi-touch touch ended signal, resulting in code doing unintended things.
debug2.text = prevTouches
debug3.text = numTotalTouches
box:toFront()
debug2:toFront()
debug3:toFront()
--]]
return true
end