Merge pull request #776 from chrox/mt_hold

add multiple slots in gesture detector
This commit is contained in:
{Qingping,Dave} Hou
2013-03-03 08:01:42 -08:00
9 changed files with 260 additions and 128 deletions

View File

@@ -24,6 +24,13 @@ function BBoxWidget:init()
GestureRange:new{
ges = "pan",
range = self.view.dimen,
rate = 3.0, -- emit up to 3 evs per second
}
},
HoldAdjust = {
GestureRange:new{
ges = "hold",
range = self.view.dimen,
}
},
ConfirmAdjust = {
@@ -87,7 +94,7 @@ function BBoxWidget:inPageArea(ges)
return not ges.pos:notIntersectWith(page_dimen)
end
function BBoxWidget:adjustScreenBBox(ges, rate)
function BBoxWidget:adjustScreenBBox(ges)
--DEBUG("adjusting crop bbox with pos", ges.pos)
if not self:inPageArea(ges) then return end
local bbox = self.screen_bbox
@@ -135,17 +142,8 @@ function BBoxWidget:adjustScreenBBox(ges, rate)
x1 = math.round(bottom_right.x),
y1 = math.round(bottom_right.y)
}
if rate then
local last_time = self.last_time or {0, 0}
local this_time = { util.gettime() }
local elap_time = (this_time[1] - last_time[1]) * 1000 + (this_time[2] - last_time[2]) / 1000 -- in millisec
if elap_time > 1000 / rate then
UIManager.repaint_all = true
self.last_time = this_time
end
else
UIManager.repaint_all = true
end
UIManager.repaint_all = true
end
function BBoxWidget:getModifiedPageBBox()
@@ -158,8 +156,12 @@ function BBoxWidget:onTapAdjust(arg, ges)
end
function BBoxWidget:onPanAdjust(arg, ges)
-- up to 3 updates per second
self:adjustScreenBBox(ges, 3.0)
self:adjustScreenBBox(ges)
return true
end
function BBoxWidget:onHoldAdjust(arg, ges)
self:adjustScreenBBox(ges)
return true
end

View File

