otclient-redemption/modules/corelib/ui/uiinputbox.lua

143 lines
3.9 KiB
Lua
Raw Permalink Normal View History

if not UIWindow then
dofile 'uiwindow'
end
-- @docclass
2022-04-29 23:14:24 -03:00
UIInputBox = extends(UIWindow, 'UIInputBox')
function UIInputBox.create(title, okCallback, cancelCallback)
2021-07-19 19:24:59 -03:00
local inputBox = UIInputBox.internalCreate()
inputBox:setText(title)
inputBox.inputs = {}
inputBox.onEnter = function()
local results = {}
for _, func in pairs(inputBox.inputs) do
table.insert(results, func())
end
2021-07-19 19:24:59 -03:00
okCallback(unpack(results))
inputBox:destroy()
end
2021-07-19 19:24:59 -03:00
inputBox.onEscape = function()
if cancelCallback then
cancelCallback()
end
2021-07-19 19:24:59 -03:00
inputBox:destroy()
end
2021-07-19 19:24:59 -03:00
return inputBox
end
function UIInputBox:addLabel(text)
2021-07-19 19:24:59 -03:00
local label = g_ui.createWidget('InputBoxLabel', self)
label:setText(text)
return label
end
function UIInputBox:addLineEdit(labelText, defaultText, maxLength)
if labelText then
self:addLabel(labelText)
end
2021-07-19 19:24:59 -03:00
local lineEdit = g_ui.createWidget('InputBoxLineEdit', self)
if defaultText then
lineEdit:setText(defaultText)
end
if maxLength then
lineEdit:setMaxLength(maxLength)
end
table.insert(self.inputs, function()
return lineEdit:getText()
end)
2021-07-19 19:24:59 -03:00
return lineEdit
end
function UIInputBox:addTextEdit(labelText, defaultText, maxLength, visibleLines)
if labelText then
self:addLabel(labelText)
end
2021-07-19 19:24:59 -03:00
local textEdit = g_ui.createWidget('InputBoxTextEdit', self)
if defaultText then
textEdit:setText(defaultText)
end
if maxLength then
textEdit:setMaxLength(maxLength)
end
2021-07-19 19:24:59 -03:00
visibleLines = visibleLines or 1
textEdit:setHeight(textEdit:getHeight() * visibleLines)
table.insert(self.inputs, function()
return textEdit:getText()
end)
2021-07-19 19:24:59 -03:00
return textEdit
end
function UIInputBox:addCheckBox(text, checked)
2021-07-19 19:24:59 -03:00
local checkBox = g_ui.createWidget('InputBoxCheckBox', self)
checkBox:setText(text)
checkBox:setChecked(checked)
table.insert(self.inputs, function()
return checkBox:isChecked()
end)
2021-07-19 19:24:59 -03:00
return checkBox
end
function UIInputBox:addComboBox(labelText, ...)
if labelText then
self:addLabel(labelText)
end
2021-07-19 19:24:59 -03:00
local comboBox = g_ui.createWidget('InputBoxComboBox', self)
local options = {...}
for i = 1, #options do
comboBox:addOption(options[i])
end
table.insert(self.inputs, function()
return comboBox:getCurrentOption()
end)
2021-07-19 19:24:59 -03:00
return comboBox
end
function UIInputBox:addSpinBox(labelText, minimum, maximum, value, step)
if labelText then
self:addLabel(labelText)
end
2021-07-19 19:24:59 -03:00
local spinBox = g_ui.createWidget('InputBoxSpinBox', self)
spinBox:setMinimum(minimum)
spinBox:setMaximum(maximum)
spinBox:setValue(value)
spinBox:setStep(step)
table.insert(self.inputs, function()
return spinBox:getValue()
end)
2021-07-19 19:24:59 -03:00
return spinBox
end
function UIInputBox:display(okButtonText, cancelButtonText)
2021-07-19 19:24:59 -03:00
okButtonText = okButtonText or tr('Ok')
cancelButtonText = cancelButtonText or tr('Cancel')
2021-07-19 19:24:59 -03:00
local buttonsWidget = g_ui.createWidget('InputBoxButtonsPanel', self)
local okButton = g_ui.createWidget('InputBoxButton', buttonsWidget)
okButton:setText(okButtonText)
okButton.onClick = self.onEnter
2021-07-19 19:24:59 -03:00
local cancelButton = g_ui.createWidget('InputBoxButton', buttonsWidget)
cancelButton:setText(cancelButtonText)
cancelButton.onClick = self.onEscape
2021-07-19 19:24:59 -03:00
buttonsWidget:setHeight(okButton:getHeight())
2021-07-19 19:24:59 -03:00
rootWidget:addChild(self)
self:setStyle('InputBoxWindow')
end
function displayTextInputBox(title, label, okCallback, cancelCallback)
2021-07-19 19:24:59 -03:00
local inputBox = UIInputBox.create(title, okCallback, cancelCallback)
inputBox:addLineEdit(label)
inputBox:display()
end
2022-04-29 23:14:24 -03:00
function displayNumberInputBox(title, label, okCallback, cancelCallback, min, max, value, step)
2021-07-19 19:24:59 -03:00
local inputBox = UIInputBox.create(title, okCallback, cancelCallback)
inputBox:addSpinBox(label, min, max, value, step)
inputBox:display()
end