2016-05-02 02:39:31 +00:00
|
|
|
--[[--
|
|
|
|
Widget for taking user input.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
local UIManager = require("ui/uimanager")
|
2017-04-04 07:57:14 +00:00
|
|
|
local _ = require("gettext")
|
2016-05-02 02:39:31 +00:00
|
|
|
local sample_input
|
|
|
|
sample_input = InputDialog:new{
|
|
|
|
title = _("Dialog title"),
|
|
|
|
input = "default value",
|
|
|
|
input_hint = "hint text",
|
2016-09-22 06:52:40 +00:00
|
|
|
input_type = "string",
|
2017-03-27 04:42:58 +00:00
|
|
|
description = "Some more description",
|
2016-06-26 00:53:08 +00:00
|
|
|
-- text_type = "password",
|
2016-05-02 02:39:31 +00:00
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(sample_input)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Save"),
|
2016-05-26 06:09:49 +00:00
|
|
|
-- button with is_enter_default set to true will be
|
|
|
|
-- triggered after user press the enter key from keyboard
|
|
|
|
is_enter_default = true,
|
|
|
|
callback = function()
|
2016-09-22 06:52:40 +00:00
|
|
|
print('Got user input as raw text:', sample_input:getInputText())
|
|
|
|
print('Got user input as value:', sample_input:getInputValue())
|
2016-05-26 06:09:49 +00:00
|
|
|
end,
|
2016-05-02 02:39:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sample_input:onShowKeyboard()
|
|
|
|
UIManager:show(sample_input)
|
|
|
|
|
2017-04-04 13:31:13 +00:00
|
|
|
If it would take the user more than half a minute to recover from a mistake,
|
|
|
|
a "Cancel" button <em>must</em> be added to the dialog. The cancellation button
|
|
|
|
should be kept on the left and the button executing the action on the right.
|
|
|
|
|
|
|
|
It is strongly recommended to use a text describing the action to be
|
|
|
|
executed, as demonstrated in the example above. If the resulting phrase would be
|
|
|
|
longer than three words it should just read "OK".
|
|
|
|
|
2016-05-02 02:39:31 +00:00
|
|
|
]]
|
|
|
|
|
2017-03-27 04:42:58 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local ButtonTable = require("ui/widget/buttontable")
|
2017-03-27 04:42:58 +00:00
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
|
|
|
local Font = require("ui/font")
|
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local InputText = require("ui/widget/inputtext")
|
2017-03-27 04:42:58 +00:00
|
|
|
local LineWidget = require("ui/widget/linewidget")
|
2014-09-05 13:06:58 +00:00
|
|
|
local RenderText = require("ui/rendertext")
|
2017-03-27 04:42:58 +00:00
|
|
|
local TextBoxWidget = require("ui/widget/textboxwidget")
|
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
2017-03-28 22:47:18 +00:00
|
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Screen = require("device").screen
|
2013-07-30 15:07:33 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local InputDialog = InputContainer:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
title = "",
|
|
|
|
input = "",
|
|
|
|
input_hint = "",
|
2017-03-27 04:42:58 +00:00
|
|
|
description = nil,
|
2014-03-13 13:52:43 +00:00
|
|
|
buttons = nil,
|
|
|
|
input_type = nil,
|
|
|
|
enter_callback = nil,
|
2014-04-23 14:19:29 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
width = nil,
|
2014-04-23 14:19:29 +00:00
|
|
|
|
2016-02-02 17:38:14 +00:00
|
|
|
text_width = nil,
|
|
|
|
text_height = nil,
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
title_face = Font:getFace("tfont", 22),
|
2017-03-27 04:42:58 +00:00
|
|
|
description_face = Font:getFace("cfont", 20),
|
2014-03-13 13:52:43 +00:00
|
|
|
input_face = Font:getFace("cfont", 20),
|
2014-04-23 14:19:29 +00:00
|
|
|
|
2014-11-20 22:07:39 +00:00
|
|
|
title_padding = Screen:scaleBySize(5),
|
|
|
|
title_margin = Screen:scaleBySize(2),
|
|
|
|
input_padding = Screen:scaleBySize(10),
|
|
|
|
input_margin = Screen:scaleBySize(10),
|
|
|
|
button_padding = Screen:scaleBySize(14),
|
2013-07-30 15:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function InputDialog:init()
|
2016-05-02 02:39:31 +00:00
|
|
|
self.width = self.width or Screen:getWidth() * 0.8
|
2014-09-05 13:06:58 +00:00
|
|
|
local title_width = RenderText:sizeUtf8Text(0, self.width,
|
|
|
|
self.title_face, self.title, true).x
|
|
|
|
if title_width > self.width then
|
|
|
|
local indicator = " >> "
|
|
|
|
local indicator_w = RenderText:sizeUtf8Text(0, self.width,
|
|
|
|
self.title_face, indicator, true).x
|
|
|
|
self.title = RenderText:getSubTextByWidth(self.title, self.title_face,
|
|
|
|
self.width - indicator_w, true) .. indicator
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
self.title = FrameContainer:new{
|
|
|
|
padding = self.title_padding,
|
|
|
|
margin = self.title_margin,
|
|
|
|
bordersize = 0,
|
|
|
|
TextWidget:new{
|
|
|
|
text = self.title,
|
|
|
|
face = self.title_face,
|
|
|
|
width = self.width,
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 06:09:49 +00:00
|
|
|
|
2017-03-27 04:42:58 +00:00
|
|
|
if self.description then
|
|
|
|
self.description = FrameContainer:new{
|
|
|
|
padding = self.title_padding,
|
|
|
|
margin = self.title_margin,
|
|
|
|
bordersize = 0,
|
|
|
|
TextBoxWidget:new{
|
|
|
|
text = self.description,
|
|
|
|
face = self.description_face,
|
|
|
|
width = self.width,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-03-28 22:47:18 +00:00
|
|
|
self.description = WidgetContainer:new()
|
2017-03-27 04:42:58 +00:00
|
|
|
end
|
|
|
|
|
2016-02-17 17:41:17 +00:00
|
|
|
self._input_widget = InputText:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
text = self.input,
|
|
|
|
hint = self.input_hint,
|
|
|
|
face = self.input_face,
|
2016-02-02 17:38:14 +00:00
|
|
|
width = self.text_width or self.width * 0.9,
|
|
|
|
height = self.text_height or nil,
|
2014-03-13 13:52:43 +00:00
|
|
|
input_type = self.input_type,
|
2015-10-06 13:18:07 +00:00
|
|
|
text_type = self.text_type,
|
2016-05-26 06:09:49 +00:00
|
|
|
enter_callback = self.enter_callback or function()
|
|
|
|
for _,btn_row in ipairs(self.buttons) do
|
|
|
|
for _,btn in ipairs(btn_row) do
|
|
|
|
if btn.is_enter_default then
|
|
|
|
btn.callback()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2014-03-13 13:52:43 +00:00
|
|
|
scroll = false,
|
|
|
|
parent = self,
|
|
|
|
}
|
2017-03-27 04:42:58 +00:00
|
|
|
|
2014-04-23 14:19:29 +00:00
|
|
|
self.button_table = ButtonTable:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
width = self.width,
|
|
|
|
button_font_face = "cfont",
|
|
|
|
button_font_size = 20,
|
|
|
|
buttons = self.buttons,
|
|
|
|
zero_sep = true,
|
2014-05-01 10:37:12 +00:00
|
|
|
show_parent = self,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
2017-03-27 04:42:58 +00:00
|
|
|
|
2014-04-23 14:19:29 +00:00
|
|
|
self.title_bar = LineWidget:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
dimen = Geom:new{
|
2014-04-23 14:19:29 +00:00
|
|
|
w = self.button_table:getSize().w + self.button_padding,
|
2014-11-20 22:07:39 +00:00
|
|
|
h = Screen:scaleBySize(2),
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-23 14:19:29 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
self.dialog_frame = FrameContainer:new{
|
|
|
|
radius = 8,
|
|
|
|
bordersize = 3,
|
|
|
|
padding = 0,
|
|
|
|
margin = 0,
|
2014-10-22 13:34:11 +00:00
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
2014-03-13 13:52:43 +00:00
|
|
|
VerticalGroup:new{
|
|
|
|
align = "left",
|
|
|
|
self.title,
|
2014-04-23 14:19:29 +00:00
|
|
|
self.title_bar,
|
2017-03-27 04:42:58 +00:00
|
|
|
self.description,
|
2014-03-13 13:52:43 +00:00
|
|
|
-- input
|
|
|
|
CenterContainer:new{
|
|
|
|
dimen = Geom:new{
|
2014-04-23 14:19:29 +00:00
|
|
|
w = self.title_bar:getSize().w,
|
2016-02-17 17:41:17 +00:00
|
|
|
h = self._input_widget:getSize().h,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2016-02-17 17:41:17 +00:00
|
|
|
self._input_widget,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
|
|
|
-- buttons
|
|
|
|
CenterContainer:new{
|
|
|
|
dimen = Geom:new{
|
2014-04-23 14:19:29 +00:00
|
|
|
w = self.title_bar:getSize().w,
|
|
|
|
h = self.button_table:getSize().h,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2014-04-23 14:19:29 +00:00
|
|
|
self.button_table,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 14:19:29 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
self[1] = CenterContainer:new{
|
|
|
|
dimen = Geom:new{
|
|
|
|
w = Screen:getWidth(),
|
2016-02-17 17:41:17 +00:00
|
|
|
h = Screen:getHeight() - self._input_widget:getKeyboardDimen().h,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
|
|
|
self.dialog_frame,
|
|
|
|
}
|
2014-12-01 14:39:41 +00:00
|
|
|
end
|
|
|
|
|
2016-02-17 17:41:17 +00:00
|
|
|
function InputDialog:getInputText()
|
|
|
|
return self._input_widget:getText()
|
|
|
|
end
|
|
|
|
|
2016-09-22 06:52:40 +00:00
|
|
|
function InputDialog:getInputValue()
|
|
|
|
local text = self:getInputText()
|
|
|
|
if self.input_type == "number" then
|
|
|
|
return tonumber(text)
|
|
|
|
else
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-17 17:41:17 +00:00
|
|
|
function InputDialog:setInputText(text)
|
|
|
|
self._input_widget:setText(text)
|
|
|
|
end
|
|
|
|
|
2014-12-01 14:39:41 +00:00
|
|
|
function InputDialog:onShow()
|
|
|
|
UIManager:setDirty(self, function()
|
2015-04-26 18:07:17 +00:00
|
|
|
return "ui", self.dialog_frame.dimen
|
2014-12-01 14:39:41 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputDialog:onCloseWidget()
|
2016-05-02 02:39:31 +00:00
|
|
|
self:onClose()
|
2014-12-01 14:39:41 +00:00
|
|
|
UIManager:setDirty(nil, function()
|
|
|
|
return "partial", self.dialog_frame.dimen
|
|
|
|
end)
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function InputDialog:onShowKeyboard()
|
2016-02-17 17:41:17 +00:00
|
|
|
self._input_widget:onShowKeyboard()
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function InputDialog:onClose()
|
2016-02-17 17:41:17 +00:00
|
|
|
self._input_widget:onCloseKeyboard()
|
2013-07-30 15:07:33 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return InputDialog
|