2013-10-18 20:38:07 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local Screen = require("ui/screen")
|
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local Input = require("ui/input")
|
|
|
|
local GestureRange = require("ui/gesturerange")
|
|
|
|
local Device = require("ui/device")
|
|
|
|
local Event = require("ui/event")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local Math = require("optmath")
|
2013-10-22 18:51:29 +00:00
|
|
|
local DEBUG = require("dbg")
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
local ReaderPaging = InputContainer:new{
|
2012-05-18 22:50:26 +00:00
|
|
|
current_page = 0,
|
2012-12-03 05:48:41 +00:00
|
|
|
number_of_pages = 0,
|
2013-10-25 08:07:04 +00:00
|
|
|
last_pan_relative_y = 0,
|
2012-12-03 05:48:41 +00:00
|
|
|
visible_area = nil,
|
|
|
|
page_area = nil,
|
2013-09-14 11:35:32 +00:00
|
|
|
show_overlap_enable = DSHOWOVERLAP,
|
2013-10-18 20:38:07 +00:00
|
|
|
overlap = Screen:scaleByDPI(20),
|
2013-04-14 10:02:34 +00:00
|
|
|
flip_steps = {0,1,2,5,10,20,50,100}
|
2012-05-18 22:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ReaderPaging:init()
|
2013-02-02 06:36:29 +00:00
|
|
|
if Device:hasKeyboard() then
|
2012-11-11 07:24:11 +00:00
|
|
|
self.key_events = {
|
2013-03-16 00:03:50 +00:00
|
|
|
GotoNextPage = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{Input.group.PgFwd}, doc = "go to next page",
|
|
|
|
event = "GotoPageRel", args = 1 },
|
2013-03-16 00:03:50 +00:00
|
|
|
GotoPrevPage = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{Input.group.PgBack}, doc = "go to previous page",
|
|
|
|
event = "GotoPageRel", args = -1 },
|
|
|
|
|
2013-03-16 00:03:50 +00:00
|
|
|
GotoFirst = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{"1"}, doc = "go to start", event = "GotoPercent", args = 0},
|
2013-03-16 00:03:50 +00:00
|
|
|
Goto11 = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{"2"}, doc = "go to 11%", event = "GotoPercent", args = 11},
|
2013-03-16 00:03:50 +00:00
|
|
|
Goto22 = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{"3"}, doc = "go to 22%", event = "GotoPercent", args = 22},
|
2013-03-16 00:03:50 +00:00
|
|
|
Goto33 = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{"4"}, doc = "go to 33%", event = "GotoPercent", args = 33},
|
2013-03-16 00:03:50 +00:00
|
|
|
Goto44 = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{"5"}, doc = "go to 44%", event = "GotoPercent", args = 44},
|
2013-03-16 00:03:50 +00:00
|
|
|
Goto55 = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{"6"}, doc = "go to 55%", event = "GotoPercent", args = 55},
|
2013-03-16 00:03:50 +00:00
|
|
|
Goto66 = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{"7"}, doc = "go to 66%", event = "GotoPercent", args = 66},
|
2013-03-16 00:03:50 +00:00
|
|
|
Goto77 = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{"8"}, doc = "go to 77%", event = "GotoPercent", args = 77},
|
2013-03-16 00:03:50 +00:00
|
|
|
Goto88 = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{"9"}, doc = "go to 88%", event = "GotoPercent", args = 88},
|
2013-03-16 00:03:50 +00:00
|
|
|
GotoLast = {
|
2012-11-11 07:24:11 +00:00
|
|
|
{"0"}, doc = "go to end", event = "GotoPercent", args = 100},
|
|
|
|
}
|
|
|
|
end
|
2012-05-18 22:50:26 +00:00
|
|
|
self.number_of_pages = self.ui.document.info.number_of_pages
|
|
|
|
end
|
|
|
|
|
2013-02-02 06:36:29 +00:00
|
|
|
-- This method will be called in onSetDimensions handler
|
|
|
|
function ReaderPaging:initGesListener()
|
|
|
|
self.ges_events = {
|
|
|
|
TapForward = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = Geom:new{
|
|
|
|
x = Screen:getWidth()/4,
|
|
|
|
y = Screen:getHeight()/4,
|
|
|
|
w = 3*Screen:getWidth()/4,
|
2013-03-31 08:40:43 +00:00
|
|
|
h = 2*Screen:getHeight()/3,
|
2013-02-02 06:36:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
TapBackward = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = Geom:new{
|
2013-03-16 00:03:50 +00:00
|
|
|
x = 0,
|
2013-02-02 06:36:29 +00:00
|
|
|
y = Screen:getHeight()/4,
|
|
|
|
w = Screen:getWidth()/4,
|
2013-03-31 08:40:43 +00:00
|
|
|
h = 2*Screen:getHeight()/3,
|
2013-02-02 06:36:29 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-03 14:23:28 +00:00
|
|
|
},
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-03-26 05:18:09 +00:00
|
|
|
TwoFingerSwipe = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "two_finger_swipe",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-03-03 14:23:28 +00:00
|
|
|
Pan = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "pan",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight(),
|
|
|
|
},
|
2013-07-23 06:51:38 +00:00
|
|
|
rate = 4.0,
|
2013-03-03 14:23:28 +00:00
|
|
|
}
|
|
|
|
},
|
2013-03-12 03:28:47 +00:00
|
|
|
PanRelease = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "pan_release",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2013-02-02 06:36:29 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-12-04 07:19:50 +00:00
|
|
|
function ReaderPaging:onReadSettings(config)
|
2013-04-27 08:37:18 +00:00
|
|
|
self.page_positions = config:readSetting("page_positions") or {}
|
2012-12-04 07:19:50 +00:00
|
|
|
self:gotoPage(config:readSetting("last_page") or 1)
|
2012-12-08 06:05:10 +00:00
|
|
|
local soe = config:readSetting("show_overlap_enable")
|
|
|
|
if not soe then
|
|
|
|
self.show_overlap_enable = soe
|
|
|
|
end
|
2012-12-04 07:19:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:onCloseDocument()
|
2013-04-27 08:37:18 +00:00
|
|
|
self.ui.doc_settings:saveSetting("page_positions", self.page_positions)
|
|
|
|
self.ui.doc_settings:saveSetting("last_page", self:getTopPage())
|
2013-01-07 05:29:00 +00:00
|
|
|
self.ui.doc_settings:saveSetting("percent_finished", self.current_page/self.number_of_pages)
|
2012-12-04 07:19:50 +00:00
|
|
|
end
|
|
|
|
|
2013-04-27 08:37:18 +00:00
|
|
|
--[[
|
|
|
|
Set reading position on certain page
|
|
|
|
Page position is a fractional number ranging from 0 to 1, indicating the read percentage on
|
|
|
|
certain page. With the position information on each page whenever users change font size,
|
2013-04-27 09:50:20 +00:00
|
|
|
page margin or line spacing or close and reopen the book, the page view will be roughly the same.
|
2013-04-27 08:37:18 +00:00
|
|
|
--]]
|
|
|
|
function ReaderPaging:setPagePosition(page, pos)
|
|
|
|
DEBUG("set page position", pos)
|
|
|
|
self.page_positions[page] = pos
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Get reading position on certain page
|
|
|
|
--]]
|
|
|
|
function ReaderPaging:getPagePosition(page)
|
|
|
|
-- Page number ought to be integer, somehow I notice that with
|
|
|
|
-- fractional page number the reader runs silently well, but the
|
|
|
|
-- number won't fit to retrieve page position.
|
|
|
|
page = math.floor(page)
|
|
|
|
DEBUG("get page position", self.page_positions[page])
|
|
|
|
return self.page_positions[page] or 0
|
|
|
|
end
|
|
|
|
|
2012-12-04 09:05:58 +00:00
|
|
|
function ReaderPaging:onTapForward()
|
2013-03-10 06:23:26 +00:00
|
|
|
self:onPagingRel(1)
|
2012-12-04 09:05:58 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:onTapBackward()
|
2013-03-10 06:23:26 +00:00
|
|
|
self:onPagingRel(-1)
|
2012-12-04 09:05:58 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-03-03 14:23:28 +00:00
|
|
|
function ReaderPaging:onToggleFlipping()
|
|
|
|
self.view.flipping_visible = not self.view.flipping_visible
|
2013-03-12 03:28:47 +00:00
|
|
|
self.flipping_mode = self.view.flipping_visible
|
|
|
|
self.flipping_page = self.current_page
|
|
|
|
if self.flipping_mode then
|
|
|
|
self:updateOriginalPage(self.current_page)
|
2013-03-18 08:07:07 +00:00
|
|
|
else
|
|
|
|
self:updateOriginalPage(nil)
|
2013-03-12 03:28:47 +00:00
|
|
|
end
|
2013-03-31 13:37:57 +00:00
|
|
|
self.ui:handleEvent(Event:new("SetFlippingMode", self.flipping_mode))
|
2013-03-12 12:28:01 +00:00
|
|
|
self.ui:handleEvent(Event:new("SetHinting", not self.flipping_mode))
|
2013-04-20 08:15:50 +00:00
|
|
|
self.ui:handleEvent(Event:new("ReZoom"))
|
2013-03-12 03:28:47 +00:00
|
|
|
UIManager:setDirty(self.view.dialog, "partial")
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:updateOriginalPage(page)
|
|
|
|
self.original_page = page
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:updateFlippingPage(page)
|
|
|
|
self.flipping_page = page
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:flipping(flipping_page, flipping_ges)
|
|
|
|
local whole = self.number_of_pages
|
2013-04-14 10:02:34 +00:00
|
|
|
local steps = #self.flip_steps
|
|
|
|
local stp_proportion = flipping_ges.distance / Screen:getWidth()
|
2013-03-12 03:28:47 +00:00
|
|
|
local abs_proportion = flipping_ges.distance / Screen:getHeight()
|
2013-03-28 13:07:38 +00:00
|
|
|
if flipping_ges.direction == "east" then
|
2013-04-14 10:02:34 +00:00
|
|
|
self:gotoPage(flipping_page - self.flip_steps[math.ceil(steps*stp_proportion)])
|
2013-03-28 13:07:38 +00:00
|
|
|
elseif flipping_ges.direction == "west" then
|
2013-04-14 10:02:34 +00:00
|
|
|
self:gotoPage(flipping_page + self.flip_steps[math.ceil(steps*stp_proportion)])
|
2013-03-28 13:07:38 +00:00
|
|
|
elseif flipping_ges.direction == "south" then
|
2013-03-12 03:28:47 +00:00
|
|
|
self:gotoPage(flipping_page - math.floor(whole*abs_proportion))
|
2013-03-28 13:07:38 +00:00
|
|
|
elseif flipping_ges.direction == "north" then
|
2013-03-12 03:28:47 +00:00
|
|
|
self:gotoPage(flipping_page + math.floor(whole*abs_proportion))
|
|
|
|
end
|
2013-03-03 14:23:28 +00:00
|
|
|
UIManager:setDirty(self.view.dialog, "partial")
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:onSwipe(arg, ges)
|
2013-03-12 03:28:47 +00:00
|
|
|
if self.flipping_mode then
|
|
|
|
self:flipping(self.flipping_page, ges)
|
|
|
|
self:updateFlippingPage(self.current_page)
|
2013-04-14 09:39:12 +00:00
|
|
|
elseif ges.direction == "west" then
|
2013-03-12 03:28:47 +00:00
|
|
|
self:onPagingRel(1)
|
2013-04-14 09:39:12 +00:00
|
|
|
elseif ges.direction == "east" then
|
2013-03-12 03:28:47 +00:00
|
|
|
self:onPagingRel(-1)
|
2013-08-17 03:01:57 +00:00
|
|
|
else
|
|
|
|
UIManager.full_refresh = true
|
2013-03-03 14:23:28 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-03-26 05:18:09 +00:00
|
|
|
function ReaderPaging:onTwoFingerSwipe(arg, ges)
|
|
|
|
if self.original_page then
|
|
|
|
self:gotoPage(self.original_page)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-03 14:23:28 +00:00
|
|
|
function ReaderPaging:onPan(arg, ges)
|
2013-03-12 03:28:47 +00:00
|
|
|
if self.flipping_mode then
|
|
|
|
self:flipping(self.flipping_page, ges)
|
2013-10-25 08:07:04 +00:00
|
|
|
elseif ges.direction == "north" or ges.direction == "south" then
|
|
|
|
self:onPanningRel(self.last_pan_relative_y - ges.relative.y)
|
|
|
|
self.last_pan_relative_y = ges.relative.y
|
2013-03-03 14:23:28 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-03-12 03:28:47 +00:00
|
|
|
function ReaderPaging:onPanRelease(arg, ges)
|
|
|
|
if self.flipping_mode then
|
|
|
|
self:updateFlippingPage(self.current_page)
|
2013-07-22 14:07:49 +00:00
|
|
|
else
|
2013-10-25 08:07:04 +00:00
|
|
|
self.last_pan_relative_y = 0
|
2013-07-22 14:07:49 +00:00
|
|
|
UIManager.full_refresh = true
|
2013-03-12 03:28:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-03 05:48:41 +00:00
|
|
|
function ReaderPaging:onZoomModeUpdate(new_mode)
|
|
|
|
-- we need to remember zoom mode to handle page turn event
|
|
|
|
self.zoom_mode = new_mode
|
|
|
|
end
|
|
|
|
|
2013-03-10 06:23:26 +00:00
|
|
|
function ReaderPaging:onPageUpdate(new_page_no, orig)
|
2012-05-18 22:50:26 +00:00
|
|
|
self.current_page = new_page_no
|
2013-03-18 09:08:01 +00:00
|
|
|
if orig ~= "scrolling" then
|
|
|
|
self.ui:handleEvent(Event:new("InitScrollPageStates", orig))
|
|
|
|
end
|
2012-05-18 22:50:26 +00:00
|
|
|
end
|
|
|
|
|
2012-12-03 05:48:41 +00:00
|
|
|
function ReaderPaging:onViewRecalculate(visible_area, page_area)
|
|
|
|
-- we need to remember areas to handle page turn event
|
2012-12-08 06:05:10 +00:00
|
|
|
self.visible_area = visible_area:copy()
|
2012-12-03 05:48:41 +00:00
|
|
|
self.page_area = page_area
|
|
|
|
end
|
|
|
|
|
2012-05-18 22:50:26 +00:00
|
|
|
function ReaderPaging:onGotoPercent(percent)
|
2012-05-28 16:59:16 +00:00
|
|
|
DEBUG("goto document offset in percent:", percent)
|
2012-05-18 22:50:26 +00:00
|
|
|
local dest = math.floor(self.number_of_pages * percent / 100)
|
|
|
|
if dest < 1 then dest = 1 end
|
|
|
|
if dest > self.number_of_pages then
|
|
|
|
dest = self.number_of_pages
|
|
|
|
end
|
|
|
|
self:gotoPage(dest)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-03-10 06:23:26 +00:00
|
|
|
function ReaderPaging:onPagingRel(diff)
|
|
|
|
if self.view.page_scroll then
|
|
|
|
self:onScrollPageRel(diff)
|
|
|
|
else
|
|
|
|
self:onGotoPageRel(diff)
|
|
|
|
end
|
2013-04-27 08:37:18 +00:00
|
|
|
self:setPagePosition(self:getTopPage(), self:getTopPosition())
|
2013-03-10 06:23:26 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-04-14 09:39:12 +00:00
|
|
|
function ReaderPaging:onPanningRel(diff)
|
|
|
|
if self.view.page_scroll then
|
|
|
|
self:onScrollPanRel(diff)
|
|
|
|
end
|
2013-04-27 08:37:18 +00:00
|
|
|
self:setPagePosition(self:getTopPage(), self:getTopPosition())
|
2013-04-14 09:39:12 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-04-27 08:37:18 +00:00
|
|
|
--[[
|
|
|
|
Get read percentage on current page.
|
|
|
|
--]]
|
|
|
|
function ReaderPaging:getTopPosition()
|
|
|
|
if self.view.page_scroll then
|
|
|
|
local state = self.view.page_states[1]
|
|
|
|
return (state.visible_area.y - state.page_area.y)/state.page_area.h
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Get page number of the page drawn at the very top part of the screen.
|
|
|
|
--]]
|
|
|
|
function ReaderPaging:getTopPage()
|
|
|
|
if self.view.page_scroll then
|
|
|
|
local state = self.view.page_states[1]
|
|
|
|
return state.page
|
|
|
|
else
|
|
|
|
return self.current_page
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-10 06:23:26 +00:00
|
|
|
function ReaderPaging:onInitScrollPageStates(orig)
|
2013-04-27 08:37:18 +00:00
|
|
|
DEBUG("init scroll page states", orig)
|
2013-03-10 06:23:26 +00:00
|
|
|
if self.view.page_scroll then
|
2013-03-18 09:08:01 +00:00
|
|
|
self.orig_page = self.current_page
|
2013-03-10 06:23:26 +00:00
|
|
|
self.view.page_states = {}
|
|
|
|
local blank_area = Geom:new{}
|
|
|
|
blank_area:setSizeTo(self.view.dimen)
|
|
|
|
while blank_area.h > 0 do
|
2013-04-27 08:37:18 +00:00
|
|
|
local offset = Geom:new{}
|
|
|
|
-- caculate position in current page
|
|
|
|
if self.current_page == self.orig_page then
|
|
|
|
local page_area = self.view:getPageArea(
|
|
|
|
self.view.state.page,
|
|
|
|
self.view.state.zoom,
|
|
|
|
self.view.state.rotation)
|
|
|
|
offset.y = page_area.h * self:getPagePosition(self.current_page)
|
|
|
|
end
|
|
|
|
local state = self:getNextPageState(blank_area, offset)
|
2013-03-10 06:23:26 +00:00
|
|
|
--DEBUG("init new state", state)
|
|
|
|
table.insert(self.view.page_states, state)
|
|
|
|
if blank_area.h > 0 then
|
|
|
|
blank_area.h = blank_area.h - self.view.page_gap.height
|
|
|
|
end
|
|
|
|
if blank_area.h > 0 then
|
|
|
|
self:gotoPage(self.current_page + 1, "scrolling")
|
|
|
|
end
|
|
|
|
end
|
2013-03-18 09:08:01 +00:00
|
|
|
self:gotoPage(self.orig_page, "scrolling")
|
2013-03-10 06:23:26 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:onUpdateScrollPageRotation(rotation)
|
|
|
|
for _, state in ipairs(self.view.page_states) do
|
|
|
|
state.rotation = rotation
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:onUpdateScrollPageGamma(gamma)
|
|
|
|
for _, state in ipairs(self.view.page_states) do
|
|
|
|
state.gamma = gamma
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:getNextPageState(blank_area, offset)
|
|
|
|
local page_area = self.view:getPageArea(
|
|
|
|
self.view.state.page,
|
|
|
|
self.view.state.zoom,
|
|
|
|
self.view.state.rotation)
|
|
|
|
local visible_area = Geom:new{x = 0, y = 0}
|
|
|
|
visible_area.w, visible_area.h = blank_area.w, blank_area.h
|
|
|
|
visible_area.x, visible_area.y = page_area.x, page_area.y
|
|
|
|
visible_area = visible_area:shrinkInside(page_area, offset.x, offset.y)
|
|
|
|
-- shrink blank area by the height of visible area
|
|
|
|
blank_area.h = blank_area.h - visible_area.h
|
|
|
|
return {
|
|
|
|
page = self.view.state.page,
|
|
|
|
zoom = self.view.state.zoom,
|
|
|
|
rotation = self.view.state.rotation,
|
|
|
|
gamma = self.view.state.gamma,
|
|
|
|
offset = Geom:new{ x = self.view.state.offset.x, y = 0},
|
|
|
|
visible_area = visible_area,
|
|
|
|
page_area = page_area,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:getPrevPageState(blank_area, offset)
|
|
|
|
local page_area = self.view:getPageArea(
|
|
|
|
self.view.state.page,
|
|
|
|
self.view.state.zoom,
|
|
|
|
self.view.state.rotation)
|
|
|
|
local visible_area = Geom:new{x = 0, y = 0}
|
|
|
|
visible_area.w, visible_area.h = blank_area.w, blank_area.h
|
|
|
|
visible_area.x = page_area.x
|
|
|
|
visible_area.y = page_area.y + page_area.h - visible_area.h
|
|
|
|
visible_area = visible_area:shrinkInside(page_area, offset.x, offset.y)
|
|
|
|
-- shrink blank area by the height of visible area
|
|
|
|
blank_area.h = blank_area.h - visible_area.h
|
|
|
|
return {
|
|
|
|
page = self.view.state.page,
|
|
|
|
zoom = self.view.state.zoom,
|
|
|
|
rotation = self.view.state.rotation,
|
|
|
|
gamma = self.view.state.gamma,
|
|
|
|
offset = Geom:new{ x = self.view.state.offset.x, y = 0},
|
|
|
|
visible_area = visible_area,
|
|
|
|
page_area = page_area,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-04-14 09:39:12 +00:00
|
|
|
function ReaderPaging:updateTopPageState(state, blank_area, offset)
|
2013-03-10 06:23:26 +00:00
|
|
|
local visible_area = Geom:new{x = 0, y = 0}
|
|
|
|
visible_area.w, visible_area.h = blank_area.w, blank_area.h
|
|
|
|
visible_area.x, visible_area.y = state.visible_area.x, state.visible_area.y
|
2013-03-14 06:51:52 +00:00
|
|
|
if state.page == self.number_of_pages then
|
|
|
|
visible_area:offsetWithin(state.page_area, offset.x, offset.y)
|
|
|
|
else
|
|
|
|
visible_area = visible_area:shrinkInside(state.page_area, offset.x, offset.y)
|
|
|
|
end
|
2013-03-10 06:23:26 +00:00
|
|
|
-- shrink blank area by the height of visible area
|
|
|
|
blank_area.h = blank_area.h - visible_area.h
|
|
|
|
state.visible_area = visible_area
|
|
|
|
return state
|
|
|
|
end
|
|
|
|
|
2013-04-14 09:39:12 +00:00
|
|
|
function ReaderPaging:updateBottomPageState(state, blank_area, offset)
|
2013-03-10 06:23:26 +00:00
|
|
|
local visible_area = Geom:new{x = 0, y = 0}
|
|
|
|
visible_area.w, visible_area.h = blank_area.w, blank_area.h
|
|
|
|
visible_area.x = state.page_area.x
|
|
|
|
visible_area.y = state.visible_area.y + state.visible_area.h - visible_area.h
|
2013-03-14 06:51:52 +00:00
|
|
|
if state.page == 1 then
|
|
|
|
visible_area:offsetWithin(state.page_area, offset.x, offset.y)
|
|
|
|
else
|
|
|
|
visible_area = visible_area:shrinkInside(state.page_area, offset.x, offset.y)
|
|
|
|
end
|
2013-03-10 06:23:26 +00:00
|
|
|
-- shrink blank area by the height of visible area
|
|
|
|
blank_area.h = blank_area.h - visible_area.h
|
|
|
|
state.visible_area = visible_area
|
|
|
|
return state
|
|
|
|
end
|
|
|
|
|
2013-04-14 09:39:12 +00:00
|
|
|
function ReaderPaging:genPageStatesFromTop(top_page_state, blank_area, offset)
|
|
|
|
-- Offset should always be greater than 0
|
|
|
|
-- otherwise if offset is less than 0 the height of blank area will be
|
|
|
|
-- larger than 0 even if page area is much larger than visible area,
|
|
|
|
-- which will trigger the drawing of next page leaving part of current
|
|
|
|
-- page undrawn. This should also be true for generating from bottom.
|
|
|
|
if offset.y < 0 then offset.y = 0 end
|
|
|
|
local state = self:updateTopPageState(top_page_state, blank_area, offset)
|
|
|
|
--DEBUG("updated state", state)
|
|
|
|
local page_states = {}
|
|
|
|
if state.visible_area.h > 0 then
|
|
|
|
table.insert(page_states, state)
|
|
|
|
end
|
|
|
|
--DEBUG("blank area", blank_area)
|
|
|
|
local current_page = state.page
|
|
|
|
while blank_area.h > 0 do
|
|
|
|
blank_area.h = blank_area.h - self.view.page_gap.height
|
|
|
|
if blank_area.h > 0 then
|
|
|
|
if self.current_page == self.number_of_pages then break end
|
|
|
|
self:gotoPage(current_page + 1, "scrolling")
|
|
|
|
current_page = current_page + 1
|
|
|
|
local state = self:getNextPageState(blank_area, Geom:new{})
|
|
|
|
--DEBUG("new state", state)
|
|
|
|
table.insert(page_states, state)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return page_states
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:genPageStatesFromBottom(bottom_page_state, blank_area, offset)
|
|
|
|
-- scroll up offset should always be less than 0
|
|
|
|
if offset.y > 0 then offset.y = 0 end
|
|
|
|
local state = self:updateBottomPageState(bottom_page_state, blank_area, offset)
|
|
|
|
--DEBUG("updated state", state)
|
|
|
|
local page_states = {}
|
|
|
|
if state.visible_area.h > 0 then
|
|
|
|
table.insert(page_states, state)
|
|
|
|
end
|
|
|
|
--DEBUG("blank area", blank_area)
|
|
|
|
local current_page = state.page
|
|
|
|
while blank_area.h > 0 do
|
|
|
|
blank_area.h = blank_area.h - self.view.page_gap.height
|
|
|
|
if blank_area.h > 0 then
|
|
|
|
if self.current_page == 1 then break end
|
|
|
|
self:gotoPage(current_page - 1, "scrolling")
|
|
|
|
current_page = current_page - 1
|
|
|
|
local state = self:getPrevPageState(blank_area, Geom:new{})
|
|
|
|
--DEBUG("new state", state)
|
|
|
|
table.insert(page_states, 1, state)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return page_states
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderPaging:onScrollPanRel(diff)
|
|
|
|
DEBUG("pan relative height:", diff)
|
|
|
|
local blank_area = Geom:new{}
|
|
|
|
blank_area:setSizeTo(self.view.dimen)
|
|
|
|
if diff > 0 then
|
|
|
|
local first_page_state = table.remove(self.view.page_states, 1)
|
|
|
|
local offset = Geom:new{
|
|
|
|
x = 0,
|
|
|
|
y = diff,
|
|
|
|
}
|
|
|
|
self.view.page_states = self:genPageStatesFromTop(first_page_state, blank_area, offset)
|
|
|
|
end
|
|
|
|
if diff < 0 then
|
|
|
|
local last_page_state = table.remove(self.view.page_states)
|
|
|
|
local offset = Geom:new{
|
|
|
|
x = 0,
|
|
|
|
y = diff
|
|
|
|
}
|
|
|
|
self.view.page_states = self:genPageStatesFromBottom(last_page_state, blank_area, offset)
|
|
|
|
end
|
|
|
|
-- update current pageno to the very last part in current view
|
|
|
|
self:gotoPage(self.view.page_states[#self.view.page_states].page, "scrolling")
|
2013-07-22 14:07:49 +00:00
|
|
|
|
2013-07-23 06:51:38 +00:00
|
|
|
UIManager:setDirty(self.view.dialog, "fast")
|
2013-04-14 09:39:12 +00:00
|
|
|
end
|
|
|
|
|
2013-03-10 06:23:26 +00:00
|
|
|
function ReaderPaging:onScrollPageRel(diff)
|
|
|
|
DEBUG("scroll relative page:", diff)
|
|
|
|
local blank_area = Geom:new{}
|
|
|
|
blank_area:setSizeTo(self.view.dimen)
|
|
|
|
if diff > 0 then
|
|
|
|
local last_page_state = table.remove(self.view.page_states)
|
|
|
|
local offset = Geom:new{
|
|
|
|
x = 0,
|
|
|
|
y = last_page_state.visible_area.h - self.overlap
|
|
|
|
}
|
2013-04-14 09:39:12 +00:00
|
|
|
self.view.page_states = self:genPageStatesFromTop(last_page_state, blank_area, offset)
|
2013-03-10 06:23:26 +00:00
|
|
|
end
|
|
|
|
if diff < 0 then
|
|
|
|
local first_page_state = table.remove(self.view.page_states, 1)
|
|
|
|
local offset = Geom:new{
|
|
|
|
x = 0,
|
|
|
|
y = -first_page_state.visible_area.h + self.overlap
|
|
|
|
}
|
2013-04-14 09:39:12 +00:00
|
|
|
self.view.page_states = self:genPageStatesFromBottom(first_page_state, blank_area, offset)
|
2013-03-10 06:23:26 +00:00
|
|
|
end
|
2013-03-23 08:32:13 +00:00
|
|
|
-- update current pageno to the very last part in current view
|
|
|
|
self:gotoPage(self.view.page_states[#self.view.page_states].page, "scrolling")
|
2013-03-10 06:23:26 +00:00
|
|
|
UIManager:setDirty(self.view.dialog)
|
|
|
|
end
|
|
|
|
|
2012-05-18 22:50:26 +00:00
|
|
|
function ReaderPaging:onGotoPageRel(diff)
|
2012-05-28 16:59:16 +00:00
|
|
|
DEBUG("goto relative page:", diff)
|
2012-12-03 05:48:41 +00:00
|
|
|
local new_va = self.visible_area:copy()
|
|
|
|
local x_pan_off, y_pan_off = 0, 0
|
|
|
|
|
|
|
|
if self.zoom_mode:find("width") then
|
|
|
|
y_pan_off = self.visible_area.h * diff
|
|
|
|
elseif self.zoom_mode:find("height") then
|
|
|
|
x_pan_off = self.visible_area.w * diff
|
|
|
|
else
|
|
|
|
-- must be fit content or page zoom mode
|
|
|
|
if self.visible_area.w == self.page_area.w then
|
|
|
|
y_pan_off = self.visible_area.h * diff
|
|
|
|
else
|
|
|
|
x_pan_off = self.visible_area.w * diff
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- adjust offset to help with page turn decision
|
2012-12-08 06:05:10 +00:00
|
|
|
-- we dont take overlap into account here yet, otherwise new_va will
|
|
|
|
-- always intersect with page_area
|
2013-10-18 20:38:07 +00:00
|
|
|
x_pan_off = Math.roundAwayFromZero(x_pan_off)
|
|
|
|
y_pan_off = Math.roundAwayFromZero(y_pan_off)
|
|
|
|
new_va.x = Math.roundAwayFromZero(self.visible_area.x+x_pan_off)
|
|
|
|
new_va.y = Math.roundAwayFromZero(self.visible_area.y+y_pan_off)
|
2012-12-03 05:48:41 +00:00
|
|
|
|
2012-12-08 06:05:10 +00:00
|
|
|
if new_va:notIntersectWith(self.page_area) then
|
|
|
|
-- view area out of page area, do a page turn
|
2012-12-03 05:48:41 +00:00
|
|
|
self:gotoPage(self.current_page + diff)
|
|
|
|
-- if we are going back to previous page, reset
|
2012-12-08 06:05:10 +00:00
|
|
|
-- view area to bottom of previous page
|
2012-12-03 05:48:41 +00:00
|
|
|
if x_pan_off < 0 then
|
|
|
|
self.view:PanningUpdate(self.page_area.w, 0)
|
|
|
|
elseif y_pan_off < 0 then
|
|
|
|
self.view:PanningUpdate(0, self.page_area.h)
|
|
|
|
end
|
2012-12-08 06:05:10 +00:00
|
|
|
-- reset dim_area
|
|
|
|
--self.view.dim_area.h = 0
|
|
|
|
--self.view.dim_area.w = 0
|
|
|
|
--
|
2012-12-03 05:48:41 +00:00
|
|
|
else
|
2012-12-08 06:05:10 +00:00
|
|
|
-- not end of page yet, goto next view
|
|
|
|
-- adjust panning step according to overlap
|
|
|
|
if self.show_overlap_enable then
|
|
|
|
if x_pan_off > self.overlap then
|
|
|
|
-- moving to next view, move view
|
|
|
|
x_pan_off = x_pan_off - self.overlap
|
|
|
|
elseif x_pan_off < -self.overlap then
|
|
|
|
x_pan_off = x_pan_off + self.overlap
|
|
|
|
end
|
|
|
|
if y_pan_off > self.overlap then
|
|
|
|
y_pan_off = y_pan_off - self.overlap
|
|
|
|
elseif y_pan_off < -self.overlap then
|
|
|
|
y_pan_off = y_pan_off + self.overlap
|
|
|
|
end
|
|
|
|
-- we have to calculate again to count into overlap
|
2013-10-18 20:38:07 +00:00
|
|
|
new_va.x = Math.roundAwayFromZero(self.visible_area.x+x_pan_off)
|
|
|
|
new_va.y = Math.roundAwayFromZero(self.visible_area.y+y_pan_off)
|
2012-12-08 06:05:10 +00:00
|
|
|
end
|
2012-12-03 05:48:41 +00:00
|
|
|
-- fit new view area into page area
|
|
|
|
new_va:offsetWithin(self.page_area, 0, 0)
|
2012-12-08 06:05:10 +00:00
|
|
|
-- calculate panning offsets
|
|
|
|
local panned_x = new_va.x - self.visible_area.x
|
|
|
|
local panned_y = new_va.y - self.visible_area.y
|
|
|
|
-- adjust for crazy float point overflow...
|
|
|
|
if math.abs(panned_x) < 1 then
|
|
|
|
panned_x = 0
|
|
|
|
end
|
|
|
|
if math.abs(panned_y) < 1 then
|
|
|
|
panned_y = 0
|
|
|
|
end
|
|
|
|
-- singal panning update
|
|
|
|
self.view:PanningUpdate(panned_x, panned_y)
|
|
|
|
-- update dime area in ReaderView
|
|
|
|
if self.show_overlap_enable then
|
|
|
|
self.view.dim_area.h = new_va.h - math.abs(panned_y)
|
|
|
|
self.view.dim_area.w = new_va.w - math.abs(panned_x)
|
|
|
|
if panned_y < 0 then
|
|
|
|
self.view.dim_area.y = new_va.y - panned_y
|
|
|
|
else
|
|
|
|
self.view.dim_area.y = 0
|
|
|
|
end
|
|
|
|
if panned_x < 0 then
|
|
|
|
self.view.dim_area.x = new_va.x - panned_x
|
|
|
|
else
|
|
|
|
self.view.dim_area.x = 0
|
|
|
|
end
|
|
|
|
end
|
2012-12-03 05:48:41 +00:00
|
|
|
-- update self.visible_area
|
|
|
|
self.visible_area = new_va
|
|
|
|
end
|
|
|
|
|
2012-05-18 22:50:26 +00:00
|
|
|
return true
|
|
|
|
end
|
2012-06-26 17:00:21 +00:00
|
|
|
|
2013-01-03 14:24:38 +00:00
|
|
|
function ReaderPaging:onRedrawCurrentPage()
|
|
|
|
self.ui:handleEvent(Event:new("PageUpdate", self.current_page))
|
2013-01-07 21:43:28 +00:00
|
|
|
return true
|
2013-01-03 14:24:38 +00:00
|
|
|
end
|
2013-02-02 06:36:29 +00:00
|
|
|
|
|
|
|
function ReaderPaging:onSetDimensions()
|
|
|
|
-- update listening according to new screen dimen
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self:initGesListener()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- wrapper for bounds checking
|
2013-03-10 06:23:26 +00:00
|
|
|
function ReaderPaging:gotoPage(number, orig)
|
2013-02-02 06:36:29 +00:00
|
|
|
if number == self.current_page then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if number > self.number_of_pages
|
|
|
|
or number < 1 then
|
|
|
|
DEBUG("wrong page number: "..number.."!")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
DEBUG("going to page number", number)
|
|
|
|
|
|
|
|
-- this is an event to allow other controllers to be aware of this change
|
2013-03-10 06:23:26 +00:00
|
|
|
self.ui:handleEvent(Event:new("PageUpdate", number, orig))
|
2013-02-02 06:36:29 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-07-30 15:37:51 +00:00
|
|
|
function ReaderPaging:onGotoPage(number)
|
|
|
|
self:gotoPage(number)
|
|
|
|
return true
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return ReaderPaging
|