@@ -2,7 +2,10 @@ require "ui/geometry"
GestureRange = {
ges = nil,
-- spatial range limits the gesture emitting position
range = nil,
-- temproal range limits the gesture emitting rate
rate = nil,
}
function GestureRange:new(o)
@@ -18,6 +21,15 @@ function GestureRange:match(gs)
end
if self.range:contains(gs.pos) then
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
return true
else
return false
end
end
return true
end
@@ -59,30 +71,30 @@ GestureDetector = {
-- distance parameters
DOUBLE_TAP_DISTANCE = 50,
PAN_THRESHOLD = 50,
track_id = {},
tev_stack = {},
-- latest feeded touch event
last_tev = {},
is_on_detecting = false,
first_tev = nil,
state = function(self, tev)
self:switchState("initialState", tev)
end,
last_tap = nil, -- for single/double tap
-- 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 = {},
}
function GestureDetector:feedEvent(tev)
-- for now, only handle single point touch
if tev.slot ~= 0 then
return nil
local slot = tev.slot
if not self.states[slot] then
self:clearState(slot) -- initiate state
end
re = self.state(self, tev)
local ges = self.states[slot](self, tev)
if tev.id ~= -1 then
self.last_tev = tev
self.last_tevs[slot] = tev
end
return re
return ges
end
function GestureDetector:deepCopyEv(tev)
@@ -111,32 +123,32 @@ function GestureDetector:isDoubleTap(tap1, tap2)
end
--[[
compare last_pan with self.first_tev
if it is a swipe, return direction of swipe gesture.
compare last_pan with first_tev in this slot
return pan direction and distance
--]]
function GestureDetector:isSwipe()
local tv_diff = self.first_tev.timev - self.last_tev.timev
if (tv_diff.sec == 0) and (tv_diff.usec < self.SWIPE_INTERVAL) then
x_diff = self.last_tev.x - self.first_tev.x
y_diff = self.last_tev.y - self.first_tev.y
if x_diff == 0 and y_diff == 0 then
return nil
end
function GestureDetector:getPath(tev)
local slot = tev.slot
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
elseif (math.abs(x_diff) > math.abs(y_diff)) then
direction = x_diff < 0 and "left" or "right"
else
direction = y_diff < 0 and "up" or "down"
end
return direction, distance
end
if (math.abs(x_diff) > math.abs(y_diff)) then
-- left or right
if x_diff < 0 then
return "left"
else
return "right"
end
else
-- up or down
if y_diff < 0 then
return "up"
else
return "down"
end
function GestureDetector:isSwipe(tev)
local slot = tev.slot
local tv_diff = self.first_tevs[slot].timev - self.last_tevs[slot].timev
if (tv_diff.sec == 0) and (tv_diff.usec < self.SWIPE_INTERVAL) then
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
if x_diff ~= 0 or y_diff ~= 0 then
return true
end
end
end
@@ -145,32 +157,33 @@ end
Warning! this method won't update self.state, you need to do it
in each state method!
--]]
function GestureDetector:switchState(state_new, tev)
function GestureDetector:switchState(state_new, tev, param)
--@TODO do we need to check whether state is valid? (houqp)
return self[state_new](self, tev)
return self[state_new](self, tev, param)
end
function GestureDetector:clearState()
self.state = self.initialState
self.last_tev = {}
self.is_on_detecting = false
self.first_tev = nil
function GestureDetector:clearState(slot)
self.states[slot] = self.initialState
self.last_tevs[slot] = {}
self.detectings[slot] = false
self.first_tevs[slot] = nil
end
function GestureDetector:initialState(tev)
local slot = tev.slot
if tev.id then
-- a event ends
if tev.id == -1 then
self.is_on_detecting = false
self.detectings[slot] = false
else
self.track_id[tev.id] = tev.slot
self.track_ids[slot] = tev.id
end
end
if tev.x and tev.y then
-- user starts a new touch motion
if not self.is_on_detecting then
self.is_on_detecting = true
self.first_tev = self:deepCopyEv(tev)
if not self.detectings[slot] then
self.detectings[slot] = true
self.first_tevs[slot] = self:deepCopyEv(tev)
-- default to tap state
return self:switchState("tapState", tev)
end
@@ -181,17 +194,19 @@ end
this method handles both single and double tap
--]]
function GestureDetector:tapState(tev)
DEBUG("in tap state...", tev)
DEBUG("in tap state...")
local slot = tev.slot
if tev.id == -1 then
-- end of tap event
local ges_ev = {
-- default to single tap
ges = "tap",
pos = Geom:new{
x = self.last_tev.x,
y = self.last_tev.y,
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 = {
@@ -200,57 +215,58 @@ function GestureDetector:tapState(tev)
timev = tev.timev,
}
if self.last_tap ~= nil and
self:isDoubleTap(self.last_tap, cur_tap) then
if self.last_taps[slot] ~= nil and
self:isDoubleTap(self.last_taps[slot], cur_tap) then
-- it is a double tap
self:clearState()
self:clearState(slot)
ges_ev.ges = "double_tap"
self.last_tap = nil
DEBUG("double tap detected")
self.last_taps[slot] = nil
DEBUG("double tap detected in slot", slot)
return ges_ev
end
-- set current tap to last tap
self.last_tap = cur_tap
self.last_taps[slot] = cur_tap
DEBUG("set up tap timer")
local deadline = self.last_tev.timev + TimeVal:new{
sec = 0, usec = self.DOUBLE_TAP_INTERVAL,
}
-- 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_tap ~= nil)
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_tap ~= nil then
self.last_tap = nil
if self.last_taps[slot] ~= nil then
self.last_taps[slot] = nil
-- we are using closure here
DEBUG("single tap detected")
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()
elseif self.state ~= self.tapState then
self:clearState(slot)
elseif self.states[slot] ~= self.tapState then
-- switched from other state, probably from initialState
-- we return nil in this case
self.state = self.tapState
self.states[slot] = self.tapState
DEBUG("set up hold timer")
local deadline = tev.timev + TimeVal:new{
sec = 0, usec = self.HOLD_INTERVAL
}
sec = 0, usec = self.HOLD_INTERVAL
}
Input:setTimeout(function()
if self.state == self.tapState then
if self.states[slot] == self.tapState then
-- timer set in tapState, so we switch to hold
DEBUG("hold gesture detected")
return self:switchState("holdState")
DEBUG("hold gesture detected in slot", slot)
return self:switchState("holdState", tev, true)
end
end, deadline)
else
-- it is not end of touch event, see if we need to switch to
-- other states
if (tev.x and math.abs(tev.x - self.first_tev.x) >= self.PAN_THRESHOLD) or
(tev.y and math.abs(tev.y - self.first_tev.y) >= self.PAN_THRESHOLD) then
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
-- if user's finger moved long enough in X or
-- Y distance, we switch to pan state
return self:switchState("panState", tev)
@@ -260,31 +276,33 @@ end
function GestureDetector:panState(tev)
DEBUG("in pan state...")
local slot = tev.slot
if tev.id == -1 then
-- end of pan, signal swipe gesture if necessary
swipe_direct = self:isSwipe()
if swipe_direct then
DEBUG("swipe detected!")
if self:isSwipe(tev) then
local swipe_direction, swipe_distance = self:getPath(tev)
DEBUG("swipe", swipe_direction, swipe_distance, "detected in slot", slot)
local start_pos = Geom:new{
x = self.first_tev.x,
y = self.first_tev.y,
x = self.first_tevs[slot].x,
y = self.first_tevs[slot].y,
w = 0, h = 0,
}
self:clearState()
self:clearState(slot)
return {
ges = "swipe",
direction = swipe_direct,
-- use first pan tev coordination as swipe start point
pos = start_pos,
--@TODO add start and end points? (houqp)
direction = swipe_direction,
distance = swipe_distance,
time = tev.timev,
}
end
self:clearState()
self:clearState(slot)
else
if self.state ~= self.panState then
self.state = self.panState
if self.states[slot] ~= self.panState then
self.states[slot] = self.panState
end
local pan_direction, pan_distance = self:getPath(tev)
local pan_ev = {
ges = "pan",
relative = {
@@ -293,43 +311,51 @@ function GestureDetector:panState(tev)
y = 0,
},
pos = nil,
direction = pan_direction,
distance = pan_distance,
time = tev.timev,
}
pan_ev.relative.x = tev.x - self.last_tev.x
pan_ev.relative.y = tev.y - self.last_tev.y
pan_ev.relative.x = tev.x - self.last_tevs[slot].x
pan_ev.relative.y = tev.y - self.last_tevs[slot].y
pan_ev.pos = Geom:new{
x = self.last_tev.x,
y = self.last_tev.y,
x = self.last_tevs[slot].x,
y = self.last_tevs[slot].y,
w = 0, h = 0,
}
return pan_ev
end
end
function GestureDetector:holdState(tev)
function GestureDetector:holdState(tev, hold)
DEBUG("in hold state...")
-- when we switch to hold state, we pass no ev
-- so ev = nil
if not tev and self.last_tev.x and self.last_tev.y then
self.state = self.holdState
local slot = tev.slot
-- 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
return {
ges = "hold",
pos = Geom:new{
x = self.last_tev.x,
y = self.last_tev.y,
x = self.last_tevs[slot].x,
y = self.last_tevs[slot].y,
w = 0, h = 0,
}
},
time = tev.timev,
}
end
if tev.id == -1 then
-- end of hold, signal hold release
self:clearState()
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)
return {
ges = "hold_release",
pos = Geom:new{
x = self.last_tev.x,
y = self.last_tev.y,
x = last_x,
y = last_y,
w = 0, h = 0,
}
},
time = tev.timev,
}
end
end

