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

403 lines
9.7 KiB
Lua
Raw Normal View History

require "font"
2012-02-18 10:21:03 +00:00
require "rendertext"
require "keys"
require "graphics"
----------------------------------------------------
-- General inputbox
----------------------------------------------------
2012-02-18 10:21:03 +00:00
InputBox = {
-- Class vars:
2012-03-15 09:03:09 +00:00
h = 100,
2012-03-15 13:43:55 +00:00
input_slot_w = nil,
2012-02-18 10:21:03 +00:00
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-03-15 09:03:09 +00:00
cursor = nil,
2012-02-19 06:41:44 +00:00
-- font for displaying input content
2012-03-15 13:43:55 +00:00
-- we have to use mono here for better distance controlling
face = Font:getFace("infont", 25),
2012-02-19 06:41:44 +00:00
fheight = 25,
2012-03-15 13:43:55 +00:00
fwidth = 15,
2012-04-09 09:17:23 +00:00
commands = nil,
initialized = false,
2012-02-18 10:21:03 +00:00
}
function InputBox:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
2012-02-19 06:41:44 +00:00
end
function InputBox:init()
if not self.initialized then
self:addAllCommands()
self.initialized = true
2012-02-18 10:21:03 +00:00
end
end
2012-03-15 13:43:55 +00:00
function InputBox:refreshText()
-- clear previous painted text
fb.bb:paintRect(140, self.input_start_y-19,
self.input_slot_w, self.fheight, self.input_bg)
-- paint new text
renderUtf8Text(fb.bb, self.input_start_x, self.input_start_y,
self.face,
2012-03-15 13:43:55 +00:00
self.input_string, 0)
end
2012-02-19 06:41:44 +00:00
function InputBox:addChar(char)
2012-03-15 13:43:55 +00:00
self.cursor:clear()
-- draw new text
local cur_index = (self.cursor.x_pos + 3 - self.input_start_x)
/ self.fwidth
self.input_string = self.input_string:sub(1, cur_index)..char..
2012-03-15 13:43:55 +00:00
self.input_string:sub(cur_index+1)
self:refreshText()
2012-02-19 06:41:44 +00:00
self.input_cur_x = self.input_cur_x + self.fwidth
2012-03-15 13:43:55 +00:00
-- draw new cursor
self.cursor:moveHorizontal(self.fwidth)
self.cursor:draw()
fb:refresh(1, self.input_start_x-5, self.input_start_y-25,
self.input_slot_w, self.h-25)
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-03-15 13:43:55 +00:00
local cur_index = (self.cursor.x_pos + 3 - self.input_start_x)
/ self.fwidth
if cur_index == 0 then return end
2012-03-15 13:43:55 +00:00
self.cursor:clear()
-- draw new text
self.input_string = self.input_string:sub(1, cur_index-1)..
2012-03-15 13:43:55 +00:00
self.input_string:sub(cur_index+1, -1)
self:refreshText()
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-04-15 14:05:44 +00:00
fb.bb:paintRect(self.input_cur_x, self.input_start_y-19,
2012-02-19 06:41:44 +00:00
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-03-15 13:43:55 +00:00
-- draw new cursor
2012-03-15 09:03:09 +00:00
self.cursor:moveHorizontal(-self.fwidth)
2012-03-15 13:43:55 +00:00
self.cursor:draw()
fb:refresh(1, self.input_start_x-5, self.input_start_y-25,
self.input_slot_w, self.h-25)
end
function InputBox:clearText()
self.cursor:clear()
self.input_string = ""
self:refreshText()
self.cursor.x_pos = self.input_start_x - 3
self.cursor:draw()
fb:refresh(1, self.input_start_x-5, self.input_start_y-25,
self.input_slot_w, self.h-25)
2012-02-18 10:21:03 +00:00
end
function InputBox:drawHelpMsg(ypos, w, h)
return
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,
2012-02-19 06:41:44 +00:00
title, true)
end
2012-03-15 09:03:09 +00:00
----------------------------------------------------------------------
-- InputBox:input()
--
-- @title: input prompt for the box
-- @d_text: default to nil (used to set default text in input slot)
-- @is_hint: if this arg is true, default text will be used as hint
-- message for input
2012-03-15 09:03:09 +00:00
----------------------------------------------------------------------
function InputBox:input(ypos, height, title, d_text, is_hint)
self:init()
2012-02-19 06:41:44 +00:00
-- do some initilization
2012-04-09 09:17:23 +00:00
self.ypos = ypos
2012-03-15 09:03:09 +00:00
self.h = height
2012-02-18 10:21:03 +00:00
self.input_start_y = ypos + 35
self.input_cur_x = self.input_start_x
2012-03-15 13:43:55 +00:00
self.input_slot_w = fb.bb:getWidth() - 170
2012-03-15 09:03:09 +00:00
self.cursor = Cursor:new {
2012-03-15 13:43:55 +00:00
x_pos = self.input_start_x - 3,
2012-03-15 09:03:09 +00:00
y_pos = ypos + 13,
h = 30,
}
2012-02-18 10:21:03 +00:00
2012-03-15 09:03:09 +00:00
-- draw box and content
w = fb.bb:getWidth() - 40
h = height - 45
self:drawHelpMsg(ypos, w, h)
2012-03-15 09:03:09 +00:00
self:drawBox(ypos, w, h, title)
2012-03-15 13:43:55 +00:00
if d_text then
if is_hint then
-- print hint text
fb.bb:paintRect(140, self.input_start_y-19,
self.input_slot_w, self.fheight, self.input_bg)
renderUtf8Text(fb.bb, self.input_start_x+5, self.input_start_y,
self.face,
d_text, 0)
fb.bb:dimRect(140, self.input_start_y-19,
self.input_slot_w, self.fheight, self.input_bg)
else
self.input_string = d_text
self.input_cur_x = self.input_cur_x + (self.fwidth * d_text:len())
self.cursor.x_pos = self.cursor.x_pos + (self.fwidth * d_text:len())
self:refreshText()
end
2012-02-19 06:41:44 +00:00
end
2012-03-15 14:15:09 +00:00
self.cursor:draw()
2012-03-15 09:03:09 +00:00
fb:refresh(1, 20, ypos, w, h)
2012-02-19 06:41:44 +00:00
2012-02-18 10:21:03 +00:00
while true do
local ev = input.saveWaitForEvent()
ev.code = adjustKeyEvents(ev)
if ev.type == EV_KEY and ev.value ~= EVENT_VALUE_KEY_RELEASE then
2012-04-09 09:17:23 +00:00
keydef = Keydef:new(ev.code, getKeyModifier())
2012-04-18 16:16:49 +00:00
debug("key pressed: "..tostring(keydef))
2012-04-09 09:17:23 +00:00
command = self.commands:getByKeydef(keydef)
if command ~= nil then
2012-04-18 16:16:49 +00:00
debug("command to execute: "..tostring(command))
2012-04-09 09:17:23 +00:00
ret_code = command.func(self, keydef)
else
2012-04-18 16:16:49 +00:00
debug("command not found: "..tostring(command))
2012-02-18 10:21:03 +00:00
end
2012-02-19 11:51:08 +00:00
2012-04-09 09:17:23 +00:00
if ret_code == "break" then
ret_code = nil
2012-04-09 09:17:23 +00:00
break
end
2012-03-11 07:46:25 +00:00
end -- if
end -- while
local return_str = self.input_string
self.input_string = ""
return return_str
2012-02-18 10:21:03 +00:00
end
2012-04-09 09:17:23 +00:00
function InputBox:addAllCommands()
if self.commands then
-- we only initialize once
return
end
self.commands = Commands:new{}
INPUT_KEYS = {
{KEY_Q, "q"}, {KEY_W, "w"}, {KEY_E, "e"}, {KEY_R, "r"}, {KEY_T, "t"},
{KEY_Y, "y"}, {KEY_U, "u"}, {KEY_I, "i"}, {KEY_O, "o"}, {KEY_P, "p"},
{KEY_A, "a"}, {KEY_S, "s"}, {KEY_D, "d"}, {KEY_F, "f"}, {KEY_G, "g"},
{KEY_H, "h"}, {KEY_J, "j"}, {KEY_K, "k"}, {KEY_L, "l"},
{KEY_Z, "z"}, {KEY_X, "x"}, {KEY_C, "c"}, {KEY_V, "v"}, {KEY_B, "b"},
{KEY_N, "n"}, {KEY_M, "m"},
{KEY_1, "1"}, {KEY_2, "2"}, {KEY_3, "3"}, {KEY_4, "4"}, {KEY_5, "5"},
{KEY_6, "6"}, {KEY_7, "7"}, {KEY_8, "8"}, {KEY_9, "9"}, {KEY_0, "0"},
2012-04-13 18:52:30 +00:00
{KEY_SPACE, " "},
-- DXG keys
{KEY_DOT, "."}, {KEY_SLASH, "/"},
2012-04-09 09:17:23 +00:00
}
for k,v in ipairs(INPUT_KEYS) do
self.commands:add(v[1], nil, "",
"input "..v[2],
function(self)
self:addChar(v[2])
end
)
end
self.commands:add(KEY_FW_LEFT, nil, "",
"move cursor left",
function(self)
if (self.cursor.x_pos + 3) > self.input_start_x then
self.cursor:moveHorizontalAndDraw(-self.fwidth)
fb:refresh(1, self.input_start_x-5, self.ypos,
self.input_slot_w, self.h)
end
end
)
self.commands:add(KEY_FW_RIGHT, nil, "",
"move cursor right",
function(self)
if (self.cursor.x_pos + 3) < self.input_cur_x then
self.cursor:moveHorizontalAndDraw(self.fwidth)
fb:refresh(1, self.input_start_x-5, self.ypos,
2012-04-09 09:17:23 +00:00
self.input_slot_w, self.h)
end
end
)
self.commands:add({KEY_ENTER, KEY_FW_PRESS}, nil, "",
"submit input content",
function(self)
if self.input_string == "" then
self.input_string = nil
end
return "break"
end
)
self.commands:add(KEY_DEL, nil, "",
"delete one character",
function(self)
self:delChar()
end
)
self.commands:add(KEY_DEL, MOD_SHIFT, "",
"empty inputbox",
function(self)
self:clearText()
end
)
self.commands:add({KEY_BACK, KEY_HOME}, nil, "",
"cancel inputbox",
function(self)
self.input_string = nil
return "break"
end
)
end
----------------------------------------------------
-- Inputbox for numbers only
-- Designed by eLiNK
----------------------------------------------------
NumInputBox = InputBox:new{
initialized = false,
commands = Commands:new{},
}
function NumInputBox:addAllCommands()
self.commands = Commands:new{}
INPUT_NUM_KEYS = {
{KEY_Q, "1"}, {KEY_W, "2"}, {KEY_E, "3"}, {KEY_R, "4"}, {KEY_T, "5"},
{KEY_Y, "6"}, {KEY_U, "7"}, {KEY_I, "8"}, {KEY_O, "9"}, {KEY_P, "0"},
{KEY_1, "1"}, {KEY_2, "2"}, {KEY_3, "3"}, {KEY_4, "4"}, {KEY_5, "5"},
{KEY_6, "6"}, {KEY_7, "7"}, {KEY_8, "8"}, {KEY_9, "9"}, {KEY_0, "0"},
}
for k,v in ipairs(INPUT_NUM_KEYS) do
self.commands:add(v[1], nil, "",
"input "..v[2],
function(self)
self:addChar(v[2])
end
)
end -- for
self.commands:add(KEY_FW_LEFT, nil, "",
"move cursor left",
function(self)
if (self.cursor.x_pos + 3) > self.input_start_x then
self.cursor:moveHorizontalAndDraw(-self.fwidth)
fb:refresh(1, self.input_start_x-5, self.ypos,
self.input_slot_w, self.h)
end
end
)
self.commands:add(KEY_FW_RIGHT, nil, "",
"move cursor right",
function(self)
if (self.cursor.x_pos + 3) < self.input_cur_x then
self.cursor:moveHorizontalAndDraw(self.fwidth)
fb:refresh(1, self.input_start_x-5, self.ypos,
self.input_slot_w, self.h)
end
end
)
self.commands:add({KEY_ENTER, KEY_FW_PRESS}, nil, "",
"submit input content",
function(self)
if self.input_string == "" then
self.input_string = nil
end
return "break"
end
)
self.commands:add(KEY_DEL, nil, "",
"delete one character",
function(self)
self:delChar()
end
)
self.commands:add(KEY_DEL, MOD_SHIFT, "",
"empty inputbox",
function(self)
self:clearText()
end
)
self.commands:add({KEY_BACK, KEY_HOME}, nil, "",
"cancel inputbox",
function(self)
self.input_string = nil
return "break"
end
)
end
function NumInputBox:drawHelpMsg(ypos, w, h)
local w = 415
local y = ypos - 60
local x = (G_width - w) / 2
local h = 50
local bw = 2
local face = Font:getFace("scfont", 22)
fb.bb:paintRect(x, y, w, h, 15)
fb.bb:paintRect(x+bw, y+bw, w-2*bw, h-2*bw, 0)
local font_y = y + 22
local font_x = x + 22
INPUT_NUM_KEYS = {
{"Q", "1"}, {"W", "2"}, {"E", "3"}, {"R", "4"}, {"T", "5"},
{"Y", "6"}, {"U", "7"}, {"I", "8"}, {"O", "9"}, {"P", "0"},
}
for k,v in ipairs(INPUT_NUM_KEYS) do
renderUtf8Text(fb.bb, font_x, font_y, face,
v[1], true)
renderUtf8Text(fb.bb, font_x, font_y + 22, face,
v[2], true)
font_x = font_x + 40
end
fb:refresh(1, x, y, w, h)
2012-02-18 10:21:03 +00:00
end