2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/widget/container/inputcontainer.lua

97 lines
2.6 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local Event = require("ui/event")
local Geom = require("ui/geometry")
2013-10-22 18:51:29 +00:00
local DEBUG = require("dbg")
2013-10-18 20:38:07 +00:00
--[[
an InputContainer is an WidgetContainer that handles input events
an example for a key_event is this:
2014-03-13 13:52:43 +00:00
PanBy20 = {
{ "Shift", Input.group.Cursor },
seqtext = "Shift+Cursor",
doc = "pan by 20px",
event = "Pan", args = 20, is_inactive = true,
},
PanNormal = {
{ Input.group.Cursor },
seqtext = "Cursor",
doc = "pan by 10 px", event = "Pan", args = 10,
},
Quit = { {"Home"} },
2013-10-18 20:38:07 +00:00
it is suggested to reference configurable sequences from another table
and store that table as configuration setting
--]]
local InputContainer = WidgetContainer:new{
2014-03-13 13:52:43 +00:00
vertical_align = "top",
2013-10-18 20:38:07 +00:00
}
function InputContainer:_init()
2014-03-13 13:52:43 +00:00
-- we need to do deep copy here
local new_key_events = {}
if self.key_events then
for k,v in pairs(self.key_events) do
new_key_events[k] = v
end
end
self.key_events = new_key_events
2013-10-18 20:38:07 +00:00
2014-03-13 13:52:43 +00:00
local new_ges_events = {}
if self.ges_events then
for k,v in pairs(self.ges_events) do
new_ges_events[k] = v
end
end
self.ges_events = new_ges_events
2013-10-18 20:38:07 +00:00
2014-03-13 13:52:43 +00:00
if not self.dimen then
self.dimen = Geom:new{}
end
2013-10-18 20:38:07 +00:00
end
function InputContainer:paintTo(bb, x, y)
2014-03-13 13:52:43 +00:00
self.dimen.x = x
self.dimen.y = y
if self[1] then
if self.vertical_align == "center" then
local content_size = self[1]:getSize()
self[1]:paintTo(bb, x, y + math.floor((self.dimen.h - content_size.h)/2))
else
self[1]:paintTo(bb, x, y)
end
end
2013-10-18 20:38:07 +00:00
end
--[[
the following handler handles keypresses and checks if they lead to a command.
if this is the case, we retransmit another event within ourselves
--]]
function InputContainer:onKeyPress(key)
2014-03-13 13:52:43 +00:00
for name, seq in pairs(self.key_events) do
if not seq.is_inactive then
for _, oneseq in ipairs(seq) do
if key:match(oneseq) then
local eventname = seq.event or name
return self:handleEvent(Event:new(eventname, seq.args, key))
end
end
end
end
2013-10-18 20:38:07 +00:00
end
function InputContainer:onGesture(ev)
2014-03-13 13:52:43 +00:00
for name, gsseq in pairs(self.ges_events) do
for _, gs_range in ipairs(gsseq) do
--DEBUG("gs_range", gs_range)
if gs_range:match(ev) then
local eventname = gsseq.event or name
return self:handleEvent(Event:new(eventname, gsseq.args, ev))
end
end
end
2013-10-18 20:38:07 +00:00
end
return InputContainer