Full modal dialog functionality

This commit is contained in:
Sam
2013-11-18 18:58:15 +01:00
parent 25d3019d1a
commit fc54a6e418
8 changed files with 133 additions and 90 deletions

View File

@@ -1,71 +1,117 @@
modalDialog = nil
function init()
g_ui.importStyle('modaldialog')
connect(g_game, { onModalDialog = onModalDialog,
onGameEnd = destroy })
onGameEnd = destroyDialog })
local dialog = rootWidget:recursiveGetChildById('modalDialog')
if dialog then
modalDialog = dialog
end
end
function terminate()
disconnect(g_game, { onModalDialog = onModalDialog,
onGameEnd = destroy })
destroy()
onGameEnd = destroyDialog })
end
function destroy()
function destroyDialog()
if modalDialog then
modalDialog:destroy()
modalDialog = nil
end
end
function onModalDialog(id, title, message, enterId, enterText, escapeId, escapeText, choices)
if modalDialog then return end
function onModalDialog(id, title, message, buttons, enterButton, escapeButton, choices, priority)
-- priority parameter is unused, not sure what its use is.
if modalDialog then
return
end
modalDialog = g_ui.createWidget('ModalDialog', rootWidget)
local enterButton = modalDialog:getChildById('enterButton')
local escapeButton = modalDialog:getChildById('escapeButton')
local messageLabel = modalDialog:getChildById('messageLabel')
local choiceList = modalDialog:getChildById('choiceList')
local choiceScrollbar = modalDialog:getChildById('choiceScrollBar')
local buttonList = modalDialog:getChildById('buttonList')
modalDialog:setText(title)
messageLabel:setText(message)
enterButton:setText(enterText)
escapeButton:setText(escapeText)
local focusLabel = nil
for k, v in pairs(choices) do
local choiceId = v[1]
local choiceName = v[2]
local horizontalPadding = modalDialog:getPaddingLeft() + modalDialog:getPaddingRight()
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
local choiceId = choices[i][1]
local choiceName = choices[i][2]
local label = g_ui.createWidget('ChoiceListLabel', choiceList)
label.choiceId = choiceId
label:setText(choiceName)
label:setPhantom(false)
if not focusLabel then
focusLabel = label
if not labelHeight then
labelHeight = label:getHeight()
end
end
choiceList:focusChild(focusLabel)
choiceList:focusNextChild()
for i = 1, #buttons do
local buttonId = buttons[i][1]
local buttonText = buttons[i][2]
local button = g_ui.createWidget('ModalButton', buttonList)
button:setText(buttonText)
button.onClick = function(self)
local focusedChoice = choiceList:getFocusedChild()
local choice = 0xFF
if focusedChoice then
choice = focusedChoice.choiceId
end
g_game.answerModalDialog(id, buttonId, choice)
destroyDialog()
end
end
local additionalHeight = 0
if #choices > 0 then
choiceList:setVisible(true)
choiceScrollbar:setVisible(true)
additionalHeight = math.min(modalDialog.maximumChoices, math.max(modalDialog.minimumChoices, #choices)) * labelHeight
additionalHeight = additionalHeight + choiceList:getPaddingTop() + choiceList:getPaddingBottom()
end
modalDialog:setHeight(modalDialog:getHeight() + additionalHeight)
addEvent(function()
modalDialog:setHeight(modalDialog:getHeight() + messageLabel:getHeight() - 14)
end)
local enterFunc = function()
g_game.answerModalDialog(id, enterId, choiceList:getFocusedChild().choiceId)
destroy()
local focusedChoice = choiceList:getFocusedChild()
local choice = 0xFF
if focusedChoice then
choice = focusedChoice.choiceId
end
g_game.answerModalDialog(id, enterButton, choice)
destroyDialog()
end
local escapeFunc = function()
g_game.answerModalDialog(id, escapeId, choiceList:getFocusedChild().choiceId)
destroy()
local focusedChoice = choiceList:getFocusedChild()
local choice = 0xFF
if focusedChoice then
choice = focusedChoice.choiceId
end
g_game.answerModalDialog(id, escapeButton, choice)
destroyDialog()
end
choiceList.onDoubleClick = enterFunc
enterButton.onClick = enterFunc
modalDialog.onEnter = enterFunc
escapeButton.onClick = escapeFunc
modalDialog.onEscape = escapeFunc
return
end

View File

@@ -8,52 +8,61 @@ ChoiceListLabel < Label
background-color: #ffffff22
color: #ffffff
ChoiceList < TextList
id: choiceList
vertical-scrollbar: choiceScrollBar
anchors.fill: parent
anchors.top: prev.bottom
anchors.bottom: next.top
padding: 1
margin-top: 4
margin-bottom: 10
focusable: false
visible: false
ChoiceScrollBar < VerticalScrollBar
id: choiceScrollBar
anchors.top: choiceList.top
anchors.bottom: choiceList.bottom
anchors.right: choiceList.right
step: 14
pixels-scroll: true
visible: false
ModalButton < Button
width: 60
margin: 2
ModalDialog < MainWindow
id: modalDialog
!text: tr('Title')
size: 280 230
@onEscape: self:destroy()
size: 280 97
&minimumWidth: 200
&maximumWidth: 500
&minimumChoices: 4
&maximumChoices: 10
Label
id: messageLabel
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
text-align: left
text: Message
height: 60
text-auto-resize: true
text-wrap: true
TextList
id: choiceList
vertical-scrollbar: choiceScrollBar
anchors.fill: parent
anchors.top: prev.bottom
anchors.bottom: next.top
margin-bottom: 10
margin-top: 10
padding: 1
focusable: false
Button
id: enterButton
!text: tr('Ok')
anchors.top: next.top
anchors.right: next.left
margin-right: 8
width: 60
ChoiceList
Button
id: escapeButton
!text: tr('Cancel')
anchors.bottom: parent.bottom
HorizontalSeparator
anchors.left: parent.left
anchors.right: parent.right
margin-top: 10
width: 60
anchors.bottom: next.top
VerticalScrollBar
id: choiceScrollBar
anchors.top: choiceList.top
anchors.bottom: choiceList.bottom
anchors.right: choiceList.right
step: 14
pixels-scroll: true
Panel
id: buttonList
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
height: 24
layout: horizontalBox
align-right: true
ChoiceScrollBar

View File

@@ -61,7 +61,7 @@ end
function terminate()
disconnect(g_game, 'onTextMessage', displayMessage)
disconnect(g_game, 'onGameEnd',clearMessages)
disconnect(g_game, 'onGameEnd', clearMessages)
clearMessages()
messagesPanel:destroy()
end