1modalDialog = nil
2lastDialogChoices = 0
3lastDialogChoice = 0
4lastDialogAnswer = 0
5
6function init()
7 g_ui.importStyle('modaldialog')
8
9 connect(g_game, { onModalDialog = onModalDialog,
10 onGameEnd = destroyDialog })
11
12 local dialog = rootWidget:recursiveGetChildById('modalDialog')
13 if dialog then
14 modalDialog = dialog
15 end
16end
17
18function terminate()
19 disconnect(g_game, { onModalDialog = onModalDialog,
20 onGameEnd = destroyDialog })
21end
22
23function destroyDialog()
24 if modalDialog then
25 modalDialog:destroy()
26 modalDialog = nil
27 end
28end
29
30function onModalDialog(id, title, message, buttons, enterButton, escapeButton, choices, priority)
31 -- priority parameter is unused, not sure what its use is.
32 if modalDialog then
33 return
34 end
35
36 modalDialog = g_ui.createWidget('ModalDialog', rootWidget)
37
38 local messageLabel = modalDialog:getChildById('messageLabel')
39 local choiceList = modalDialog:getChildById('choiceList')
40 local choiceScrollbar = modalDialog:getChildById('choiceScrollBar')
41 local buttonsPanel = modalDialog:getChildById('buttonsPanel')
42
43 modalDialog:setText(title)
44 messageLabel:setText(message)
45
46 local labelHeight
47 for i = 1, #choices do
48 local choiceId = choices[i][1]
49 local choiceName = choices[i][2]
50
51 local label = g_ui.createWidget('ChoiceListLabel', choiceList)
52 label.choiceId = choiceId
53 label:setText(choiceName)
54 label:setPhantom(false)
55 if not labelHeight then
56 labelHeight = label:getHeight()
57 end
58 end
59 if #choices > 0 then
60 if g_clock.millis() < lastDialogAnswer + 1000 and lastDialogChoices == #choices then
61 choiceList:focusChild(choiceList:getChildByIndex(lastDialogChoice))
62 else
63 choiceList:focusChild(choiceList:getFirstChild())
64 end
65 end
66
67 local buttonsWidth = 0
68 for i = 1, #buttons do
69 local buttonId = buttons[i][1]
70 local buttonText = buttons[i][2]
71
72 local button = g_ui.createWidget('ModalButton', buttonsPanel)
73 button:setText(buttonText)
74 button.onClick = function(self)
75 local focusedChoice = choiceList:getFocusedChild()
76 local choice = 0xFF
77 if focusedChoice then
78 choice = focusedChoice.choiceId
79 lastDialogChoice = choiceList:getChildIndex(focusedChoice)
80 lastDialogAnswer = g_clock.millis()
81 end
82 g_game.answerModalDialog(id, buttonId, choice)
83 destroyDialog()
84 end
85 buttonsWidth = buttonsWidth + button:getWidth() + button:getMarginLeft() + button:getMarginRight()
86 end
87
88 local additionalHeight = 0
89 if #choices > 0 then
90 choiceList:setVisible(true)
91 choiceScrollbar:setVisible(true)
92
93 additionalHeight = math.min(modalDialog.maximumChoices, math.max(modalDialog.minimumChoices, #choices)) * labelHeight
94 additionalHeight = additionalHeight + choiceList:getPaddingTop() + choiceList:getPaddingBottom()
95 end
96
97 local horizontalPadding = modalDialog:getPaddingLeft() + modalDialog:getPaddingRight()
98 buttonsWidth = buttonsWidth + horizontalPadding
99
100 local labelWidth = math.min(600, math.floor(message:len() * 1.5))
101 modalDialog:setWidth(math.min(modalDialog.maximumWidth, math.max(buttonsWidth, labelWidth, modalDialog.minimumWidth)))
102 messageLabel:setTextWrap(true)
103
104 modalDialog:setHeight(90 + additionalHeight + messageLabel:getHeight())
105
106 local enterFunc = function()
107 local focusedChoice = choiceList:getFocusedChild()
108 local choice = 0xFF
109 if focusedChoice then
110 choice = focusedChoice.choiceId
111 lastDialogChoice = choiceList:getChildIndex(focusedChoice)
112 lastDialogAnswer = g_clock.millis()
113 end
114 g_game.answerModalDialog(id, enterButton, choice)
115 destroyDialog()
116 end
117
118 local escapeFunc = function()
119 local focusedChoice = choiceList:getFocusedChild()
120 local choice = 0xFF
121 if focusedChoice then
122 choice = focusedChoice.choiceId
123 lastDialogChoice = choiceList:getChildIndex(focusedChoice)
124 lastDialogAnswer = g_clock.millis()
125 end
126 g_game.answerModalDialog(id, escapeButton, choice)
127 destroyDialog()
128 end
129
130 choiceList.onDoubleClick = enterFunc
131
132 modalDialog.onEnter = enterFunc
133 modalDialog.onEscape = escapeFunc
134
135 lastDialogChoices = #choices
136end