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")
|
|
|
|
local Font = require("ui/font")
|
|
|
|
local Screen = require("ui/screen")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2013-07-30 15:07:33 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local InputText = InputContainer:new{
|
2013-07-30 15:07:33 +00:00
|
|
|
text = "",
|
|
|
|
hint = "demo hint",
|
|
|
|
charlist = {}, -- table to store input string
|
|
|
|
charpos = 1,
|
|
|
|
input_type = nil,
|
|
|
|
|
|
|
|
width = nil,
|
|
|
|
height = nil,
|
|
|
|
face = Font:getFace("cfont", 22),
|
|
|
|
|
|
|
|
padding = 5,
|
|
|
|
margin = 5,
|
|
|
|
bordersize = 2,
|
|
|
|
|
|
|
|
parent = nil, -- parent dialog that will be set dirty
|
|
|
|
scroll = false,
|
|
|
|
}
|
|
|
|
|
|
|
|
function InputText:init()
|
|
|
|
self:StringToCharlist(self.text)
|
|
|
|
self:initTextBox()
|
|
|
|
self:initKeyboard()
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:initTextBox()
|
|
|
|
local bgcolor = nil
|
|
|
|
local fgcolor = nil
|
|
|
|
if self.text == "" then
|
|
|
|
self.text = self.hint
|
|
|
|
bgcolor = 0.0
|
|
|
|
fgcolor = 0.5
|
|
|
|
else
|
|
|
|
bgcolor = 0.0
|
|
|
|
fgcolor = 1.0
|
|
|
|
end
|
|
|
|
local text_widget = nil
|
|
|
|
if self.scroll then
|
|
|
|
text_widget = ScrollTextWidget:new{
|
|
|
|
text = self.text,
|
|
|
|
face = self.face,
|
|
|
|
bgcolor = bgcolor,
|
|
|
|
fgcolor = fgcolor,
|
|
|
|
width = self.width,
|
|
|
|
height = self.height,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
text_widget = TextBoxWidget:new{
|
|
|
|
text = self.text,
|
|
|
|
face = self.face,
|
|
|
|
bgcolor = bgcolor,
|
|
|
|
fgcolor = fgcolor,
|
|
|
|
width = self.width,
|
|
|
|
height = self.height,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
self[1] = FrameContainer:new{
|
|
|
|
bordersize = self.bordersize,
|
|
|
|
padding = self.padding,
|
|
|
|
margin = self.margin,
|
|
|
|
text_widget,
|
|
|
|
}
|
|
|
|
self.dimen = self[1]:getSize()
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:initKeyboard()
|
|
|
|
local keyboard_layout = 2
|
|
|
|
if self.input_type == "number" then
|
|
|
|
keyboard_layout = 3
|
|
|
|
end
|
|
|
|
self.keyboard = VirtualKeyboard:new{
|
|
|
|
layout = keyboard_layout,
|
|
|
|
inputbox = self,
|
2013-07-30 15:38:57 +00:00
|
|
|
width = Screen:getWidth(),
|
2013-07-31 05:51:01 +00:00
|
|
|
height = math.max(Screen:getWidth(), Screen:getHeight())*0.33,
|
2013-07-30 15:07:33 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:onShowKeyboard()
|
|
|
|
UIManager:show(self.keyboard)
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:onCloseKeyboard()
|
|
|
|
UIManager:close(self.keyboard)
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:getKeyboardDimen()
|
|
|
|
return self.keyboard.dimen
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:addChar(char)
|
2013-10-17 15:56:03 +00:00
|
|
|
if self.enter_callback and char == '\n' then
|
2013-10-28 12:01:44 +00:00
|
|
|
UIManager:scheduleIn(0.3, function() self.enter_callback() end)
|
2013-10-17 15:56:03 +00:00
|
|
|
return
|
|
|
|
end
|
2013-07-30 15:07:33 +00:00
|
|
|
table.insert(self.charlist, self.charpos, char)
|
|
|
|
self.charpos = self.charpos + 1
|
|
|
|
self.text = self:CharlistToString()
|
|
|
|
self:initTextBox()
|
|
|
|
UIManager:setDirty(self.parent, "partial")
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:delChar()
|
2013-07-31 11:33:36 +00:00
|
|
|
if self.charpos == 1 then return end
|
2013-07-30 15:07:33 +00:00
|
|
|
self.charpos = self.charpos - 1
|
|
|
|
table.remove(self.charlist, self.charpos)
|
|
|
|
self.text = self:CharlistToString()
|
|
|
|
self:initTextBox()
|
|
|
|
UIManager:setDirty(self.parent, "partial")
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:getText()
|
|
|
|
return self.text
|
|
|
|
end
|
|
|
|
|
2013-08-21 11:54:46 +00:00
|
|
|
function InputText:setText(text)
|
|
|
|
self:StringToCharlist(text)
|
|
|
|
self:initTextBox()
|
|
|
|
UIManager:setDirty(self.parent, "partial")
|
|
|
|
end
|
|
|
|
|
2013-07-30 15:07:33 +00:00
|
|
|
function InputText:StringToCharlist(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.text = self:CharlistToString()
|
|
|
|
self.charpos = #self.charlist+1
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputText:CharlistToString()
|
|
|
|
local s, i = ""
|
|
|
|
for i=1, #self.charlist do
|
|
|
|
s = s .. self.charlist[i]
|
|
|
|
end
|
|
|
|
return s
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return InputText
|