2013-10-18 20:38:07 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local ScrollTextWidget = require("ui/widget/scrolltextwidget")
|
|
|
|
local TextBoxWidget = require("ui/widget/textboxwidget")
|
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
|
|
|
local VirtualKeyboard = require("ui/widget/virtualkeyboard")
|
2014-04-23 14:19:29 +00:00
|
|
|
local GestureRange = require("ui/gesturerange")
|
2013-10-18 20:38:07 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2014-04-23 14:19:29 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
|
|
|
local Screen = require("device").screen
|
2014-04-23 14:19:29 +00:00
|
|
|
local Font = require("ui/font")
|
|
|
|
local DEBUG = require("dbg")
|
2014-06-04 09:22:45 +00:00
|
|
|
local util = require("ffi/util")
|
2014-10-22 13:34:11 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2013-07-30 15:07:33 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local InputText = InputContainer:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
text = "",
|
|
|
|
hint = "demo hint",
|
|
|
|
charlist = {}, -- table to store input string
|
|
|
|
charpos = 1,
|
|
|
|
input_type = nil,
|
2014-04-23 14:19:29 +00:00
|
|
|
text_type = nil,
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
width = nil,
|
|
|
|
height = nil,
|
|
|
|
face = Font:getFace("cfont", 22),
|
2014-04-23 14:19:29 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
padding = 5,
|
|
|
|
margin = 5,
|
|
|
|
bordersize = 2,
|
2014-04-23 14:19:29 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
parent = nil, -- parent dialog that will be set dirty
|
|
|
|
scroll = false,
|
2014-04-23 14:19:29 +00:00
|
|
|
focused = true,
|
2013-07-30 15:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function InputText:init()
|
2014-09-10 07:30:59 +00:00
|
|
|
self:initTextBox(self.text)
|
2014-03-13 13:52:43 +00:00
|
|
|
self:initKeyboard()
|
2014-04-23 14:19:29 +00:00
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
|
|
|
TapTextBox = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = self.dimen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
2014-09-10 07:30:59 +00:00
|
|
|
function InputText:initTextBox(text)
|
|
|
|
self.text = text
|
|
|
|
self:initCharlist(text)
|
2014-10-22 13:34:11 +00:00
|
|
|
local fgcolor = Blitbuffer.gray(self.text == "" and 0.5 or 1.0)
|
2014-04-23 14:19:29 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
local text_widget = nil
|
2014-04-23 14:19:29 +00:00
|
|
|
local show_text = self.text
|
|
|
|
if self.text_type == "password" and show_text ~= "" then
|
|
|
|
show_text = self.text:gsub("(.-).", function() return "*" end)
|
|
|
|
show_text = show_text:gsub("(.)$", function() return self.text:sub(-1) end)
|
|
|
|
elseif show_text == "" then
|
|
|
|
show_text = self.hint
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.scroll then
|
|
|
|
text_widget = ScrollTextWidget:new{
|
2014-04-23 14:19:29 +00:00
|
|
|
text = show_text,
|
2014-03-13 13:52:43 +00:00
|
|
|
face = self.face,
|
|
|
|
fgcolor = fgcolor,
|
|
|
|
width = self.width,
|
|
|
|
height = self.height,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
text_widget = TextBoxWidget:new{
|
2014-04-23 14:19:29 +00:00
|
|
|
text = show_text,
|
2014-03-13 13:52:43 +00:00
|
|
|
face = self.face,
|
|
|
|
fgcolor = fgcolor,
|
|
|
|
width = self.width,
|
|
|
|
height = self.height,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
self[1] = FrameContainer:new{
|
|
|
|
bordersize = self.bordersize,
|
|
|
|
padding = self.padding,
|
|
|
|
margin = self.margin,
|
2014-10-22 13:34:11 +00:00
|
|
|
color = Blitbuffer.gray(self.focused and 1.0 or 0.5),
|
2014-03-13 13:52:43 +00:00
|
|
|
text_widget,
|
|
|
|
}
|
|
|
|
self.dimen = self[1]:getSize()
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
2014-09-10 07:30:59 +00:00
|
|
|
function InputText:initCharlist(text)
|
|
|
|
if text == nil then return end
|
|
|
|
-- clear
|
|
|
|
self.charlist = {}
|
|
|
|
self.charpos = 1
|
|
|
|
local prevcharcode, charcode = 0
|
|
|
|
for uchar in string.gfind(text, "([%z\1-\127\194-\244][\128-\191]*)") do
|
|
|
|
charcode = util.utf8charcode(uchar)
|
|
|
|
if prevcharcode then -- utf8
|
|
|
|
self.charlist[#self.charlist+1] = uchar
|
|
|
|
end
|
|
|
|
prevcharcode = charcode
|
|
|
|
end
|
|
|
|
self.charpos = #self.charlist+1
|
|
|
|
end
|
|
|
|
|
2013-07-30 15:07:33 +00:00
|
|
|
function InputText:initKeyboard()
|
2014-03-13 13:52:43 +00:00
|
|
|
local keyboard_layout = 2
|
|
|
|
if self.input_type == "number" then
|
|
|
|
keyboard_layout = 3
|
|
|
|
end
|
|
|
|
self.keyboard = VirtualKeyboard:new{
|
|
|
|
layout = keyboard_layout,
|
|
|
|
inputbox = self,
|
|
|
|
width = Screen:getWidth(),
|
|
|
|
height = math.max(Screen:getWidth(), Screen:getHeight())*0.33,
|
|
|
|
}
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
2014-04-23 14:19:29 +00:00
|
|
|
function InputText:onTapTextBox()
|
|
|
|
if self.parent.onSwitchFocus then
|
|
|
|
self.parent:onSwitchFocus(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:unfocus()
|
|
|
|
self.focused = false
|
2014-10-22 13:34:11 +00:00
|
|
|
self[1].color = Blitbuffer.gray(0.5)
|
2014-04-23 14:19:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:focus()
|
|
|
|
self.focused = true
|
2014-10-22 13:34:11 +00:00
|
|
|
self[1].color = Blitbuffer.COLOR_BLACK
|
2014-04-23 14:19:29 +00:00
|
|
|
end
|
|
|
|
|
2013-07-30 15:07:33 +00:00
|
|
|
function InputText:onShowKeyboard()
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:show(self.keyboard)
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:onCloseKeyboard()
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:close(self.keyboard)
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:getKeyboardDimen()
|
2014-03-13 13:52:43 +00:00
|
|
|
return self.keyboard.dimen
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:addChar(char)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.enter_callback and char == '\n' then
|
|
|
|
UIManager:scheduleIn(0.3, function() self.enter_callback() end)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
table.insert(self.charlist, self.charpos, char)
|
|
|
|
self.charpos = self.charpos + 1
|
2014-09-10 07:30:59 +00:00
|
|
|
self:initTextBox(table.concat(self.charlist))
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:setDirty(self.parent, "partial")
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:delChar()
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.charpos == 1 then return end
|
|
|
|
self.charpos = self.charpos - 1
|
|
|
|
table.remove(self.charlist, self.charpos)
|
2014-09-10 07:30:59 +00:00
|
|
|
self:initTextBox(table.concat(self.charlist))
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:setDirty(self.parent, "partial")
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
2014-08-20 06:45:38 +00:00
|
|
|
function InputText:clear()
|
2014-09-10 07:30:59 +00:00
|
|
|
self:initTextBox("")
|
2014-08-20 06:45:38 +00:00
|
|
|
UIManager:setDirty(self.parent, "partial")
|
|
|
|
end
|
|
|
|
|
2013-07-30 15:07:33 +00:00
|
|
|
function InputText:getText()
|
2014-03-13 13:52:43 +00:00
|
|
|
return self.text
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
2013-08-21 11:54:46 +00:00
|
|
|
function InputText:setText(text)
|
2014-09-10 07:30:59 +00:00
|
|
|
self:initTextBox(text)
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:setDirty(self.parent, "partial")
|
2013-08-21 11:54:46 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return InputText
|