View File

@@ -0,0 +1,16 @@
ReaderFlipping = LeftContainer:new{}
function ReaderFlipping:init()
local widget = ImageWidget:new{
file = "resources/icons/appbar.book.open.png",
}
local icon_size = widget:getSize()
self.dimen = Geom:new{w = Screen:getWidth(), h = icon_size.h}
self[1] = widget
end
function ReaderFlipping:onSetDogearVisibility(visible)
self.view.dogear_visible = visible
return true
end

View File

@@ -21,8 +21,9 @@ function ReaderMenu:initGesListener()
GestureRange:new{
ges = "tap",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth()*7/8,
x = Screen:getWidth()/8,
y = 0,
w = Screen:getWidth()*3/4,
h = Screen:getHeight()/4,
}
}

View File

@@ -68,7 +68,38 @@ function ReaderPaging:initGesListener()
h = 5*Screen:getHeight()/8,
}
}
}
},
ToggleFlipping = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth()/8,
h = Screen:getHeight()/8
}
}
},
Swipe = {
GestureRange:new{
ges = "swipe",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
}
},
Pan = {
GestureRange:new{
ges = "pan",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
},
rate = 4.0,
}
},
}
end
@@ -95,6 +126,47 @@ function ReaderPaging:onTapBackward()
return true
end
function ReaderPaging:onToggleFlipping()
self.view.flipping_visible = not self.view.flipping_visible
self.flipping_page = self.view.flipping_visible and self.current_page or nil
UIManager:setDirty(self.view.dialog, "partial")
end
function ReaderPaging:onSwipe(arg, ges)
if self.flipping_page == nil then
if ges.direction == "left" or ges.direction == "up" then
self:onGotoPageRel(1)
elseif ges.direction == "right" or ges.direction == "down" then
self:onGotoPageRel(-1)
end
elseif self.flipping_page then
self:gotoPage(self.flipping_page)
end
return true
end
function ReaderPaging:onPan(arg, ges)
if self.flipping_page then
local read = self.flipping_page - 1
local unread = self.number_of_pages - self.flipping_page
local whole = self.number_of_pages
local rel_proportion = ges.distance / Screen:getWidth()
local abs_proportion = ges.distance / Screen:getHeight()
if ges.direction == "right" then
self:gotoPage(self.flipping_page - math.floor(read*rel_proportion))
elseif ges.direction == "left" then
self:gotoPage(self.flipping_page + math.floor(unread*rel_proportion))
elseif ges.direction == "down" then
self:gotoPage(self.flipping_page - math.floor(whole*abs_proportion))
elseif ges.direction == "up" then
self:gotoPage(self.flipping_page + math.floor(whole*abs_proportion))
end
UIManager:setDirty(self.view.dialog, "partial")
end
return true
end
function ReaderPaging:onZoomModeUpdate(new_mode)
-- we need to remember zoom mode to handle page turn event
self.zoom_mode = new_mode

