| 1 |
--[[ Copyright (c) 2010 Manuel "Roujin" Wolf |
| 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("ui") |
| 22 |
corsixth.require("announcer") |
| 23 |
|
| 24 |
--! Variant of UI for running games |
| 25 |
class "GameUI" (UI) |
| 26 |
|
| 27 |
---@type GameUI |
| 28 |
local GameUI = _G["GameUI"] |
| 29 |
|
| 30 |
local TH = require("TH") |
| 31 |
|
| 32 |
local Announcer = _G["Announcer"] |
| 33 |
|
| 34 |
-- The maximum distance to shake the screen from the origin during an |
| 35 |
-- earthquake with full intensity. |
| 36 |
local shake_screen_max_movement = 50 --pixels |
| 37 |
|
| 38 |
-- 0.002 is about 5 pixels on a 1920 pixel display |
| 39 |
local multigesture_pinch_sensitivity_factor = 0.002 |
| 40 |
-- combined with the above, multiplying by 100 means minimum current_momentum.z for any detected pinch |
| 41 |
-- will result in a call to adjustZoom in the onTick method |
| 42 |
local multigesture_pinch_amplification_factor = 100 |
| 43 |
|
| 44 |
-- Speed of scrolling when using keys. Pixels / tick (18ms) |
| 45 |
-- This scroll speed is further adjusted by the configured scroll_speed |
| 46 |
local key_scroll_speed = 10 |
| 47 |
|
| 48 |
--! Game UI constructor. |
| 49 |
--!param app (Application) Application object. |
| 50 |
--!param local_hospital Hospital to display |
| 51 |
--!param map_editor (bool) Whether the map is editable. |
| 52 |
function GameUI:GameUI(app, local_hospital, map_editor) |
| 53 |
self:UI(app) |
| 54 |
self.app = app |
| 55 |
|
| 56 |
self.hospital = local_hospital |
| 57 |
self.tutorial = { chapter = 0, phase = 0 } |
| 58 |
if map_editor then |
| 59 |
self.map_editor = UIMapEditor(self) |
| 60 |
self:addWindow(self.map_editor) |
| 61 |
else |
| 62 |
self.adviser = UIAdviser(self) |
| 63 |
self.bottom_panel = UIBottomPanel(self) |
| 64 |
self.bottom_panel:addWindow(self.adviser) |
| 65 |
self:addWindow(self.bottom_panel) |
| 66 |
end |
| 67 |
|
| 68 |
-- UI widgets |
| 69 |
self.menu_bar = UIMenuBar(self, self.map_editor) |
| 70 |
self:addWindow(self.menu_bar) |
| 71 |
self.subtitles = Subtitles(self) |
| 72 |
self:addWindow(self.subtitles) |
| 73 |
|
| 74 |
local scr_w = app.config.width |
| 75 |
local scr_h = app.config.height |
| 76 |
self.visible_diamond = self:makeVisibleDiamond(scr_w, scr_h) |
| 77 |
if self.visible_diamond.w <= 0 or self.visible_diamond.h <= 0 then |
| 78 |
-- For a standard 128x128 map, screen size would have to be in the |
| 79 |
-- region of 3276x2457 in order to be too large. |
| 80 |
if not self.map_editor then |
| 81 |
error("Screen size too large for the map. Adjust your resolution in Settings.") |
| 82 |
end |
| 83 |
end |
| 84 |
self.screen_offset_x, self.screen_offset_y = app.map:WorldToScreen( |
| 85 |
app.map.th:getCameraTile(local_hospital:getPlayerIndex())) |
| 86 |
self.zoom_factor = 1 |
| 87 |
self:scrollMap(-scr_w / 2, 16 - scr_h / 2) |
| 88 |
self.limit_to_visible_diamond = not self.map_editor |
| 89 |
self.transparent_walls = false |
| 90 |
self.do_world_hit_test = true |
| 91 |
|
| 92 |
self.momentum = app.config.scrolling_momentum |
| 93 |
self.current_momentum = {x = 0.0, y = 0.0, z = 0.0} |
| 94 |
self.multigesturemove = {x = 0.0, y = 0.0} |
| 95 |
|
| 96 |
self.recallpositions = {} |
| 97 |
|
| 98 |
self.speed_up_key_pressed = false |
| 99 |
self.last_hovered_entity = nil |
| 100 |
|
| 101 |
-- The currently specified intensity value for earthquakes. To abstract |
| 102 |
-- the effect from the implementation this value is a number between 0 |
| 103 |
-- and 1. |
| 104 |
self.shake_screen_intensity = 0 |
| 105 |
|
| 106 |
self.announcer = Announcer(app) |
| 107 |
self.app:setCaptureMouse() |
| 108 |
end |
| 109 |
|
| 110 |
function GameUI:setupGlobalKeyHandlers() |
| 111 |
UI.setupGlobalKeyHandlers(self) |
| 112 |
|
| 113 |
-- Set the scrolling keys. |
| 114 |
self.scroll_keys = { |
| 115 |
[tostring(self.app.hotkeys["ingame_scroll_up"])] = {x = 0, y = -key_scroll_speed}, |
| 116 |
[tostring(self.app.hotkeys["ingame_scroll_down"])] = {x = 0, y = key_scroll_speed}, |
| 117 |
[tostring(self.app.hotkeys["ingame_scroll_left"])] = {x = -key_scroll_speed, y = 0}, |
| 118 |
[tostring(self.app.hotkeys["ingame_scroll_right"])] = {x = key_scroll_speed, y = 0}, |
| 119 |
} |
| 120 |
|
| 121 |
-- This is the long version of the shift speed key. |
| 122 |
-- i.e. if the "ingame_scroll_shift" key is "ctrl", then it will give us |
| 123 |
-- "left ctrl" and "right ctrl" for reference against the rawchar in |
| 124 |
-- "onKeyDown()" and "onKeyUp()" |
| 125 |
self.shift_scroll_key_long = {} |
| 126 |
self.shift_scroll_speed_pressed = false |
| 127 |
local temp_table = {} |
| 128 |
local shift_scroll_key_index = 1 |
| 129 |
if type(self.app.hotkeys["ingame_scroll_shift"]) == "string" then |
| 130 |
temp_table = {self.app.hotkeys["ingame_scroll_shift"]} |
| 131 |
elseif type(self.app.hotkeys["ingame_scroll_shift"]) == "table" then |
| 132 |
temp_table = shallow_clone(self.app.hotkeys["ingame_scroll_shift"]) |
| 133 |
end |
| 134 |
-- Go through the "ingame_scroll_shift" key table and see if it has any modifier names. |
| 135 |
for _, v in pairs (temp_table) do |
| 136 |
-- If it does then add long name version of them into the long key table. |
| 137 |
if v == "ctrl" then |
| 138 |
self.shift_scroll_key_long[shift_scroll_key_index] = "left ctrl" |
| 139 |
shift_scroll_key_index = shift_scroll_key_index + 1 |
| 140 |
self.shift_scroll_key_long[shift_scroll_key_index] = "right ctrl" |
| 141 |
shift_scroll_key_index = shift_scroll_key_index + 1 |
| 142 |
elseif v == "alt" then |
| 143 |
self.shift_scroll_key_long[shift_scroll_key_index] = "left alt" |
| 144 |
shift_scroll_key_index = shift_scroll_key_index + 1 |
| 145 |
self.shift_scroll_key_long[shift_scroll_key_index] = "right alt" |
| 146 |
shift_scroll_key_index = shift_scroll_key_index + 1 |
| 147 |
elseif v == "shift" then |
| 148 |
self.shift_scroll_key_long[shift_scroll_key_index] = "left shift" |
| 149 |
shift_scroll_key_index = shift_scroll_key_index + 1 |
| 150 |
self.shift_scroll_key_long[shift_scroll_key_index] = "right shift" |
| 151 |
shift_scroll_key_index = shift_scroll_key_index + 1 |
| 152 |
end |
| 153 |
end |
| 154 |
|
| 155 |
self:addKeyHandler("global_window_close", self, self.setEditRoom, false) |
| 156 |
self:addKeyHandler("ingame_showmenubar", self, self.showMenuBar) |
| 157 |
self:addKeyHandler("ingame_gamespeed_speedup", self, self.keySpeedUp) |
| 158 |
self:addKeyHandler("ingame_setTransparent", self, self.keyTransparent) |
| 159 |
self:addKeyHandler("ingame_toggleTransparent", self, self.toggleTransparent) |
| 160 |
self:addKeyHandler("ingame_toggleAdvisor", self, self.toggleAdviser) |
| 161 |
self:addKeyHandler("ingame_poopLog", self.app.world, self.app.world.dumpGameLog) |
| 162 |
self:addKeyHandler("ingame_poopStrings", self.app, self.app.dumpStrings) |
| 163 |
self:addKeyHandler("ingame_toggleAnnouncements", self, self.togglePlayAnnouncements) |
| 164 |
self:addKeyHandler("ingame_toggleSounds", self, self.togglePlaySounds) |
| 165 |
self:addKeyHandler("ingame_toggleMusic", self, self.togglePlayMusic) |
| 166 |
|
| 167 |
-- scroll to map position |
| 168 |
for i = 0, 9 do |
| 169 |
-- set camera view |
| 170 |
self:addKeyHandler(string.format("ingame_storePosition_%d", i), self, self.setMapRecallPosition, i) |
| 171 |
-- recall camera view |
| 172 |
self:addKeyHandler(string.format("ingame_recallPosition_%d", i), self, self.recallMapPosition, i) |
| 173 |
end |
| 174 |
|
| 175 |
if self.app.config.debug and self.app.world.map.level_number ~= "MAP EDITOR" then |
| 176 |
self:addKeyHandler("ingame_showCheatWindow", self, self.showCheatsWindow) |
| 177 |
end |
| 178 |
end |
| 179 |
|
| 180 |
function GameUI:makeVisibleDiamond(scr_w, scr_h) |
| 181 |
local map_w = self.app.map.width |
| 182 |
local map_h = self.app.map.height |
| 183 |
assert(map_w == map_h, "UI limiter requires square map") |
| 184 |
|
| 185 |
-- The visible diamond is the region which the top-left corner of the screen |
| 186 |
-- is limited to, and ensures that the map always covers all of the screen. |
| 187 |
-- Its vertices are at (x + w, y), (x - w, y), (x, y + h), (x, y - h). |
| 188 |
return { |
| 189 |
x = - scr_w / 2, |
| 190 |
y = 16 * map_h - scr_h / 2, |
| 191 |
w = 32 * map_h - scr_h - scr_w / 2, |
| 192 |
h = 16 * map_h - scr_h / 2 - scr_w / 4, |
| 193 |
} |
| 194 |
end |
| 195 |
|
| 196 |
--! Calculate the minimum valid zoom value |
| 197 |
--! |
| 198 |
--! Zooming out too much would cause negative width/height to be returned from |
| 199 |
--! makeVisibleDiamond. This function calculates the minimum zoom_factor that |
| 200 |
--! would be allowed. |
| 201 |
function GameUI:calculateMinimumZoom() |
| 202 |
local scr_w = self.app.config.width |
| 203 |
local scr_h = self.app.config.height |
| 204 |
local map_h = self.app.map.height |
| 205 |
|
| 206 |
-- Minimum width: 0 = 32 * map_h - (scr_h/factor) - (scr_w/factor) / 2, |
| 207 |
-- Minimum height: 0 = 16 * map_h - (scr_h/factor) / 2 - (scr_w/factor) / 4 |
| 208 |
-- Both rearrange to: |
| 209 |
local factor = (scr_w + 2 * scr_h) / (64 * map_h) |
| 210 |
|
| 211 |
-- Due to precision issues a tolerance is needed otherwise setZoom might fail |
| 212 |
factor = factor + 0.001 |
| 213 |
|
| 214 |
return factor |
| 215 |
end |
| 216 |
|
| 217 |
function GameUI:setZoom(factor) |
| 218 |
if factor <= 0 then |
| 219 |
return false |
| 220 |
end |
| 221 |
if not factor or math.abs(factor - 1) < 0.001 then |
| 222 |
factor = 1 |
| 223 |
end |
| 224 |
|
| 225 |
local scr_w = self.app.config.width |
| 226 |
local scr_h = self.app.config.height |
| 227 |
local new_diamond = self:makeVisibleDiamond(scr_w / factor, scr_h / factor) |
| 228 |
if new_diamond.w < 0 or new_diamond.h < 0 then |
| 229 |
return false |
| 230 |
end |
| 231 |
|
| 232 |
self.visible_diamond = new_diamond |
| 233 |
local refx, refy = self.cursor_x or scr_w / 2, self.cursor_y or scr_h / 2 |
| 234 |
local cx, cy = self:ScreenToWorld(refx, refy) |
| 235 |
self.zoom_factor = factor |
| 236 |
|
| 237 |
cx, cy = self.app.map:WorldToScreen(cx, cy) |
| 238 |
cx = cx - self.screen_offset_x - refx / factor |
| 239 |
cy = cy - self.screen_offset_y - refy / factor |
| 240 |
self:scrollMap(cx, cy) |
| 241 |
return true |
| 242 |
end |
| 243 |
|
| 244 |
function GameUI:draw(canvas) |
| 245 |
local app = self.app |
| 246 |
local config = app.config |
| 247 |
if self.map_editor or not self.in_visible_diamond then |
| 248 |
canvas:fillBlack() |
| 249 |
end |
| 250 |
local zoom = self.zoom_factor |
| 251 |
local dx = self.screen_offset_x + |
| 252 |
math.floor((0.5 - math.random()) * self.shake_screen_intensity * shake_screen_max_movement * 2) |
| 253 |
local dy = self.screen_offset_y + |
| 254 |
math.floor((0.5 - math.random()) * self.shake_screen_intensity * shake_screen_max_movement * 2) |
| 255 |
if canvas:scale(zoom) then |
| 256 |
app.map:draw(canvas, dx, dy, math.ceil(config.width / zoom), math.ceil(config.height / zoom), 0, 0) |
| 257 |
canvas:scale(1) |
| 258 |
else |
| 259 |
self:setZoom(1) |
| 260 |
app.map:draw(canvas, dx, dy, config.width, config.height, 0, 0) |
| 261 |
end |
| 262 |
Window.draw(self, canvas, 0, 0) -- NB: not calling UI.draw on purpose |
| 263 |
self:drawTooltip(canvas) |
| 264 |
if self.simulated_cursor then |
| 265 |
self.simulated_cursor.draw(canvas, self.cursor_x, self.cursor_y) |
| 266 |
end |
| 267 |
end |
| 268 |
|
| 269 |
function GameUI:onChangeResolution() |
| 270 |
-- Calculate and enforce minimum zoom |
| 271 |
local minimum_zoom = self:calculateMinimumZoom() |
| 272 |
if self.zoom_factor < minimum_zoom then |
| 273 |
self:setZoom(minimum_zoom) |
| 274 |
end |
| 275 |
-- Recalculate scrolling bounds |
| 276 |
local scr_w = self.app.config.width |
| 277 |
local scr_h = self.app.config.height |
| 278 |
self.visible_diamond = self:makeVisibleDiamond(scr_w / self.zoom_factor, scr_h / self.zoom_factor) |
| 279 |
self:scrollMap(0, 0) |
| 280 |
|
| 281 |
UI.onChangeResolution(self) |
| 282 |
end |
| 283 |
|
| 284 |
--! Update UI state after the UI has been depersisted |
| 285 |
--! When an UI object is depersisted, its state will reflect how the UI was at |
| 286 |
-- the moment of persistence, which may be different to the keyboard / mouse |
| 287 |
-- state at the moment of depersistence. |
| 288 |
--!param ui (UI) The previously existing UI object, from which values should be |
| 289 |
-- taken. |
| 290 |
function GameUI:resync(ui) |
| 291 |
if self.drag_mouse_move then |
| 292 |
-- Check that a window is actually being dragged. If none is found, then |
| 293 |
-- remove the drag handler. |
| 294 |
local something_being_dragged = false |
| 295 |
for _, window in ipairs(self.windows) do |
| 296 |
if window.dragging then |
| 297 |
something_being_dragged = true |
| 298 |
break |
| 299 |
end |
| 300 |
end |
| 301 |
if not something_being_dragged then |
| 302 |
self.drag_mouse_move = nil |
| 303 |
end |
| 304 |
end |
| 305 |
self.tick_scroll_amount = ui.tick_scroll_amount |
| 306 |
self.down_count = ui.down_count |
| 307 |
if ui.limit_to_visible_diamond ~= nil then |
| 308 |
self.limit_to_visible_diamond = ui.limit_to_visible_diamond |
| 309 |
end |
| 310 |
|
| 311 |
self.key_remaps = ui.key_remaps |
| 312 |
self.key_to_button_remaps = ui.key_to_button_remaps |
| 313 |
end |
| 314 |
|
| 315 |
function GameUI:updateKeyScroll() |
| 316 |
local dx, dy = 0, 0 |
| 317 |
for key, scr in pairs(self.scroll_keys) do |
| 318 |
if self.buttons_down[key] then |
| 319 |
dx = dx + scr.x |
| 320 |
dy = dy + scr.y |
| 321 |
end |
| 322 |
end |
| 323 |
--If there is any movement on the x or y axis... |
| 324 |
if dx ~= 0 or dy ~= 0 then |
| 325 |
--Get the length of the scrolling vector. |
| 326 |
local mag = (dx^2 + dy^2) ^ 0.5 |
| 327 |
--Then normalize the scrolling vector, after which multiply it by the scroll speed variable. |
| 328 |
dx = (dx / mag) * key_scroll_speed |
| 329 |
dy = (dy / mag) * key_scroll_speed |
| 330 |
-- Set the scroll amount to be used. |
| 331 |
self.tick_scroll_amount = {x = dx, y = dy} |
| 332 |
return true |
| 333 |
else |
| 334 |
self.tick_scroll_amount = false |
| 335 |
return false |
| 336 |
end |
| 337 |
end |
| 338 |
|
| 339 |
function GameUI:keySpeedUp() |
| 340 |
self.speed_up_key_pressed = true |
| 341 |
self.app.world:speedUp() |
| 342 |
end |
| 343 |
|
| 344 |
function GameUI:keyTransparent() |
| 345 |
self:setWallsTransparent(true) |
| 346 |
end |
| 347 |
|
| 348 |
function GameUI:toggleTransparent() |
| 349 |
self.toggled_transparency = not self.toggled_transparency |
| 350 |
self:setWallsTransparent(self.toggled_transparency) |
| 351 |
end |
| 352 |
|
| 353 |
function GameUI:onKeyDown(rawchar, modifiers, is_repeat) |
| 354 |
if UI.onKeyDown(self, rawchar, modifiers, is_repeat) then |
| 355 |
-- Key has been handled already |
| 356 |
return true |
| 357 |
end |
| 358 |
local key = rawchar:lower() |
| 359 |
-- If key is shift speed key... |
| 360 |
for _, v in pairs(self.shift_scroll_key_long) do |
| 361 |
if v == key then |
| 362 |
self.shift_scroll_speed_pressed = true |
| 363 |
end |
| 364 |
end |
| 365 |
if self.scroll_keys[key] then |
| 366 |
self:updateKeyScroll() |
| 367 |
return |
| 368 |
end |
| 369 |
end |
| 370 |
|
| 371 |
function GameUI:onKeyUp(rawchar) |
| 372 |
if UI.onKeyUp(self, rawchar) then |
| 373 |
return true |
| 374 |
end |
| 375 |
|
| 376 |
local key = rawchar:lower() |
| 377 |
for _, v in pairs(self.shift_scroll_key_long) do |
| 378 |
if v == key then |
| 379 |
self.shift_scroll_speed_pressed = false |
| 380 |
end |
| 381 |
end |
| 382 |
if self.scroll_keys[key] then |
| 383 |
self:updateKeyScroll() |
| 384 |
return |
| 385 |
end |
| 386 |
|
| 387 |
-- Guess that the "Speed Up" key was released because the |
| 388 |
-- code parameter can't provide UTF-8 key codes: |
| 389 |
self.speed_up_key_pressed = false |
| 390 |
if self.app.world:isCurrentSpeed("Speed Up") then |
| 391 |
self.app.world:previousSpeed() |
| 392 |
end |
| 393 |
|
| 394 |
if not self.toggled_transparency then self:setWallsTransparent(false) end |
| 395 |
end |
| 396 |
|
| 397 |
function GameUI:makeDebugFax() |
| 398 |
local message = { |
| 399 |
{text = "debug fax"}, -- no translation needed imo |
| 400 |
choices = {{text = "close debug fax", choice = "close"}}, |
| 401 |
} |
| 402 |
-- Don't use "strike" type here, as these open a different window and must have an owner |
| 403 |
local types = {"emergency", "epidemy", "personality", "information", "disease", "report"} |
| 404 |
self.bottom_panel:queueMessage(types[math.random(1, #types)], message) |
| 405 |
end |
| 406 |
|
| 407 |
function GameUI:ScreenToWorld(x, y) |
| 408 |
local zoom = self.zoom_factor |
| 409 |
return self.app.map:ScreenToWorld(self.screen_offset_x + x / zoom, self.screen_offset_y + y / zoom) |
| 410 |
end |
| 411 |
|
| 412 |
function GameUI:WorldToScreen(x, y) |
| 413 |
local zoom = self.zoom_factor |
| 414 |
x, y = self.app.map:WorldToScreen(x, y) |
| 415 |
x = x - self.screen_offset_x |
| 416 |
y = y - self.screen_offset_y |
| 417 |
return x * zoom, y * zoom |
| 418 |
end |
| 419 |
|
| 420 |
function GameUI:getScreenOffset() |
| 421 |
return self.screen_offset_x, self.screen_offset_y |
| 422 |
end |
| 423 |
|
| 424 |
--! Change if the World should be tested for entities under the cursor |
| 425 |
--!param mode (boolean or room) true to enable hit test (normal), false |
| 426 |
--! to disable, room to enable only for non-door objects in given room |
| 427 |
function GameUI:setWorldHitTest(mode) |
| 428 |
self.do_world_hit_test = mode |
| 429 |
end |
| 430 |
|
| 431 |
function GameUI:onCursorWorldPositionChange() |
| 432 |
local zoom = self.zoom_factor |
| 433 |
local x = math.floor(self.screen_offset_x + self.cursor_x / zoom) |
| 434 |
local y = math.floor(self.screen_offset_y + self.cursor_y / zoom) |
| 435 |
local entity = nil |
| 436 |
local overwindow = self:hitTest(self.cursor_x, self.cursor_y) |
| 437 |
if self.do_world_hit_test and not overwindow then |
| 438 |
entity = self.app.map.th:hitTestObjects(x, y) |
| 439 |
if self.do_world_hit_test ~= true then |
| 440 |
-- limit to non-door objects in room |
| 441 |
local room = self.do_world_hit_test |
| 442 |
entity = entity and class.is(entity, Object) and |
| 443 |
entity:getRoom() == room and entity ~= room.door and entity |
| 444 |
end |
| 445 |
end |
| 446 |
if entity ~= self.cursor_entity then |
| 447 |
-- Stop displaying hoverable moods for the old entity |
| 448 |
if self.cursor_entity then |
| 449 |
self.cursor_entity:setMood(nil) |
| 450 |
end |
| 451 |
|
| 452 |
-- Make the entity easily accessible when debugging, and ignore "deselecting" an entity. |
| 453 |
if entity then |
| 454 |
self.debug_cursor_entity = entity |
| 455 |
end |
| 456 |
|
| 457 |
local epidemic = self.hospital.epidemic |
| 458 |
local infected_cursor = TheApp.gfx:loadMainCursor("epidemic") |
| 459 |
local epidemic_cursor = TheApp.gfx:loadMainCursor("epidemic_hover") |
| 460 |
|
| 461 |
self.cursor_entity = entity |
| 462 |
if self.cursor ~= self.edit_room_cursor and self.cursor ~= self.waiting_cursor then |
| 463 |
local cursor = self.default_cursor |
| 464 |
if self.app.world.user_actions_allowed then |
| 465 |
--- If the patient is infected show the infected cursor |
| 466 |
if epidemic and epidemic.coverup_selected and |
| 467 |
entity and entity.infected and not epidemic.timer.closed then |
| 468 |
cursor = infected_cursor |
| 469 |
-- In vaccination mode display epidemic hover cursor for all entities |
| 470 |
elseif epidemic and epidemic.vaccination_mode_active then |
| 471 |
cursor = epidemic_cursor |
| 472 |
-- Otherwise just show the normal cursor and hover if appropriate |
| 473 |
else |
| 474 |
cursor = entity and entity.hover_cursor or |
| 475 |
(self.down_count ~= 0 and self.down_cursor or self.default_cursor) |
| 476 |
end |
| 477 |
end |
| 478 |
self:setCursor(cursor) |
| 479 |
end |
| 480 |
if self.bottom_panel then |
| 481 |
self.bottom_panel:setDynamicInfo(nil) |
| 482 |
end |
| 483 |
end |
| 484 |
|
| 485 |
-- Queueing icons over patients |
| 486 |
local wx, wy = self:ScreenToWorld(self.cursor_x, self.cursor_y) |
| 487 |
wx = math.floor(wx) |
| 488 |
wy = math.floor(wy) |
| 489 |
local room |
| 490 |
if not overwindow and wx > 0 and wy > 0 and wx < self.app.map.width and wy < self.app.map.height then |
| 491 |
room = self.app.world:getRoom(wx, wy) |
| 492 |
end |
| 493 |
-- Find the room associated with the current entity (Usually only applies to doors) |
| 494 |
if entity and not room then |
| 495 |
if entity.room then |
| 496 |
room = entity.room |
| 497 |
elseif entity.object_type and entity.object_type.id == "swing_door_left" then |
| 498 |
-- Special case to catch the non-dominant side of a double-door |
| 499 |
room = entity.master.room |
| 500 |
end |
| 501 |
end |
| 502 |
if room ~= self.cursor_room then |
| 503 |
-- Unset queue mood for patients queueing the old room |
| 504 |
if self.cursor_room then |
| 505 |
local queue = self.cursor_room.door.queue |
| 506 |
if queue then |
| 507 |
for _, humanoid in ipairs(queue) do |
| 508 |
humanoid:setMood("queue", "deactivate") |
| 509 |
end |
| 510 |
end |
| 511 |
end |
| 512 |
-- Set queue mood for patients queueing the new room |
| 513 |
if room then |
| 514 |
local queue = room.door.queue |
| 515 |
if queue and #queue > 0 then |
| 516 |
TheApp.ui:playSound("HLIGHTP2.wav") |
| 517 |
end |
| 518 |
if queue then |
| 519 |
for _, humanoid in ipairs(queue) do |
| 520 |
humanoid:setMood("queue", "activate") |
| 521 |
end |
| 522 |
end |
| 523 |
end |
| 524 |
self.cursor_room = room |
| 525 |
end |
| 526 |
|
| 527 |
-- Any hoverable mood should be displayed on the new entity |
| 528 |
if class.is(entity, Humanoid) then |
| 529 |
for _, value in pairs(entity.active_moods) do |
| 530 |
if value.on_hover then |
| 531 |
if not self.mood_info then |
| 532 |
if self.last_hovered_entity ~= entity then |
| 533 |
TheApp.ui:playSound("HLIGHTP2.wav") |
| 534 |
self.last_hovered_entity = entity |
| 535 |
end |
| 536 |
if entity.hover_moods then |
| 537 |
entity:setMoodInfo(value) |
| 538 |
end |
| 539 |
end |
| 540 |
break |
| 541 |
end |
| 542 |
end |
| 543 |
else |
| 544 |
self.last_hovered_entity = nil |
| 545 |
end |
| 546 |
|
| 547 |
-- Dynamic Info |
| 548 |
if entity and self.bottom_panel then |
| 549 |
self.bottom_panel:setDynamicInfo(entity:getDynamicInfo()) |
| 550 |
end |
| 551 |
|
| 552 |
return Window.onCursorWorldPositionChange(self, self.cursor_x, self.cursor_y) |
| 553 |
end |
| 554 |
|
| 555 |
local UpdateCursorPosition = TH.cursor.setPosition |
| 556 |
|
| 557 |
local highlight_x, highlight_y |
| 558 |
|
| 559 |
--! Called when focus changes on game window. |
| 560 |
--!param gain (number) 1 for in-focus, 0 for out-of-focus |
| 561 |
function GameUI:onWindowActive(gain) |
| 562 |
if gain == 0 then |
| 563 |
self.tick_scroll_amount_mouse = false |
| 564 |
end |
| 565 |
UI.onWindowActive(self, gain) |
| 566 |
end |
| 567 |
|
| 568 |
-- TODO: try to remove duplication with UI:onMouseMove |
| 569 |
function GameUI:onMouseMove(x, y, dx, dy) |
| 570 |
if self.mouse_released then |
| 571 |
return false |
| 572 |
end |
| 573 |
|
| 574 |
local repaint = UpdateCursorPosition(self.app.video, x, y) |
| 575 |
if self.app.moviePlayer.playing then |
| 576 |
return false |
| 577 |
end |
| 578 |
|
| 579 |
self.cursor_x = x |
| 580 |
self.cursor_y = y |
| 581 |
if self:onCursorWorldPositionChange() or self.simulated_cursor then |
| 582 |
repaint = true |
| 583 |
end |
| 584 |
|
| 585 |
if self:_isMouseScrollButtonDown() then |
| 586 |
local zoom = self.zoom_factor |
| 587 |
self.current_momentum.x = self.current_momentum.x - dx/zoom |
| 588 |
self.current_momentum.y = self.current_momentum.y - dy/zoom |
| 589 |
|
| 590 |
local momentum_x_int = math.round(self.current_momentum.x) |
| 591 |
local momentum_y_int = math.round(self.current_momentum.y) |
| 592 |
|
| 593 |
-- Stop zooming when the middle mouse button is pressed |
| 594 |
self.current_momentum.z = 0 |
| 595 |
self:scrollMap(momentum_x_int, momentum_y_int) |
| 596 |
|
| 597 |
self.current_momentum.x = self.current_momentum.x - momentum_x_int |
| 598 |
self.current_momentum.y = self.current_momentum.y - momentum_y_int |
| 599 |
|
| 600 |
repaint = true |
| 601 |
end |
| 602 |
|
| 603 |
if self.drag_mouse_move then |
| 604 |
self.drag_mouse_move(x, y) |
| 605 |
return true |
| 606 |
end |
| 607 |
|
| 608 |
local scroll_region_size |
| 609 |
if self.app.config.fullscreen then |
| 610 |
-- As the mouse is locked within the window, a 1px region feels a lot |
| 611 |
-- larger than it actually is. |
| 612 |
scroll_region_size = 1 |
| 613 |
else |
| 614 |
-- In windowed mode, a reasonable size is needed, though not too large. |
| 615 |
scroll_region_size = 8 |
| 616 |
end |
| 617 |
if not self.app.config.prevent_edge_scrolling and |
| 618 |
(x < scroll_region_size or y < scroll_region_size or |
| 619 |
x >= self.app.config.width - scroll_region_size or |
| 620 |
y >= self.app.config.height - scroll_region_size) then |
| 621 |
local scroll_dx = 0 |
| 622 |
local scroll_dy = 0 |
| 623 |
local scroll_power = 7 |
| 624 |
if x < scroll_region_size then |
| 625 |
scroll_dx = -scroll_power |
| 626 |
elseif x >= self.app.config.width - scroll_region_size then |
| 627 |
scroll_dx = scroll_power |
| 628 |
end |
| 629 |
if y < scroll_region_size then |
| 630 |
scroll_dy = -scroll_power |
| 631 |
elseif y >= self.app.config.height - scroll_region_size then |
| 632 |
scroll_dy = scroll_power |
| 633 |
end |
| 634 |
|
| 635 |
if not self.tick_scroll_amount_mouse then |
| 636 |
self.tick_scroll_amount_mouse = {x = scroll_dx, y = scroll_dy} |
| 637 |
else |
| 638 |
self.tick_scroll_amount_mouse.x = scroll_dx |
| 639 |
self.tick_scroll_amount_mouse.y = scroll_dy |
| 640 |
end |
| 641 |
else |
| 642 |
self.tick_scroll_amount_mouse = false |
| 643 |
end |
| 644 |
|
| 645 |
if Window.onMouseMove(self, x, y, dx, dy) then |
| 646 |
repaint = true |
| 647 |
end |
| 648 |
|
| 649 |
self:updateTooltip() |
| 650 |
|
| 651 |
local map = self.app.map |
| 652 |
local wx, wy = self:ScreenToWorld(x, y) |
| 653 |
wx = math.floor(wx) |
| 654 |
wy = math.floor(wy) |
| 655 |
if highlight_x then |
| 656 |
--map.th:setCell(highlight_x, highlight_y, 4, 0) |
| 657 |
highlight_x = nil |
| 658 |
end |
| 659 |
local map_width, map_height = map.th:size() |
| 660 |
if 1 <= wx and wx <= map_width and 1 <= wy and wy <= map_height then |
| 661 |
if map.th:getCellFlags(wx, wy).passable then |
| 662 |
--map.th:setCell(wx, wy, 4, 24 + 8 * 256) |
| 663 |
highlight_x = wx |
| 664 |
highlight_y = wy |
| 665 |
end |
| 666 |
end |
| 667 |
|
| 668 |
return repaint |
| 669 |
end |
| 670 |
|
| 671 |
function GameUI:onMouseUp(code, x, y) |
| 672 |
if self.app.moviePlayer.playing then |
| 673 |
return UI.onMouseUp(self, code, x, y) |
| 674 |
end |
| 675 |
|
| 676 |
-- Controlling debug patients movement with a cursor |
| 677 |
local button = self.button_codes[code] |
| 678 |
if button == "right" and not self.map_editor and highlight_x then |
| 679 |
local window = self:getWindow(UIPatient) |
| 680 |
local patient = (window and window.patient.is_debug and window.patient) or self.hospital:getDebugPatient() |
| 681 |
if patient then |
| 682 |
patient:walkTo(highlight_x, highlight_y) |
| 683 |
patient:queueAction(IdleAction()) |
| 684 |
end |
| 685 |
end |
| 686 |
|
| 687 |
if self.edit_room then |
| 688 |
if class.is(self.edit_room, Room) then |
| 689 |
if button == "right" and self.cursor == self.waiting_cursor then |
| 690 |
-- Still waiting for people to leave the room, abort editing it. |
| 691 |
self:setEditRoom(false) |
| 692 |
end |
| 693 |
else -- No room chosen yet, but about to edit one. |
| 694 |
if button == "left" then -- Take the clicked one. |
| 695 |
local room = self.app.world:getRoom(self:ScreenToWorld(x, y)) |
| 696 |
if room then |
| 697 |
if not room.crashed then |
| 698 |
self:setCursor(self.waiting_cursor) |
| 699 |
self.edit_room = room |
| 700 |
room:tryToEdit() |
| 701 |
else |
| 702 |
if self.app.config.remove_destroyed_rooms then |
| 703 |
local room_cost = room:calculateRemovalCost() |
| 704 |
self:setEditRoom(false) |
| 705 |
-- show confirmation dialog for removing the room |
| 706 |
self:addWindow(UIConfirmDialog(self, false, _S.confirmation.remove_destroyed_room:format(room_cost), |
| 707 |
--[[persistable:remove_destroyed_room_confirm_dialog]]function() |
| 708 |
local world = room.world |
| 709 |
UIEditRoom:removeRoom(false, room, world) |
| 710 |
world:resetSideObjects() |
| 711 |
world.rooms[room.id] = nil |
| 712 |
self.hospital:spendMoney(room_cost, _S.transactions.remove_room) |
| 713 |
end |
| 714 |
)) |
| 715 |
end |
| 716 |
end |
| 717 |
end |
| 718 |
else -- right click, we don't want to edit a room after all. |
| 719 |
self:setEditRoom(false) |
| 720 |
end |
| 721 |
end |
| 722 |
end |
| 723 |
|
| 724 |
-- During vaccination mode you can only interact with infected patients |
| 725 |
local epidemic = self.hospital.epidemic |
| 726 |
if epidemic and epidemic.vaccination_mode_active then |
| 727 |
if button == "left" then |
| 728 |
if self.cursor_entity then |
| 729 |
-- Allow click behaviour for infected patients |
| 730 |
if self.cursor_entity.infected then |
| 731 |
self.cursor_entity:onClick(self,button) |
| 732 |
end |
| 733 |
end |
| 734 |
elseif button == "right" then |
| 735 |
--Right click turns vaccination mode off |
| 736 |
local watch = TheApp.ui:getWindow(UIWatch) |
| 737 |
watch:toggleVaccinationMode() |
| 738 |
end |
| 739 |
end |
| 740 |
|
| 741 |
return UI.onMouseUp(self, code, x, y) |
| 742 |
end |
| 743 |
|
| 744 |
--! Process SDL_MULTIGESTURE events for zoom and map move functionality |
| 745 |
--!param numfingers (integer) number of touch points, received from the SDL event |
| 746 |
--! This is still more info about param x. |
| 747 |
--!param dTheta (float) rotation in radians of the gesture from the SDL event |
| 748 |
--!param dDist (float) magnitude of pinch from the SDL event |
| 749 |
--!param x (float) normalised x value of the gesture |
| 750 |
--!param y (float) normalised y value of the gesture |
| 751 |
--!return (boolean) event processed indicator |
| 752 |
function GameUI:onMultiGesture(numfingers, dTheta, dDist, x, y) |
| 753 |
-- only deal with 2 finger events for now |
| 754 |
if numfingers == 2 then |
| 755 |
-- calculate magnitude of pinch |
| 756 |
local mag = math.abs(dDist) |
| 757 |
if mag > multigesture_pinch_sensitivity_factor then |
| 758 |
-- pinch action - constant needs to be tweaked |
| 759 |
self.current_momentum.z = self.current_momentum.z + dDist * multigesture_pinch_amplification_factor |
| 760 |
return true |
| 761 |
else |
| 762 |
-- scroll map |
| 763 |
local normx = self.app.config.width * x |
| 764 |
local normy = self.app.config.height * y |
| 765 |
|
| 766 |
if self.multigesturemove.x == 0.0 then |
| 767 |
self.multigesturemove.x = normx |
| 768 |
self.multigesturemove.y = normy |
| 769 |
else |
| 770 |
local dx = normx - self.multigesturemove.x |
| 771 |
local dy = normy - self.multigesturemove.y |
| 772 |
self.current_momentum.x = self.current_momentum.x - dx |
| 773 |
self.current_momentum.y = self.current_momentum.y - dy |
| 774 |
self.multigesturemove.x = normx |
| 775 |
self.multigesturemove.y = normy |
| 776 |
end |
| 777 |
return true |
| 778 |
end |
| 779 |
end |
| 780 |
return false |
| 781 |
end |
| 782 |
|
| 783 |
function GameUI:onMouseWheel(x, y) |
| 784 |
local inside_window = false |
| 785 |
if self.windows then |
| 786 |
for _, window in ipairs(self.windows) do |
| 787 |
local wx, wy = window:getRealXY() |
| 788 |
if window:hitTest(self.cursor_x - wx, self.cursor_y - wy) then |
| 789 |
inside_window = true |
| 790 |
break |
| 791 |
end |
| 792 |
end |
| 793 |
end |
| 794 |
if not inside_window then |
| 795 |
-- Apply momentum to the zoom |
| 796 |
if math.abs(self.current_momentum.z) < 12 then |
| 797 |
self.current_momentum.z = self.current_momentum.z + y |
| 798 |
end |
| 799 |
end |
| 800 |
return UI.onMouseWheel(self, x, y) |
| 801 |
end |
| 802 |
|
| 803 |
--! Announcements to the player. |
| 804 |
--!param msgs (array of string) Messages to select from. |
| 805 |
--!param priority (optional valid AnnouncementPriority selection) Priority of announcement |
| 806 |
--!param chance_to_play (optional float in range (0, 1]) Fraction of times that the |
| 807 |
-- call actually says something. |
| 808 |
--!return (boolean) Whether a message was given to the user. |
| 809 |
function GameUI:playRandomAnnouncement(msgs, priority, chance_to_play, played_callback, played_callback_delay) |
| 810 |
local max_rnd = #msgs |
| 811 |
if chance_to_play and chance_to_play > 0 and chance_to_play < 1 then |
| 812 |
-- Scale by the fraction. |
| 813 |
max_rnd = math.floor(max_rnd / chance_to_play) |
| 814 |
end |
| 815 |
|
| 816 |
local index = (max_rnd == 1) and 1 or math.random(1, max_rnd) |
| 817 |
if index <= #msgs then |
| 818 |
self:playAnnouncement(msgs[index], priority, played_callback, played_callback_delay) |
| 819 |
return true |
| 820 |
end |
| 821 |
return false |
| 822 |
end |
| 823 |
|
| 824 |
function GameUI:playAnnouncement(name, priority, played_callback, played_callback_delay) |
| 825 |
self.announcer:playAnnouncement(name, priority, played_callback, played_callback_delay) |
| 826 |
end |
| 827 |
|
| 828 |
--! Check whether the configured mouse drag button is being held down (true) or not (false). |
| 829 |
-- fixme: right mouse scrolling currently breaks other mouse operations (see issue 2469). |
| 830 |
function GameUI:_isMouseScrollButtonDown() |
| 831 |
local mouse_scroll_button_down |
| 832 |
if self.app.config.right_mouse_scrolling then |
| 833 |
mouse_scroll_button_down = self.buttons_down.mouse_right |
| 834 |
else |
| 835 |
mouse_scroll_button_down = self.buttons_down.mouse_middle |
| 836 |
end |
| 837 |
return mouse_scroll_button_down |
| 838 |
end |
| 839 |
|
| 840 |
function GameUI:onTick() |
| 841 |
local repaint = UI.onTick(self) |
| 842 |
if not self:_isMouseScrollButtonDown() then |
| 843 |
if math.abs(self.current_momentum.x) < 0.2 and math.abs(self.current_momentum.y) < 0.2 then |
| 844 |
-- Stop scrolling |
| 845 |
self.current_momentum.x = 0.0 |
| 846 |
self.current_momentum.y = 0.0 |
| 847 |
else |
| 848 |
self.current_momentum.x = self.current_momentum.x * self.momentum |
| 849 |
self.current_momentum.y = self.current_momentum.y * self.momentum |
| 850 |
self:scrollMap(self.current_momentum.x, self.current_momentum.y) |
| 851 |
end |
| 852 |
if math.abs(self.current_momentum.z) < 0.2 then |
| 853 |
self.current_momentum.z = 0.0 |
| 854 |
else |
| 855 |
self.current_momentum.z = self.current_momentum.z * self.momentum |
| 856 |
self.app.world:adjustZoom(self.current_momentum.z) |
| 857 |
end |
| 858 |
self.multigesturemove.x = 0.0 |
| 859 |
self.multigesturemove.y = 0.0 |
| 860 |
end |
| 861 |
if self.tick_scroll_amount or self.tick_scroll_amount_mouse then |
| 862 |
-- The scroll amount per tick gradually increases as the duration of the |
| 863 |
-- scroll increases due to this multiplier. |
| 864 |
local mult = self.tick_scroll_mult |
| 865 |
mult = mult + 0.02 |
| 866 |
if mult > 2 then |
| 867 |
mult = 2 |
| 868 |
end |
| 869 |
self.tick_scroll_mult = mult |
| 870 |
|
| 871 |
-- Combine the mouse scroll and keyboard scroll |
| 872 |
local dx, dy = 0, 0 |
| 873 |
if self.tick_scroll_amount_mouse then |
| 874 |
dx, dy = self.tick_scroll_amount_mouse.x, self.tick_scroll_amount_mouse.y |
| 875 |
-- If the middle mouse button is down, then the world is being dragged, |
| 876 |
-- and so the scroll direction due to the cursor being at the map edge |
| 877 |
-- should be opposite to normal to make it feel more natural. |
| 878 |
if self:_isMouseScrollButtonDown() then |
| 879 |
dx, dy = -dx, -dy |
| 880 |
end |
| 881 |
end |
| 882 |
if self.tick_scroll_amount then |
| 883 |
dx = dx + self.tick_scroll_amount.x |
| 884 |
dy = dy + self.tick_scroll_amount.y |
| 885 |
end |
| 886 |
|
| 887 |
-- Adjust scroll speed based on config value: |
| 888 |
-- there is a separate config value for whether or not shift is held. |
| 889 |
-- the speed is multiplied by 0.5 for consistency between the old and |
| 890 |
-- new configuration. In the past scroll_speed applied only to shift |
| 891 |
-- and defaulted to 2, where 1 was regular scroll speed. By |
| 892 |
-- By multiplying by 0.5, we allow for setting slower than normal |
| 893 |
-- scroll speeds, and ensure there is no behaviour change for players |
| 894 |
-- who do not modify their config file. Later the tick speed was |
| 895 |
-- doubled so now we multiply by 0.25. |
| 896 |
if self.shift_scroll_speed_pressed then |
| 897 |
mult = mult * self.app.config.shift_scroll_speed * 0.25 |
| 898 |
else |
| 899 |
mult = mult * self.app.config.scroll_speed * 0.25 |
| 900 |
end |
| 901 |
|
| 902 |
self:scrollMap(dx * mult, dy * mult) |
| 903 |
repaint = true |
| 904 |
else |
| 905 |
self.tick_scroll_mult = 1 |
| 906 |
end |
| 907 |
if self:onCursorWorldPositionChange() then |
| 908 |
repaint = true |
| 909 |
end |
| 910 |
|
| 911 |
self.announcer:onTick() |
| 912 |
|
| 913 |
return repaint |
| 914 |
end |
| 915 |
|
| 916 |
local abs, sqrt_5, floor = math.abs, math.sqrt(1 / 5), math.floor |
| 917 |
|
| 918 |
function GameUI:scrollMapTo(x, y) |
| 919 |
local zoom = 2 * self.zoom_factor |
| 920 |
local config = self.app.config |
| 921 |
return self:scrollMap(x - self.screen_offset_x - config.width / zoom, |
| 922 |
y - self.screen_offset_y - config.height / zoom) |
| 923 |
end |
| 924 |
|
| 925 |
function GameUI.limitPointToDiamond(dx, dy, visible_diamond, do_limit) |
| 926 |
-- If point outside visible diamond, then move point to the nearest position |
| 927 |
-- on the edge of the diamond (NB: relies on diamond.w == 2 * diamond.h). |
| 928 |
local rx = dx - visible_diamond.x |
| 929 |
local ry = dy - visible_diamond.y |
| 930 |
if abs(rx) + abs(ry) * 2 > visible_diamond.w then |
| 931 |
if do_limit then |
| 932 |
-- Determine the quadrant which the point lies in and accordingly set: |
| 933 |
-- (vx, vy) : a unit vector perpendicular to the diamond edge in the quadrant |
| 934 |
-- (p1x, p1y), (p2x, p2y) : the two diamond vertices in the quadrant |
| 935 |
-- d : distance from the point to the line defined by the diamond edge (not the line segment itself) |
| 936 |
local vx, vy, d |
| 937 |
local p1x, p1y, p2x, p2y = 0, 0, 0, 0 |
| 938 |
if rx >= 0 and ry >= 0 then |
| 939 |
p1x, p2y = visible_diamond.w, visible_diamond.h |
| 940 |
vx, vy = sqrt_5, 2 * sqrt_5 |
| 941 |
d = (rx * vx + ry * vy) - (p1x * vx) |
| 942 |
elseif rx >= 0 and ry < 0 then |
| 943 |
p2x, p1y = visible_diamond.w, -visible_diamond.h |
| 944 |
vx, vy = sqrt_5, -2 * sqrt_5 |
| 945 |
d = (rx * vx + ry * vy) - (p2x * vx) |
| 946 |
elseif rx < 0 and ry >= 0 then |
| 947 |
p2x, p1y = -visible_diamond.w, visible_diamond.h |
| 948 |
vx, vy = -sqrt_5, 2 * sqrt_5 |
| 949 |
d = (rx * vx + ry * vy) - (p2x * vx) |
| 950 |
else--if rx < 0 and ry < 0 then |
| 951 |
p1x, p2y = -visible_diamond.w, -visible_diamond.h |
| 952 |
vx, vy = -sqrt_5, -2 * sqrt_5 |
| 953 |
d = (rx * vx + ry * vy) - (p1x * vx) |
| 954 |
end |
| 955 |
-- In the unit vector parallel to the diamond edge, resolve the two vertices and |
| 956 |
-- the point, and either move the point to the edge or to one of the two vertices. |
| 957 |
-- NB: vx, vy, p1x, p1y, p2x, p2y are set such that p1 < p2. |
| 958 |
local p1 = vx * p1y - vy * p1x |
| 959 |
local p2 = vx * p2y - vy * p2x |
| 960 |
local pd = vx * ry - vy * rx |
| 961 |
if pd < p1 then |
| 962 |
dx, dy = p1x + visible_diamond.x, p1y + visible_diamond.y |
| 963 |
elseif pd > p2 then |
| 964 |
dx, dy = p2x + visible_diamond.x, p2y + visible_diamond.y |
| 965 |
else--if p1 <= pd and pd <= p2 then |
| 966 |
dx, dy = dx - d * vx, dy - d * vy |
| 967 |
end |
| 968 |
return math.floor(dx), math.floor(dy), true |
| 969 |
else |
| 970 |
return dx, dy, false |
| 971 |
end |
| 972 |
end |
| 973 |
return dx, dy, true |
| 974 |
end |
| 975 |
|
| 976 |
function GameUI:scrollMap(dx, dy) |
| 977 |
dx = dx + self.screen_offset_x |
| 978 |
dy = dy + self.screen_offset_y |
| 979 |
|
| 980 |
dx, dy, self.in_visible_diamond = self.limitPointToDiamond(dx, dy, |
| 981 |
self.visible_diamond, self.limit_to_visible_diamond) |
| 982 |
|
| 983 |
self.screen_offset_x = floor(dx + 0.5) |
| 984 |
self.screen_offset_y = floor(dy + 0.5) |
| 985 |
end |
| 986 |
|
| 987 |
--! Start shaking the screen, e.g. an earthquake effect (unless disabled in config) |
| 988 |
--!param intensity (number) The magnitude of the effect, between 0 for no |
| 989 |
-- movement to 1 for significant shaking. |
| 990 |
function GameUI:beginShakeScreen(intensity) |
| 991 |
if self.app.config.enable_screen_shake then |
| 992 |
self.shake_screen_intensity = intensity |
| 993 |
else |
| 994 |
self.shake_screen_intensity = 0 |
| 995 |
end |
| 996 |
end |
| 997 |
|
| 998 |
--! Stop the screen from shaking after beginShakeScreen is called. |
| 999 |
function GameUI:endShakeScreen() |
| 1000 |
self.shake_screen_intensity = 0 |
| 1001 |
end |
| 1002 |
|
| 1003 |
function GameUI:limitCamera(mode) |
| 1004 |
self.limit_to_visible_diamond = mode |
| 1005 |
self:scrollMap(0, 0) |
| 1006 |
end |
| 1007 |
|
| 1008 |
--! Applies the current setting for wall transparency to the map |
| 1009 |
function GameUI:applyTransparency() |
| 1010 |
self.app.map.th:setWallDrawFlags(self.transparent_walls and 4 or 0) |
| 1011 |
end |
| 1012 |
|
| 1013 |
--! Sets wall transparency to the specified parameter |
| 1014 |
--!param mode (boolean) whether to enable or disable wall transparency |
| 1015 |
function GameUI:setWallsTransparent(mode) |
| 1016 |
if mode ~= self.transparent_walls then |
| 1017 |
self.transparent_walls = mode |
| 1018 |
self:applyTransparency() |
| 1019 |
end |
| 1020 |
end |
| 1021 |
|
| 1022 |
function UI:toggleAdviser() |
| 1023 |
self.app.config.adviser_disabled = not self.app.config.adviser_disabled |
| 1024 |
self.app:saveConfig() |
| 1025 |
end |
| 1026 |
|
| 1027 |
function UI:togglePlaySounds() |
| 1028 |
self.app.config.play_sounds = not self.app.config.play_sounds |
| 1029 |
self.app.audio:playSoundEffects(self.app.config.play_sounds) |
| 1030 |
self.app:saveConfig() |
| 1031 |
end |
| 1032 |
|
| 1033 |
function UI:togglePlayAnnouncements() |
| 1034 |
self.app.config.play_announcements = not self.app.config.play_announcements |
| 1035 |
self.app:saveConfig() |
| 1036 |
end |
| 1037 |
|
| 1038 |
function UI:togglePlayMusic() |
| 1039 |
if not self.app.audio.background_music then |
| 1040 |
self.app.config.play_music = true |
| 1041 |
self.app.audio:playRandomBackgroundTrack() -- play |
| 1042 |
else |
| 1043 |
self.app.config.play_music = false |
| 1044 |
self.app.audio:stopBackgroundTrack() -- stop |
| 1045 |
end |
| 1046 |
-- self.app.config.play_music = not self.app.config.play_music |
| 1047 |
self.app:saveConfig() |
| 1048 |
end |
| 1049 |
|
| 1050 |
local tutorial_phases |
| 1051 |
local function make_tutorial_phases() |
| 1052 |
tutorial_phases = { |
| 1053 |
{ |
| 1054 |
-- 1) build reception |
| 1055 |
{ text = _A.tutorial.build_reception, -- 1 |
| 1056 |
begin_callback = function() TheApp.ui:getWindow(UIBottomPanel):startButtonBlinking(3) end, |
| 1057 |
end_callback = function() TheApp.ui:getWindow(UIBottomPanel):stopButtonBlinking() end, }, |
| 1058 |
{ text = _A.tutorial.order_one_reception, -- 2 |
| 1059 |
begin_callback = function() TheApp.ui:getWindow(UIFurnishCorridor):startButtonBlinking(3) end, |
| 1060 |
end_callback = function() TheApp.ui:getWindow(UIFurnishCorridor):stopButtonBlinking(3) end, }, |
| 1061 |
{ text = _A.tutorial.accept_purchase, -- 3 |
| 1062 |
begin_callback = function() TheApp.ui:getWindow(UIFurnishCorridor):startButtonBlinking(2) end, |
| 1063 |
end_callback = function() TheApp.ui:getWindow(UIFurnishCorridor):stopButtonBlinking(2) end, }, |
| 1064 |
_A.tutorial.rotate_and_place_reception, -- 4 |
| 1065 |
_A.tutorial.reception_invalid_position, -- 5 |
| 1066 |
-- 6: object other than reception selected. currently no text for this phase. |
| 1067 |
}, |
| 1068 |
|
| 1069 |
{ |
| 1070 |
-- 2) hire receptionist |
| 1071 |
{ text = _A.tutorial.hire_receptionist, -- 1 |
| 1072 |
begin_callback = function() TheApp.ui:getWindow(UIBottomPanel):startButtonBlinking(5) end, |
| 1073 |
end_callback = function() TheApp.ui:getWindow(UIBottomPanel):stopButtonBlinking() end, }, |
| 1074 |
{ text = _A.tutorial.select_receptionists, -- 2 |
| 1075 |
begin_callback = function() TheApp.ui:getWindow(UIHireStaff):startButtonBlinking(4) end, |
| 1076 |
end_callback = function() TheApp.ui:getWindow(UIHireStaff):stopButtonBlinking() end, }, |
| 1077 |
{ text = _A.tutorial.next_receptionist, -- 3 |
| 1078 |
begin_callback = function() TheApp.ui:getWindow(UIHireStaff):startButtonBlinking(8) end, |
| 1079 |
end_callback = function() TheApp.ui:getWindow(UIHireStaff):stopButtonBlinking() end, }, |
| 1080 |
{ text = _A.tutorial.prev_receptionist, -- 4 |
| 1081 |
begin_callback = function() TheApp.ui:getWindow(UIHireStaff):startButtonBlinking(5) end, |
| 1082 |
end_callback = function() TheApp.ui:getWindow(UIHireStaff):stopButtonBlinking() end, }, |
| 1083 |
{ text = _A.tutorial.choose_receptionist, -- 5 |
| 1084 |
begin_callback = function() TheApp.ui:getWindow(UIHireStaff):startButtonBlinking(6) end, |
| 1085 |
end_callback = function() TheApp.ui:getWindow(UIHireStaff):stopButtonBlinking() end, }, |
| 1086 |
_A.tutorial.place_receptionist, -- 6 |
| 1087 |
_A.tutorial.receptionist_invalid_position, -- 7 |
| 1088 |
}, |
| 1089 |
|
| 1090 |
{ |
| 1091 |
-- 3) build GP's office |
| 1092 |
-- 3.1) room window |
| 1093 |
{ text = _A.tutorial.build_gps_office, -- 1 |
| 1094 |
begin_callback = function() TheApp.ui:getWindow(UIBottomPanel):startButtonBlinking(2) end, |
| 1095 |
end_callback = function() TheApp.ui:getWindow(UIBottomPanel):stopButtonBlinking() end, }, |
| 1096 |
{ text = _A.tutorial.select_diagnosis_rooms, -- 2 |
| 1097 |
begin_callback = function() TheApp.ui:getWindow(UIBuildRoom):startButtonBlinking(1) end, |
| 1098 |
end_callback = function() TheApp.ui:getWindow(UIBuildRoom):stopButtonBlinking() end, }, |
| 1099 |
{ text = _A.tutorial.click_gps_office, -- 3 |
| 1100 |
begin_callback = function() TheApp.ui:getWindow(UIBuildRoom):startButtonBlinking(5) end, |
| 1101 |
end_callback = function() TheApp.ui:getWindow(UIBuildRoom):stopButtonBlinking() end, }, |
| 1102 |
|
| 1103 |
-- 3.2) blueprint |
| 1104 |
-- [11][58] was maybe planned to be used in this place, but is not needed. |
| 1105 |
_A.tutorial.click_and_drag_to_build, -- 4 |
| 1106 |
_A.tutorial.room_in_invalid_position, -- 5 |
| 1107 |
_A.tutorial.room_too_small, -- 6 |
| 1108 |
_A.tutorial.room_too_small_and_invalid, -- 7 |
| 1109 |
{ text = _A.tutorial.room_big_enough, -- 8 |
| 1110 |
begin_callback = function() TheApp.ui:getWindow(UIEditRoom):startButtonBlinking(4) end, |
| 1111 |
end_callback = function() TheApp.ui:getWindow(UIEditRoom):stopButtonBlinking() end, }, |
| 1112 |
|
| 1113 |
-- 3.3) door and windows |
| 1114 |
_A.tutorial.place_door, -- 9 |
| 1115 |
_A.tutorial.door_in_invalid_position, -- 10 |
| 1116 |
{ text = _A.tutorial.place_windows, -- 11 |
| 1117 |
begin_callback = function() TheApp.ui:getWindow(UIEditRoom):startButtonBlinking(4) end, |
| 1118 |
end_callback = function() TheApp.ui:getWindow(UIEditRoom):stopButtonBlinking() end, }, |
| 1119 |
{ text = _A.tutorial.window_in_invalid_position, -- 12 |
| 1120 |
begin_callback = function() TheApp.ui:getWindow(UIEditRoom):startButtonBlinking(4) end, |
| 1121 |
end_callback = function() TheApp.ui:getWindow(UIEditRoom):stopButtonBlinking() end, }, |
| 1122 |
|
| 1123 |
-- 3.4) objects |
| 1124 |
_A.tutorial.place_objects, -- 13 |
| 1125 |
_A.tutorial.object_in_invalid_position, -- 14 |
| 1126 |
{ text = _A.tutorial.confirm_room, -- 15 |
| 1127 |
begin_callback = function() TheApp.ui:getWindow(UIEditRoom):startButtonBlinking(4) end, |
| 1128 |
end_callback = function() TheApp.ui:getWindow(UIEditRoom):stopButtonBlinking() end, }, |
| 1129 |
{ text = _A.tutorial.information_window, -- 16 |
| 1130 |
begin_callback = function() TheApp.ui:getWindow(UIInformation):startButtonBlinking(1) end, |
| 1131 |
end_callback = function() TheApp.ui:getWindow(UIInformation):stopButtonBlinking() end, }, |
| 1132 |
}, |
| 1133 |
|
| 1134 |
{ |
| 1135 |
-- 4) hire doctor |
| 1136 |
{ text = _A.tutorial.hire_doctor, -- 1 |
| 1137 |
begin_callback = function() TheApp.ui:getWindow(UIBottomPanel):startButtonBlinking(5) end, |
| 1138 |
end_callback = function() TheApp.ui:getWindow(UIBottomPanel):stopButtonBlinking() end, }, |
| 1139 |
{ text = _A.tutorial.select_doctors, -- 2 |
| 1140 |
begin_callback = function() TheApp.ui:getWindow(UIHireStaff):startButtonBlinking(1) end, |
| 1141 |
end_callback = function() TheApp.ui:getWindow(UIHireStaff):stopButtonBlinking() end, }, |
| 1142 |
{ text = _A.tutorial.choose_doctor, -- 3 |
| 1143 |
begin_callback = function() TheApp.ui:getWindow(UIHireStaff):startButtonBlinking(6) end, |
| 1144 |
end_callback = function() TheApp.ui:getWindow(UIHireStaff):stopButtonBlinking() end, }, |
| 1145 |
_A.tutorial.place_doctor, -- 4 |
| 1146 |
_A.tutorial.doctor_in_invalid_position, -- 5 |
| 1147 |
}, |
| 1148 |
{ |
| 1149 |
-- 5) end of tutorial |
| 1150 |
{ begin_callback = function() |
| 1151 |
-- The demo uses a single string for the post-tutorial info while |
| 1152 |
-- the real game uses three. |
| 1153 |
local texts = TheApp.using_demo_files and { |
| 1154 |
{_S.introduction_texts["level15"]}, |
| 1155 |
} or { |
| 1156 |
{_S.introduction_texts["level15"]}, |
| 1157 |
{_S.introduction_texts["level16"]}, |
| 1158 |
{_S.introduction_texts["level17"]}, |
| 1159 |
} |
| 1160 |
if TheApp.world.map.level_number ~= 1 then |
| 1161 |
table.insert(texts, _S.introduction_texts["level1"]) |
| 1162 |
end |
| 1163 |
TheApp.ui:addWindow(UIInformation(TheApp.ui, texts)) |
| 1164 |
TheApp.ui:addWindow(UIWatch(TheApp.ui, "initial_opening")) |
| 1165 |
end, |
| 1166 |
}, |
| 1167 |
}, |
| 1168 |
} |
| 1169 |
end |
| 1170 |
tutorial_phases = setmetatable({}, {__index = function(_, k) |
| 1171 |
make_tutorial_phases() |
| 1172 |
return tutorial_phases[k] |
| 1173 |
end}) |
| 1174 |
|
| 1175 |
-- Called to trigger step to another part of the tutorial. |
| 1176 |
-- chapter: Individual parts of the tutorial. Step will only happen if it's the current chapter. |
| 1177 |
-- phase_from: Phase we need to be in for this step to happen. Multiple phases can be given here in an array. |
| 1178 |
-- phase_to: Phase we want to step to or "next" to go to next chapter or "end" to end tutorial. |
| 1179 |
-- returns true if we changed phase, false if we didn't |
| 1180 |
function GameUI:tutorialStep(chapter, phase_from, phase_to, ...) |
| 1181 |
if self.tutorial.chapter ~= chapter then |
| 1182 |
return false |
| 1183 |
end |
| 1184 |
if type(phase_from) == "table" then |
| 1185 |
local contains_current = false |
| 1186 |
for _, phase in ipairs(phase_from) do |
| 1187 |
if phase == self.tutorial.phase then |
| 1188 |
contains_current = true |
| 1189 |
break |
| 1190 |
end |
| 1191 |
end |
| 1192 |
if not contains_current then return false end |
| 1193 |
else |
| 1194 |
if self.tutorial.phase ~= phase_from then return false end |
| 1195 |
end |
| 1196 |
|
| 1197 |
local old_phase = tutorial_phases[self.tutorial.chapter][self.tutorial.phase] |
| 1198 |
if old_phase and old_phase.end_callback and type(old_phase.end_callback) == "function" then |
| 1199 |
old_phase.end_callback(...) |
| 1200 |
end |
| 1201 |
|
| 1202 |
if phase_to == "end" then |
| 1203 |
self.tutorial.chapter = 0 |
| 1204 |
self.tutorial.phase = 0 |
| 1205 |
return true |
| 1206 |
elseif phase_to == "next" then |
| 1207 |
self.tutorial.chapter = self.tutorial.chapter + 1 |
| 1208 |
self.tutorial.phase = 1 |
| 1209 |
else |
| 1210 |
self.tutorial.phase = phase_to |
| 1211 |
end |
| 1212 |
|
| 1213 |
if TheApp.config.debug then print("Tutorial: Now in " .. self.tutorial.chapter .. ", " .. self.tutorial.phase) end |
| 1214 |
local new_phase = tutorial_phases[self.tutorial.chapter][self.tutorial.phase] |
| 1215 |
local str, callback |
| 1216 |
if (type(new_phase) == "table" and type(new_phase.text) == "table") or not new_phase.text then |
| 1217 |
str = new_phase.text |
| 1218 |
callback = new_phase.begin_callback |
| 1219 |
else |
| 1220 |
str = new_phase |
| 1221 |
end |
| 1222 |
if str and str.text then |
| 1223 |
self.adviser:say(str, true, true) |
| 1224 |
else |
| 1225 |
self.adviser.stay_up = nil |
| 1226 |
end |
| 1227 |
if callback then |
| 1228 |
callback(...) |
| 1229 |
end |
| 1230 |
return true |
| 1231 |
end |
| 1232 |
|
| 1233 |
function GameUI:startTutorial(chapter) |
| 1234 |
chapter = chapter or 1 |
| 1235 |
self.tutorial.chapter = chapter |
| 1236 |
self.tutorial.phase = 0 |
| 1237 |
|
| 1238 |
self:tutorialStep(chapter, 0, 1) |
| 1239 |
end |
| 1240 |
|
| 1241 |
--! Converts centre of screen coordinates to world tile positions and stores the values for later recall |
| 1242 |
-- param index (integer) Position in recallpositions table |
| 1243 |
function GameUI:setMapRecallPosition(index) |
| 1244 |
local cx, cy = self:ScreenToWorld(self.app.config.width / 2, self.app.config.height / 2) |
| 1245 |
self.recallpositions[index] = {x = cx, y = cy, z = self.zoom_factor} |
| 1246 |
end |
| 1247 |
|
| 1248 |
--! Retrieves stored recall position and attempts to scroll to that position - will be limited to the bounds of the camera when zoomed out |
| 1249 |
-- param index (integer) Position in recallpositions table |
| 1250 |
function GameUI:recallMapPosition(index) |
| 1251 |
if self.recallpositions[index] ~= nil then |
| 1252 |
local sx, sy = self.app.map:WorldToScreen(self.recallpositions[index].x, self.recallpositions[index].y) |
| 1253 |
local dx, dy = self.app.map:ScreenToWorld(self.app.config.width / 2, self.app.config.height / 2) |
| 1254 |
self:setZoom(self.recallpositions[index].z) |
| 1255 |
self:scrollMapTo(sx + dx, sy + dy) |
| 1256 |
end |
| 1257 |
end |
| 1258 |
|
| 1259 |
function GameUI:setEditRoom(enabled) |
| 1260 |
-- TODO: Make the room the cursor is over flash |
| 1261 |
if enabled then |
| 1262 |
self:setCursor(self.edit_room_cursor) |
| 1263 |
self.edit_room = true |
| 1264 |
else |
| 1265 |
-- If the actual editing hasn't started yet but is on its way, |
| 1266 |
-- activate the room again. |
| 1267 |
if class.is(self.edit_room, Room) and self.cursor == self.waiting_cursor then |
| 1268 |
self.app.world:markRoomAsBuilt(self.edit_room) |
| 1269 |
else |
| 1270 |
-- If we are currently editing a room it may happen that we need to abort it. |
| 1271 |
-- Also remove any dialog where the user is buying items. |
| 1272 |
local item_window = self.app.ui:getWindow(UIFurnishCorridor) |
| 1273 |
if item_window and item_window.edit_dialog then |
| 1274 |
item_window:close() |
| 1275 |
end |
| 1276 |
local edit_window = self.app.ui:getWindow(UIEditRoom) |
| 1277 |
if edit_window then |
| 1278 |
edit_window:verifyOrAbortRoom() |
| 1279 |
end |
| 1280 |
end |
| 1281 |
self:setCursor(self.default_cursor) |
| 1282 |
self.edit_room = false |
| 1283 |
end |
| 1284 |
end |
| 1285 |
|
| 1286 |
function GameUI:afterLoad(old, new) |
| 1287 |
if old < 16 then |
| 1288 |
self.zoom_factor = 1 |
| 1289 |
end |
| 1290 |
if old < 23 then |
| 1291 |
self.do_world_hit_test = not self:getWindow(UIPlaceObjects) |
| 1292 |
end |
| 1293 |
if old < 34 then |
| 1294 |
self.adviser.queued_messages = {} |
| 1295 |
self.adviser.phase = 0 |
| 1296 |
self.adviser.timer = nil |
| 1297 |
self.adviser.frame = 1 |
| 1298 |
self.adviser.number_frames = 4 |
| 1299 |
end |
| 1300 |
if old < 75 then |
| 1301 |
self.current_momentum = { x = 0, y = 0 } |
| 1302 |
self.momentum = self.app.config.scrolling_momentum |
| 1303 |
end |
| 1304 |
if old < 78 then |
| 1305 |
self.current_momentum = { x = 0, y = 0, z = 0} |
| 1306 |
end |
| 1307 |
if old < 115 then |
| 1308 |
self.shake_screen_intensity = 0 |
| 1309 |
end |
| 1310 |
if old < 122 then |
| 1311 |
self.multigesturemove = {x = 0.0, y = 0.0} |
| 1312 |
end |
| 1313 |
if old < 129 then |
| 1314 |
self.recallpositions = {} |
| 1315 |
end |
| 1316 |
if old < 130 then |
| 1317 |
self.ticks_since_last_announcement = nil -- cleanup |
| 1318 |
self.announcer = Announcer(self.app) |
| 1319 |
end |
| 1320 |
if old < 240 then |
| 1321 |
self.subtitles = Subtitles(self) |
| 1322 |
self:addWindow(self.subtitles) |
| 1323 |
end |
| 1324 |
|
| 1325 |
self.announcer.playing = false |
| 1326 |
|
| 1327 |
self.app:setCaptureMouse() |
| 1328 |
return UI.afterLoad(self, old, new) |
| 1329 |
end |
| 1330 |
|
| 1331 |
function GameUI:showBriefing() |
| 1332 |
local level = self.app.world.map.level_number |
| 1333 |
local text = {_S.information.custom_game} |
| 1334 |
if type(level) == "number" then |
| 1335 |
text = {_S.introduction_texts[TheApp.using_demo_files and "demo" or "level" .. level]} |
| 1336 |
elseif self.app.world.map.level_intro then |
| 1337 |
text = {self.app.world.map.level_intro} |
| 1338 |
end |
| 1339 |
self:addWindow(UIInformation(self, text)) |
| 1340 |
end |
| 1341 |
|
| 1342 |
--! Offers a confirmation window to quit the game and return to main menu |
| 1343 |
-- NB: overrides UI.quit, do NOT call it from here |
| 1344 |
--!param mapeditor (boolean) If the user is quitting the map editor |
| 1345 |
function GameUI:quit(mapeditor) |
| 1346 |
local msg = mapeditor and _S.confirmation.quit_mapeditor or _S.confirmation.quit |
| 1347 |
self:addWindow(UIConfirmDialog(self, false, msg, --[[persistable:gameui_confirm_quit]] function() |
| 1348 |
self.app:loadMainMenu() |
| 1349 |
-- Release the mouse regardless of setting |
| 1350 |
self.app.video:setCaptureMouse(false) |
| 1351 |
end)) |
| 1352 |
end |
| 1353 |
|
| 1354 |
function GameUI:showCheatsWindow() |
| 1355 |
self:addWindow(UICheats(self)) |
| 1356 |
end |
| 1357 |
|
| 1358 |
function GameUI:showMenuBar() |
| 1359 |
self.menu_bar:appear() |
| 1360 |
end |
| 1361 |
|
| 1362 |
function GameUI:restartMapEditor() |
| 1363 |
self:addWindow(UIConfirmDialog(self, false, _S.confirmation.restart_mapeditor, |
| 1364 |
--[[persistable:app_hotkey_confirm_mapeditor_restart]] function() self.app:mapEdit() end)) |
| 1365 |
end |