2
0
mirror of https://github.com/koreader/koreader synced 2024-11-18 03:25:46 +00:00
koreader/frontend/apps/reader/modules/readerpaging.lua

797 lines
28 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local InputContainer = require("ui/widget/container/inputcontainer")
local Screen = require("device").screen
2013-10-18 20:38:07 +00:00
local Geom = require("ui/geometry")
local Input = require("device").input
2013-10-18 20:38:07 +00:00
local GestureRange = require("ui/gesturerange")
local Device = require("device")
2013-10-18 20:38:07 +00:00
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")
local _ = require("gettext")
2013-10-18 20:38:07 +00:00
local ReaderPaging = InputContainer:new{
2014-03-13 13:52:43 +00:00
current_page = 0,
number_of_pages = 0,
last_pan_relative_y = 0,
visible_area = nil,
page_area = nil,
show_overlap_enable = nil,
overlap = Screen:scaleBySize(DOVERLAPPIXELS),
2014-03-13 13:52:43 +00:00
page_flipping_mode = false,
bookmark_flipping_mode = false,
flip_steps = {0,1,2,5,10,20,50,100}
}
function ReaderPaging:init()
if Device:hasKeyboard() or Device:hasKeys() then
2014-03-13 13:52:43 +00:00
self.key_events = {
GotoNextPage = {
{Input.group.PgFwd}, doc = "go to next page",
event = "GotoPageRel", args = 1 },
GotoPrevPage = {
{Input.group.PgBack}, doc = "go to previous page",
event = "GotoPageRel", args = -1 },
GotoFirst = {
{"1"}, doc = "go to start", event = "GotoPercent", args = 0},
Goto11 = {
{"2"}, doc = "go to 11%", event = "GotoPercent", args = 11},
Goto22 = {
{"3"}, doc = "go to 22%", event = "GotoPercent", args = 22},
Goto33 = {
{"4"}, doc = "go to 33%", event = "GotoPercent", args = 33},
Goto44 = {
{"5"}, doc = "go to 44%", event = "GotoPercent", args = 44},
Goto55 = {
{"6"}, doc = "go to 55%", event = "GotoPercent", args = 55},
Goto66 = {
{"7"}, doc = "go to 66%", event = "GotoPercent", args = 66},
Goto77 = {
{"8"}, doc = "go to 77%", event = "GotoPercent", args = 77},
Goto88 = {
{"9"}, doc = "go to 88%", event = "GotoPercent", args = 88},
GotoLast = {
{"0"}, doc = "go to end", event = "GotoPercent", args = 100},
}
end
self.number_of_pages = self.ui.document.info.number_of_pages
self.ui.menu:registerToMainMenu(self)
end
2013-02-02 06:36:29 +00:00
-- This method will be called in onSetDimensions handler
function ReaderPaging:initGesListener()
2014-03-13 13:52:43 +00:00
self.ges_events = {
TapForward = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = Screen:getWidth()*DTAP_ZONE_FORWARD.x,
y = Screen:getHeight()*DTAP_ZONE_FORWARD.y,
w = Screen:getWidth()*DTAP_ZONE_FORWARD.w,
h = Screen:getHeight()*DTAP_ZONE_FORWARD.h,
}
}
},
TapBackward = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = Screen:getWidth()*DTAP_ZONE_BACKWARD.x,
y = Screen:getHeight()*DTAP_ZONE_BACKWARD.y,
w = Screen:getWidth()*DTAP_ZONE_BACKWARD.w,
h = Screen:getHeight()*DTAP_ZONE_BACKWARD.h,
}
}
},
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(),
},
2014-11-11 02:44:54 +00:00
rate = Screen.eink and 4.0 or 10.0,
2014-03-13 13:52:43 +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
function ReaderPaging:onReadSettings(config)
2014-03-13 13:52:43 +00:00
self.page_positions = config:readSetting("page_positions") or {}
self:gotoPage(config:readSetting("last_page") or 1)
self.show_overlap_enable = config:readSetting("show_overlap_enable")
if self.show_overlap_enable == nil then
self.show_overlap_enable = DSHOWOVERLAP
end
end
2013-12-27 15:18:16 +00:00
function ReaderPaging:onSaveSettings()
2014-03-13 13:52:43 +00:00
self.ui.doc_settings:saveSetting("page_positions", self.page_positions)
self.ui.doc_settings:saveSetting("last_page", self:getTopPage())
self.ui.doc_settings:saveSetting("percent_finished", self.current_page/self.number_of_pages)
self.ui.doc_settings:saveSetting("show_overlap_enable", self.show_overlap_enable)
end
function ReaderPaging:addToMainMenu(tab_item_table)
-- FIXME: repeated code with page overlap menu for readerrolling
-- needs to keep only one copy of the logic as for the DRY principle.
-- The difference between the two menus is only the enabled func.
local page_overlap_menu = {
{
text_func = function()
return self.show_overlap_enable and _("Disable") or _("Enable")
end,
callback = function()
self.show_overlap_enable = not self.show_overlap_enable
if not self.show_overlap_enable then
self.view:resetDimArea()
end
end
},
}
for _, menu_entry in ipairs(self.view:genOverlapStyleMenu()) do
table.insert(page_overlap_menu, menu_entry)
end
table.insert(tab_item_table.typeset, {
text = _("Page overlap"),
enabled_func = function()
return not self.view.page_scroll and self.zoom_mode ~= "page"
and not self.zoom_mode:find("height")
end,
sub_item_table = page_overlap_menu,
})
end
--[[
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.
--]]
function ReaderPaging:setPagePosition(page, pos)
2014-03-13 13:52:43 +00:00
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.
2014-03-13 13:52:43 +00:00
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()
2014-03-13 13:52:43 +00:00
self:onPagingRel(1)
return true
2012-12-04 09:05:58 +00:00
end
function ReaderPaging:onTapBackward()
2014-03-13 13:52:43 +00:00
self:onPagingRel(-1)
return true
2012-12-04 09:05:58 +00:00
end
function ReaderPaging:onTogglePageFlipping()
2014-03-13 13:52:43 +00:00
self.view.flipping_visible = not self.view.flipping_visible
self.page_flipping_mode = self.view.flipping_visible
self.flipping_page = self.current_page
2014-03-13 13:52:43 +00:00
if self.page_flipping_mode then
self:updateOriginalPage(self.current_page)
self:enterFlippingMode()
else
self:updateOriginalPage(nil)
self:exitFlippingMode()
end
self.ui:handleEvent(Event:new("SetHinting", not self.page_flipping_mode))
self.ui:handleEvent(Event:new("ReZoom"))
UIManager:setDirty(self.view.dialog, "partial")
end
function ReaderPaging:onToggleBookmarkFlipping()
2014-03-13 13:52:43 +00:00
self.bookmark_flipping_mode = not self.bookmark_flipping_mode
2014-03-13 13:52:43 +00:00
if self.bookmark_flipping_mode then
self.orig_flipping_mode = self.view.flipping_visible
self.orig_dogear_mode = self.view.dogear_visible
2014-03-13 13:52:43 +00:00
self.view.flipping_visible = true
self.view.dogear_visible = true
self.bm_flipping_orig_page = self.current_page
self:enterFlippingMode()
else
self.view.flipping_visible = self.orig_flipping_mode
self.view.dogear_visible = self.orig_dogear_mode
self:exitFlippingMode()
self:gotoPage(self.bm_flipping_orig_page)
end
self.ui:handleEvent(Event:new("SetHinting", not self.bookmark_flipping_mode))
self.ui:handleEvent(Event:new("ReZoom"))
UIManager:setDirty(self.view.dialog, "partial")
end
function ReaderPaging:enterFlippingMode()
self.ui:handleEvent(Event:new("EnterFlippingMode"))
2014-03-13 13:52:43 +00:00
self.orig_reflow_mode = self.view.document.configurable.text_wrap
self.orig_scroll_mode = self.view.page_scroll
self.orig_zoom_mode = self.view.zoom_mode
DEBUG("store zoom mode", self.orig_zoom_mode)
self.DGESDETECT_DISABLE_DOUBLE_TAP = DGESDETECT_DISABLE_DOUBLE_TAP
2014-03-13 13:52:43 +00:00
self.view.document.configurable.text_wrap = 0
self.view.page_scroll = false
Input.disable_double_tap = false
DGESDETECT_DISABLE_DOUBLE_TAP = false
self.ui:handleEvent(Event:new("SetZoomMode", "page"))
end
function ReaderPaging:exitFlippingMode()
self.ui:handleEvent(Event:new("ExitFlippingMode"))
2014-03-13 13:52:43 +00:00
self.view.document.configurable.text_wrap = self.orig_reflow_mode
self.view.page_scroll = self.orig_scroll_mode
DGESDETECT_DISABLE_DOUBLE_TAP = self.DGESDETECT_DISABLE_DOUBLE_TAP
Input.disable_double_tap = DGESDETECT_DISABLE_DOUBLE_TAP
DEBUG("restore zoom mode", self.orig_zoom_mode)
self.ui:handleEvent(Event:new("SetZoomMode", self.orig_zoom_mode))
end
function ReaderPaging:updateOriginalPage(page)
2014-03-13 13:52:43 +00:00
self.original_page = page
end
function ReaderPaging:updateFlippingPage(page)
2014-03-13 13:52:43 +00:00
self.flipping_page = page
end
function ReaderPaging:pageFlipping(flipping_page, flipping_ges)
2014-03-13 13:52:43 +00:00
local whole = self.number_of_pages
local steps = #self.flip_steps
local stp_proportion = flipping_ges.distance / Screen:getWidth()
local abs_proportion = flipping_ges.distance / Screen:getHeight()
if flipping_ges.direction == "east" then
self:gotoPage(flipping_page - self.flip_steps[math.ceil(steps*stp_proportion)])
elseif flipping_ges.direction == "west" then
self:gotoPage(flipping_page + self.flip_steps[math.ceil(steps*stp_proportion)])
elseif flipping_ges.direction == "south" then
self:gotoPage(flipping_page - math.floor(whole*abs_proportion))
elseif flipping_ges.direction == "north" then
self:gotoPage(flipping_page + math.floor(whole*abs_proportion))
end
UIManager:setDirty(self.view.dialog, "partial")
end
function ReaderPaging:bookmarkFlipping(flipping_page, flipping_ges)
2014-03-13 13:52:43 +00:00
if flipping_ges.direction == "east" then
self.ui:handleEvent(Event:new("GotoPreviousBookmark", flipping_page))
elseif flipping_ges.direction == "west" then
self.ui:handleEvent(Event:new("GotoNextBookmark", flipping_page))
end
UIManager:setDirty(self.view.dialog, "partial")
end
function ReaderPaging:onSwipe(arg, ges)
2014-03-13 13:52:43 +00:00
if self.bookmark_flipping_mode then
self:bookmarkFlipping(self.current_page, ges)
elseif self.page_flipping_mode and self.original_page then
self:gotoPage(self.original_page)
elseif ges.direction == "west" then
2014-07-03 16:20:14 +00:00
if DCHANGE_WEST_SWIPE_TO_EAST then
self:onPagingRel(-1)
else
self:onPagingRel(1)
end
2014-03-13 13:52:43 +00:00
elseif ges.direction == "east" then
2014-07-03 16:20:14 +00:00
if DCHANGE_EAST_SWIPE_TO_WEST then
self:onPagingRel(1)
else
self:onPagingRel(-1)
end
2014-03-13 13:52:43 +00:00
else
-- trigger full refresh
UIManager:setDirty(nil, "full")
2014-03-13 13:52:43 +00:00
end
end
function ReaderPaging:onPan(arg, ges)
2014-03-13 13:52:43 +00:00
if self.bookmark_flipping_mode then
return true
elseif self.page_flipping_mode then
if self.view.zoom_mode == "page" then
self:pageFlipping(self.flipping_page, ges)
else
self.view:PanningStart(-ges.relative.x, -ges.relative.y)
end
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
end
return true
end
function ReaderPaging:onPanRelease(arg, ges)
2014-03-13 13:52:43 +00:00
if self.page_flipping_mode then
if self.view.zoom_mode == "page" then
self:updateFlippingPage(self.current_page)
else
self.view:PanningStop()
end
else
self.last_pan_relative_y = 0
-- trigger full refresh to clear ghosting generated by previous fast refresh
2014-12-03 03:33:54 +00:00
UIManager:setDirty(nil, "full")
2014-03-13 13:52:43 +00:00
end
end
function ReaderPaging:onZoomModeUpdate(new_mode)
2014-03-13 13:52:43 +00:00
-- 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)
2014-03-13 13:52:43 +00:00
self.current_page = new_page_no
if orig ~= "scrolling" then
self.ui:handleEvent(Event:new("InitScrollPageStates", orig))
end
end
function ReaderPaging:onViewRecalculate(visible_area, page_area)
2014-03-13 13:52:43 +00:00
-- we need to remember areas to handle page turn event
self.visible_area = visible_area:copy()
self.page_area = page_area
end
function ReaderPaging:onGotoPercent(percent)
2014-03-13 13:52:43 +00:00
DEBUG("goto document offset in percent:", percent)
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)
2014-03-13 13:52:43 +00:00
if self.view.page_scroll then
self:onScrollPageRel(diff)
else
self:onGotoPageRel(diff)
end
self:setPagePosition(self:getTopPage(), self:getTopPosition())
return true
2013-03-10 06:23:26 +00:00
end
2013-04-14 09:39:12 +00:00
function ReaderPaging:onPanningRel(diff)
2014-03-13 13:52:43 +00:00
if self.view.page_scroll then
self:onScrollPanRel(diff)
end
self:setPagePosition(self:getTopPage(), self:getTopPosition())
return true
2013-04-14 09:39:12 +00:00
end
--[[
Get read percentage on current page.
--]]
function ReaderPaging:getTopPosition()
2014-03-13 13:52:43 +00:00
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()
2014-03-13 13:52:43 +00:00
if self.view.page_scroll then
local state = self.view.page_states[1]
return state and state.page or self.current_page
else
return self.current_page
end
end
2013-03-10 06:23:26 +00:00
function ReaderPaging:onInitScrollPageStates(orig)
2014-03-13 13:52:43 +00:00
--DEBUG.traceback()
DEBUG("init scroll page states", orig)
if self.view.page_scroll and self.view.state.page then
self.orig_page = self.current_page
self.view.page_states = {}
local blank_area = Geom:new{}
blank_area:setSizeTo(self.view.dimen)
while blank_area.h > 0 do
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)
--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
self:gotoPage(self.orig_page, "scrolling")
end
return true
2013-03-10 06:23:26 +00:00
end
function ReaderPaging:onUpdateScrollPageRotation(rotation)
2014-03-13 13:52:43 +00:00
for _, state in ipairs(self.view.page_states) do
state.rotation = rotation
end
return true
2013-03-10 06:23:26 +00:00
end
function ReaderPaging:onUpdateScrollPageGamma(gamma)
2014-03-13 13:52:43 +00:00
for _, state in ipairs(self.view.page_states) do
state.gamma = gamma
end
return true
2013-03-10 06:23:26 +00:00
end
function ReaderPaging:getNextPageState(blank_area, offset)
2014-03-13 13:52:43 +00:00
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,
}
2013-03-10 06:23:26 +00:00
end
function ReaderPaging:getPrevPageState(blank_area, offset)
2014-03-13 13:52:43 +00:00
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,
}
2013-03-10 06:23:26 +00:00
end
2013-04-14 09:39:12 +00:00
function ReaderPaging:updateTopPageState(state, blank_area, offset)
2014-03-13 13:52:43 +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
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
-- 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
2013-03-10 06:23:26 +00:00
end
2013-04-14 09:39:12 +00:00
function ReaderPaging:updateBottomPageState(state, blank_area, offset)
2014-03-13 13:52:43 +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
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
-- 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
2013-03-10 06:23:26 +00:00
end
2013-04-14 09:39:12 +00:00
function ReaderPaging:genPageStatesFromTop(top_page_state, blank_area, offset)
2014-03-13 13:52:43 +00:00
-- 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
2013-04-14 09:39:12 +00:00
end
function ReaderPaging:genPageStatesFromBottom(bottom_page_state, blank_area, offset)
2014-03-13 13:52:43 +00:00
-- 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
2013-04-14 09:39:12 +00:00
end
function ReaderPaging:onScrollPanRel(diff)
2014-03-13 13:52:43 +00:00
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")
2014-03-13 13:52:43 +00:00
UIManager:setDirty(self.view.dialog, "fast")
2013-04-14 09:39:12 +00:00
end
2014-07-17 13:27:35 +00:00
function ReaderPaging:calculateOverlap()
local footer_height = (self.view.footer_visible and 1 or 0) * self.view.footer.bar_height
return self.overlap + footer_height
end
2013-03-10 06:23:26 +00:00
function ReaderPaging:onScrollPageRel(diff)
2014-03-13 13:52:43 +00:00
DEBUG("scroll relative page:", diff)
local blank_area = Geom:new{}
blank_area:setSizeTo(self.view.dimen)
2014-07-17 13:27:35 +00:00
local overlap = self:calculateOverlap()
2014-03-13 13:52:43 +00:00
if diff > 0 then
local last_page_state = table.remove(self.view.page_states)
local offset = Geom:new{
x = 0,
2014-07-17 13:27:35 +00:00
y = last_page_state.visible_area.h - overlap
2014-03-13 13:52:43 +00:00
}
self.view.page_states = self:genPageStatesFromTop(last_page_state, blank_area, offset)
end
if diff < 0 then
local first_page_state = table.remove(self.view.page_states, 1)
local offset = Geom:new{
x = 0,
2014-07-17 13:27:35 +00:00
y = -first_page_state.visible_area.h + overlap
2014-03-13 13:52:43 +00:00
}
self.view.page_states = self:genPageStatesFromBottom(first_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")
UIManager:setDirty(self.view.dialog, "partial")
2013-03-10 06:23:26 +00:00
end
function ReaderPaging:onGotoPageRel(diff)
2014-03-13 13:52:43 +00:00
DEBUG("goto relative page:", diff)
local new_va = self.visible_area:copy()
local x_pan_off, y_pan_off = 0, 0
if self.zoom_mode == "free" then
-- do nothing in free zoom mode
elseif self.zoom_mode:find("width") then
y_pan_off = self.visible_area.h * diff
elseif self.zoom_mode:find("height") then
-- negative x panning if writing direction is right to left
local direction = self.ui.document.configurable.writing_direction
x_pan_off = self.visible_area.w * diff * (direction == 1 and -1 or 1)
2014-03-13 13:52:43 +00:00
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
-- we dont take overlap into account here yet, otherwise new_va will
-- always intersect with page_area
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)
if new_va:notIntersectWith(self.page_area) then
-- view area out of page area, do a page turn
self:gotoPage(self.current_page + diff)
-- if we are going back to previous page, reset
-- view area to bottom of previous page
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
-- reset dim_area
--self.view.dim_area.h = 0
--self.view.dim_area.w = 0
--
else
-- not end of page yet, goto next view
-- adjust panning step according to overlap
2014-07-17 13:27:35 +00:00
local overlap = self:calculateOverlap()
if x_pan_off > overlap then
2014-03-13 13:52:43 +00:00
-- moving to next view, move view
2014-07-17 13:27:35 +00:00
x_pan_off = x_pan_off - overlap
elseif x_pan_off < -overlap then
x_pan_off = x_pan_off + overlap
2014-03-13 13:52:43 +00:00
end
2014-07-17 13:27:35 +00:00
if y_pan_off > overlap then
y_pan_off = y_pan_off - overlap
elseif y_pan_off < -overlap then
y_pan_off = y_pan_off + overlap
2014-03-13 13:52:43 +00:00
end
-- we have to calculate again to count into overlap
new_va.x = Math.roundAwayFromZero(self.visible_area.x+x_pan_off)
new_va.y = Math.roundAwayFromZero(self.visible_area.y+y_pan_off)
-- fit new view area into page area
new_va:offsetWithin(self.page_area, 0, 0)
-- 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
-- update self.visible_area
self.visible_area = new_va
end
return true
end
function ReaderPaging:onRedrawCurrentPage()
2014-03-13 13:52:43 +00:00
self.ui:handleEvent(Event:new("PageUpdate", self.current_page))
return true
end
2013-02-02 06:36:29 +00:00
function ReaderPaging:onSetDimensions()
2014-03-13 13:52:43 +00:00
-- update listening according to new screen dimen
if Device:isTouchDevice() then
self:initGesListener()
end
2013-02-02 06:36:29 +00:00
end
-- wrapper for bounds checking
2013-03-10 06:23:26 +00:00
function ReaderPaging:gotoPage(number, orig)
2014-03-13 13:52:43 +00:00
--DEBUG.traceback()
if number == self.current_page or not number 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
self.ui:handleEvent(Event:new("PageUpdate", number, orig))
return true
2013-02-02 06:36:29 +00:00
end
2013-07-30 15:37:51 +00:00
function ReaderPaging:onGotoPage(number)
2014-03-13 13:52:43 +00:00
self:gotoPage(number)
return true
2013-07-30 15:37:51 +00:00
end
2013-10-18 20:38:07 +00:00
function ReaderPaging:onGotoPercentage(percentage)
2014-03-13 13:52:43 +00:00
if percentage < 0 then percentage = 0 end
if percentage > 1 then percentage = 1 end
self:gotoPage(math.floor(percentage*self.number_of_pages))
return true
end
2013-10-18 20:38:07 +00:00
return ReaderPaging