uiinputbox.lua

main 115 lines · 3.6 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1if not UIWindow then dofile 'uiwindow' end
2
3-- @docclass
4UIInputBox = extends(UIWindow, "UIInputBox")
5
6function UIInputBox.create(title, okCallback, cancelCallback)
7 local inputBox = UIInputBox.internalCreate()
8
9 inputBox:setText(title)
10 inputBox.inputs = {}
11 inputBox.onEnter = function()
12 local results = {}
13 for _,func in pairs(inputBox.inputs) do
14 table.insert(results, func())
15 end
16 okCallback(unpack(results))
17 inputBox:destroy()
18 end
19 inputBox.onEscape = function()
20 if cancelCallback then
21 cancelCallback()
22 end
23 inputBox:destroy()
24 end
25
26 return inputBox
27end
28
29function UIInputBox:addLabel(text)
30 local label = g_ui.createWidget('InputBoxLabel', self)
31 label:setText(text)
32 return label
33end
34
35function UIInputBox:addLineEdit(labelText, defaultText, maxLength)
36 if labelText then self:addLabel(labelText) end
37 local lineEdit = g_ui.createWidget('InputBoxLineEdit', self)
38 if defaultText then lineEdit:setText(defaultText) end
39 if maxLength then lineEdit:setMaxLength(maxLength) end
40 table.insert(self.inputs, function() return lineEdit:getText() end)
41 return lineEdit
42end
43
44function UIInputBox:addTextEdit(labelText, defaultText, maxLength, visibleLines)
45 if labelText then self:addLabel(labelText) end
46 local textEdit = g_ui.createWidget('InputBoxTextEdit', self)
47 if defaultText then textEdit:setText(defaultText) end
48 if maxLength then textEdit:setMaxLength(maxLength) end
49 visibleLines = visibleLines or 1
50 textEdit:setHeight(textEdit:getHeight() * visibleLines)
51 table.insert(self.inputs, function() return textEdit:getText() end)
52 return textEdit
53end
54
55function UIInputBox:addCheckBox(text, checked)
56 local checkBox = g_ui.createWidget('InputBoxCheckBox', self)
57 checkBox:setText(text)
58 checkBox:setChecked(checked)
59 table.insert(self.inputs, function() return checkBox:isChecked() end)
60 return checkBox
61end
62
63function UIInputBox:addComboBox(labelText, ...)
64 if labelText then self:addLabel(labelText) end
65 local comboBox = g_ui.createWidget('InputBoxComboBox', self)
66 local options = {...}
67 for i=1,#options do
68 comboBox:addOption(options[i])
69 end
70 table.insert(self.inputs, function() return comboBox:getCurrentOption() end)
71 return comboBox
72end
73
74function UIInputBox:addSpinBox(labelText, minimum, maximum, value, step)
75 if labelText then self:addLabel(labelText) end
76 local spinBox = g_ui.createWidget('InputBoxSpinBox', self)
77 spinBox:setMinimum(minimum)
78 spinBox:setMaximum(maximum)
79 spinBox:setValue(value)
80 spinBox:setStep(step)
81 table.insert(self.inputs, function() return spinBox:getValue() end)
82 return spinBox
83end
84
85function UIInputBox:display(okButtonText, cancelButtonText)
86 okButtonText = okButtonText or tr('Ok')
87 cancelButtonText = cancelButtonText or tr('Cancel')
88
89 local buttonsWidget = g_ui.createWidget('InputBoxButtonsPanel', self)
90 local okButton = g_ui.createWidget('InputBoxButton', buttonsWidget)
91 okButton:setText(okButtonText)
92 okButton.onClick = self.onEnter
93
94 local cancelButton = g_ui.createWidget('InputBoxButton', buttonsWidget)
95 cancelButton:setText(cancelButtonText)
96 cancelButton.onClick = self.onEscape
97
98 buttonsWidget:setHeight(okButton:getHeight())
99
100 rootWidget:addChild(self)
101 self:setStyle('InputBoxWindow')
102end
103
104function displayTextInputBox(title, label, okCallback, cancelCallback)
105 local inputBox = UIInputBox.create(title, okCallback, cancelCallback)
106 inputBox:addLineEdit(label)
107 inputBox:display()
108end
109
110function displayNumberInputBox(title, label, okCallback, cancelCallback, min, max, value, step)
111 local inputBox = UIInputBox.create(title, okCallback, cancelCallback)
112 inputBox:addSpinBox(label, min, max, value, step)
113 inputBox:display()
114end
115
Top