2
0
mirror of https://github.com/koreader/koreader synced 2024-11-13 19:11:25 +00:00
koreader/inputbox.lua

210 lines
5.6 KiB
Lua
Raw Normal View History

2012-02-18 10:21:03 +00:00
require "rendertext"
require "keys"
require "graphics"
InputBox = {
-- Class vars:
input_start_x = 145,
input_start_y = nil,
2012-02-19 06:41:44 +00:00
input_cur_x = nil, -- points to the start of next input pos
2012-02-18 10:21:03 +00:00
input_bg = 0,
2012-02-18 10:21:03 +00:00
2012-02-18 12:32:38 +00:00
input_string = "",
2012-02-19 06:41:44 +00:00
2012-02-19 11:51:08 +00:00
shiftmode = false,
altmode = false,
2012-02-19 06:41:44 +00:00
-- font for displaying input content
face = freetype.newBuiltinFace("mono", 25),
fhash = "m25",
fheight = 25,
fwidth = 16,
2012-02-18 10:21:03 +00:00
}
2012-02-19 06:41:44 +00:00
function InputBox:setDefaultInput(text)
self.input_string = ""
self:addString(text)
--renderUtf8Text(fb.bb, self.input_start_x, self.input_start_y,
--self.face, self.fhash, text, true)
--self.input_cur_x = self.input_start_x + (string.len(text) * self.fwidth)
--self.input_string = text
end
function InputBox:addString(str)
for i = 1, #str do
self:addChar(str:sub(i,i))
2012-02-18 10:21:03 +00:00
end
end
2012-02-19 06:41:44 +00:00
function InputBox:addChar(char)
renderUtf8Text(fb.bb, self.input_cur_x, self.input_start_y, self.face, self.fhash,
char, true)
fb:refresh(1, self.input_cur_x, self.input_start_y-19, self.fwidth, self.fheight)
self.input_cur_x = self.input_cur_x + self.fwidth
self.input_string = self.input_string .. char
2012-02-18 10:21:03 +00:00
end
function InputBox:delChar()
if self.input_start_x == self.input_cur_x then
return
end
2012-02-19 06:41:44 +00:00
self.input_cur_x = self.input_cur_x - self.fwidth
2012-02-18 10:21:03 +00:00
--fill last character with blank rectangle
2012-02-19 06:41:44 +00:00
fb.bb:paintRect(self.input_cur_x, self.input_start_y-19,
self.fwidth, self.fheight, self.input_bg)
fb:refresh(1, self.input_cur_x, self.input_start_y-19, self.fwidth, self.fheight)
2012-02-18 12:32:38 +00:00
self.input_string = self.input_string:sub(0,-2)
2012-02-18 10:21:03 +00:00
end
2012-02-19 06:41:44 +00:00
function InputBox:drawBox(ypos, w, h, title)
-- draw input border
fb.bb:paintRect(20, ypos, w, h, 5)
-- draw input slot
fb.bb:paintRect(140, ypos + 10, w - 130, h - 20, self.input_bg)
-- draw input title
renderUtf8Text(fb.bb, 35, self.input_start_y, self.face, self.fhash,
title, true)
end
--[[
|| d_text default to nil (used to set default text in input slot)
--]]
function InputBox:input(ypos, height, title, d_text)
2012-02-18 10:21:03 +00:00
local pagedirty = true
2012-02-19 06:41:44 +00:00
-- do some initilization
2012-02-18 10:21:03 +00:00
self.input_start_y = ypos + 35
self.input_cur_x = self.input_start_x
2012-02-19 06:41:44 +00:00
if d_text then -- if specified default text, draw it
w = fb.bb:getWidth() - 40
h = height - 45
self:drawBox(ypos, w, h, title)
self:setDefaultInput(d_text)
fb:refresh(1, 20, ypos, w, h)
pagedirty = false
else -- otherwise, leave the draw task to the main loop
self.input_string = ""
end
2012-02-18 10:21:03 +00:00
while true do
if pagedirty then
w = fb.bb:getWidth() - 40
h = height - 45
2012-02-19 06:41:44 +00:00
self:drawBox(ypos, w, h, title)
2012-02-18 12:32:38 +00:00
fb:refresh(1, 20, ypos, w, h)
2012-02-18 10:21:03 +00:00
pagedirty = false
end
local ev = input.waitForEvent()
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
ev.code = adjustFWKey(ev.code)
2012-02-19 11:51:08 +00:00
--local secs, usecs = util.gettime()
if ev.code == KEY_SHIFT then
Keys.shiftmode = true
2012-02-19 11:51:08 +00:00
elseif ev.code == KEY_ALT then
Keys.altmode = true
2012-02-19 11:51:08 +00:00
elseif ev.code == KEY_FW_UP then
2012-02-18 10:21:03 +00:00
elseif ev.code == KEY_FW_DOWN then
elseif ev.code == KEY_A then
self:addChar("a")
elseif ev.code == KEY_B then
self:addChar("b")
elseif ev.code == KEY_C then
self:addChar("c")
elseif ev.code == KEY_D then
self:addChar("d")
elseif ev.code == KEY_E then
self:addChar("e")
elseif ev.code == KEY_F then
self:addChar("f")
elseif ev.code == KEY_G then
self:addChar("g")
elseif ev.code == KEY_H then
self:addChar("h")
elseif ev.code == KEY_I then
self:addChar("i")
elseif ev.code == KEY_J then
self:addChar("j")
elseif ev.code == KEY_K then
self:addChar("k")
elseif ev.code == KEY_L then
self:addChar("l")
elseif ev.code == KEY_M then
self:addChar("m")
elseif ev.code == KEY_N then
self:addChar("n")
elseif ev.code == KEY_O then
self:addChar("o")
elseif ev.code == KEY_P then
self:addChar("p")
elseif ev.code == KEY_Q then
self:addChar("q")
elseif ev.code == KEY_R then
self:addChar("r")
elseif ev.code == KEY_S then
self:addChar("s")
elseif ev.code == KEY_T then
self:addChar("t")
elseif ev.code == KEY_U then
self:addChar("u")
elseif ev.code == KEY_V then
self:addChar("v")
elseif ev.code == KEY_W then
self:addChar("w")
elseif ev.code == KEY_X then
self:addChar("x")
elseif ev.code == KEY_Y then
self:addChar("y")
elseif ev.code == KEY_Z then
self:addChar("z")
2012-02-19 11:51:08 +00:00
elseif ev.code == KEY_1 then
self:addChar("1")
elseif ev.code == KEY_2 then
self:addChar("2")
elseif ev.code == KEY_3 then
self:addChar("3")
elseif ev.code == KEY_4 then
self:addChar("4")
elseif ev.code == KEY_5 then
self:addChar("5")
elseif ev.code == KEY_6 then
self:addChar("6")
elseif ev.code == KEY_7 then
self:addChar("7")
elseif ev.code == KEY_8 then
self:addChar("8")
elseif ev.code == KEY_9 then
self:addChar("9")
elseif ev.code == KEY_0 then
self:addChar("0")
2012-02-18 12:32:38 +00:00
elseif ev.code == KEY_SPACE then
self:addChar(" ")
2012-02-18 10:21:03 +00:00
elseif ev.code == KEY_PGFWD then
elseif ev.code == KEY_PGBCK then
elseif ev.code == KEY_ENTER or ev.code == KEY_FW_PRESS then
2012-02-18 23:21:50 +00:00
if self.input_string == "" then
return nil
else
return self.input_string
end
2012-02-18 12:32:38 +00:00
elseif ev.code == KEY_DEL then
2012-02-18 10:21:03 +00:00
self:delChar()
2012-02-18 12:32:38 +00:00
elseif ev.code == KEY_BACK then
2012-02-18 23:21:50 +00:00
return nil
2012-02-18 10:21:03 +00:00
end
2012-02-19 11:51:08 +00:00
--local nsecs, nusecs = util.gettime()
--local dur = (nsecs - secs) * 1000000 + nusecs - usecs
--print("E: T="..ev.type.." V="..ev.value.." C="..ev.code.." DUR="..dur)
elseif ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_RELEASE
and ev.code == KEY_SHIFT then
Keys.shiftmode = false
2012-02-19 11:51:08 +00:00
elseif ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_RELEASE
and ev.code == KEY_ALT then
Keys.altmode = false
2012-02-18 10:21:03 +00:00
end
end
end