2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/tools/kobo_touch_probe.lua

126 lines
4.2 KiB
Lua
Raw Normal View History

-- touch probe utility
-- usage: ./luajit tools/kobo_touch_probe.lua
require "defaults"
package.path = "common/?.lua;rocks/share/lua/5.1/?.lua;frontend/?.lua;" .. package.path
package.cpath = "common/?.so;common/?.dll;/usr/lib/lua/?.so;rocks/lib/lua/5.1/?.so;" .. package.cpath
2016-08-11 14:53:00 +00:00
local DataStorage = require("datastorage")
local _ = require("gettext")
-- read settings and check for language override
-- but don't re-read if already done, to avoid causing problems for unit tests
-- has to be done before requiring other files because
-- they might call gettext on load
if G_reader_settings == nil then
G_reader_settings = require("luasettings"):open(
DataStorage:getDataDir().."/settings.reader.lua")
end
local lang_locale = G_reader_settings:readSetting("language")
if lang_locale then
_.changeLang(lang_locale)
end
local InputContainer = require("ui/widget/container/inputcontainer")
local CenterContainer = require("ui/widget/container/centercontainer")
2016-04-03 04:52:30 +00:00
local FrameContainer = require("ui/widget/container/framecontainer")
local OverlapGroup = require("ui/widget/overlapgroup")
local ImageWidget = require("ui/widget/imagewidget")
local TextWidget = require("ui/widget/textwidget")
local GestureRange = require("ui/gesturerange")
local UIManager = require("ui/uimanager")
local Blitbuffer = require("ffi/blitbuffer")
local Device = require("device")
2016-04-03 04:52:30 +00:00
local Screen = Device.screen
local Font = require("ui/font")
2016-04-03 04:52:30 +00:00
--dbg:turnOn()
Clarify our OOP semantics across the codebase (#9586) Basically: * Use `extend` for class definitions * Use `new` for object instantiations That includes some minor code cleanups along the way: * Updated `Widget`'s docs to make the semantics clearer. * Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283) * Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass). * Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events. * Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier. * Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references. * ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak). * Terminal: Make sure the shell is killed on plugin teardown. * InputText: Fix Home/End/Del physical keys to behave sensibly. * InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...). * OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of. * ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed! * Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
local TouchProbe = InputContainer:extend{
2016-04-03 04:52:30 +00:00
curr_probe_step = 1,
}
function TouchProbe:init()
self.ges_events.TapProbe = {
GestureRange:new{
ges = "tap",
}
}
2016-04-03 04:52:30 +00:00
self.image_widget = ImageWidget:new{
file = "tools/kobo-touch-probe.png",
}
2016-04-03 04:52:30 +00:00
local screen_w, screen_h = Screen:getWidth(), Screen:getHeight()
local img_w, img_h = self.image_widget:getSize().w, self.image_widget:getSize().h
self.probe_steps = {
{
hint_text = _("Tap the lower right corner"),
hint_icon_pos = {
x = screen_w-img_w,
y = screen_h-img_h,
}
},
2016-04-03 04:52:30 +00:00
{
hint_text = _("Tap the upper right corner"),
hint_icon_pos = {
x = screen_w-img_w,
y = 0,
}
},
}
self.hint_text_widget = TextWidget:new{
text = '',
face = Font:getFace("cfont", 30),
}
self[1] = FrameContainer:new{
bordersize = 0,
background = Blitbuffer.COLOR_WHITE,
OverlapGroup:new{
dimen = Screen:getSize(),
CenterContainer:new{
dimen = Screen:getSize(),
self.hint_text_widget,
},
2016-04-03 04:52:30 +00:00
self.image_widget,
},
}
2016-04-03 04:52:30 +00:00
self:updateProbeInstruction()
end
2016-04-03 04:52:30 +00:00
function TouchProbe:updateProbeInstruction()
local probe_step = self.probe_steps[self.curr_probe_step]
self.image_widget.overlap_offset = {
probe_step.hint_icon_pos.x,
probe_step.hint_icon_pos.y,
}
self.hint_text_widget:setText(probe_step.hint_text)
end
function TouchProbe:saveSwitchXYSetting(need_to_switch_xy)
-- save the settings here so device.input can pick it up
G_reader_settings:saveSetting("kobo_touch_switch_xy", need_to_switch_xy)
2016-04-03 04:52:30 +00:00
G_reader_settings:flush()
UIManager:quit()
end
2016-04-03 04:52:30 +00:00
function TouchProbe:onTapProbe(arg, ges)
if self.curr_probe_step == 1 then
local shorter_edge = math.min(Screen:getHeight(), Screen:getWidth())
if math.min(ges.pos.x, ges.pos.y) < shorter_edge/2 then
-- x mirrored, x should be close to zero and y should be close to
-- screen height
local need_to_switch_xy = ges.pos.x > ges.pos.y
self:saveSwitchXYSetting(need_to_switch_xy)
else
-- x not mirroed, need one more probe
self.curr_probe_step = 2
self:updateProbeInstruction()
UIManager:setDirty(self)
end
elseif self.curr_probe_step == 2 then
-- x not mirrored, y should be close to zero and x should be close
-- TouchProbe screen width
local need_to_switch_xy = ges.pos.x < ges.pos.y
self:saveSwitchXYSetting(need_to_switch_xy)
end
end
2016-04-03 04:52:30 +00:00
return TouchProbe