parent
a3fcf2e8e7
commit
2e75380218
@ -0,0 +1,114 @@
|
|||||||
|
if not UIWindow then dofile 'uiwindow' end
|
||||||
|
|
||||||
|
-- @docclass
|
||||||
|
UIInputBox = extends(UIWindow)
|
||||||
|
|
||||||
|
function UIInputBox.create(title, okCallback, cancelCallback)
|
||||||
|
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
|
||||||
|
okCallback(unpack(results))
|
||||||
|
inputBox:destroy()
|
||||||
|
end
|
||||||
|
inputBox.onEscape = function()
|
||||||
|
if cancelCallback then
|
||||||
|
cancelCallback()
|
||||||
|
end
|
||||||
|
inputBox:destroy()
|
||||||
|
end
|
||||||
|
|
||||||
|
return inputBox
|
||||||
|
end
|
||||||
|
|
||||||
|
function UIInputBox:addLabel(text)
|
||||||
|
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
|
||||||
|
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)
|
||||||
|
return lineEdit
|
||||||
|
end
|
||||||
|
|
||||||
|
function UIInputBox:addTextEdit(labelText, defaultText, maxLength, visibleLines)
|
||||||
|
if labelText then self:addLabel(labelText) end
|
||||||
|
local textEdit = g_ui.createWidget('InputBoxTextEdit', self)
|
||||||
|
if defaultText then textEdit:setText(defaultText) end
|
||||||
|
if maxLength then textEdit:setMaxLength(maxLength) end
|
||||||
|
visibleLines = visibleLines or 1
|
||||||
|
textEdit:setHeight(textEdit:getHeight() * visibleLines)
|
||||||
|
table.insert(self.inputs, function() return textEdit:getText() end)
|
||||||
|
return textEdit
|
||||||
|
end
|
||||||
|
|
||||||
|
function UIInputBox:addCheckBox(text, checked)
|
||||||
|
local checkBox = g_ui.createWidget('InputBoxCheckBox', self)
|
||||||
|
checkBox:setText(text)
|
||||||
|
checkBox:setChecked(checked)
|
||||||
|
table.insert(self.inputs, function() return checkBox:isChecked() end)
|
||||||
|
return checkBox
|
||||||
|
end
|
||||||
|
|
||||||
|
function UIInputBox:addComboBox(labelText, ...)
|
||||||
|
if labelText then self:addLabel(labelText) end
|
||||||
|
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)
|
||||||
|
return comboBox
|
||||||
|
end
|
||||||
|
|
||||||
|
function UIInputBox:addSpinBox(labelText, minimum, maximum, value, step)
|
||||||
|
if labelText then self:addLabel(labelText) end
|
||||||
|
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)
|
||||||
|
return spinBox
|
||||||
|
end
|
||||||
|
|
||||||
|
function UIInputBox:display(okButtonText, cancelButtonText)
|
||||||
|
okButtonText = okButtonText or tr('Ok')
|
||||||
|
cancelButtonText = cancelButtonText or tr('Cancel')
|
||||||
|
|
||||||
|
local buttonsWidget = g_ui.createWidget('InputBoxButtonsPanel', self)
|
||||||
|
local okButton = g_ui.createWidget('InputBoxButton', buttonsWidget)
|
||||||
|
okButton:setText(okButtonText)
|
||||||
|
okButton.onClick = self.onEnter
|
||||||
|
|
||||||
|
local cancelButton = g_ui.createWidget('InputBoxButton', buttonsWidget)
|
||||||
|
cancelButton:setText(cancelButtonText)
|
||||||
|
cancelButton.onClick = self.onEscape
|
||||||
|
|
||||||
|
buttonsWidget:setHeight(okButton:getHeight())
|
||||||
|
|
||||||
|
rootWidget:addChild(self)
|
||||||
|
self:setStyle('InputBoxWindow')
|
||||||
|
end
|
||||||
|
|
||||||
|
function displayTextInputBox(title, label, okCallback, cancelCallback)
|
||||||
|
local inputBox = UIInputBox.create(title, okCallback, cancelCallback)
|
||||||
|
inputBox:addLineEdit(label)
|
||||||
|
inputBox:display()
|
||||||
|
end
|
||||||
|
|
||||||
|
function displayNumberInputBox(title, label, okCallback, cancelCallback, min, max, value, step)
|
||||||
|
local inputBox = UIInputBox.create(title, okCallback, cancelCallback)
|
||||||
|
inputBox:addSpinBox(label, min, max, value, step)
|
||||||
|
inputBox:display()
|
||||||
|
end
|
@ -0,0 +1,77 @@
|
|||||||
|
function UITextEdit:onStyleApply(styleName, styleNode)
|
||||||
|
for name,value in pairs(styleNode) do
|
||||||
|
if name == 'vertical-scrollbar' then
|
||||||
|
addEvent(function()
|
||||||
|
self:setVerticalScrollBar(self:getParent():getChildById(value))
|
||||||
|
end)
|
||||||
|
elseif name == 'horizontal-scrollbar' then
|
||||||
|
addEvent(function()
|
||||||
|
self:setHorizontalScrollBar(self:getParent():getChildById(value))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function UITextEdit:onMouseWheel(mousePos, mouseWheel)
|
||||||
|
if self.verticalScrollBar and self:isMultiline() then
|
||||||
|
if mouseWheel == MouseWheelUp then
|
||||||
|
self.verticalScrollBar:decrement()
|
||||||
|
else
|
||||||
|
self.verticalScrollBar:increment()
|
||||||
|
end
|
||||||
|
elseif self.horizontalScrollBar then
|
||||||
|
if mouseWheel == MouseWheelUp then
|
||||||
|
self.horizontalScrollBar:increment()
|
||||||
|
else
|
||||||
|
self.horizontalScrollBar:decrement()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function UITextEdit:onTextAreaUpdate(virtualOffset, virtualSize, totalSize)
|
||||||
|
self:updateScrollBars()
|
||||||
|
end
|
||||||
|
|
||||||
|
function UITextEdit:setVerticalScrollBar(scrollbar)
|
||||||
|
self.verticalScrollBar = scrollbar
|
||||||
|
self.verticalScrollBar.onValueChange = function(scrollbar, value)
|
||||||
|
local virtualOffset = self:getTextVirtualOffset()
|
||||||
|
virtualOffset.y = value
|
||||||
|
self:setTextVirtualOffset(virtualOffset)
|
||||||
|
end
|
||||||
|
self:updateScrollBars()
|
||||||
|
end
|
||||||
|
|
||||||
|
function UITextEdit:setHorizontalScrollBar(scrollbar)
|
||||||
|
self.horizontalScrollBar = scrollbar
|
||||||
|
self.horizontalScrollBar.onValueChange = function(scrollbar, value)
|
||||||
|
local virtualOffset = self:getTextVirtualOffset()
|
||||||
|
virtualOffset.x = value
|
||||||
|
self:setTextVirtualOffset(virtualOffset)
|
||||||
|
end
|
||||||
|
self:updateScrollBars()
|
||||||
|
end
|
||||||
|
|
||||||
|
function UITextEdit:updateScrollBars()
|
||||||
|
local scrollSize = self:getTextTotalSize()
|
||||||
|
local scrollWidth = math.max(scrollSize.width - self:getTextVirtualSize().width, 0)
|
||||||
|
local scrollHeight = math.max(scrollSize.height - self:getTextVirtualSize().height, 0)
|
||||||
|
|
||||||
|
local scrollbar = self.verticalScrollBar
|
||||||
|
if scrollbar then
|
||||||
|
scrollbar:setMinimum(0)
|
||||||
|
scrollbar:setMaximum(scrollHeight)
|
||||||
|
scrollbar:setValue(self:getTextVirtualOffset().y)
|
||||||
|
end
|
||||||
|
|
||||||
|
local scrollbar = self.horizontalScrollBar
|
||||||
|
if scrollbar then
|
||||||
|
scrollbar:setMinimum(0)
|
||||||
|
scrollbar:setMaximum(scrollWidth)
|
||||||
|
scrollbar:setValue(self:getTextVirtualOffset().x)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
-- todo: ontext change, focus to cursor
|
Loading…
Reference in new issue