Fix modal dialog auto sizing, fixes #556

This commit is contained in:
TheSumm 2015-04-20 19:22:50 +02:00
parent 8e5bbcd3a1
commit fe98efdc21
2 changed files with 21 additions and 17 deletions

View File

@ -35,16 +35,12 @@ function onModalDialog(id, title, message, buttons, enterButton, escapeButton, c
local messageLabel = modalDialog:getChildById('messageLabel') local messageLabel = modalDialog:getChildById('messageLabel')
local choiceList = modalDialog:getChildById('choiceList') local choiceList = modalDialog:getChildById('choiceList')
local choiceScrollbar = modalDialog:getChildById('choiceScrollBar') local choiceScrollbar = modalDialog:getChildById('choiceScrollBar')
local buttonList = modalDialog:getChildById('buttonList') local buttonsPanel = modalDialog:getChildById('buttonsPanel')
modalDialog:setText(title) modalDialog:setText(title)
messageLabel:setText(message) messageLabel:setText(message)
local horizontalPadding = modalDialog:getPaddingLeft() + modalDialog:getPaddingRight() local labelHeight
modalDialog:setWidth(math.min(modalDialog.maximumWidth, math.max(messageLabel:getWidth(), modalDialog.minimumWidth)))
messageLabel:setWidth(math.min(modalDialog.maximumWidth, math.max(messageLabel:getWidth(), modalDialog.minimumWidth)) - horizontalPadding)
local labelHeight = nil
for i = 1, #choices do for i = 1, #choices do
local choiceId = choices[i][1] local choiceId = choices[i][1]
local choiceName = choices[i][2] local choiceName = choices[i][2]
@ -59,11 +55,12 @@ function onModalDialog(id, title, message, buttons, enterButton, escapeButton, c
end end
choiceList:focusNextChild() choiceList:focusNextChild()
local buttonsWidth = 0
for i = 1, #buttons do for i = 1, #buttons do
local buttonId = buttons[i][1] local buttonId = buttons[i][1]
local buttonText = buttons[i][2] local buttonText = buttons[i][2]
local button = g_ui.createWidget('ModalButton', buttonList) local button = g_ui.createWidget('ModalButton', buttonsPanel)
button:setText(buttonText) button:setText(buttonText)
button.onClick = function(self) button.onClick = function(self)
local focusedChoice = choiceList:getFocusedChild() local focusedChoice = choiceList:getFocusedChild()
@ -74,6 +71,7 @@ function onModalDialog(id, title, message, buttons, enterButton, escapeButton, c
g_game.answerModalDialog(id, buttonId, choice) g_game.answerModalDialog(id, buttonId, choice)
destroyDialog() destroyDialog()
end end
buttonsWidth = buttonsWidth + button:getWidth() + button:getMarginLeft() + button:getMarginRight()
end end
local additionalHeight = 0 local additionalHeight = 0
@ -84,11 +82,13 @@ function onModalDialog(id, title, message, buttons, enterButton, escapeButton, c
additionalHeight = math.min(modalDialog.maximumChoices, math.max(modalDialog.minimumChoices, #choices)) * labelHeight additionalHeight = math.min(modalDialog.maximumChoices, math.max(modalDialog.minimumChoices, #choices)) * labelHeight
additionalHeight = additionalHeight + choiceList:getPaddingTop() + choiceList:getPaddingBottom() additionalHeight = additionalHeight + choiceList:getPaddingTop() + choiceList:getPaddingBottom()
end end
modalDialog:setHeight(modalDialog:getHeight() + additionalHeight)
addEvent(function() local horizontalPadding = modalDialog:getPaddingLeft() + modalDialog:getPaddingRight()
modalDialog:setHeight(modalDialog:getHeight() + messageLabel:getHeight() - 14) buttonsWidth = buttonsWidth + horizontalPadding
end)
modalDialog:setWidth(math.min(modalDialog.maximumWidth, math.max(buttonsWidth, messageLabel:getWidth(), modalDialog.minimumWidth)))
messageLabel:setWidth(math.min(modalDialog.maximumWidth, math.max(buttonsWidth, messageLabel:getWidth(), modalDialog.minimumWidth)) - horizontalPadding)
modalDialog:setHeight(modalDialog:getHeight() + additionalHeight + messageLabel:getHeight() - 8)
local enterFunc = function() local enterFunc = function()
local focusedChoice = choiceList:getFocusedChild() local focusedChoice = choiceList:getFocusedChild()

View File

@ -5,7 +5,7 @@ ChoiceListLabel < Label
focusable: true focusable: true
$focus: $focus:
background-color: #ffffff22 background-color: #00000055
color: #ffffff color: #ffffff
ChoiceList < TextList ChoiceList < TextList
@ -14,7 +14,6 @@ ChoiceList < TextList
anchors.fill: parent anchors.fill: parent
anchors.top: prev.bottom anchors.top: prev.bottom
anchors.bottom: next.top anchors.bottom: next.top
padding: 1
margin-top: 4 margin-top: 4
margin-bottom: 10 margin-bottom: 10
focusable: false focusable: false
@ -30,14 +29,19 @@ ChoiceScrollBar < VerticalScrollBar
visible: false visible: false
ModalButton < Button ModalButton < Button
width: 60 text-auto-resize: true
margin: 2 margin-top: 2
margin-bottom: 2
margin-left: 2
$pressed:
text-offset: 0 0
ModalDialog < MainWindow ModalDialog < MainWindow
id: modalDialog id: modalDialog
size: 280 97 size: 280 97
&minimumWidth: 200 &minimumWidth: 200
&maximumWidth: 500 &maximumWidth: 600
&minimumChoices: 4 &minimumChoices: 4
&maximumChoices: 10 &maximumChoices: 10
@ -57,7 +61,7 @@ ModalDialog < MainWindow
anchors.bottom: next.top anchors.bottom: next.top
Panel Panel
id: buttonList id: buttonsPanel
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
anchors.bottom: parent.bottom anchors.bottom: parent.bottom