| 1 |
--[[ Copyright (c) 2009 Peter "Corsix" Cawley |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
--! Dialog for "Are you sure you want to quit?" and similar yes/no questions. |
| 22 |
--! This dialog is also used for errors and requires some special handling |
| 23 |
class "UIConfirmDialog" (Window) |
| 24 |
|
| 25 |
---@type UIConfirmDialog |
| 26 |
local UIConfirmDialog = _G["UIConfirmDialog"] |
| 27 |
|
| 28 |
local top_frame = 357 |
| 29 |
local top_frame_height = 22 |
| 30 |
local middle_frame = 358 |
| 31 |
local middle_frame_height = 11 |
| 32 |
local bottom_frame = 359 |
| 33 |
local text_width = 153 |
| 34 |
|
| 35 |
--! Initialise the Confirmation Dialog |
| 36 |
--!param ui The UI |
| 37 |
--!param must_pause (boolean) set whether this dialog should pause the game |
| 38 |
--!param text (string) message to show |
| 39 |
--!param callback_ok (function) what to do on yes/ok |
| 40 |
--!param callback_cancel (function) what to do on no/cancel/close |
| 41 |
function UIConfirmDialog:UIConfirmDialog(ui, must_pause, text, callback_ok, callback_cancel) |
| 42 |
self:Window() |
| 43 |
|
| 44 |
local app = ui.app |
| 45 |
self.modal_class = "information" |
| 46 |
self.esc_closes = true |
| 47 |
self.on_top = true |
| 48 |
self.ui = ui |
| 49 |
self.width = 183 |
| 50 |
self.height = 199 |
| 51 |
self:setDefaultPosition(0.5, 0.5) |
| 52 |
self.panel_sprites = app.gfx:loadSpriteTable("QData", "Req04V", true) |
| 53 |
self.white_font = app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true }) |
| 54 |
self.text = text |
| 55 |
self.callback_ok = callback_ok -- Callback function to launch if user chooses ok |
| 56 |
self.callback_cancel = callback_cancel -- Callback function to launch if user chooses cancel |
| 57 |
self.must_pause = must_pause |
| 58 |
|
| 59 |
-- Check how "high" the dialog must be |
| 60 |
local _, text_height = self.white_font:sizeOf(text, text_width * TheApp.config.ui_scale) |
| 61 |
text_height = text_height / TheApp.config.ui_scale -- Scale independent pixels |
| 62 |
|
| 63 |
self:addPanel(top_frame, 0, 0) -- Dialog header |
| 64 |
local last_y = top_frame_height |
| 65 |
|
| 66 |
for _ = 1, math.ceil(text_height / middle_frame_height) do |
| 67 |
self:addPanel(middle_frame, 0, last_y) -- Dialog background |
| 68 |
self.height = self.height + middle_frame_height |
| 69 |
last_y = last_y + middle_frame_height |
| 70 |
end |
| 71 |
|
| 72 |
self:addPanel(bottom_frame, 0, last_y) -- Dialog footer |
| 73 |
self:addPanel(360, 0, last_y + 10):makeButton(8, 10, 82, 34, 361, self.cancel) |
| 74 |
:setTooltip(_S.tooltip.window_general.cancel):setSound("No4.wav") |
| 75 |
self:addPanel(362, 90, last_y + 10):makeButton(0, 10, 82, 34, 363, self.ok) |
| 76 |
:setTooltip(_S.tooltip.window_general.confirm):setSound("YesX.wav") |
| 77 |
|
| 78 |
-- Correct window height based on text height |
| 79 |
self.height = last_y + 63 |
| 80 |
|
| 81 |
self:registerKeyHandlers() |
| 82 |
end |
| 83 |
|
| 84 |
-- Confirm dialogs are used for errors, if it is an error then pause the game |
| 85 |
function UIConfirmDialog:mustPause() |
| 86 |
return self.must_pause |
| 87 |
end |
| 88 |
|
| 89 |
function UIConfirmDialog:registerKeyHandlers() |
| 90 |
self:addKeyHandler("global_confirm", self.ok) |
| 91 |
self:addKeyHandler("global_confirm_alt", self.ok) |
| 92 |
end |
| 93 |
|
| 94 |
function UIConfirmDialog:cancel() |
| 95 |
self:close(false) |
| 96 |
end |
| 97 |
|
| 98 |
function UIConfirmDialog:ok() |
| 99 |
self:close(true) |
| 100 |
end |
| 101 |
|
| 102 |
--! Closes the confirm dialog |
| 103 |
--!param confirmed (boolean or nil) whether to call the confirm callback (true) or cancel callback (false/nil) |
| 104 |
function UIConfirmDialog:close(confirmed) |
| 105 |
-- NB: Window is closed before executing the callback in order to not save the confirmation dialog in a savegame |
| 106 |
Window.close(self) |
| 107 |
if confirmed then |
| 108 |
if self.callback_ok then |
| 109 |
self.callback_ok() |
| 110 |
end |
| 111 |
else |
| 112 |
if self.callback_cancel then |
| 113 |
self.callback_cancel() |
| 114 |
end |
| 115 |
end |
| 116 |
end |
| 117 |
|
| 118 |
function UIConfirmDialog:draw(canvas, x, y) |
| 119 |
Window.draw(self, canvas, x, y) |
| 120 |
|
| 121 |
local s = TheApp.config.ui_scale |
| 122 |
x, y = x + self.x * s, y + self.y * s |
| 123 |
self.white_font:drawWrapped(canvas, self.text, x + 17 * s, y + 17 * s, text_width * s) |
| 124 |
end |
| 125 |
|
| 126 |
function UIConfirmDialog:afterLoad(old, new) |
| 127 |
Window.afterLoad(self, old, new) |
| 128 |
self:registerKeyHandlers() |
| 129 |
|
| 130 |
if old < 236 then |
| 131 |
self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true }) |
| 132 |
end |
| 133 |
end |