| 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 |
corsixth.require("window") |
| 22 |
|
| 23 |
--! Top-level container for all other user-interface components. |
| 24 |
class "UI" (Window) |
| 25 |
|
| 26 |
---@type UI |
| 27 |
local UI = _G["UI"] |
| 28 |
|
| 29 |
local TH = require("TH") |
| 30 |
local SDL = require("sdl") |
| 31 |
local WM = SDL.wm |
| 32 |
local lfs = require("lfs") |
| 33 |
|
| 34 |
local function invert(t) |
| 35 |
local r = {} |
| 36 |
for k, v in pairs(t) do |
| 37 |
if type(v) == "table" then |
| 38 |
for _, val in ipairs(v) do |
| 39 |
r[val] = k |
| 40 |
end |
| 41 |
else |
| 42 |
r[v] = k |
| 43 |
end |
| 44 |
end |
| 45 |
return r |
| 46 |
end |
| 47 |
|
| 48 |
function UI:initKeyAndButtonCodes() |
| 49 |
local key_remaps = {} |
| 50 |
local button_remaps = {} |
| 51 |
local key_to_button_remaps = {} |
| 52 |
local key_norms = setmetatable({ |
| 53 |
[" "] = "space", |
| 54 |
esc = "escape", |
| 55 |
}, {__index = function(t, k) |
| 56 |
k = tostring(k):lower() |
| 57 |
return rawget(t, k) or k |
| 58 |
end}) |
| 59 |
--[===[ |
| 60 |
do |
| 61 |
local ourpath = debug.getinfo(1, "S").source:sub(2, -7) |
| 62 |
local result, err = loadfile_envcall(ourpath .. "key_mapping.txt") |
| 63 |
if not result then |
| 64 |
print("Cannot load key mapping:" .. err) |
| 65 |
else |
| 66 |
local env = { |
| 67 |
key_remaps = function(t) |
| 68 |
for k, v in pairs(t) do |
| 69 |
key_remaps[key_norms[k]] = key_norms[v] |
| 70 |
end |
| 71 |
end, |
| 72 |
button_remaps = function(t) |
| 73 |
for k, v in pairs(t) do |
| 74 |
k = key_norms[k] |
| 75 |
if k == "left" or k == "middle" or k == "right" then |
| 76 |
button_remaps[k] = key_norms[v] |
| 77 |
else |
| 78 |
key_to_button_remaps[k] = key_norms[v] |
| 79 |
end |
| 80 |
end |
| 81 |
end, |
| 82 |
} |
| 83 |
setmetatable(env, {__index = function(_, k) |
| 84 |
return k |
| 85 |
end}) |
| 86 |
result(env) |
| 87 |
end |
| 88 |
end |
| 89 |
]===] |
| 90 |
|
| 91 |
local keypad = { |
| 92 |
["Keypad 0"] = "insert", |
| 93 |
["Keypad 1"] = "end", |
| 94 |
["Keypad 2"] = "down", |
| 95 |
["Keypad 3"] = "pagedown", |
| 96 |
["Keypad 4"] = "left", |
| 97 |
["Keypad 6"] = "right", |
| 98 |
["Keypad 7"] = "home", |
| 99 |
["Keypad 8"] = "up", |
| 100 |
["Keypad 9"] = "pageup", |
| 101 |
["Keypad ."] = "delete", |
| 102 |
} |
| 103 |
|
| 104 |
-- Apply keypad remapping |
| 105 |
for k, v in pairs(keypad) do |
| 106 |
key_remaps[key_norms[k]] = key_norms[v] |
| 107 |
end |
| 108 |
|
| 109 |
self.key_remaps = key_remaps |
| 110 |
self.key_to_button_remaps = key_to_button_remaps |
| 111 |
|
| 112 |
self.button_codes = { |
| 113 |
left = 1, |
| 114 |
middle = 2, |
| 115 |
right = 3, |
| 116 |
} |
| 117 |
|
| 118 |
-- Apply button remaps directly to codes, as mouse button codes are reliable |
| 119 |
-- (keyboard key codes are not). |
| 120 |
local original_button_codes = {} |
| 121 |
for input, behave_as in pairs(button_remaps) do |
| 122 |
local code = original_button_codes[input] or self.button_codes[input] or {} |
| 123 |
if not original_button_codes[input] then |
| 124 |
original_button_codes[input] = code |
| 125 |
self.button_codes[input] = nil |
| 126 |
end |
| 127 |
if not original_button_codes[behave_as] then |
| 128 |
original_button_codes[behave_as] = self.button_codes[behave_as] |
| 129 |
end |
| 130 |
self.button_codes[behave_as] = code |
| 131 |
end |
| 132 |
|
| 133 |
self.button_codes = invert(self.button_codes) |
| 134 |
end |
| 135 |
|
| 136 |
local LOADED_DIALOGS = false |
| 137 |
|
| 138 |
function UI:UI(app, minimal) |
| 139 |
self:Window() |
| 140 |
self:initKeyAndButtonCodes() |
| 141 |
self.app = app |
| 142 |
self.screen_offset_x = 0 |
| 143 |
self.screen_offset_y = 0 |
| 144 |
self.cursor = nil |
| 145 |
self.cursor_entity = nil |
| 146 |
self.debug_cursor_entity = nil |
| 147 |
-- through trial and error, this palette seems to give the desired result (white background, black text) |
| 148 |
-- NB: Need a palette present in both the full game and in the demo data |
| 149 |
if minimal then |
| 150 |
self.tooltip_font = app.gfx:loadBuiltinFont() |
| 151 |
else |
| 152 |
local palette = app.gfx:getPalette("Pref01V.pal") |
| 153 |
self.tooltip_font = app.gfx:loadFontAndSpriteTable("QData", "Font00V", false, palette, { apply_ui_scale = true }) |
| 154 |
end |
| 155 |
self.tooltip = nil |
| 156 |
self.tooltip_counter = 0 |
| 157 |
self.background = false |
| 158 |
-- tick_scroll_amount will either hold a table containing x and y values, at |
| 159 |
-- at least one of which being non-zero. If both x and y are zero, then the |
| 160 |
-- value false should be used instead, so that tests to see if there is any |
| 161 |
-- scrolling to be done are quick and simple. |
| 162 |
self.tick_scroll_amount = false |
| 163 |
self.tick_scroll_amount_mouse = false |
| 164 |
self.tick_scroll_mult = 1 |
| 165 |
self.modal_windows = { |
| 166 |
-- [class_name] -> window, |
| 167 |
} |
| 168 |
-- Windows can tell UI to pass specific codes forward to them. See addKeyHandler and removeKeyHandler |
| 169 |
self.key_handlers = {} |
| 170 |
-- For use in onKeyUp when assigning hotkeys in the "Assign Hotkeys" window. |
| 171 |
self.temp_button_down = false |
| 172 |
-- |
| 173 |
self.key_noted = false |
| 174 |
self.mouse_released = false |
| 175 |
|
| 176 |
self.down_count = 0 |
| 177 |
if not minimal then |
| 178 |
self.default_cursor = app.gfx:loadMainCursor("default") |
| 179 |
self.down_cursor = app.gfx:loadMainCursor("clicked") |
| 180 |
self.grab_cursor = app.gfx:loadMainCursor("grab") |
| 181 |
self.edit_room_cursor = app.gfx:loadMainCursor("edit_room") |
| 182 |
self.waiting_cursor = app.gfx:loadMainCursor("sleep") |
| 183 |
end |
| 184 |
self.editing_allowed = true |
| 185 |
|
| 186 |
if not LOADED_DIALOGS then |
| 187 |
app:loadLuaFolder("dialogs", true) |
| 188 |
app:loadLuaFolder("dialogs/fullscreen", true) |
| 189 |
app:loadLuaFolder("dialogs/resizables", true) |
| 190 |
app:loadLuaFolder("dialogs/resizables/menu_list_dialogs", true) |
| 191 |
app:loadLuaFolder("dialogs/resizables/file_browsers", true) |
| 192 |
LOADED_DIALOGS = true |
| 193 |
end |
| 194 |
|
| 195 |
self:setCursor(self.default_cursor) |
| 196 |
self:setupGlobalKeyHandlers() |
| 197 |
self.casebook_selected_disease = false |
| 198 |
end |
| 199 |
|
| 200 |
function UI:runDebugScript() |
| 201 |
-- luacheck: ignore 111 _ is set in debug code |
| 202 |
print("Executing Debug Script...") |
| 203 |
local debug_script = self.app:getFullPath({"Lua", "debug_script.lua"}) |
| 204 |
_ = TheApp.ui and TheApp.ui.debug_cursor_entity |
| 205 |
local script = assert(loadfile(debug_script)) |
| 206 |
script() |
| 207 |
-- Clear _ after the script to prevent save corruption |
| 208 |
_ = nil |
| 209 |
end |
| 210 |
|
| 211 |
function UI:setupGlobalKeyHandlers() |
| 212 |
-- Add some global keyhandlers |
| 213 |
self:addKeyHandler("global_cancel", self, self.closeWindow) |
| 214 |
self:addKeyHandler("global_cancel_alt", self, self.closeWindow) |
| 215 |
self:addKeyHandler("global_stop_movie", self, self.stopMovie) |
| 216 |
self:addKeyHandler("global_stop_movie_alt", self, self.stopMovie) |
| 217 |
self:addKeyHandler("global_pause_movie", self, self.pauseMovie) |
| 218 |
self:addKeyHandler("global_screenshot", self, self.makeScreenshot) |
| 219 |
self:addKeyHandler("global_fullscreen_toggle", self, self.fullscreenHotkey) |
| 220 |
self:addKeyHandler("global_exitApp", self, self.exitApplication) |
| 221 |
self:addKeyHandler("global_resetApp", self, self.resetApp) |
| 222 |
self:addKeyHandler("global_releaseMouse", self, self.releaseMouse) |
| 223 |
|
| 224 |
self:addOrRemoveDebugModeKeyHandlers() |
| 225 |
end |
| 226 |
|
| 227 |
--! Play a sound effect |
| 228 |
--!param name (string) The name of the sound to be played. Can include |
| 229 |
-- wildcards (*). |
| 230 |
--!param played_callback (function) A function to be called when the sound has |
| 231 |
-- finished playing. Can be nil. |
| 232 |
--!param played_callback_delay (integer) An optional delay in milliseconds |
| 233 |
-- before the played_callback is called. |
| 234 |
--!param loops (integer) number of times to play the audio. -1 for infinite. |
| 235 |
--!return (table) A `sound` table for passing into functions that act on the |
| 236 |
-- playing sound. The fields are an implementation detail that should not be |
| 237 |
-- used outside of the Audio class. |
| 238 |
function UI:playSound(name, played_callback, played_callback_delay, loops) |
| 239 |
if self.app.config.play_sounds then |
| 240 |
return self.app.audio:playSound(name, nil, false, played_callback, played_callback_delay, loops) |
| 241 |
end |
| 242 |
end |
| 243 |
|
| 244 |
--! Stop the given sound |
| 245 |
-- see Audio:stopSound |
| 246 |
--!param sound (table) sound to stop |
| 247 |
function UI:stopSound(sound) |
| 248 |
self.app.audio:stopSound(sound) |
| 249 |
end |
| 250 |
|
| 251 |
-- Stub with args for subclass GameUI. |
| 252 |
function UI:playAnnouncement(name, priority, played_callback, played_callback_delay) |
| 253 |
end |
| 254 |
|
| 255 |
function UI:setDefaultCursor(cursor) |
| 256 |
if cursor == nil then |
| 257 |
cursor = "default" |
| 258 |
end |
| 259 |
if type(cursor) == "string" then |
| 260 |
cursor = self.app.gfx:loadMainCursor(cursor) |
| 261 |
end |
| 262 |
if self.cursor == self.default_cursor then |
| 263 |
self:setCursor(cursor) |
| 264 |
end |
| 265 |
self.default_cursor = cursor |
| 266 |
end |
| 267 |
|
| 268 |
function UI:setCursor(cursor) |
| 269 |
if cursor ~= self.cursor then |
| 270 |
self.cursor = cursor |
| 271 |
if cursor.use then |
| 272 |
-- Cursor is a true C cursor, perhaps even a hardware cursor. |
| 273 |
-- Make the real cursor visible, and use this as it. |
| 274 |
self.simulated_cursor = nil |
| 275 |
WM.showCursor(true) |
| 276 |
cursor:use(self.app.video) |
| 277 |
else |
| 278 |
-- Cursor is a Lua simulated cursor. |
| 279 |
-- Make the real cursor invisible, and simulate it with this. |
| 280 |
WM.showCursor(self.mouse_released) |
| 281 |
self.simulated_cursor = cursor |
| 282 |
end |
| 283 |
end |
| 284 |
end |
| 285 |
|
| 286 |
function UI:drawTooltip(canvas) |
| 287 |
if not self.tooltip or not self.tooltip_counter or self.tooltip_counter > 0 then |
| 288 |
return |
| 289 |
end |
| 290 |
|
| 291 |
local x, y = self.tooltip.x, self.tooltip.y |
| 292 |
if not self.tooltip.x then |
| 293 |
-- default to cursor position for (lower left corner of) tooltip |
| 294 |
x, y = self:getCursorPosition() |
| 295 |
end |
| 296 |
|
| 297 |
if self.tooltip_font then |
| 298 |
self.tooltip_font:drawTooltip(canvas, self.tooltip.text, x, y, 200 * TheApp.config.ui_scale) |
| 299 |
end |
| 300 |
end |
| 301 |
|
| 302 |
function UI:draw(canvas) |
| 303 |
local app = self.app |
| 304 |
if self.background then |
| 305 |
local bg_w, bg_h = self.background_width, self.background_height |
| 306 |
local screen_w, screen_h = app.config.width, app.config.height |
| 307 |
local factor = math.max(screen_w / bg_w, screen_h / bg_h) |
| 308 |
if canvas:scale(factor, "bitmap") or canvas:scale(factor) then |
| 309 |
self.background:draw(canvas, math.floor((screen_w - bg_w * factor) / 2), math.floor((screen_h - bg_h * factor) / 2)) |
| 310 |
canvas:scale(1) |
| 311 |
else |
| 312 |
canvas:fillBlack() |
| 313 |
self.background:draw(canvas, math.floor((screen_w - bg_w) / 2), math.floor((screen_h - bg_h) / 2)) |
| 314 |
end |
| 315 |
end |
| 316 |
Window.draw(self, canvas, 0, 0) |
| 317 |
self:drawTooltip(canvas) |
| 318 |
if self.simulated_cursor then |
| 319 |
self.simulated_cursor.draw(canvas, self.cursor_x, self.cursor_y) |
| 320 |
end |
| 321 |
end |
| 322 |
|
| 323 |
--! Register a key handler / hotkey for a window. |
| 324 |
--!param keys (string or table) The keyboard key which should trigger the callback (for |
| 325 |
-- example, "left" or "z" or "F9"), or a list with modifier(s) and the key (e.g. {"ctrl", "s"}). |
| 326 |
--!param window (Window) The UI window which should receive the callback. |
| 327 |
--!param callback (function) The method to be called on `window` when `key` is |
| 328 |
-- pressed. |
| 329 |
--!param ... Additional arguments to `callback`. |
| 330 |
function UI:addKeyHandler(keys, window, callback, ...) |
| 331 |
-- It is necessary to clone the key table into another temporary table, as if we don't the original table that we take it from will lose |
| 332 |
-- the last key of that table permanently in the next line of code after this one, until the program is restarted. |
| 333 |
-- I.E. if the "ingame_quitLevel" hotkey from the "hotkeys_values" table in "config_finder.lua" is a table that looks like this: |
| 334 |
-- {"shift", "q"} |
| 335 |
-- We would lose the "q" element until we restarted the game and the "hotkey.txt" was read from again, causing the "ingame_quitLevel" |
| 336 |
-- table to be reset back to {"shift, "q"} |
| 337 |
local temp_keys = {} |
| 338 |
|
| 339 |
-- Check to see if "keys" key exist in the hotkeys table. |
| 340 |
if self.app.hotkeys[keys] ~= nil then |
| 341 |
if type(self.app.hotkeys[keys]) == "table" then |
| 342 |
temp_keys = shallow_clone(self.app.hotkeys[keys]) |
| 343 |
elseif type(self.app.hotkeys[keys]) == "string" then |
| 344 |
temp_keys = shallow_clone({self.app.hotkeys[keys]}) |
| 345 |
end |
| 346 |
else |
| 347 |
if type(keys) == "string" then |
| 348 |
print(string.format("\"%s\" does not exist in the hotkeys configuration file.", keys)) |
| 349 |
else |
| 350 |
print("Usage of addKeyHandler() requires the first argument to be a string of a key that can be found in the hotkeys configuration file.") |
| 351 |
end |
| 352 |
end |
| 353 |
|
| 354 |
if temp_keys ~= nil then |
| 355 |
local has_enterOrPlus |
| 356 |
local temp_keys_copy = {} |
| 357 |
|
| 358 |
if type(temp_keys) == "table" then |
| 359 |
temp_keys_copy = shallow_clone(temp_keys) |
| 360 |
elseif type(temp_keys) == "string" then |
| 361 |
temp_keys_copy = {temp_keys} |
| 362 |
end |
| 363 |
|
| 364 |
for _, v in pairs(temp_keys_copy) do |
| 365 |
if v == "enter" then |
| 366 |
has_enterOrPlus = true |
| 367 |
elseif v == "return" then |
| 368 |
has_enterOrPlus = true |
| 369 |
elseif v == "+" then |
| 370 |
has_enterOrPlus = true |
| 371 |
elseif v == "=" then |
| 372 |
has_enterOrPlus = true |
| 373 |
else |
| 374 |
has_enterOrPlus = false |
| 375 |
end |
| 376 |
end |
| 377 |
|
| 378 |
local key = table.remove(temp_keys, #temp_keys):lower() |
| 379 |
local modifiers = list_to_set(temp_keys) -- SET of modifiers |
| 380 |
if not self.key_handlers[key] then |
| 381 |
-- No handlers for this key? Create a new table. |
| 382 |
self.key_handlers[key] = {} |
| 383 |
end |
| 384 |
|
| 385 |
table.insert(self.key_handlers[key], { |
| 386 |
modifiers = modifiers, |
| 387 |
window = window, |
| 388 |
callback = callback, |
| 389 |
... |
| 390 |
}) |
| 391 |
|
| 392 |
-- If the handler added has enter, return, plus, or minus in it... |
| 393 |
if has_enterOrPlus then |
| 394 |
for k, _ in pairs(temp_keys_copy) do |
| 395 |
if temp_keys_copy[k] == "enter" then |
| 396 |
temp_keys_copy[k] = "return" |
| 397 |
elseif temp_keys_copy[k] == "return" then |
| 398 |
temp_keys_copy[k] = "enter" |
| 399 |
elseif temp_keys_copy[k] == "+" then |
| 400 |
temp_keys_copy[k] = "=" |
| 401 |
elseif temp_keys_copy[k] == "=" then |
| 402 |
temp_keys_copy[k] = "+" |
| 403 |
end |
| 404 |
end |
| 405 |
|
| 406 |
local key_02 = table.remove(temp_keys_copy, #temp_keys_copy):lower() |
| 407 |
local modifiers_02 = list_to_set(temp_keys_copy) -- SET of modifiers |
| 408 |
if not self.key_handlers[key_02] then |
| 409 |
-- No handlers for this key? Create a new table. |
| 410 |
self.key_handlers[key_02] = {} |
| 411 |
end |
| 412 |
|
| 413 |
-- Then make the same handler, but with the complementary button. |
| 414 |
-- i.e. If it asks for "enter", it will also add "return". |
| 415 |
table.insert(self.key_handlers[key_02], { |
| 416 |
modifiers = modifiers_02, |
| 417 |
window = window, |
| 418 |
callback = callback, |
| 419 |
... |
| 420 |
}) |
| 421 |
end |
| 422 |
else |
| 423 |
print("addKeyHandler() failed.") |
| 424 |
end |
| 425 |
end |
| 426 |
|
| 427 |
--! Unregister a key handler previously registered by `addKeyHandler`. |
| 428 |
--!param keys (string or table) The key or list of modifiers+key of a key / window |
| 429 |
-- pair previously passed to `addKeyHandler`. |
| 430 |
--!param window (Window) The window of a key / window pair previously passed |
| 431 |
-- to `addKeyHandler`. |
| 432 |
function UI:removeKeyHandler(keys, window) |
| 433 |
local temp_keys = nil |
| 434 |
|
| 435 |
-- Check to see if "keys" key exist in the hotkeys table. |
| 436 |
if self.app.hotkeys[keys] ~= nil then |
| 437 |
if type(self.app.hotkeys[keys]) == "table" then |
| 438 |
temp_keys = shallow_clone(self.app.hotkeys[keys]) |
| 439 |
elseif type(self.app.hotkeys[keys]) == "string" then |
| 440 |
temp_keys = shallow_clone({self.app.hotkeys[keys]}) |
| 441 |
end |
| 442 |
else |
| 443 |
if type(keys) == "string" then |
| 444 |
print(string.format("\"%s\" does not exist in the \"ui.key_handlers\" table.", keys)) |
| 445 |
else |
| 446 |
print("Usage of removeKeyHandler() requires the first argument to be a string of a key that can be found in the \"ui.key_handlers\" table.") |
| 447 |
end |
| 448 |
end |
| 449 |
|
| 450 |
if temp_keys ~= nil then |
| 451 |
local has_enterOrPlus |
| 452 |
local temp_keys_copy = {} |
| 453 |
|
| 454 |
if type(temp_keys) == "table" then |
| 455 |
temp_keys_copy = shallow_clone(temp_keys) |
| 456 |
elseif type(temp_keys) == "string" then |
| 457 |
temp_keys_copy = shallow_clone({temp_keys}) |
| 458 |
end |
| 459 |
|
| 460 |
for _, v in pairs(temp_keys_copy) do |
| 461 |
if v == "enter" then |
| 462 |
has_enterOrPlus = true |
| 463 |
elseif v == "return" then |
| 464 |
has_enterOrPlus = true |
| 465 |
elseif v == "+" then |
| 466 |
has_enterOrPlus = true |
| 467 |
elseif v == "=" then |
| 468 |
has_enterOrPlus = true |
| 469 |
else |
| 470 |
has_enterOrPlus = false |
| 471 |
end |
| 472 |
end |
| 473 |
|
| 474 |
local key = table.remove(temp_keys, #temp_keys):lower() |
| 475 |
local modifiers = list_to_set(temp_keys) -- SET of modifiers |
| 476 |
if self.key_handlers[key] then |
| 477 |
for index, info in ipairs(self.key_handlers[key]) do |
| 478 |
if info.window == window and compare_tables(info.modifiers, modifiers) then |
| 479 |
table.remove(self.key_handlers[key], index) |
| 480 |
end |
| 481 |
end |
| 482 |
-- If last key handler was removed, delete the (now empty) list. |
| 483 |
if #self.key_handlers[key] == 0 then |
| 484 |
self.key_handlers[key] = nil |
| 485 |
end |
| 486 |
end |
| 487 |
|
| 488 |
-- If the handler added has enter, return, plus, or minus in it... |
| 489 |
if has_enterOrPlus then |
| 490 |
for k, _ in pairs(temp_keys_copy) do |
| 491 |
if temp_keys_copy[k] == "enter" then |
| 492 |
temp_keys_copy[k] = "return" |
| 493 |
elseif temp_keys_copy[k] == "return" then |
| 494 |
temp_keys_copy[k] = "enter" |
| 495 |
elseif temp_keys_copy[k] == "+" then |
| 496 |
temp_keys_copy[k] = "=" |
| 497 |
elseif temp_keys_copy[k] == "=" then |
| 498 |
temp_keys_copy[k] = "+" |
| 499 |
end |
| 500 |
end |
| 501 |
|
| 502 |
local key_02 = table.remove(temp_keys_copy, #temp_keys_copy):lower() |
| 503 |
local modifiers_02 = list_to_set(temp_keys_copy) -- SET of modifiers |
| 504 |
if self.key_handlers[key_02] then |
| 505 |
for index, info in ipairs(self.key_handlers[key_02]) do |
| 506 |
if info.window == window and compare_tables(info.modifiers, modifiers_02) then |
| 507 |
table.remove(self.key_handlers[key_02], index) |
| 508 |
end |
| 509 |
end |
| 510 |
-- If last key handler was removed, delete the (now empty) list. |
| 511 |
if #self.key_handlers[key_02] == 0 then |
| 512 |
self.key_handlers[key_02] = nil |
| 513 |
end |
| 514 |
end |
| 515 |
end |
| 516 |
end |
| 517 |
end |
| 518 |
|
| 519 |
--! Set the menu background image |
| 520 |
--! |
| 521 |
--! The menu size closest to, but no larger than the height of the currently |
| 522 |
--! set game window is selected. If no image fits that criteria the smallest |
| 523 |
--! available image is used. |
| 524 |
function UI:setMenuBackground() |
| 525 |
local screen_h = self.app.config.height |
| 526 |
local bg_size_idx = 1 |
| 527 |
|
| 528 |
-- Available mainmenu*.bmp sizes |
| 529 |
local menu_bg_sizes = { |
| 530 |
{640, 480}, |
| 531 |
{1280, 720}, |
| 532 |
{1920, 1080}, |
| 533 |
} |
| 534 |
|
| 535 |
for i, bg_size in ipairs(menu_bg_sizes) do |
| 536 |
if screen_h >= bg_size[2] then |
| 537 |
bg_size_idx = i |
| 538 |
else |
| 539 |
break |
| 540 |
end |
| 541 |
end |
| 542 |
|
| 543 |
local bg_size = menu_bg_sizes[bg_size_idx] |
| 544 |
self.background_width = bg_size[1] |
| 545 |
self.background_height = bg_size[2] |
| 546 |
self.background = self.app.gfx:loadRaw("mainmenu" .. bg_size[2], bg_size[1], bg_size[2], "Bitmap") |
| 547 |
end |
| 548 |
|
| 549 |
function UI:onChangeResolution() |
| 550 |
-- If we are in the main menu (== no world), reselect the background |
| 551 |
if not self.app.world then |
| 552 |
self:setMenuBackground() |
| 553 |
end |
| 554 |
-- Inform windows of resolution change |
| 555 |
if not self.windows then |
| 556 |
return |
| 557 |
end |
| 558 |
for _, window in ipairs(self.windows) do |
| 559 |
window:onChangeResolution() |
| 560 |
end |
| 561 |
self.app.audio:setSoundStage() |
| 562 |
end |
| 563 |
|
| 564 |
function UI:registerTextBox(box) |
| 565 |
self.textboxes[#self.textboxes + 1] = box |
| 566 |
end |
| 567 |
|
| 568 |
function UI:unregisterTextBox(box) |
| 569 |
for num, b in ipairs(self.textboxes) do |
| 570 |
if b == box then |
| 571 |
table.remove(self.textboxes, num) |
| 572 |
break |
| 573 |
end |
| 574 |
end |
| 575 |
end |
| 576 |
|
| 577 |
function UI:registerHotkeyBox(box) |
| 578 |
self.hotkeyboxes[#self.hotkeyboxes + 1] = box |
| 579 |
end |
| 580 |
|
| 581 |
function UI:unregisterHotkeyBox(box) |
| 582 |
for num, b in ipairs(self.hotkeyboxes) do |
| 583 |
if b == box then |
| 584 |
table.remove(self.hotkeyboxes, num) |
| 585 |
break |
| 586 |
end |
| 587 |
end |
| 588 |
end |
| 589 |
|
| 590 |
function UI:changeResolution(width, height) |
| 591 |
self.app:prepareVideoUpdate() |
| 592 |
local error_message = self.app.video:update( |
| 593 |
width, |
| 594 |
height, |
| 595 |
App.MIN_WINDOW_WIDTH * TheApp.config.ui_scale, |
| 596 |
App.MIN_WINDOW_HEIGHT * TheApp.config.ui_scale, |
| 597 |
unpack(self.app.modes)) |
| 598 |
self.app:finishVideoUpdate() |
| 599 |
|
| 600 |
if error_message then |
| 601 |
print("Warning: Could not change resolution to " .. width .. "x" .. height .. ".") |
| 602 |
print("The error was: ") |
| 603 |
print(error_message) |
| 604 |
return false |
| 605 |
end |
| 606 |
|
| 607 |
self.app.config.width = width |
| 608 |
self.app.config.height = height |
| 609 |
|
| 610 |
-- Redraw cursor |
| 611 |
local cursor = self.cursor |
| 612 |
self.cursor = nil |
| 613 |
self:setCursor(cursor) |
| 614 |
-- Save new setting in config |
| 615 |
self.app:saveConfig() |
| 616 |
|
| 617 |
self:onChangeResolution() |
| 618 |
|
| 619 |
return true |
| 620 |
end |
| 621 |
|
| 622 |
function UI:toggleCaptureMouse() |
| 623 |
self.app.capturemouse = not self.app.capturemouse |
| 624 |
self.app.video:setCaptureMouse(self.app.capturemouse) |
| 625 |
end |
| 626 |
|
| 627 |
function UI:setMouseReleased(released) |
| 628 |
if released == self.mouse_released then |
| 629 |
return |
| 630 |
end |
| 631 |
|
| 632 |
self.mouse_released = released |
| 633 |
|
| 634 |
-- If we are using a software cursor, show the hardware cursor on release |
| 635 |
-- and hide it again on capture. |
| 636 |
if self.cursor and not self.cursor.use then |
| 637 |
WM.showCursor(released) |
| 638 |
end |
| 639 |
|
| 640 |
self.app.video:setCaptureMouse(self.app.capturemouse and not self.app.mouse_released) |
| 641 |
end |
| 642 |
|
| 643 |
function UI:releaseMouse() |
| 644 |
self:setMouseReleased(true) |
| 645 |
end |
| 646 |
|
| 647 |
--! Dedicated hotkey function for toggling fullscreen |
| 648 |
function UI:fullscreenHotkey() |
| 649 |
local toggle = self:toggleFullscreen() |
| 650 |
if not toggle then |
| 651 |
local err = {_S.errors.unavailable_screen_size} |
| 652 |
self:addWindow(UIInformation(self, err)) |
| 653 |
end |
| 654 |
-- Update the Options window, if open |
| 655 |
local window = self:getWindow(UIOptions) |
| 656 |
if window then |
| 657 |
if toggle then window.fullscreen_button:toggle() end |
| 658 |
window.fullscreen_panel:setLabel(self.app.fullscreen and _S.options_window.option_on or _S.options_window.option_off) |
| 659 |
end |
| 660 |
end |
| 661 |
|
| 662 |
--! Turns fullscreen on and off |
| 663 |
--!return success true if toggle succeeded |
| 664 |
function UI:toggleFullscreen() |
| 665 |
local modes = self.app.modes |
| 666 |
|
| 667 |
local function toggleMode(index) |
| 668 |
self.app.fullscreen = not self.app.fullscreen |
| 669 |
if self.app.fullscreen then |
| 670 |
modes[index] = "fullscreen" |
| 671 |
else |
| 672 |
modes[index] = "" |
| 673 |
end |
| 674 |
end |
| 675 |
|
| 676 |
-- Search in modes table if it contains a fullscreen value and keep the index |
| 677 |
-- If not found, we will add an index at end of table |
| 678 |
local index = #modes + 1 |
| 679 |
for i=1, #modes do |
| 680 |
if modes[i] == "fullscreen" then |
| 681 |
index = i |
| 682 |
break |
| 683 |
end |
| 684 |
end |
| 685 |
|
| 686 |
-- Toggle Fullscreen mode |
| 687 |
toggleMode(index) |
| 688 |
|
| 689 |
local success = true |
| 690 |
self.app:prepareVideoUpdate() |
| 691 |
local error_message = self.app.video:update(self.app.config.width, self.app.config.height, |
| 692 |
self.app.MIN_WINDOW_WIDTH * self.app.config.ui_scale, |
| 693 |
self.app.MIN_WINDOW_HEIGHT * self.app.config.ui_scale, |
| 694 |
unpack(self.app.modes)) |
| 695 |
self.app:finishVideoUpdate() |
| 696 |
|
| 697 |
if error_message then |
| 698 |
success = false |
| 699 |
local mode_string = modes[index] or "windowed" |
| 700 |
print("Warning: Could not toggle to " .. mode_string .. " mode with resolution of " .. self.app.config.width .. "x" .. self.app.config.height .. ".") |
| 701 |
-- Revert fullscreen mode modifications |
| 702 |
toggleMode(index) |
| 703 |
end |
| 704 |
|
| 705 |
-- Redraw cursor |
| 706 |
local cursor = self.cursor |
| 707 |
self.cursor = nil |
| 708 |
self:setCursor(cursor) |
| 709 |
|
| 710 |
if success then |
| 711 |
-- Save new setting in config |
| 712 |
self.app.config.fullscreen = self.app.fullscreen |
| 713 |
self.app:saveConfig() |
| 714 |
end |
| 715 |
|
| 716 |
return success |
| 717 |
end |
| 718 |
|
| 719 |
--! Private function to determine the key pressed based on current modifiers |
| 720 |
--!param rawchar (string) The key pressed as given by SDL |
| 721 |
--!param modifiers (table) Current key modifiers, e.g. numlock |
| 722 |
--!param btn_maps (table) Used for keydown events only, a table of keys that |
| 723 |
-- are designated to perform mouse actions. |
| 724 |
--!return (string, string) Modified raw character, or false if remapped to a button. |
| 725 |
-- And lowercase or remapped key |
| 726 |
function UI:_determineKeyPressed(rawchar, modifiers, btn_maps) |
| 727 |
-- Apply key-remapping and normalisation |
| 728 |
rawchar = string.sub(rawchar,1,6) == "Keypad" and |
| 729 |
modifiers["numlockactive"] and string.sub(rawchar,8) or rawchar |
| 730 |
local key = rawchar:lower() |
| 731 |
if btn_maps and btn_maps[key] then |
| 732 |
-- Buttons remaps to mouse |
| 733 |
self:onMouseDown(btn_maps[key], self.cursor_x, self.cursor_y) |
| 734 |
return false |
| 735 |
end |
| 736 |
key = self.key_remaps[key] or key |
| 737 |
|
| 738 |
return rawchar, key |
| 739 |
end |
| 740 |
|
| 741 |
--! Called when the user presses a key on the keyboard |
| 742 |
--!param rawchar (string) The name of the key the user pressed. |
| 743 |
--!param modifiers (table) The current applied modifiers to key input e.g. numlock |
| 744 |
function UI:onKeyDown(rawchar, modifiers) |
| 745 |
local handled = false |
| 746 |
local rawchar_transformed, key = self:_determineKeyPressed( |
| 747 |
rawchar, modifiers, self.key_to_button_remaps) |
| 748 |
if not rawchar_transformed then return end |
| 749 |
|
| 750 |
-- Remove numlock modifier |
| 751 |
modifiers["numlockactive"] = nil |
| 752 |
-- If there is one, the current textbox gets the key. |
| 753 |
-- It will not process any text at this point though. |
| 754 |
for _, box in ipairs(self.textboxes) do |
| 755 |
if box.enabled and box.active and not handled then |
| 756 |
handled = box:keyInput(key, rawchar_transformed) |
| 757 |
end |
| 758 |
end |
| 759 |
|
| 760 |
-- If there is a hotkey box |
| 761 |
for _, hotkeybox in ipairs(self.hotkeyboxes) do |
| 762 |
if hotkeybox.enabled and hotkeybox.active and not handled then |
| 763 |
handled = hotkeybox:keyInput(key, rawchar_transformed, modifiers) |
| 764 |
end |
| 765 |
end |
| 766 |
|
| 767 |
-- Otherwise, if there is a key handler bound to the given key, then it gets |
| 768 |
-- the key. |
| 769 |
if not handled then |
| 770 |
local keyHandlers = self.key_handlers[key] |
| 771 |
if keyHandlers then |
| 772 |
-- Iterate over key handlers and call each one whose modifier(s) are pressed |
| 773 |
-- NB: Only if the exact correct modifiers are pressed will the shortcut get processed. |
| 774 |
for _, handler in ipairs(keyHandlers) do |
| 775 |
if compare_tables(handler.modifiers, modifiers) then |
| 776 |
handler.callback(handler.window, unpack(handler)) |
| 777 |
handled = true |
| 778 |
end |
| 779 |
end |
| 780 |
end |
| 781 |
end |
| 782 |
|
| 783 |
-- Store information about the key |
| 784 |
self.buttons_down[key] = true |
| 785 |
self.modifiers_down = modifiers |
| 786 |
self.key_press_handled = handled |
| 787 |
return handled |
| 788 |
end |
| 789 |
|
| 790 |
--! Called when the user releases a key on the keyboard |
| 791 |
--!param rawchar (string) The name of the key the user pressed. |
| 792 |
--!param modifiers (table) The current applied modifiers to key input e.g. numlock |
| 793 |
function UI:onKeyUp(rawchar, modifiers) |
| 794 |
local _, key = self:_determineKeyPressed(rawchar, modifiers) |
| 795 |
self.buttons_down[key] = nil |
| 796 |
|
| 797 |
-- Go through all the hotkeyboxes. |
| 798 |
for _, hotkeybox in ipairs(self.hotkeyboxes) do |
| 799 |
-- If one is enabled and active... |
| 800 |
if hotkeybox.enabled and hotkeybox.active then |
| 801 |
-- If the key lifted is escape... |
| 802 |
if(key == "escape") then |
| 803 |
hotkeybox:abort() |
| 804 |
hotkeybox.noted_keys = {} |
| 805 |
else |
| 806 |
-- Check if the current key lifted has already been noted. |
| 807 |
self.key_noted = false |
| 808 |
for _, v in pairs(hotkeybox.noted_keys) do |
| 809 |
if v == key then |
| 810 |
self.key_noted = true |
| 811 |
end |
| 812 |
end |
| 813 |
|
| 814 |
-- If the current key hasn't been noted... |
| 815 |
if self.key_noted == false then |
| 816 |
hotkeybox.noted_keys[#hotkeybox.noted_keys + 1] = key |
| 817 |
end |
| 818 |
|
| 819 |
-- Says if there is still a button being pressed. |
| 820 |
self.temp_button_down = false |
| 821 |
|
| 822 |
-- Go through and check if there are still any buttons pressed. If so... |
| 823 |
for _, _ in pairs(self.buttons_down) do |
| 824 |
-- Then toggle the corresponding bool. |
| 825 |
self.temp_button_down = true |
| 826 |
end |
| 827 |
|
| 828 |
--If there ISN'T still a button down when a button was released... |
| 829 |
if self.temp_button_down == false then |
| 830 |
-- Activate the confirm function on the hotkey box. |
| 831 |
hotkeybox:confirm() |
| 832 |
hotkeybox.noted_keys = {} |
| 833 |
end |
| 834 |
end |
| 835 |
end |
| 836 |
end |
| 837 |
|
| 838 |
-- Clean up |
| 839 |
self.modifiers_down = nil |
| 840 |
self.key_press_handled = nil |
| 841 |
end |
| 842 |
|
| 843 |
function UI:onEditingText(text, start, length) |
| 844 |
-- Does nothing at the moment. We are handling text input ourselves. |
| 845 |
end |
| 846 |
|
| 847 |
--! Called in-between onKeyDown and onKeyUp. The argument 'text' is a |
| 848 |
--! string containing the input localized according to the keyboard layout |
| 849 |
--! the user uses. |
| 850 |
function UI:onTextInput(text) |
| 851 |
-- It's time for any active textbox to get input. |
| 852 |
for _, box in ipairs(self.textboxes) do |
| 853 |
if box.enabled and box.active then |
| 854 |
box:textInput(text) |
| 855 |
end |
| 856 |
end |
| 857 |
|
| 858 |
-- Finally it might happen that a hotkey was not recognized because of |
| 859 |
-- differing local keyboard layout. Give it another shot. |
| 860 |
if not self.key_press_handled then |
| 861 |
local keyHandlers = self.key_handlers[text] |
| 862 |
if keyHandlers then |
| 863 |
-- Iterate over key handlers and call each one whose modifier(s) are pressed |
| 864 |
-- NB: Only if the exact correct modifiers are pressed will the shortcut get processed. |
| 865 |
for _, handler in ipairs(keyHandlers) do |
| 866 |
if compare_tables(handler.modifiers, self.modifiers_down) then |
| 867 |
handler.callback(handler.window, unpack(handler)) |
| 868 |
end |
| 869 |
end |
| 870 |
end |
| 871 |
end |
| 872 |
end |
| 873 |
|
| 874 |
function UI:onMouseDown(code, x, y) |
| 875 |
self:setMouseReleased(false) |
| 876 |
local repaint = false |
| 877 |
local button = self.button_codes[code] or code |
| 878 |
if self.app.moviePlayer.playing then |
| 879 |
if button == "left" then |
| 880 |
self.app.moviePlayer:stop() |
| 881 |
end |
| 882 |
return true |
| 883 |
end |
| 884 |
if self.cursor_entity == nil and self.down_count == 0 and |
| 885 |
self.cursor == self.default_cursor then |
| 886 |
self:setCursor(self.down_cursor) |
| 887 |
repaint = true |
| 888 |
end |
| 889 |
self.down_count = self.down_count + 1 |
| 890 |
if x >= 3 and y >= 3 and x < self.app.config.width - 3 and y < self.app.config.height - 3 then |
| 891 |
self.buttons_down["mouse_"..button] = true |
| 892 |
end |
| 893 |
|
| 894 |
self:updateTooltip() |
| 895 |
return Window.onMouseDown(self, button, x, y) or repaint |
| 896 |
end |
| 897 |
|
| 898 |
function UI:onMouseUp(code, x, y) |
| 899 |
local repaint = false |
| 900 |
local button = self.button_codes[code] or code |
| 901 |
self.down_count = self.down_count - 1 |
| 902 |
if self.down_count <= 0 then |
| 903 |
if self.cursor_entity == nil and self.cursor == self.down_cursor then |
| 904 |
self:setCursor(self.default_cursor) |
| 905 |
repaint = true |
| 906 |
end |
| 907 |
self.down_count = 0 |
| 908 |
end |
| 909 |
self.buttons_down["mouse_"..button] = nil |
| 910 |
|
| 911 |
if Window.onMouseUp(self, button, x, y) then |
| 912 |
repaint = true |
| 913 |
else |
| 914 |
if self:ableToClickEntity(self.cursor_entity) then |
| 915 |
self.cursor_entity:onClick(self, button) |
| 916 |
repaint = true |
| 917 |
end |
| 918 |
end |
| 919 |
|
| 920 |
self:updateTooltip() |
| 921 |
return repaint |
| 922 |
end |
| 923 |
|
| 924 |
function UI:onMouseWheel(x, y) |
| 925 |
Window.onMouseWheel(self, x, y) |
| 926 |
end |
| 927 |
|
| 928 |
--[[ Determines if a cursor entity can be clicked |
| 929 |
@param entity (Entity,nil) cursor entity clicked on if any |
| 930 |
@return true if can be clicked on, false otherwise (boolean) ]] |
| 931 |
function UI:ableToClickEntity(entity) |
| 932 |
if self.cursor_entity and self.cursor_entity.onClick then |
| 933 |
local hospital = entity.hospital |
| 934 |
local epidemic = hospital and hospital.epidemic |
| 935 |
|
| 936 |
return self.app.world.user_actions_allowed and not epidemic or |
| 937 |
(epidemic and not epidemic.vaccination_mode_active) |
| 938 |
else |
| 939 |
return false |
| 940 |
end |
| 941 |
end |
| 942 |
|
| 943 |
function UI:getScreenOffset() |
| 944 |
return self.screen_offset_x, self.screen_offset_y |
| 945 |
end |
| 946 |
|
| 947 |
local tooltip_ticks = 30 -- Amount of ticks until a tooltip is displayed |
| 948 |
|
| 949 |
function UI:updateTooltip() |
| 950 |
if self.buttons_down.mouse_left then |
| 951 |
-- Disable tooltips altogether while left button is pressed. |
| 952 |
self.tooltip = nil |
| 953 |
self.tooltip_counter = nil |
| 954 |
return |
| 955 |
elseif self.tooltip_counter == nil then |
| 956 |
self.tooltip_counter = tooltip_ticks |
| 957 |
end |
| 958 |
local tooltip = self:getTooltipAt(self.cursor_x, self.cursor_y) |
| 959 |
if tooltip then |
| 960 |
-- NB: Do not set counter if tooltip changes here. This allows quick tooltip reading of adjacent buttons. |
| 961 |
self.tooltip = tooltip |
| 962 |
else |
| 963 |
-- Not hovering over any button with tooltip -> reset |
| 964 |
self.tooltip = nil |
| 965 |
self.tooltip_counter = tooltip_ticks |
| 966 |
end |
| 967 |
end |
| 968 |
|
| 969 |
local UpdateCursorPosition = TH.cursor.setPosition |
| 970 |
|
| 971 |
--! Called when focus changes on game window. |
| 972 |
--!param gain (number) 1 for in-focus, 0 for out-of-focus |
| 973 |
function UI:onWindowActive(gain) |
| 974 |
self:setWindowActiveStatus(gain == 1) |
| 975 |
end |
| 976 |
|
| 977 |
--! Stores the game window active status |
| 978 |
--!param state (boolean) true for in-focus, false for out-of-focus |
| 979 |
function UI:setWindowActiveStatus(state) |
| 980 |
self.app.window_active_status = state |
| 981 |
end |
| 982 |
|
| 983 |
--! Gets the game window active status |
| 984 |
--!return true for in-focus, false for out-of-focus |
| 985 |
function UI:getWindowActiveStatus() |
| 986 |
return self.app.window_active_status |
| 987 |
end |
| 988 |
|
| 989 |
--! Window has been resized by the user |
| 990 |
--!param width (integer) New window width |
| 991 |
--!param height (integer) New window height |
| 992 |
function UI:onWindowResize(width, height) |
| 993 |
if not self.app.config.fullscreen then |
| 994 |
self:changeResolution(width, height) |
| 995 |
end |
| 996 |
end |
| 997 |
|
| 998 |
function UI:onMouseMove(x, y, dx, dy) |
| 999 |
if self.mouse_released then |
| 1000 |
return false |
| 1001 |
end |
| 1002 |
|
| 1003 |
local repaint = UpdateCursorPosition(self.app.video, x, y) |
| 1004 |
|
| 1005 |
self.cursor_x = x |
| 1006 |
self.cursor_y = y |
| 1007 |
|
| 1008 |
if self.drag_mouse_move then |
| 1009 |
self.drag_mouse_move(x, y) |
| 1010 |
return true |
| 1011 |
end |
| 1012 |
|
| 1013 |
if Window.onMouseMove(self, x, y, dx, dy) then |
| 1014 |
repaint = true |
| 1015 |
end |
| 1016 |
|
| 1017 |
self:updateTooltip() |
| 1018 |
|
| 1019 |
return repaint |
| 1020 |
end |
| 1021 |
|
| 1022 |
--! Process SDL_MULTIGESTURE events. |
| 1023 |
--! |
| 1024 |
--!return (boolean) event processed indicator |
| 1025 |
function UI:onMultiGesture() |
| 1026 |
return false |
| 1027 |
end |
| 1028 |
|
| 1029 |
function UI:onTick() |
| 1030 |
Window.onTick(self) |
| 1031 |
local repaint = false |
| 1032 |
if self.tooltip_counter and self.tooltip_counter > 0 then |
| 1033 |
self.tooltip_counter = self.tooltip_counter - 1 |
| 1034 |
repaint = (self.tooltip_counter == 0) |
| 1035 |
end |
| 1036 |
-- If a tooltip is currently shown, update each tick (may be dynamic) |
| 1037 |
if self.tooltip then |
| 1038 |
self:updateTooltip() |
| 1039 |
end |
| 1040 |
return repaint |
| 1041 |
end |
| 1042 |
|
| 1043 |
|
| 1044 |
function UI:addWindow(window) |
| 1045 |
if window.closed then |
| 1046 |
return |
| 1047 |
end |
| 1048 |
if window.modal_class then |
| 1049 |
-- NB: while instead of if in case of another window being created during the close function |
| 1050 |
while self.modal_windows[window.modal_class] do |
| 1051 |
self.modal_windows[window.modal_class]:close() |
| 1052 |
end |
| 1053 |
self.modal_windows[window.modal_class] = window |
| 1054 |
end |
| 1055 |
if window.modal_class == "main" or window.modal_class == "fullscreen" then |
| 1056 |
self.editing_allowed = false -- do not allow editing rooms if main windows (build, furnish, hire) are open |
| 1057 |
end |
| 1058 |
Window.addWindow(self, window) |
| 1059 |
self:_onAddWindow(window) |
| 1060 |
end |
| 1061 |
|
| 1062 |
function UI:removeWindow(closing_window) |
| 1063 |
if Window.removeWindow(self, closing_window) then |
| 1064 |
local class = closing_window.modal_class |
| 1065 |
if class and self.modal_windows[class] == closing_window then |
| 1066 |
self.modal_windows[class] = nil |
| 1067 |
end |
| 1068 |
if closing_window.modal_class == "main" or closing_window.modal_class == "fullscreen" then |
| 1069 |
self.editing_allowed = true -- allow editing rooms again when main window is closed |
| 1070 |
end |
| 1071 |
self:_onRemoveWindow(closing_window) |
| 1072 |
return true |
| 1073 |
else |
| 1074 |
return false |
| 1075 |
end |
| 1076 |
end |
| 1077 |
|
| 1078 |
-- Function for signaling that the window is added. |
| 1079 |
-- To pause game if the necessary requirements are met. |
| 1080 |
--!param window (object) window added |
| 1081 |
function UI:_onAddWindow(window) |
| 1082 |
if self.app.world and window:mustPause() then |
| 1083 |
self.app.world:mustPauseWindowAdd() |
| 1084 |
end |
| 1085 |
end |
| 1086 |
|
| 1087 |
-- Function for signaling that the window is to be removed. |
| 1088 |
-- To unpause game if the necessary requirements are met. |
| 1089 |
--!param window (object) window to be removed |
| 1090 |
function UI:_onRemoveWindow(window) |
| 1091 |
if self.app.world and window:mustPause() then |
| 1092 |
self.app.world:mustPauseWindowRemoved() |
| 1093 |
end |
| 1094 |
end |
| 1095 |
|
| 1096 |
--! Function to check if we have any must pause windows open |
| 1097 |
--!return (bool) Returns true if a must pause window is found |
| 1098 |
function UI:anyMustPauseWindowOpen() |
| 1099 |
for _, window in pairs(self.windows) do |
| 1100 |
if window:mustPause() then return true end |
| 1101 |
end |
| 1102 |
return false |
| 1103 |
end |
| 1104 |
|
| 1105 |
function UI:getCursorPosition(window) |
| 1106 |
-- Given no argument, returns the cursor position in screen space |
| 1107 |
-- Otherwise, returns the cursor position in the space of the given window |
| 1108 |
local x, y = self.cursor_x, self.cursor_y |
| 1109 |
while window ~= nil and window ~= self do |
| 1110 |
x = x - window.x |
| 1111 |
y = y - window.y |
| 1112 |
window = window.parent |
| 1113 |
end |
| 1114 |
return x, y |
| 1115 |
end |
| 1116 |
|
| 1117 |
function UI:addOrRemoveDebugModeKeyHandlers() |
| 1118 |
self:removeKeyHandler("global_showLuaConsole", self) |
| 1119 |
self:removeKeyHandler("global_runDebugScript", self) |
| 1120 |
if self.app.config.debug then |
| 1121 |
self:addKeyHandler("global_showLuaConsole", self, self.showLuaConsole) |
| 1122 |
self:addKeyHandler("global_runDebugScript", self, self.runDebugScript) |
| 1123 |
end |
| 1124 |
end |
| 1125 |
|
| 1126 |
function UI:afterLoad(old, new) |
| 1127 |
-- Get rid of old key handlers from save file. |
| 1128 |
self.key_handlers = {} |
| 1129 |
if old < 5 then |
| 1130 |
self.editing_allowed = true |
| 1131 |
end |
| 1132 |
if old < 179 then |
| 1133 |
if self.app.good_install_folder and not self.app.using_demo_files then |
| 1134 |
local gfx = self.app.gfx |
| 1135 |
gfx.cache.raw = {} |
| 1136 |
gfx.cache.tabled = {} |
| 1137 |
gfx.cache.language_fonts = {} |
| 1138 |
gfx.builtin_font = nil |
| 1139 |
end |
| 1140 |
end |
| 1141 |
if old < 236 then |
| 1142 |
local gfx = self.app.gfx |
| 1143 |
local palette = gfx:getPalette("Pref01V.pal") |
| 1144 |
self.tooltip_font = gfx:loadFontAndSpriteTable("QData", "Font00V", false, palette, { apply_ui_scale = true }) |
| 1145 |
end |
| 1146 |
|
| 1147 |
self:setupGlobalKeyHandlers() |
| 1148 |
|
| 1149 |
-- Cancel any saved screen movement from edge scrolling |
| 1150 |
self.tick_scroll_amount_mouse = nil |
| 1151 |
|
| 1152 |
Window.afterLoad(self, old, new) |
| 1153 |
end |
| 1154 |
|
| 1155 |
-- Stub to allow the function to be called in e.g. the information |
| 1156 |
-- dialog without having to worry about a GameUI being present |
| 1157 |
function UI:tutorialStep(...) |
| 1158 |
end |
| 1159 |
|
| 1160 |
--! Perform the Screenshot action (usually by key bind 'global_screenshot') |
| 1161 |
function UI:makeScreenshot() |
| 1162 |
-- Generate filename |
| 1163 |
local timestamp = os.date("%Y%m%d-%H%M%S") |
| 1164 |
local filename = TheApp.screenshot_dir .. ("Screenshot_%s.png"):format(timestamp) |
| 1165 |
|
| 1166 |
-- It's very unlikely you intentionally want multiple screenshots a second |
| 1167 |
if lfs.attributes(filename, "size") ~= nil then |
| 1168 |
print("Screenshot failed: File already exists") |
| 1169 |
return |
| 1170 |
end |
| 1171 |
|
| 1172 |
-- Take screenshot |
| 1173 |
local res, err = self.app.video:takeScreenshot(filename) |
| 1174 |
if not res then |
| 1175 |
print("Screenshot failed: " .. err) |
| 1176 |
else |
| 1177 |
self.app.audio:playSound("SNAPSHOT.WAV") |
| 1178 |
end |
| 1179 |
end |
| 1180 |
|
| 1181 |
--! Closes one window (the topmost / active window, if possible) |
| 1182 |
--!return true if a window was closed |
| 1183 |
function UI:closeWindow() |
| 1184 |
if not self.windows then |
| 1185 |
return false |
| 1186 |
end |
| 1187 |
|
| 1188 |
-- Stop the lose message being closed prematurely because we pressed "Escape" on the lose movie |
| 1189 |
if self.app.moviePlayer.playing then |
| 1190 |
return false |
| 1191 |
end |
| 1192 |
|
| 1193 |
-- Close the topmost window first |
| 1194 |
local first = self.windows[1] |
| 1195 |
if first.on_top and first.esc_closes then |
| 1196 |
first:close() |
| 1197 |
return true |
| 1198 |
end |
| 1199 |
for i = #self.windows, 1, -1 do |
| 1200 |
local window = self.windows[i] |
| 1201 |
if window.esc_closes then |
| 1202 |
window:close() |
| 1203 |
return true |
| 1204 |
end |
| 1205 |
end |
| 1206 |
end |
| 1207 |
|
| 1208 |
--! Shows the Lua console |
| 1209 |
function UI:showLuaConsole() |
| 1210 |
self:addWindow(UILuaConsole(self)) |
| 1211 |
end |
| 1212 |
|
| 1213 |
--! Triggers reset of the application (reloads .lua files) |
| 1214 |
function UI:resetApp() |
| 1215 |
self.app:reset() |
| 1216 |
end |
| 1217 |
|
| 1218 |
-- Added this function as quit does not exit the application, it only exits the game to the menu screen |
| 1219 |
function UI:exitApplication() |
| 1220 |
self.app:abandon() |
| 1221 |
end |
| 1222 |
|
| 1223 |
--! Triggers quitting the application |
| 1224 |
function UI:quit() |
| 1225 |
self.app:exit() |
| 1226 |
end |
| 1227 |
|
| 1228 |
--! Tries to stop a video, if one is currently playing |
| 1229 |
function UI:stopMovie() |
| 1230 |
if self.app.moviePlayer.playing then |
| 1231 |
self.app.moviePlayer:stop() |
| 1232 |
end |
| 1233 |
end |
| 1234 |
|
| 1235 |
function UI:pauseMovie() |
| 1236 |
if self.app.moviePlayer.playing then |
| 1237 |
self.app.moviePlayer:togglePause() |
| 1238 |
end |
| 1239 |
end |
| 1240 |
|
| 1241 |
-- Stub for compatibility with savegames r1896-1921 |
| 1242 |
function UI:stopVideo() end |