2012-11-11 06:00:52 +00:00
|
|
|
require "ui/geometry"
|
|
|
|
|
|
|
|
GestureRange = {
|
|
|
|
ges = nil,
|
2013-03-02 15:11:23 +00:00
|
|
|
-- spatial range limits the gesture emitting position
|
2012-11-11 06:00:52 +00:00
|
|
|
range = nil,
|
2013-03-02 15:11:23 +00:00
|
|
|
-- temproal range limits the gesture emitting rate
|
|
|
|
rate = nil,
|
2013-03-06 13:04:33 +00:00
|
|
|
-- span limits of this gesture
|
2013-03-06 14:36:51 +00:00
|
|
|
scale = nil,
|
2012-11-11 06:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function GestureRange:new(o)
|
|
|
|
local o = o or {}
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
function GestureRange:match(gs)
|
|
|
|
if gs.ges ~= self.ges then
|
|
|
|
return false
|
|
|
|
end
|
2013-03-06 13:04:33 +00:00
|
|
|
if self.range then
|
|
|
|
if not self.range:contains(gs.pos) then
|
|
|
|
return false
|
2013-03-02 15:11:23 +00:00
|
|
|
end
|
2012-11-11 06:00:52 +00:00
|
|
|
end
|
2013-03-06 13:04:33 +00:00
|
|
|
if self.rate then
|
|
|
|
local last_time = self.last_time or TimeVal:new{}
|
|
|
|
if gs.time - last_time > TimeVal:new{usec = 1000000 / self.rate} then
|
|
|
|
self.last_time = gs.time
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2013-03-06 14:36:51 +00:00
|
|
|
if self.scale then
|
|
|
|
if self.scale[1] > gs.span or self.scale[2] < gs.span then
|
2013-03-06 13:04:33 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
2012-11-11 06:00:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
2013-03-26 12:59:47 +00:00
|
|
|
Current detectable gestures:
|
|
|
|
* tap
|
2012-12-30 05:44:55 +00:00
|
|
|
* pan
|
2013-03-26 12:59:47 +00:00
|
|
|
* hold
|
2012-12-30 05:44:55 +00:00
|
|
|
* swipe
|
2013-03-28 13:06:13 +00:00
|
|
|
* pinch
|
|
|
|
* spread
|
|
|
|
* rotate
|
2013-03-26 12:59:47 +00:00
|
|
|
* double_tap
|
2013-03-28 13:06:13 +00:00
|
|
|
* inward_pan
|
|
|
|
* outward_pan
|
2013-03-26 12:59:47 +00:00
|
|
|
* pan_release
|
|
|
|
* two_finger_tap
|
|
|
|
* two_finger_pan
|
|
|
|
* two_finger_swipe
|
|
|
|
* two_finger_pan_release
|
2012-12-30 05:44:55 +00:00
|
|
|
|
2013-02-02 06:36:29 +00:00
|
|
|
You change the state machine by feeding it touch events, i.e. calling
|
|
|
|
GestureDetector:feedEvent(tev).
|
2012-11-11 06:00:52 +00:00
|
|
|
|
2013-02-26 04:32:46 +00:00
|
|
|
a touch event should have following format:
|
2013-02-02 06:36:29 +00:00
|
|
|
tev = {
|
|
|
|
slot = 1,
|
|
|
|
id = 46,
|
|
|
|
x = 0,
|
|
|
|
y = 1,
|
|
|
|
timev = TimeVal:new{...},
|
|
|
|
}
|
|
|
|
|
|
|
|
Don't confuse tev with raw evs from kernel, tev is build according to ev.
|
|
|
|
|
|
|
|
GestureDetector:feedEvent(tev) will return a detection result when you
|
|
|
|
feed a touch release event to it.
|
2012-11-11 06:00:52 +00:00
|
|
|
--]]
|
|
|
|
|
2012-11-13 06:12:01 +00:00
|
|
|
GestureDetector = {
|
2012-11-16 23:55:13 +00:00
|
|
|
-- all the time parameters are in us
|
2012-11-17 18:41:26 +00:00
|
|
|
DOUBLE_TAP_INTERVAL = 300 * 1000,
|
2013-03-06 13:04:33 +00:00
|
|
|
TWO_FINGER_TAP_DURATION = 300 * 1000,
|
2012-11-17 18:41:26 +00:00
|
|
|
HOLD_INTERVAL = 1000 * 1000,
|
2012-12-30 05:44:55 +00:00
|
|
|
SWIPE_INTERVAL = 900 * 1000,
|
2012-11-16 00:58:01 +00:00
|
|
|
-- distance parameters
|
|
|
|
DOUBLE_TAP_DISTANCE = 50,
|
2013-03-06 13:04:33 +00:00
|
|
|
TWO_FINGER_TAP_REGION = 20,
|
2012-11-16 00:58:01 +00:00
|
|
|
PAN_THRESHOLD = 50,
|
2013-03-28 13:06:13 +00:00
|
|
|
-- pinch/spread direction table
|
|
|
|
DIRECTION_TABLE = {
|
|
|
|
east = "horizontal",
|
|
|
|
west = "horizontal",
|
|
|
|
north = "vertical",
|
|
|
|
south = "vertical",
|
|
|
|
northeast = "diagonal",
|
|
|
|
northwest = "diagonal",
|
|
|
|
southeast = "diagonal",
|
|
|
|
southwest = "diagonal",
|
|
|
|
},
|
2013-03-02 14:21:18 +00:00
|
|
|
-- states are stored in separated slots
|
|
|
|
states = {},
|
|
|
|
track_ids = {},
|
|
|
|
tev_stacks = {},
|
|
|
|
-- latest feeded touch event in each slots
|
|
|
|
last_tevs = {},
|
|
|
|
first_tevs = {},
|
|
|
|
-- detecting status on each slots
|
|
|
|
detectings = {},
|
|
|
|
-- for single/double tap
|
|
|
|
last_taps = {},
|
2012-11-13 06:12:01 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 13:04:33 +00:00
|
|
|
function GestureDetector:feedEvent(tevs)
|
|
|
|
repeat
|
|
|
|
local tev = table.remove(tevs)
|
|
|
|
if tev then
|
|
|
|
local slot = tev.slot
|
|
|
|
if not self.states[slot] then
|
|
|
|
self:clearState(slot) -- initiate state
|
|
|
|
end
|
|
|
|
local ges = self.states[slot](self, tev)
|
|
|
|
if tev.id ~= -1 then
|
|
|
|
self.last_tevs[slot] = tev
|
|
|
|
end
|
|
|
|
-- return no more than one gesture
|
|
|
|
if ges then return ges end
|
|
|
|
end
|
|
|
|
until tev == nil
|
2012-11-11 06:00:52 +00:00
|
|
|
end
|
|
|
|
|
2013-02-06 05:39:06 +00:00
|
|
|
function GestureDetector:deepCopyEv(tev)
|
2012-12-30 05:44:55 +00:00
|
|
|
return {
|
2013-02-06 05:39:06 +00:00
|
|
|
x = tev.x,
|
|
|
|
y = tev.y,
|
|
|
|
id = tev.id,
|
|
|
|
slot = tev.slot,
|
2012-12-30 05:44:55 +00:00
|
|
|
timev = TimeVal:new{
|
2013-02-06 05:39:06 +00:00
|
|
|
sec = tev.timev.sec,
|
|
|
|
usec = tev.timev.usec,
|
2012-12-30 05:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-11-16 00:58:01 +00:00
|
|
|
--[[
|
|
|
|
tap2 is the later tap
|
2012-12-30 05:44:55 +00:00
|
|
|
--]]
|
2012-11-16 00:58:01 +00:00
|
|
|
function GestureDetector:isDoubleTap(tap1, tap2)
|
2012-11-16 23:55:13 +00:00
|
|
|
local tv_diff = tap2.timev - tap1.timev
|
2012-11-16 00:58:01 +00:00
|
|
|
return (
|
|
|
|
math.abs(tap1.x - tap2.x) < self.DOUBLE_TAP_DISTANCE and
|
|
|
|
math.abs(tap1.y - tap2.y) < self.DOUBLE_TAP_DISTANCE and
|
2012-11-17 18:41:26 +00:00
|
|
|
(tv_diff.sec == 0 and (tv_diff.usec) < self.DOUBLE_TAP_INTERVAL)
|
2012-11-16 00:58:01 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-03-26 12:59:47 +00:00
|
|
|
function GestureDetector:isTwoFingerTap()
|
2013-04-08 06:40:12 +00:00
|
|
|
if self.last_tevs[0] == nil or self.last_tevs[1] == nil then
|
2013-03-26 12:59:47 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
local x_diff0 = math.abs(self.last_tevs[0].x - self.first_tevs[0].x)
|
|
|
|
local x_diff1 = math.abs(self.last_tevs[1].x - self.first_tevs[1].x)
|
|
|
|
local y_diff0 = math.abs(self.last_tevs[0].y - self.first_tevs[0].y)
|
|
|
|
local y_diff1 = math.abs(self.last_tevs[1].y - self.first_tevs[1].y)
|
|
|
|
local tv_diff0 = self.last_tevs[0].timev - self.first_tevs[0].timev
|
|
|
|
local tv_diff1 = self.last_tevs[1].timev - self.first_tevs[1].timev
|
2013-03-06 13:04:33 +00:00
|
|
|
return (
|
|
|
|
x_diff0 < self.TWO_FINGER_TAP_REGION and
|
|
|
|
x_diff1 < self.TWO_FINGER_TAP_REGION and
|
|
|
|
y_diff0 < self.TWO_FINGER_TAP_REGION and
|
|
|
|
y_diff1 < self.TWO_FINGER_TAP_REGION and
|
|
|
|
tv_diff0.sec == 0 and tv_diff0.usec < self.TWO_FINGER_TAP_DURATION and
|
|
|
|
tv_diff1.sec == 0 and tv_diff1.usec < self.TWO_FINGER_TAP_DURATION
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-12-30 05:44:55 +00:00
|
|
|
--[[
|
2013-03-02 14:21:18 +00:00
|
|
|
compare last_pan with first_tev in this slot
|
2013-03-03 14:18:38 +00:00
|
|
|
return pan direction and distance
|
2012-12-30 05:44:55 +00:00
|
|
|
--]]
|
2013-03-28 13:06:13 +00:00
|
|
|
function GestureDetector:getPath(slot)
|
2013-03-03 14:18:38 +00:00
|
|
|
local x_diff = self.last_tevs[slot].x - self.first_tevs[slot].x
|
|
|
|
local y_diff = self.last_tevs[slot].y - self.first_tevs[slot].y
|
|
|
|
local direction = nil
|
|
|
|
local distance = math.sqrt(x_diff*x_diff + y_diff*y_diff)
|
|
|
|
if x_diff == 0 and y_diff == 0 then
|
|
|
|
else
|
2013-03-28 13:06:13 +00:00
|
|
|
local v_direction = y_diff < 0 and "north" or "south"
|
|
|
|
local h_direction = x_diff < 0 and "west" or "east"
|
|
|
|
if math.abs(y_diff) > 0.577*math.abs(x_diff)
|
|
|
|
and math.abs(y_diff) < 1.732*math.abs(x_diff) then
|
|
|
|
direction = v_direction..h_direction
|
|
|
|
elseif (math.abs(x_diff) > math.abs(y_diff)) then
|
|
|
|
direction = h_direction
|
|
|
|
else
|
|
|
|
direction = v_direction
|
|
|
|
end
|
2013-03-03 14:18:38 +00:00
|
|
|
end
|
|
|
|
return direction, distance
|
|
|
|
end
|
|
|
|
|
2013-03-28 13:06:13 +00:00
|
|
|
function GestureDetector:isSwipe(slot)
|
|
|
|
if not self.first_tevs[slot] or not self.last_tevs[slot] then return end
|
2013-03-02 14:21:18 +00:00
|
|
|
local tv_diff = self.first_tevs[slot].timev - self.last_tevs[slot].timev
|
2012-12-30 05:44:55 +00:00
|
|
|
if (tv_diff.sec == 0) and (tv_diff.usec < self.SWIPE_INTERVAL) then
|
2013-03-02 14:21:18 +00:00
|
|
|
local x_diff = self.last_tevs[slot].x - self.first_tevs[slot].x
|
|
|
|
local y_diff = self.last_tevs[slot].y - self.first_tevs[slot].y
|
2013-03-03 14:18:38 +00:00
|
|
|
if x_diff ~= 0 or y_diff ~= 0 then
|
|
|
|
return true
|
2012-12-30 05:44:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-28 13:06:13 +00:00
|
|
|
function GestureDetector:getRotate(orig_point, start_point, end_point)
|
|
|
|
local a = orig_point:distance(start_point)
|
|
|
|
local b = orig_point:distance(end_point)
|
|
|
|
local c = start_point:distance(end_point)
|
|
|
|
return math.acos((a*a + b*b - c*c)/(2*a*b))*180/math.pi
|
|
|
|
end
|
|
|
|
|
2012-11-17 18:41:26 +00:00
|
|
|
--[[
|
|
|
|
Warning! this method won't update self.state, you need to do it
|
|
|
|
in each state method!
|
|
|
|
--]]
|
2013-03-02 14:21:18 +00:00
|
|
|
function GestureDetector:switchState(state_new, tev, param)
|
2012-11-16 00:58:01 +00:00
|
|
|
--@TODO do we need to check whether state is valid? (houqp)
|
2013-03-02 14:21:18 +00:00
|
|
|
return self[state_new](self, tev, param)
|
2012-11-16 00:58:01 +00:00
|
|
|
end
|
|
|
|
|
2013-03-02 14:21:18 +00:00
|
|
|
function GestureDetector:clearState(slot)
|
|
|
|
self.states[slot] = self.initialState
|
|
|
|
self.detectings[slot] = false
|
|
|
|
self.first_tevs[slot] = nil
|
2013-03-06 13:04:33 +00:00
|
|
|
self.last_tevs[slot] = nil
|
2012-11-16 00:58:01 +00:00
|
|
|
end
|
|
|
|
|
2013-03-26 12:59:47 +00:00
|
|
|
function GestureDetector:clearStates()
|
|
|
|
self:clearState(0)
|
|
|
|
self:clearState(1)
|
|
|
|
end
|
|
|
|
|
2013-02-06 05:39:06 +00:00
|
|
|
function GestureDetector:initialState(tev)
|
2013-03-02 14:21:18 +00:00
|
|
|
local slot = tev.slot
|
2013-02-06 05:39:06 +00:00
|
|
|
if tev.id then
|
2012-11-16 00:58:01 +00:00
|
|
|
-- a event ends
|
2013-02-06 05:39:06 +00:00
|
|
|
if tev.id == -1 then
|
2013-03-02 14:21:18 +00:00
|
|
|
self.detectings[slot] = false
|
2012-11-16 00:58:01 +00:00
|
|
|
else
|
2013-03-02 14:21:18 +00:00
|
|
|
self.track_ids[slot] = tev.id
|
2012-11-16 00:58:01 +00:00
|
|
|
end
|
2012-11-11 06:00:52 +00:00
|
|
|
end
|
2013-02-06 05:39:06 +00:00
|
|
|
if tev.x and tev.y then
|
2012-11-17 18:41:26 +00:00
|
|
|
-- user starts a new touch motion
|
2013-03-02 14:21:18 +00:00
|
|
|
if not self.detectings[slot] then
|
|
|
|
self.detectings[slot] = true
|
|
|
|
self.first_tevs[slot] = self:deepCopyEv(tev)
|
2012-11-16 00:58:01 +00:00
|
|
|
-- default to tap state
|
2013-02-06 05:39:06 +00:00
|
|
|
return self:switchState("tapState", tev)
|
2012-11-16 00:58:01 +00:00
|
|
|
end
|
2012-11-11 06:00:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-16 00:58:01 +00:00
|
|
|
--[[
|
|
|
|
this method handles both single and double tap
|
2012-11-17 18:41:26 +00:00
|
|
|
--]]
|
2013-02-06 05:39:06 +00:00
|
|
|
function GestureDetector:tapState(tev)
|
2013-03-26 12:59:47 +00:00
|
|
|
DEBUG("in tap state...")
|
2013-03-02 14:21:18 +00:00
|
|
|
local slot = tev.slot
|
2013-03-26 12:59:47 +00:00
|
|
|
if tev.id == -1 then
|
2013-03-06 13:04:33 +00:00
|
|
|
-- end of tap event
|
2013-03-26 12:59:47 +00:00
|
|
|
if self.detectings[0] and self.detectings[1] then
|
|
|
|
if self:isTwoFingerTap() then
|
|
|
|
local pos0 = Geom:new{
|
|
|
|
x = self.last_tevs[0].x,
|
|
|
|
y = self.last_tevs[0].y,
|
|
|
|
w = 0, h = 0,
|
|
|
|
}
|
|
|
|
local pos1 = Geom:new{
|
|
|
|
x = self.last_tevs[1].x,
|
|
|
|
y = self.last_tevs[1].y,
|
|
|
|
w = 0, h = 0,
|
|
|
|
}
|
|
|
|
local tap_span = pos0:distance(pos1)
|
|
|
|
DEBUG("two-finger tap detected with span", tap_span)
|
|
|
|
self:clearStates()
|
|
|
|
return {
|
|
|
|
ges = "two_finger_tap",
|
|
|
|
pos = pos0:midpoint(pos1),
|
|
|
|
span = tap_span,
|
|
|
|
time = tev.timev,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
self:clearState(slot)
|
|
|
|
end
|
|
|
|
elseif self.last_tevs[slot] ~= nil then
|
2013-03-16 14:48:35 +00:00
|
|
|
return self:handleDoubleTap(tev)
|
2013-03-12 17:18:53 +00:00
|
|
|
else
|
2013-03-06 13:04:33 +00:00
|
|
|
-- last tev in this slot is cleared by last two finger tap
|
|
|
|
self:clearState(slot)
|
|
|
|
return {
|
|
|
|
ges = "tap",
|
|
|
|
pos = Geom:new{
|
|
|
|
x = tev.x,
|
|
|
|
y = tev.y,
|
|
|
|
w = 0, h = 0,
|
|
|
|
},
|
|
|
|
time = tev.timev,
|
|
|
|
}
|
|
|
|
end
|
2013-03-16 14:48:35 +00:00
|
|
|
else
|
|
|
|
return self:handleNonTap(tev)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function GestureDetector:handleDoubleTap(tev)
|
|
|
|
local slot = tev.slot
|
|
|
|
local ges_ev = {
|
|
|
|
-- default to single tap
|
|
|
|
ges = "tap",
|
|
|
|
pos = Geom:new{
|
|
|
|
x = self.last_tevs[slot].x,
|
|
|
|
y = self.last_tevs[slot].y,
|
|
|
|
w = 0, h = 0,
|
|
|
|
},
|
|
|
|
time = tev.timev,
|
|
|
|
}
|
|
|
|
-- cur_tap is used for double tap detection
|
|
|
|
local cur_tap = {
|
|
|
|
x = tev.x,
|
|
|
|
y = tev.y,
|
|
|
|
timev = tev.timev,
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.last_taps[slot] ~= nil and
|
|
|
|
self:isDoubleTap(self.last_taps[slot], cur_tap) then
|
|
|
|
-- it is a double tap
|
|
|
|
self:clearState(slot)
|
|
|
|
ges_ev.ges = "double_tap"
|
|
|
|
self.last_taps[slot] = nil
|
|
|
|
DEBUG("double tap detected in slot", slot)
|
|
|
|
return ges_ev
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set current tap to last tap
|
|
|
|
self.last_taps[slot] = cur_tap
|
|
|
|
|
|
|
|
DEBUG("set up tap timer")
|
|
|
|
-- deadline should be calculated by adding current tap time and the interval
|
|
|
|
local deadline = cur_tap.timev + TimeVal:new{
|
|
|
|
sec = 0, usec = self.DOUBLE_TAP_INTERVAL,
|
|
|
|
}
|
|
|
|
Input:setTimeout(function()
|
|
|
|
DEBUG("in tap timer", self.last_taps[slot] ~= nil)
|
|
|
|
-- double tap will set last_tap to nil so if it is not, then
|
|
|
|
-- user must only tapped once
|
|
|
|
if self.last_taps[slot] ~= nil then
|
|
|
|
self.last_taps[slot] = nil
|
|
|
|
-- we are using closure here
|
|
|
|
DEBUG("single tap detected in slot", slot)
|
|
|
|
return ges_ev
|
|
|
|
end
|
|
|
|
end, deadline)
|
|
|
|
-- we are already at the end of touch event
|
|
|
|
-- so reset the state
|
|
|
|
self:clearState(slot)
|
|
|
|
end
|
|
|
|
|
|
|
|
function GestureDetector:handleNonTap(tev)
|
|
|
|
local slot = tev.slot
|
|
|
|
if self.states[slot] ~= self.tapState then
|
2012-11-16 00:58:01 +00:00
|
|
|
-- switched from other state, probably from initialState
|
|
|
|
-- we return nil in this case
|
2013-03-02 14:21:18 +00:00
|
|
|
self.states[slot] = self.tapState
|
2012-11-17 18:41:26 +00:00
|
|
|
DEBUG("set up hold timer")
|
2013-02-24 19:06:59 +00:00
|
|
|
local deadline = tev.timev + TimeVal:new{
|
2013-03-02 14:21:18 +00:00
|
|
|
sec = 0, usec = self.HOLD_INTERVAL
|
|
|
|
}
|
2012-11-25 07:57:29 +00:00
|
|
|
Input:setTimeout(function()
|
2013-03-02 14:21:18 +00:00
|
|
|
if self.states[slot] == self.tapState then
|
2012-11-16 23:55:13 +00:00
|
|
|
-- timer set in tapState, so we switch to hold
|
2013-03-02 14:21:18 +00:00
|
|
|
DEBUG("hold gesture detected in slot", slot)
|
|
|
|
return self:switchState("holdState", tev, true)
|
2012-11-16 23:55:13 +00:00
|
|
|
end
|
2012-11-17 18:51:34 +00:00
|
|
|
end, deadline)
|
2012-11-16 00:58:01 +00:00
|
|
|
else
|
|
|
|
-- it is not end of touch event, see if we need to switch to
|
|
|
|
-- other states
|
2013-03-02 14:21:18 +00:00
|
|
|
if (tev.x and math.abs(tev.x - self.first_tevs[slot].x) >= self.PAN_THRESHOLD) or
|
|
|
|
(tev.y and math.abs(tev.y - self.first_tevs[slot].y) >= self.PAN_THRESHOLD) then
|
2012-11-16 00:58:01 +00:00
|
|
|
-- if user's finger moved long enough in X or
|
2013-02-26 04:32:46 +00:00
|
|
|
-- Y distance, we switch to pan state
|
2013-02-06 05:39:06 +00:00
|
|
|
return self:switchState("panState", tev)
|
2012-11-16 00:58:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 05:39:06 +00:00
|
|
|
function GestureDetector:panState(tev)
|
2013-03-03 14:18:38 +00:00
|
|
|
DEBUG("in pan state...")
|
2013-03-02 14:21:18 +00:00
|
|
|
local slot = tev.slot
|
2013-02-06 05:39:06 +00:00
|
|
|
if tev.id == -1 then
|
2012-12-30 05:44:55 +00:00
|
|
|
-- end of pan, signal swipe gesture if necessary
|
2013-03-28 13:06:13 +00:00
|
|
|
if self:isSwipe(slot) then
|
2013-03-26 05:17:03 +00:00
|
|
|
if self.detectings[0] and self.detectings[1] then
|
2013-03-28 13:06:13 +00:00
|
|
|
local ges_ev = self:handleTwoFingerPan(tev)
|
2013-03-26 12:59:47 +00:00
|
|
|
self:clearStates()
|
2013-03-28 13:06:13 +00:00
|
|
|
if ges_ev then
|
|
|
|
if ges_ev.ges == "two_finger_pan" then
|
|
|
|
ges_ev.ges = "two_finger_swipe"
|
|
|
|
elseif ges_ev.ges == "inward_pan" then
|
|
|
|
ges_ev.ges = "pinch"
|
|
|
|
elseif ges_ev.ges == "outward_pan" then
|
|
|
|
ges_ev.ges = "spread"
|
|
|
|
end
|
2013-03-28 14:05:17 +00:00
|
|
|
DEBUG(ges_ev.ges, ges_ev.direction, ges_ev.distance, "detected")
|
2013-03-28 13:06:13 +00:00
|
|
|
end
|
|
|
|
return ges_ev
|
2013-03-26 05:17:03 +00:00
|
|
|
else
|
2013-03-28 13:06:13 +00:00
|
|
|
return self:handleSwipe(tev)
|
2013-03-26 05:17:03 +00:00
|
|
|
end
|
2013-03-26 12:59:47 +00:00
|
|
|
else -- if end of pan is not swipe then it must be pan release.
|
2013-03-28 13:06:13 +00:00
|
|
|
return self:handlePanRelease(tev)
|
2012-12-30 05:44:55 +00:00
|
|
|
end
|
2012-11-16 00:58:01 +00:00
|
|
|
else
|
2013-03-02 14:21:18 +00:00
|
|
|
if self.states[slot] ~= self.panState then
|
|
|
|
self.states[slot] = self.panState
|
2012-12-30 05:44:55 +00:00
|
|
|
end
|
2013-03-28 13:06:13 +00:00
|
|
|
return self:handlePan(tev)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function GestureDetector:handleSwipe(tev)
|
|
|
|
local slot = tev.slot
|
|
|
|
local swipe_direction, swipe_distance = self:getPath(slot)
|
|
|
|
local start_pos = Geom:new{
|
|
|
|
x = self.first_tevs[slot].x,
|
|
|
|
y = self.first_tevs[slot].y,
|
|
|
|
w = 0, h = 0,
|
|
|
|
}
|
|
|
|
DEBUG("swipe", swipe_direction, swipe_distance, "detected in slot", slot)
|
|
|
|
self:clearState(slot)
|
|
|
|
return {
|
|
|
|
ges = "swipe",
|
|
|
|
-- use first pan tev coordination as swipe start point
|
|
|
|
pos = start_pos,
|
|
|
|
direction = swipe_direction,
|
|
|
|
distance = swipe_distance,
|
|
|
|
time = tev.timev,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function GestureDetector:handlePan(tev)
|
|
|
|
local slot = tev.slot
|
|
|
|
if self.detectings[0] and self.detectings[1] then
|
|
|
|
return self:handleTwoFingerPan(tev)
|
|
|
|
else
|
|
|
|
local pan_direction, pan_distance = self:getPath(slot)
|
2012-12-30 05:44:55 +00:00
|
|
|
local pan_ev = {
|
|
|
|
ges = "pan",
|
|
|
|
relative = {
|
|
|
|
-- default to pan 0
|
|
|
|
x = 0,
|
|
|
|
y = 0,
|
|
|
|
},
|
|
|
|
pos = nil,
|
2013-03-03 14:18:38 +00:00
|
|
|
direction = pan_direction,
|
|
|
|
distance = pan_distance,
|
2013-03-02 15:11:23 +00:00
|
|
|
time = tev.timev,
|
2012-12-30 05:44:55 +00:00
|
|
|
}
|
2013-03-02 14:21:18 +00:00
|
|
|
pan_ev.relative.x = tev.x - self.last_tevs[slot].x
|
|
|
|
pan_ev.relative.y = tev.y - self.last_tevs[slot].y
|
2012-12-30 05:44:55 +00:00
|
|
|
pan_ev.pos = Geom:new{
|
2013-03-02 14:21:18 +00:00
|
|
|
x = self.last_tevs[slot].x,
|
|
|
|
y = self.last_tevs[slot].y,
|
2012-12-30 05:44:55 +00:00
|
|
|
w = 0, h = 0,
|
|
|
|
}
|
|
|
|
return pan_ev
|
2012-11-16 00:58:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-28 13:06:13 +00:00
|
|
|
function GestureDetector:handleTwoFingerPan(tev)
|
|
|
|
-- triggering slot
|
|
|
|
local tslot = tev.slot
|
|
|
|
-- reference slot
|
|
|
|
local rslot = tslot and 0 or 1
|
|
|
|
local tpan_dir, tpan_dis = self:getPath(tslot)
|
|
|
|
local tstart_pos = Geom:new{
|
|
|
|
x = self.first_tevs[tslot].x,
|
|
|
|
y = self.first_tevs[tslot].y,
|
|
|
|
w = 0, h = 0,
|
|
|
|
}
|
|
|
|
local tend_pos = Geom:new{
|
|
|
|
x = self.last_tevs[tslot].x,
|
|
|
|
y = self.last_tevs[tslot].y,
|
|
|
|
w = 0, h = 0,
|
|
|
|
}
|
|
|
|
local rstart_pos = Geom:new{
|
|
|
|
x = self.first_tevs[rslot].x,
|
|
|
|
y = self.first_tevs[rslot].y,
|
|
|
|
w = 0, h = 0,
|
|
|
|
}
|
|
|
|
if self.states[rslot] == self.panState then
|
|
|
|
local rpan_dir, rpan_dis = self:getPath(rslot)
|
|
|
|
local rend_pos = Geom:new{
|
|
|
|
x = self.last_tevs[rslot].x,
|
|
|
|
y = self.last_tevs[rslot].y,
|
|
|
|
w = 0, h = 0,
|
|
|
|
}
|
|
|
|
local start_distance = tstart_pos:distance(rstart_pos)
|
|
|
|
local end_distance = tend_pos:distance(rend_pos)
|
|
|
|
local ges_ev = {
|
|
|
|
ges = "two_finger_pan",
|
|
|
|
-- use midpoint of tstart and rstart as swipe start point
|
|
|
|
pos = tstart_pos:midpoint(rstart_pos),
|
|
|
|
distance = tpan_dis + rpan_dis,
|
|
|
|
direction = tpan_dir,
|
|
|
|
time = tev.timev,
|
|
|
|
}
|
|
|
|
if tpan_dir ~= rpan_dir then
|
|
|
|
if start_distance > end_distance then
|
|
|
|
ges_ev.ges = "inward_pan"
|
|
|
|
else
|
|
|
|
ges_ev.ges = "outward_pan"
|
|
|
|
end
|
|
|
|
ges_ev.direction = self.DIRECTION_TABLE[tpan_dir]
|
|
|
|
end
|
|
|
|
DEBUG(ges_ev.ges, ges_ev.direction, ges_ev.distance, "detected")
|
|
|
|
return ges_ev
|
|
|
|
elseif self.states[rslot] == self.holdState then
|
|
|
|
local angle = self:getRotate(rstart_pos, tstart_pos, tend_pos)
|
|
|
|
DEBUG("rotate", angle, "detected")
|
|
|
|
return {
|
|
|
|
ges = "rotate",
|
|
|
|
pos = rstart_pos,
|
|
|
|
angle = angle,
|
|
|
|
time = tev.timev,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function GestureDetector:handlePanRelease(tev)
|
|
|
|
local slot = tev.slot
|
|
|
|
local release_pos = Geom:new{
|
|
|
|
x = self.last_tevs[slot].x,
|
|
|
|
y = self.last_tevs[slot].y,
|
|
|
|
w = 0, h = 0,
|
|
|
|
}
|
|
|
|
local pan_ev = {
|
|
|
|
ges = "pan_release",
|
|
|
|
pos = release_pos,
|
|
|
|
time = tev.timev,
|
|
|
|
}
|
|
|
|
if self.detectings[0] and self.detectings[1] then
|
|
|
|
DEBUG("two finger pan release detected")
|
|
|
|
pan_ev.ges = "two_finger_pan_release"
|
|
|
|
self:clearStates()
|
|
|
|
else
|
|
|
|
DEBUG("pan release detected in slot", slot)
|
|
|
|
self:clearState(slot)
|
|
|
|
end
|
|
|
|
return pan_ev
|
|
|
|
end
|
|
|
|
|
2013-03-02 14:21:18 +00:00
|
|
|
function GestureDetector:holdState(tev, hold)
|
2013-03-03 14:18:38 +00:00
|
|
|
DEBUG("in hold state...")
|
2013-03-12 17:18:53 +00:00
|
|
|
local slot = tev.slot
|
2013-03-02 14:21:18 +00:00
|
|
|
-- when we switch to hold state, we pass additional param "hold"
|
|
|
|
if tev.id ~= -1 and hold and self.last_tevs[slot].x and self.last_tevs[slot].y then
|
|
|
|
self.states[slot] = self.holdState
|
2012-11-13 06:12:01 +00:00
|
|
|
return {
|
2013-02-26 04:32:46 +00:00
|
|
|
ges = "hold",
|
2012-11-13 06:12:01 +00:00
|
|
|
pos = Geom:new{
|
2013-03-02 14:21:18 +00:00
|
|
|
x = self.last_tevs[slot].x,
|
|
|
|
y = self.last_tevs[slot].y,
|
2012-11-13 06:12:01 +00:00
|
|
|
w = 0, h = 0,
|
2013-03-02 15:11:23 +00:00
|
|
|
},
|
|
|
|
time = tev.timev,
|
2012-11-13 06:12:01 +00:00
|
|
|
}
|
|
|
|
end
|
2013-03-06 13:04:33 +00:00
|
|
|
if tev.id == -1 and self.last_tevs[slot] ~= nil then
|
2012-11-17 18:41:26 +00:00
|
|
|
-- end of hold, signal hold release
|
2013-03-02 14:21:18 +00:00
|
|
|
DEBUG("hold_release detected in slot", slot)
|
|
|
|
local last_x = self.last_tevs[slot].x
|
|
|
|
local last_y = self.last_tevs[slot].y
|
|
|
|
self:clearState(slot)
|
2012-11-17 18:41:26 +00:00
|
|
|
return {
|
2013-02-26 04:32:46 +00:00
|
|
|
ges = "hold_release",
|
2012-11-17 18:41:26 +00:00
|
|
|
pos = Geom:new{
|
2013-03-02 14:21:18 +00:00
|
|
|
x = last_x,
|
|
|
|
y = last_y,
|
2012-11-17 18:41:26 +00:00
|
|
|
w = 0, h = 0,
|
2013-03-02 15:11:23 +00:00
|
|
|
},
|
|
|
|
time = tev.timev,
|
2012-11-17 18:41:26 +00:00
|
|
|
}
|
2012-11-16 00:58:01 +00:00
|
|
|
end
|
2012-11-13 06:12:01 +00:00
|
|
|
end
|
2012-11-11 06:00:52 +00:00
|
|
|
|
2013-02-19 03:13:24 +00:00
|
|
|
--[[
|
|
|
|
@brief change gesture's x and y coordinates according to screen view mode
|
|
|
|
|
|
|
|
@param ges gesture that you want to adjust
|
|
|
|
@return adjusted gesture.
|
|
|
|
--]]
|
|
|
|
function GestureDetector:adjustGesCoordinate(ges)
|
|
|
|
if Screen.cur_rotation_mode == 1 then
|
|
|
|
-- in landscape mode
|
2013-03-06 13:04:33 +00:00
|
|
|
if ges.pos then
|
|
|
|
ges.pos.x, ges.pos.y = (Screen.width - ges.pos.y), (ges.pos.x)
|
|
|
|
end
|
2013-04-08 06:40:12 +00:00
|
|
|
if ges.ges == "swipe" or ges.ges == "pan"
|
2013-03-28 13:06:13 +00:00
|
|
|
or ges.ges == "two_finger_swipe"
|
|
|
|
or ges.ges == "two_finger_pan" then
|
|
|
|
if ges.direction == "north" then
|
|
|
|
ges.direction = "east"
|
|
|
|
elseif ges.direction == "south" then
|
|
|
|
ges.direction = "west"
|
|
|
|
elseif ges.direction == "east" then
|
|
|
|
ges.direction = "south"
|
|
|
|
elseif ges.direction == "west" then
|
|
|
|
ges.direction = "north"
|
2013-03-28 13:40:16 +00:00
|
|
|
elseif ges.direction == "northeast" then
|
2013-03-28 13:06:13 +00:00
|
|
|
ges.direction = "southeast"
|
|
|
|
elseif ges.direction == "northwest" then
|
|
|
|
ges.direction = "northeast"
|
|
|
|
elseif ges.direction == "southeast" then
|
|
|
|
ges.direction = "southwest"
|
|
|
|
elseif ges.direction == "southwest" then
|
|
|
|
ges.direction = "northwest"
|
2013-02-19 03:13:24 +00:00
|
|
|
end
|
2013-03-28 13:40:16 +00:00
|
|
|
elseif ges.ges == "pinch" or ges.ges == "spread"
|
|
|
|
or ges.ges == "inward_pan"
|
|
|
|
or ges.ges == "outward_pan" then
|
|
|
|
if ges.direction == "horizontal" then
|
|
|
|
ges.direction = "vertical"
|
|
|
|
elseif ges.direction == "vertical" then
|
|
|
|
ges.direction = "horizontal"
|
|
|
|
end
|
2013-02-19 03:13:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return ges
|
|
|
|
end
|