View File

@@ -1,3 +1,4 @@
require "ui/reader/readerflip"
require "ui/reader/readerfooter"
require "ui/reader/readerdogear"
@@ -30,6 +31,8 @@ ReaderView = OverlapGroup:new{
footer_visible = false,
-- has dogear
dogear_visible = false,
-- in flipping state
flipping_visible = false,
}
function ReaderView:init()
@@ -43,8 +46,12 @@ function ReaderView:resetLayout()
self.footer = ReaderFooter:new{
view = self,
}
self.flipping = ReaderFlipping:new{
view = self,
}
self[1] = self.dogear
self[2] = self.footer
self[3] = self.flipping
end
function ReaderView:paintTo(bb, x, y)
@@ -109,6 +116,10 @@ function ReaderView:paintTo(bb, x, y)
if self.footer_visible then
self.footer:paintTo(bb, x, y)
end
-- paint flipping
if self.flipping_visible then
self.flipping:paintTo(bb, x, y)
end
end
--[[

View File

@@ -59,7 +59,7 @@ function TimeVal:__eq(time_b)
end
function TimeVal:__sub(time_b)
diff = TimeVal:new{}
local diff = TimeVal:new{}
diff.sec = self.sec - time_b.sec
diff.usec = self.usec - time_b.usec
@@ -76,7 +76,7 @@ function TimeVal:__sub(time_b)
end
function TimeVal:__add(time_b)
sum = TimeVal:new{}
local sum = TimeVal:new{}
sum.sec = self.sec + time_b.sec
sum.usec = self.usec + time_b.usec

Binary file not shown.

After

Width:  |  Height:  |  Size: 207 B

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_book_open" Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0" UseLayoutRounding="False">
<Path Width="26" Height="21.7888" Canvas.Left="11" Canvas.Top="14.7112" Stretch="Fill" Fill="#FF000000" Data="F1 M 24,18.75C 26,12 35,15.75 35,15.75L 35,17L 35,17.732L 37,17.25L 36.9999,32.5C 36.9999,32.5 26,33 24,36.5C 22,33 11,32.5 11,32.5L 11,17.25L 13,17.732L 13,17L 13,15.75C 13,15.75 22,12 24,18.75 Z M 33.9999,29.7634L 34,17.2813C 31,17 28,15 24.5,19.7913L 24.5,32.9783C 26.2215,30.8247 31.2061,30.5863 33.9999,29.7634 Z M 14,17.2813L 14,29.7634C 16.7938,30.5863 21.7784,30.8247 23.5,32.9784L 23.5,19.7913C 20,15 17,17 14,17.2813 Z "/>
</Canvas>