2013-11-18 18:58:15 +01:00
|
|
|
modalDialog = nil
|
|
|
|
|
2012-11-27 14:48:48 +01:00
|
|
|
function init()
|
2013-01-18 23:39:11 +01:00
|
|
|
g_ui.importStyle('modaldialog')
|
2012-11-27 14:48:48 +01:00
|
|
|
|
|
|
|
connect(g_game, { onModalDialog = onModalDialog,
|
2013-11-18 18:58:15 +01:00
|
|
|
onGameEnd = destroyDialog })
|
|
|
|
|
|
|
|
local dialog = rootWidget:recursiveGetChildById('modalDialog')
|
|
|
|
if dialog then
|
|
|
|
modalDialog = dialog
|
|
|
|
end
|
2012-11-27 14:48:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function terminate()
|
|
|
|
disconnect(g_game, { onModalDialog = onModalDialog,
|
2013-11-18 18:58:15 +01:00
|
|
|
onGameEnd = destroyDialog })
|
2012-11-27 14:48:48 +01:00
|
|
|
end
|
|
|
|
|
2013-11-18 18:58:15 +01:00
|
|
|
function destroyDialog()
|
2012-11-27 14:48:48 +01:00
|
|
|
if modalDialog then
|
|
|
|
modalDialog:destroy()
|
|
|
|
modalDialog = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-18 18:58:15 +01:00
|
|
|
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
|
|
|
|
|
2012-11-27 14:48:48 +01:00
|
|
|
modalDialog = g_ui.createWidget('ModalDialog', rootWidget)
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2012-11-27 14:48:48 +01:00
|
|
|
local messageLabel = modalDialog:getChildById('messageLabel')
|
|
|
|
local choiceList = modalDialog:getChildById('choiceList')
|
2013-11-18 18:58:15 +01:00
|
|
|
local choiceScrollbar = modalDialog:getChildById('choiceScrollBar')
|
2015-04-20 19:22:50 +02:00
|
|
|
local buttonsPanel = modalDialog:getChildById('buttonsPanel')
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2012-11-27 14:48:48 +01:00
|
|
|
modalDialog:setText(title)
|
|
|
|
messageLabel:setText(message)
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2015-04-20 19:22:50 +02:00
|
|
|
local labelHeight
|
2013-11-18 18:58:15 +01:00
|
|
|
for i = 1, #choices do
|
|
|
|
local choiceId = choices[i][1]
|
|
|
|
local choiceName = choices[i][2]
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2012-11-27 14:48:48 +01:00
|
|
|
local label = g_ui.createWidget('ChoiceListLabel', choiceList)
|
|
|
|
label.choiceId = choiceId
|
|
|
|
label:setText(choiceName)
|
|
|
|
label:setPhantom(false)
|
2013-11-18 18:58:15 +01:00
|
|
|
if not labelHeight then
|
|
|
|
labelHeight = label:getHeight()
|
2012-11-27 14:48:48 +01:00
|
|
|
end
|
|
|
|
end
|
2013-11-18 18:58:15 +01:00
|
|
|
choiceList:focusNextChild()
|
|
|
|
|
2015-04-20 19:22:50 +02:00
|
|
|
local buttonsWidth = 0
|
2013-11-18 18:58:15 +01:00
|
|
|
for i = 1, #buttons do
|
|
|
|
local buttonId = buttons[i][1]
|
|
|
|
local buttonText = buttons[i][2]
|
|
|
|
|
2015-04-20 19:22:50 +02:00
|
|
|
local button = g_ui.createWidget('ModalButton', buttonsPanel)
|
2013-11-18 18:58:15 +01:00
|
|
|
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
|
2015-04-20 19:22:50 +02:00
|
|
|
buttonsWidth = buttonsWidth + button:getWidth() + button:getMarginLeft() + button:getMarginRight()
|
2013-11-18 18:58:15 +01:00
|
|
|
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
|
|
|
|
|
2015-04-20 19:22:50 +02:00
|
|
|
local horizontalPadding = modalDialog:getPaddingLeft() + modalDialog:getPaddingRight()
|
|
|
|
buttonsWidth = buttonsWidth + horizontalPadding
|
|
|
|
|
|
|
|
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)
|
2012-11-27 14:48:48 +01:00
|
|
|
|
|
|
|
local enterFunc = function()
|
2013-11-18 18:58:15 +01:00
|
|
|
local focusedChoice = choiceList:getFocusedChild()
|
|
|
|
local choice = 0xFF
|
|
|
|
if focusedChoice then
|
|
|
|
choice = focusedChoice.choiceId
|
|
|
|
end
|
|
|
|
g_game.answerModalDialog(id, enterButton, choice)
|
|
|
|
destroyDialog()
|
2012-11-27 14:48:48 +01:00
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2012-11-27 14:48:48 +01:00
|
|
|
local escapeFunc = function()
|
2013-11-18 18:58:15 +01:00
|
|
|
local focusedChoice = choiceList:getFocusedChild()
|
|
|
|
local choice = 0xFF
|
|
|
|
if focusedChoice then
|
|
|
|
choice = focusedChoice.choiceId
|
|
|
|
end
|
|
|
|
g_game.answerModalDialog(id, escapeButton, choice)
|
|
|
|
destroyDialog()
|
2012-11-27 14:48:48 +01:00
|
|
|
end
|
2013-01-23 01:15:46 +01:00
|
|
|
|
|
|
|
choiceList.onDoubleClick = enterFunc
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2012-11-27 14:48:48 +01:00
|
|
|
modalDialog.onEnter = enterFunc
|
|
|
|
modalDialog.onEscape = escapeFunc
|
|
|
|
end
|