| 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 |
local TH = require("TH") |
| 22 |
|
| 23 |
local pathsep = package.config:sub(1, 1) |
| 24 |
|
| 25 |
--! Layer for loading (and subsequently caching) graphical resources. |
| 26 |
--! The Graphics class handles loading and caching of graphics resources. |
| 27 |
-- It can adapt as the API to C changes, and hide these changes from most of |
| 28 |
-- the other Lua code. |
| 29 |
class "Graphics" |
| 30 |
|
| 31 |
---@type Graphics |
| 32 |
local Graphics = _G["Graphics"] |
| 33 |
|
| 34 |
local cursors_name = { |
| 35 |
default = 1, |
| 36 |
clicked = 2, |
| 37 |
resize_room = 3, |
| 38 |
edit_room = 4, |
| 39 |
ns_arrow = 5, |
| 40 |
we_arrow = 6, |
| 41 |
nswe_arrow = 7, |
| 42 |
move_room = 8, |
| 43 |
sleep = 9, |
| 44 |
kill_rat = 10, |
| 45 |
kill_rat_hover = 11, |
| 46 |
epidemic_hover = 12, |
| 47 |
epidemic = 13, |
| 48 |
grab = 14, |
| 49 |
quit = 15, |
| 50 |
staff = 16, |
| 51 |
repair = 17, |
| 52 |
patient = 18, |
| 53 |
queue = 19, |
| 54 |
queue_drag = 20, |
| 55 |
bank = 36, |
| 56 |
banksummary = 44, |
| 57 |
} |
| 58 |
local cursors_palette = { |
| 59 |
[36] = "Bank01V.pal", |
| 60 |
[44] = "Stat01V.pal", |
| 61 |
} |
| 62 |
|
| 63 |
local charsets = { |
| 64 |
["cp437"] = 1, |
| 65 |
["mik"] = 2 |
| 66 |
} |
| 67 |
|
| 68 |
function Graphics:Graphics(app, gfx_set, charset) |
| 69 |
self.app = app |
| 70 |
self.target = self.app.video |
| 71 |
-- The cache is used to avoid reloading an object if it is already loaded |
| 72 |
self.cache = { |
| 73 |
raw = {}, |
| 74 |
tabled = {}, |
| 75 |
palette = {}, |
| 76 |
palette_greyscale_ghost = {}, |
| 77 |
ghosts = {}, |
| 78 |
anims = {}, |
| 79 |
language_fonts = {}, |
| 80 |
cursors = setmetatable({}, {__mode = "k"}), |
| 81 |
} |
| 82 |
self.th_charset = charset and charsets[charset] or charsets["cp437"] |
| 83 |
|
| 84 |
self.custom_graphics = {} |
| 85 |
-- The load info table records how objects were loaded, and is used to |
| 86 |
-- persist objects as instructions on how to load them. |
| 87 |
self.load_info = setmetatable({}, {__mode = "k"}) |
| 88 |
-- If the video target changes then resources will need to be reloaded |
| 89 |
-- (at least with some rendering engines). Note that reloading is different |
| 90 |
-- to loading (as in load_info), as reloading is done while the application |
| 91 |
-- is running, upon objects which are already loaded, whereas loading might |
| 92 |
-- be done with a different graphics engine, or might only need to grab an |
| 93 |
-- object from the cache. |
| 94 |
self.reload_functions = setmetatable({}, {__mode = "k"}) |
| 95 |
-- Cursors and fonts need to be reloaded after sprite sheets, as they are |
| 96 |
-- created from sprite sheets. |
| 97 |
self.reload_functions_last = setmetatable({}, {__mode = "k"}) |
| 98 |
|
| 99 |
self:loadFontFile() |
| 100 |
self:_loadPalettes(gfx_set) |
| 101 |
|
| 102 |
local graphics_folder = nil |
| 103 |
if self.app.config.use_new_graphics then |
| 104 |
-- Check if the config specifies a place to look for graphics in. |
| 105 |
-- Otherwise check in the default "Graphics" folder. |
| 106 |
graphics_folder = self.app.config.new_graphics_folder or self.app:getFullPath("Graphics", true) |
| 107 |
if graphics_folder:sub(-1) ~= pathsep then |
| 108 |
graphics_folder = graphics_folder .. pathsep |
| 109 |
end |
| 110 |
|
| 111 |
local graphics_config_file = graphics_folder .. "file_mapping.txt" |
| 112 |
local result, err = loadfile_envcall(graphics_config_file) |
| 113 |
|
| 114 |
if not result then |
| 115 |
print("Warning: Failed to read custom graphics configuration:\n" .. err) |
| 116 |
else |
| 117 |
result(self.custom_graphics) |
| 118 |
if not self.custom_graphics.file_mapping then |
| 119 |
print("Error: An invalid custom graphics mapping file was found") |
| 120 |
end |
| 121 |
end |
| 122 |
end |
| 123 |
self.custom_graphics_folder = graphics_folder |
| 124 |
end |
| 125 |
|
| 126 |
function Graphics:_loadPalettes(gfx_set) |
| 127 |
-- Load base palettes |
| 128 |
self:_loadPalette("Bitmap", "bootstrap_font.pal", true, false) |
| 129 |
self:_loadPalette("Bitmap", "lose.pl8", true, true) |
| 130 |
self:_loadPalette("Bitmap", "mainmenu1080.pal", false, false) |
| 131 |
self:_loadPalette("Bitmap", "mainmenu480.pal", false, false) |
| 132 |
self:_loadPalette("Bitmap", "mainmenu720.pal", false, false) |
| 133 |
self:_loadPalette("Bitmap", "winlevel.pl8", true, true) |
| 134 |
self:_loadPalette("Bitmap", "tree_ctrl.pal", false, false) |
| 135 |
|
| 136 |
if gfx_set == "base" then |
| 137 |
return |
| 138 |
end |
| 139 |
|
| 140 |
-- Load demo palettes |
| 141 |
self:_loadPalette("Data", "MPalette.dat", true, false) |
| 142 |
self:_loadPalette("QData", "Area01V.pal", true, false) |
| 143 |
self:_loadPalette("QData", "Award01V.pal", true, false) |
| 144 |
self:_loadPalette("QData", "Award02V.pal", true, false) |
| 145 |
self:_loadPalette("QData", "Bid01V.pal", true, false) |
| 146 |
self:_loadPalette("QData", "Brief01V.pal", true, false) |
| 147 |
self:_loadPalette("QData", "Cred01V.pal", true, false) |
| 148 |
self:_loadPalette("QData", "Fame01V.pal", true, false) |
| 149 |
self:_loadPalette("QData", "Fax01V.pal", true, false) |
| 150 |
self:_loadPalette("QData", "Load01V.pal", true, false) |
| 151 |
self:_loadPalette("QData", "Main01M.pal", true, false) |
| 152 |
self:_loadPalette("QData", "Pref01V.pal", true, false) |
| 153 |
self:_loadPalette("QData", "Score01V.pal", true, false) |
| 154 |
|
| 155 |
if gfx_set == "demo" then |
| 156 |
return |
| 157 |
end |
| 158 |
|
| 159 |
-- Load full palettes |
| 160 |
self:_loadPalette("QData", "Bank01V.pal", true, false) |
| 161 |
self:_loadPalette("QData", "DrugN01V.pal", true, false) |
| 162 |
self:_loadPalette("QData", "Graph01V.pal", true, false) |
| 163 |
self:_loadPalette("QData", "Pol01V.pal", true, false) |
| 164 |
self:_loadPalette("QData", "Rep01V.pal", true, false) |
| 165 |
self:_loadPalette("QData", "Res01V.pal", true, false) |
| 166 |
self:_loadPalette("QData", "Staff01V.pal", true, false) |
| 167 |
self:_loadPalette("QData", "Stat01V.pal", true, false) |
| 168 |
self:_loadPalette("QData", "Title01V.pal", true, false) |
| 169 |
self:_loadPalette("QData", "Town01V.pal", true, false) |
| 170 |
end |
| 171 |
|
| 172 |
--! Tries to load the font file given in the config file as unicode_font. |
| 173 |
--! If it is not found it tries to use the font file from the compile setting WITH_FONT, |
| 174 |
--! or look for one in the CorsixTH folder, the grandparent of this file. |
| 175 |
function Graphics:loadFontFile() |
| 176 |
local lfs = require("lfs") |
| 177 |
local function check(path) return lfs.attributes(path, "mode") == "file" end |
| 178 |
local config_path, compile_path = self.app.config.unicode_font, TH.GetCompileOptions().font |
| 179 |
|
| 180 |
local function getFontPath() |
| 181 |
local config_err, compile_err = "", "" |
| 182 |
-- Check for font specified in config file |
| 183 |
if config_path then |
| 184 |
if check(config_path) then return config_path |
| 185 |
else config_err = "Configured font set but not found. Check path " .. config_path |
| 186 |
end |
| 187 |
end |
| 188 |
-- Check for font specified in compile options |
| 189 |
if compile_path then |
| 190 |
if check(compile_path) then return compile_path |
| 191 |
else compile_err = " Compiled font path set but not found. Check path " .. compile_path |
| 192 |
end |
| 193 |
end |
| 194 |
-- Check the CorsixTH directory for our unicode font |
| 195 |
local path = self.app:getFullPath() |
| 196 |
local font_names = { "CorsixTHUnicode.ttf", "GoNotoKurrent-Regular.ttf" } |
| 197 |
for _, name in ipairs(font_names) do |
| 198 |
if check(path .. name) then |
| 199 |
return path .. name |
| 200 |
end |
| 201 |
end |
| 202 |
return nil, config_err .. compile_err |
| 203 |
end |
| 204 |
local font_file, err = getFontPath() |
| 205 |
|
| 206 |
if not font_file then |
| 207 |
print("Unicode font not found, no fallback available.", err) |
| 208 |
return |
| 209 |
end |
| 210 |
|
| 211 |
local font = io.open(font_file, "rb") |
| 212 |
if font then |
| 213 |
self.ttf_font_data = font:read("*a") |
| 214 |
font:close() |
| 215 |
if self.ttf_font_data and config_path ~= font_file then |
| 216 |
self.app.config.unicode_font = font_file |
| 217 |
self.app:saveConfig() |
| 218 |
print("Configured unicode font not found, using " .. font_file .. " instead.") |
| 219 |
print("This will be written to the config file.") |
| 220 |
end |
| 221 |
end |
| 222 |
end |
| 223 |
|
| 224 |
function Graphics:loadMainCursor(id) |
| 225 |
if type(id) ~= "number" then |
| 226 |
id = cursors_name[id] |
| 227 |
end |
| 228 |
if id > 20 then -- SPointer cursors |
| 229 |
local cursor_palette = self:getPalette(cursors_palette[id]) |
| 230 |
return self:loadCursor(self:loadSpriteTable("QData", "SPointer", false, cursor_palette), id - 20) |
| 231 |
else |
| 232 |
return self:loadCursor(self:loadSpriteTable("Data", "MPointer"), id) |
| 233 |
end |
| 234 |
end |
| 235 |
|
| 236 |
function Graphics:loadCursor(sheet, index, hot_x, hot_y) |
| 237 |
local sheet_cache = self.cache.cursors[sheet] |
| 238 |
if not sheet_cache then |
| 239 |
sheet_cache = {} |
| 240 |
self.cache.cursors[sheet] = sheet_cache |
| 241 |
end |
| 242 |
local cursor = sheet_cache[index] |
| 243 |
if not cursor then |
| 244 |
hot_x = hot_x or 0 |
| 245 |
hot_y = hot_y or 0 |
| 246 |
cursor = TH.cursor() |
| 247 |
if not cursor:load(sheet, index, hot_x, hot_y) then |
| 248 |
cursor = { |
| 249 |
draw = function(canvas, x, y) |
| 250 |
sheet:draw(canvas, index, x - hot_x, y - hot_y) |
| 251 |
end, |
| 252 |
} |
| 253 |
else |
| 254 |
local function cursor_reloader(res) |
| 255 |
assert(res:load(sheet, index, hot_x, hot_y)) |
| 256 |
end |
| 257 |
self.reload_functions_last[cursor] = cursor_reloader |
| 258 |
end |
| 259 |
sheet_cache[index] = cursor |
| 260 |
self.load_info[cursor] = {self.loadCursor, self, sheet, index, hot_x, hot_y} |
| 261 |
end |
| 262 |
return cursor |
| 263 |
end |
| 264 |
|
| 265 |
local function makeGreyscaleGhost(pal) |
| 266 |
local remap = {} |
| 267 |
local stride = #pal / 256 |
| 268 |
assert(stride == 3 or stride == 4, "Palette length is not valid") |
| 269 |
-- Convert pal from a string to an array of palette entries |
| 270 |
local entries = {} |
| 271 |
for i = 1, #pal, stride do |
| 272 |
local entry = {pal:byte(i, i + 2)} -- R, G, B at [1], [2], [3] |
| 273 |
entries[(i - 1) / stride] = entry |
| 274 |
end |
| 275 |
-- For each palette entry, convert it to grey and then find the nearest |
| 276 |
-- entry in the palette to that grey. |
| 277 |
for i = 0, #entries do |
| 278 |
local entry = entries[i] |
| 279 |
local grey = entry[1] * 0.299 + entry[2] * 0.587 + entry[3] * 0.114 |
| 280 |
local grey_index = 0 |
| 281 |
local grey_diff = 100000 -- greater than 3*63^2 (TH uses 6 bit colour channels) |
| 282 |
for j = 0, #entries do |
| 283 |
local replace_entry = entries[j] |
| 284 |
local diff_r = replace_entry[1] - grey |
| 285 |
local diff_g = replace_entry[2] - grey |
| 286 |
local diff_b = replace_entry[3] - grey |
| 287 |
local diff = diff_r * diff_r + diff_g * diff_g + diff_b * diff_b |
| 288 |
if diff < grey_diff then |
| 289 |
grey_diff = diff |
| 290 |
grey_index = j |
| 291 |
end |
| 292 |
end |
| 293 |
remap[i] = string.char(grey_index) |
| 294 |
end |
| 295 |
-- Convert remap from an array to a string |
| 296 |
return table.concat(remap, "", 0, 255) |
| 297 |
end |
| 298 |
|
| 299 |
--! Load a palette file |
| 300 |
--!param dir (string) The directory of the palette relative to the HOSPITAL directory |
| 301 |
--!param name (string) The name of the palette file |
| 302 |
--!param transparent_255 (boolean) Whether the 255th entry in the palette should be transparent |
| 303 |
--!return (palette, string) The palette and a string representing the palette converted to greyscale |
| 304 |
function Graphics:_loadPalette(dir, name, transparent_255, pal8bit) |
| 305 |
name = name or "MPalette.dat" |
| 306 |
|
| 307 |
if self.cache.palette[name] then |
| 308 |
local li = self.load_info[self.cache.palette[name]] |
| 309 |
if li and li[5] ~= transparent_255 then |
| 310 |
print("Warning: palette " .. name .. " requested with different flags than stored") |
| 311 |
end |
| 312 |
|
| 313 |
return self.cache.palette[name], |
| 314 |
self.cache.palette_greyscale_ghost[name] |
| 315 |
end |
| 316 |
|
| 317 |
local data = self.app:readDataFile(dir or "Data", name) |
| 318 |
local palette = TH.palette(data, pal8bit) |
| 319 |
if transparent_255 then |
| 320 |
palette:setEntry(255, 0xFF, 0x00, 0xFF) |
| 321 |
end |
| 322 |
local ghost_palette = makeGreyscaleGhost(data) |
| 323 |
self.cache.palette[name] = palette |
| 324 |
self.cache.palette_greyscale_ghost[name] = ghost_palette |
| 325 |
|
| 326 |
permanent("palette_" .. name, palette) |
| 327 |
permanent("ghost_palette_" .. name, ghost_palette) |
| 328 |
|
| 329 |
return palette, self.cache.palette_greyscale_ghost[name] |
| 330 |
end |
| 331 |
|
| 332 |
function Graphics:getPalette(name) |
| 333 |
name = name or "MPalette.dat" |
| 334 |
if not self.cache.palette[name] then |
| 335 |
error("Palette " .. name .. " not found") |
| 336 |
end |
| 337 |
return self.cache.palette[name], self.cache.palette_greyscale_ghost[name] |
| 338 |
end |
| 339 |
|
| 340 |
function Graphics:allPalettes() |
| 341 |
return self.cache.palette |
| 342 |
end |
| 343 |
|
| 344 |
function Graphics:loadGhost(dir, name, index) |
| 345 |
local cached = self.cache.ghosts[name] |
| 346 |
if not cached then |
| 347 |
local data = self.app:readDataFile(dir, name) |
| 348 |
cached = data |
| 349 |
self.cache.ghosts[name] = cached |
| 350 |
end |
| 351 |
return cached:sub(index * 256 + 1, index * 256 + 256) |
| 352 |
end |
| 353 |
|
| 354 |
--! Load a bitmap from a dat file and palette |
| 355 |
--! |
| 356 |
--!param name (string) The file name of the bitmap without the .dat extension |
| 357 |
--!param width (int) The width of the bitmap. Defaults to 640 |
| 358 |
--!param height (int) The height of the bitmap. Defaults to 480 |
| 359 |
--!param dir (string) The directory of the bitmap. Defaults to QData |
| 360 |
--!param _paldir (string) ignored |
| 361 |
--!param pal (string) The name of the palette |
| 362 |
--!param _transparent_255 (boolean) ignored |
| 363 |
--!param flags (table) Additional flags for loading the bitmap |
| 364 |
function Graphics:loadRaw(name, width, height, dir, _paldir, pal, _transparent_255, flags) |
| 365 |
if self.cache.raw[name] then |
| 366 |
return self.cache.raw[name] |
| 367 |
end |
| 368 |
|
| 369 |
width = width or 640 |
| 370 |
height = height or 480 |
| 371 |
dir = dir or "QData" |
| 372 |
pal = pal or (name .. ".pal") |
| 373 |
local data = self.app:readDataFile(dir, name .. ".dat") |
| 374 |
data = data:sub(1, width * height) |
| 375 |
|
| 376 |
local bitmap = TH.bitmap() |
| 377 |
local palette = self:getPalette(pal) |
| 378 |
bitmap:setPalette(palette) |
| 379 |
assert(bitmap:load(data, width, self.target, flags)) |
| 380 |
|
| 381 |
local function bitmap_reloader(bm) |
| 382 |
bm:setPalette(palette) |
| 383 |
local bitmap_data = self.app:readDataFile(dir, name .. ".dat") |
| 384 |
bitmap_data = bitmap_data:sub(1, width * height) |
| 385 |
assert(bm:load(bitmap_data, width, self.target)) |
| 386 |
end |
| 387 |
self.reload_functions[bitmap] = bitmap_reloader |
| 388 |
|
| 389 |
self.cache.raw[name] = bitmap |
| 390 |
-- 0 in place of nil so that unpack doesn't break |
| 391 |
self.load_info[bitmap] = {self.loadRaw, self, name, width, height, dir, 0, pal, 0, flags} |
| 392 |
return bitmap |
| 393 |
end |
| 394 |
|
| 395 |
function Graphics:loadBuiltinFont() |
| 396 |
local font = self.builtin_font |
| 397 |
if not font then |
| 398 |
local dat, tab, pal = TH.GetBuiltinFont() |
| 399 |
local function dernc(x) |
| 400 |
if x:sub(1, 3) == "RNC" then |
| 401 |
return rnc.decompress(x) |
| 402 |
else |
| 403 |
return x |
| 404 |
end |
| 405 |
end |
| 406 |
local palette = TH.palette(dernc(pal)) |
| 407 |
local sheet = TH.sheet() |
| 408 |
sheet:setPalette(palette) |
| 409 |
sheet:load(dernc(tab), dernc(dat), true, self.target) |
| 410 |
font = TH.bitmap_font() |
| 411 |
font:setSheet(sheet, charsets["cp437"]) -- CorsixTH only ships with a cp437 font |
| 412 |
font:setSeparation(1, 0) |
| 413 |
font:setScaleFactor(TheApp.config.ui_scale) |
| 414 |
self.load_info[font] = {self.loadBuiltinFont, self} |
| 415 |
self.builtin_font = font |
| 416 |
end |
| 417 |
return font |
| 418 |
end |
| 419 |
|
| 420 |
function Graphics:hasLanguageFont(font) |
| 421 |
if font == nil then |
| 422 |
-- Original game fonts are always present. |
| 423 |
return true |
| 424 |
elseif charsets[font] == self.th_charset then |
| 425 |
return true |
| 426 |
else |
| 427 |
-- TODO: Handle more than one font |
| 428 |
return not not self.ttf_font_data |
| 429 |
end |
| 430 |
end |
| 431 |
|
| 432 |
--! Font proxy meta table wrapping the C++ class. |
| 433 |
local font_proxy_mt = { |
| 434 |
__index = { |
| 435 |
sizeOf = function(self, ...) |
| 436 |
return self._proxy:sizeOf(...) |
| 437 |
end, |
| 438 |
draw = function(self, ...) |
| 439 |
return self._proxy:draw(...) |
| 440 |
end, |
| 441 |
drawWrapped = function(self, ...) |
| 442 |
return self._proxy:drawWrapped(...) |
| 443 |
end, |
| 444 |
drawTooltip = function(self, ...) |
| 445 |
return self._proxy:drawTooltip(...) |
| 446 |
end, |
| 447 |
isBitmap = function(self) |
| 448 |
return self._proxy:isBitmap() |
| 449 |
end |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
function Graphics:onChangeLanguage() |
| 454 |
pause_gc_and_use_weak_keys(function(g) |
| 455 |
-- Some fonts might need changing between bitmap and freetype |
| 456 |
local load_info = g.load_info |
| 457 |
g.load_info = {} -- Any newly made objects are temporary, and shouldn't |
| 458 |
-- remember reload information (also avoids insertions |
| 459 |
-- into a table being iterated over). |
| 460 |
for object, info in pairs(load_info) do |
| 461 |
if object._proxy then |
| 462 |
local fn = info[1] |
| 463 |
local new_object = fn(unpack(info, 2)) |
| 464 |
object._proxy = new_object._proxy |
| 465 |
end |
| 466 |
end |
| 467 |
g.load_info = load_info |
| 468 |
end, self) |
| 469 |
end |
| 470 |
|
| 471 |
function Graphics:onChangeUIScale() |
| 472 |
if self.builtin_font then |
| 473 |
self.builtin_font:setScaleFactor(TheApp.config.ui_scale) |
| 474 |
end |
| 475 |
-- Update / replace fonts |
| 476 |
self:onChangeLanguage() |
| 477 |
end |
| 478 |
|
| 479 |
--! Font reload function. |
| 480 |
--!param font The font to (force) reloading. |
| 481 |
local function font_reloader(font) |
| 482 |
font:clearCache() |
| 483 |
end |
| 484 |
|
| 485 |
function Graphics:_isLanguageSupportedByTHAssets() |
| 486 |
return self.language_font == nil or charsets[self.language_font] == self.th_charset |
| 487 |
end |
| 488 |
|
| 489 |
--! Utility function to return preferred font for main menu ui |
| 490 |
function Graphics:loadMenuFont() |
| 491 |
-- Use the defined unicode font if set |
| 492 |
local font = self:hasLanguageFont("unicode") |
| 493 |
if font then |
| 494 |
font = self:loadLanguageFont(font, self:loadSpriteTable("QData", "Font01V"), { apply_ui_scale = true }) |
| 495 |
-- Otherwise, fall back to the built in font |
| 496 |
else |
| 497 |
font = self:loadBuiltinFont() |
| 498 |
end |
| 499 |
return font |
| 500 |
end |
| 501 |
|
| 502 |
local ttf_col_to_cache_key = function(ttf_color) |
| 503 |
if type(ttf_color) == "number" then |
| 504 |
return string.format("argb:%d", ttf_color) |
| 505 |
elseif type(ttf_color) == "table" then |
| 506 |
return string.format("a%d,r%d,g%d,b%d", |
| 507 |
ttf_color.alpha or 255, |
| 508 |
ttf_color.red or 0, |
| 509 |
ttf_color.green or 0, |
| 510 |
ttf_color.blue or 0) |
| 511 |
else |
| 512 |
return "nil" |
| 513 |
end |
| 514 |
end |
| 515 |
|
| 516 |
local shadow_to_cache_key = function(ttf_shadow) |
| 517 |
if type(ttf_shadow) == "table" then |
| 518 |
return string.format("sx%d,sy%d,s%s", |
| 519 |
ttf_shadow.offset_x or 1, |
| 520 |
ttf_shadow.offset_y or 1, |
| 521 |
ttf_col_to_cache_key(ttf_shadow.color)) |
| 522 |
elseif ttf_shadow == true then |
| 523 |
return "sx1,sy1,sa255,r0,g0,b0" |
| 524 |
end |
| 525 |
return "nil" |
| 526 |
end |
| 527 |
|
| 528 |
local language_font_cache_key = function(name, font_options) |
| 529 |
return string.format("%s,%d,%d,%s,%s,%s,%s", |
| 530 |
name or "nil", |
| 531 |
font_options.x_sep or 0, |
| 532 |
font_options.y_sep or 0, |
| 533 |
ttf_col_to_cache_key(font_options.ttf_color), |
| 534 |
font_options.force_bitmap and "T" or "F", |
| 535 |
shadow_to_cache_key(font_options.ttf_shadow), |
| 536 |
font_options.apply_ui_scale and "s" or "f") |
| 537 |
end |
| 538 |
|
| 539 |
--! Load a true type font |
| 540 |
--! |
| 541 |
--!param name (string) The name of the font from the language file. Always |
| 542 |
-- 'unicode'. |
| 543 |
--!param sprite_table (userdata) The sprite table of the font this font is |
| 544 |
-- replacing. Used for default colour and metrics. |
| 545 |
--!param font_options (table) A table of font options: |
| 546 |
-- x_sep (integer) The horizontal separation of the font. Defaults to 0. |
| 547 |
-- y_sep (integer) The vertical separation of the font. Defaults to 0. |
| 548 |
-- ttf_color (table) The colour of the font in form .red, .green and .blue. If |
| 549 |
-- nil, the colour will be detected from the sprite table. |
| 550 |
-- force_bitmap (boolean) Whether to force the font to be a bitmap font. If |
| 551 |
-- nil, this will be set to true for Font05V if the language uses Arabic |
| 552 |
-- numerals. |
| 553 |
-- ttf_width (integer) The nominal width of the font, in pixels. If nil, the |
| 554 |
-- width will be detected from the sprite table. |
| 555 |
-- ttf_height (integer) The nominal height of the font, in pixels. If nil, the |
| 556 |
-- height will be detected from the sprite table. |
| 557 |
-- ttf_shadow (table|boolean) table with fields offset_x, offset_y and color. |
| 558 |
-- The color field has the same format as ttf_color. If this field is |
| 559 |
-- present, the font will be drawn with a shadow of the specified colour and |
| 560 |
-- offset. color is optional and defaults to opaque black. offset_x and |
| 561 |
-- offset_y are optional and default to 1. If the field is omitted entirely, |
| 562 |
-- no shadow is drawn. Alternately ttf_shadow can be set to true to draw a |
| 563 |
-- shadow with default parameters, or false to disable any shadow. |
| 564 |
-- apply_ui_scale (boolean) Whether to apply the UI scale factor to the font |
| 565 |
-- size. |
| 566 |
--!param y_sep (int) Deprecated: use font_options.y_sep instead. |
| 567 |
--!param ttf_color (table) Deprecated: use font_options.ttf_color instead. |
| 568 |
--!param force_bitmap (boolean) Deprecated: use font_options.force_bitmap |
| 569 |
-- instead. |
| 570 |
function Graphics:loadLanguageFont(name, sprite_table, font_options, y_sep, ttf_color, force_bitmap) |
| 571 |
-- Afterload fix for saves before 232 |
| 572 |
if font_options == nil or type(font_options) == "number" then |
| 573 |
local fo = { |
| 574 |
x_sep = font_options, |
| 575 |
y_sep = y_sep, |
| 576 |
ttf_color = ttf_color, |
| 577 |
force_bitmap = force_bitmap |
| 578 |
} |
| 579 |
font_options = fo |
| 580 |
end |
| 581 |
|
| 582 |
if name == nil or charsets[name] == self.th_charset then |
| 583 |
return self:loadFont(sprite_table, font_options) |
| 584 |
end |
| 585 |
|
| 586 |
local font = self:_loadTrueTypeFont("unicode", sprite_table, font_options) |
| 587 |
|
| 588 |
-- A change of language or scale might cause the font to change, |
| 589 |
-- so wrap it in a proxy object which allows the actual object to |
| 590 |
-- be changed easily. |
| 591 |
font = setmetatable({ _proxy = font }, font_proxy_mt) |
| 592 |
self.load_info[font] = {self.loadLanguageFont, self, name, sprite_table, font_options} |
| 593 |
return font |
| 594 |
end |
| 595 |
|
| 596 |
--! Load a true type font |
| 597 |
--! |
| 598 |
--!param name (string) The name of the font from the language file. Always |
| 599 |
-- 'unicode'. |
| 600 |
--!param sprite_table (userdata) The sprite table of the font this font is |
| 601 |
-- replacing. Used for default colour and metrics. |
| 602 |
--!param font_options (table) A table of font options: |
| 603 |
-- x_sep (integer) The horizontal separation of the font. Defaults to 0. |
| 604 |
-- y_sep (integer) The vertical separation of the font. Defaults to 0. |
| 605 |
-- ttf_color (table) The colour of the font in form .red, .green and .blue. If |
| 606 |
-- nil, the colour will be detected from the sprite table. |
| 607 |
-- ttf_width (integer) The nominal width of the font, in pixels. If nil, the |
| 608 |
-- width will be detected from the sprite table. |
| 609 |
-- ttf_height (integer) The nominal height of the font, in pixels. If nil, the |
| 610 |
-- height will be detected from the sprite table. |
| 611 |
-- ttf_shadow (table|boolean) table with fields offset_x, offset_y and color. |
| 612 |
-- The color field has the same format as ttf_color. If this field is |
| 613 |
-- present, the font will be drawn with a shadow of the specified colour and |
| 614 |
-- offset. color is optional and defaults to opaque black. offset_x and |
| 615 |
-- offset_y are optional and default to 1. If the field is omitted entirely, |
| 616 |
-- no shadow is drawn. Alternately ttf_shadow can be set to true to draw a |
| 617 |
-- shadow with default parameters, or false to disable any shadow. |
| 618 |
-- apply_ui_scale (boolean) Whether to apply the UI scale factor to the font |
| 619 |
-- size. |
| 620 |
function Graphics:_loadTrueTypeFont(name, sprite_table, font_options) |
| 621 |
local cache_key = language_font_cache_key(name, font_options) |
| 622 |
local cache = self.cache.language_fonts[cache_key] |
| 623 |
local font = cache and cache[sprite_table] |
| 624 |
|
| 625 |
if font and font_options.apply_ui_scale and font_options.scale_factor ~= self.app.config.ui_scale then |
| 626 |
font_options.scale_factor = self.app.config.ui_scale |
| 627 |
font:setFontOptions(sprite_table, font_options) |
| 628 |
font:clearCache() |
| 629 |
elseif not font then |
| 630 |
if font_options.apply_ui_scale then |
| 631 |
font_options.scale_factor = self.app.config.ui_scale |
| 632 |
end |
| 633 |
|
| 634 |
font = TH.freetype_font() |
| 635 |
-- TODO: Choose face based on "name" rather than always using same face. |
| 636 |
font:setFace(self.ttf_font_data) |
| 637 |
font:setFontOptions(sprite_table, font_options) |
| 638 |
self.reload_functions_last[font] = font_reloader |
| 639 |
|
| 640 |
if not cache then |
| 641 |
cache = {} |
| 642 |
self.cache.language_fonts[cache_key] = cache |
| 643 |
end |
| 644 |
cache[sprite_table] = font |
| 645 |
end |
| 646 |
return font |
| 647 |
end |
| 648 |
|
| 649 |
--! Load a font and the corresponding sprite sheet in one operation. |
| 650 |
--! |
| 651 |
--! This is the same as calling loadFont(Graphics:loadSpriteTable(dir, name, complex, palette), font_options) |
| 652 |
--! except that force_bitmap is set to true automatically for some sprite tables. |
| 653 |
--! |
| 654 |
--!param dir (string) The directory of the sprite table relative to the HOSPITAL directory. |
| 655 |
--!param name (string) The name of the sprite table file without the .dat extension. |
| 656 |
--!param complex (boolean) Whether the sprite table is complex or not. |
| 657 |
--!param palette (string) The name of the palette file. If nil, the default palette will be used. |
| 658 |
--!param font_options (table) A table of font options: |
| 659 |
-- x_sep (integer) The horizontal separation of the font. Defaults to 0. |
| 660 |
-- y_sep (integer) The vertical separation of the font. Defaults to 0. |
| 661 |
-- ttf_color (table) The colour of the font in form .red, .green and .blue. If |
| 662 |
-- nil, the colour will be detected from the sprite table. |
| 663 |
-- force_bitmap (boolean) Whether to force the font to be a bitmap font. If |
| 664 |
-- nil, this will be set to true for Font05V if the language uses Arabic |
| 665 |
-- numerals. |
| 666 |
-- ttf_width (integer) The nominal width of the font, in pixels. If nil, the |
| 667 |
-- width will be detected from the sprite table. |
| 668 |
-- ttf_height (integer) The nominal height of the font, in pixels. If nil, the |
| 669 |
-- height will be detected from the sprite table. |
| 670 |
-- ttf_shadow (table|boolean) table with fields offset_x, offset_y and color. |
| 671 |
-- The color field has the same format as ttf_color. If this field is |
| 672 |
-- present, the font will be drawn with a shadow of the specified colour and |
| 673 |
-- offset. color is optional and defaults to opaque black. offset_x and |
| 674 |
-- offset_y are optional and default to 1. If the field is omitted entirely, |
| 675 |
-- no shadow is drawn. Alternately ttf_shadow can be set to true to draw a |
| 676 |
-- shadow with default parameters, or false to disable any shadow. |
| 677 |
-- apply_ui_scale (boolean) Whether to apply the UI scale factor to the font |
| 678 |
-- size. |
| 679 |
function Graphics:loadFontAndSpriteTable(dir, name, complex, palette, font_options) |
| 680 |
local sprite_table = self:loadSpriteTable(dir, name, complex, palette) |
| 681 |
|
| 682 |
font_options = font_options or {} |
| 683 |
if font_options.force_bitmap == nil then |
| 684 |
font_options.force_bitmap = (name == "Font05V" and self:arabicNumerals()) |
| 685 |
end |
| 686 |
|
| 687 |
return self:loadFont(sprite_table, font_options) |
| 688 |
end |
| 689 |
|
| 690 |
--! Load a font for the given sprite table. |
| 691 |
--! |
| 692 |
--!param sprite_table (userdata) The sprite table of the font this font is replacing. Used for default colour and metrics. |
| 693 |
--!param font_options (table) A table of font options: |
| 694 |
-- x_sep (integer) The horizontal separation of the font. Defaults to 0. |
| 695 |
-- y_sep (integer) The vertical separation of the font. Defaults to 0. |
| 696 |
-- ttf_color (table) The colour of the font in form .red, .green and .blue. If |
| 697 |
-- nil, the colour will be detected from the sprite table. |
| 698 |
-- force_bitmap (boolean) Whether to force the font to be a bitmap font. If |
| 699 |
-- nil, this will be set to true for Font05V if the language uses Arabic |
| 700 |
-- numerals. |
| 701 |
-- ttf_width (integer) The nominal width of the font, in pixels. If nil, the |
| 702 |
-- width will be detected from the sprite table. |
| 703 |
-- ttf_height (integer) The nominal height of the font, in pixels. If nil, the |
| 704 |
-- height will be detected from the sprite table. |
| 705 |
-- ttf_shadow (table|boolean) table with fields offset_x, offset_y and color. |
| 706 |
-- The color field has the same format as ttf_color. If this field is |
| 707 |
-- present, the font will be drawn with a shadow of the specified colour and |
| 708 |
-- offset. color is optional and defaults to opaque black. offset_x and |
| 709 |
-- offset_y are optional and default to 1. If the field is omitted entirely, |
| 710 |
-- no shadow is drawn. Alternately ttf_shadow can be set to true to draw a |
| 711 |
-- shadow with default parameters, or false to disable any shadow. |
| 712 |
-- apply_ui_scale (boolean) Whether to apply the UI scale factor to the font |
| 713 |
-- size. |
| 714 |
function Graphics:loadFont(sprite_table, font_options, y_sep, ttf_color, force_bitmap) |
| 715 |
|
| 716 |
-- afterLoad fix. Saves before 215 would double the y_sep argument, but did not have ttf_col |
| 717 |
if type(ttf_color) == "number" then |
| 718 |
ttf_color = nil |
| 719 |
end |
| 720 |
|
| 721 |
-- Afterload fix for saves before 232 |
| 722 |
if font_options == nil or type(font_options) == "number" then |
| 723 |
local fo = { |
| 724 |
x_sep = font_options, |
| 725 |
y_sep = y_sep, |
| 726 |
ttf_color = ttf_color, |
| 727 |
force_bitmap = force_bitmap |
| 728 |
} |
| 729 |
font_options = fo |
| 730 |
end |
| 731 |
|
| 732 |
-- Use a bitmap font if forced, or the language uses bitmap, or the |
| 733 |
-- sprite_table has no M in it, indicating it's probably a symbol file. |
| 734 |
local use_bitmap_font = font_options.force_bitmap or |
| 735 |
self:_isLanguageSupportedByTHAssets() or not sprite_table:isVisible(46) |
| 736 |
local font |
| 737 |
if use_bitmap_font then |
| 738 |
font = TH.bitmap_font() |
| 739 |
font:setSeparation(font_options.x_sep or 0, font_options.y_sep or 0) |
| 740 |
font:setSheet(sprite_table, self.th_charset) |
| 741 |
if font_options.apply_ui_scale then |
| 742 |
font:setScaleFactor(TheApp.config.ui_scale) |
| 743 |
end |
| 744 |
else |
| 745 |
font = self:_loadTrueTypeFont("unicode", sprite_table, font_options) |
| 746 |
end |
| 747 |
-- A change of language or scale might cause the font to change, |
| 748 |
-- so wrap it in a proxy object which allows the actual object to |
| 749 |
-- be changed easily. |
| 750 |
font = setmetatable({ _proxy = font }, font_proxy_mt) |
| 751 |
self.load_info[font] = { self.loadFont, self, sprite_table, font_options } |
| 752 |
return font |
| 753 |
end |
| 754 |
|
| 755 |
function Graphics:arabicNumerals() |
| 756 |
local strings = self.app.strings |
| 757 |
local language = self.app.config.language |
| 758 |
local arabic_numerals = strings:isArabicNumerals(language) |
| 759 |
return arabic_numerals |
| 760 |
end |
| 761 |
|
| 762 |
function Graphics:drawNumbersFromUnicode() |
| 763 |
return self.language_font and not self:arabicNumerals() |
| 764 |
end |
| 765 |
|
| 766 |
function Graphics:loadAnimations(dir, prefix) |
| 767 |
if self.cache.anims[prefix] then |
| 768 |
return self.cache.anims[prefix] |
| 769 |
end |
| 770 |
|
| 771 |
--! Load a custom animation file (if it can be found) |
| 772 |
--!param path Path to the file. |
| 773 |
local function loadCustomAnims(path) |
| 774 |
local file, err = io.open(path, "rb") |
| 775 |
if not file then |
| 776 |
return nil, err |
| 777 |
end |
| 778 |
local data = file:read("*a") |
| 779 |
file:close() |
| 780 |
return data |
| 781 |
end |
| 782 |
|
| 783 |
local sheet = self:loadSpriteTable(dir, prefix .. "Spr-0") |
| 784 |
local anims = TH.anims() |
| 785 |
anims:setSheet(sheet) |
| 786 |
if not anims:load( |
| 787 |
self.app:readDataFile(dir, prefix .. "Start-1.ani"), |
| 788 |
self.app:readDataFile(dir, prefix .. "Fra-1.ani"), |
| 789 |
self.app:readDataFile(dir, prefix .. "List-1.ani"), |
| 790 |
self.app:readDataFile(dir, prefix .. "Ele-1.ani")) |
| 791 |
then |
| 792 |
error("Cannot load original animations " .. prefix) |
| 793 |
end |
| 794 |
|
| 795 |
if self.custom_graphics_folder and self.custom_graphics.file_mapping then |
| 796 |
for _, fname in pairs(self.custom_graphics.file_mapping) do |
| 797 |
anims:setCanvas(self.target) |
| 798 |
local data, err = loadCustomAnims(self.custom_graphics_folder .. fname) |
| 799 |
if not data then |
| 800 |
print("Error when loading custom animations:\n" .. err) |
| 801 |
elseif not anims:loadCustom(data) then |
| 802 |
print("Warning: custom animations loading failed") |
| 803 |
end |
| 804 |
end |
| 805 |
end |
| 806 |
|
| 807 |
self.cache.anims[prefix] = anims |
| 808 |
self.load_info[anims] = {self.loadAnimations, self, dir, prefix} |
| 809 |
return anims |
| 810 |
end |
| 811 |
|
| 812 |
function Graphics:loadSpriteTable(dir, name, complex, palette) |
| 813 |
local cached = self.cache.tabled[name] |
| 814 |
if cached then |
| 815 |
return cached |
| 816 |
end |
| 817 |
|
| 818 |
local function sheet_reloader(sheet) |
| 819 |
sheet:setPalette(palette or self.cache.palette["MPalette.dat"]) |
| 820 |
local data_tab, data_dat |
| 821 |
data_tab = self.app:readDataFile(dir, name .. ".tab") |
| 822 |
data_dat = self.app:readDataFile(dir, name .. ".dat") |
| 823 |
if not sheet:load(data_tab, data_dat, complex, self.target) then |
| 824 |
error("Cannot load sprite sheet " .. dir .. ":" .. name) |
| 825 |
end |
| 826 |
end |
| 827 |
local sheet = TH.sheet() |
| 828 |
self.reload_functions[sheet] = sheet_reloader |
| 829 |
sheet_reloader(sheet) |
| 830 |
|
| 831 |
if name ~= "SPointer" then |
| 832 |
self.cache.tabled[name] = sheet |
| 833 |
end |
| 834 |
self.load_info[sheet] = {self.loadSpriteTable, self, dir, name, complex, palette} |
| 835 |
return sheet |
| 836 |
end |
| 837 |
|
| 838 |
function Graphics:updateTarget(target) |
| 839 |
self.target = target |
| 840 |
pause_gc_and_use_weak_keys(function(g) |
| 841 |
for _, res_set in ipairs({"reload_functions", "reload_functions_last"}) do |
| 842 |
for resource, reloader in pairs(g[res_set]) do |
| 843 |
reloader(resource) |
| 844 |
end |
| 845 |
end |
| 846 |
end, self) |
| 847 |
end |
| 848 |
|
| 849 |
--! Utility class for setting animation markers and querying animation length. |
| 850 |
class "AnimationManager" |
| 851 |
|
| 852 |
---@type AnimationManager |
| 853 |
local AnimationManager = _G["AnimationManager"] |
| 854 |
|
| 855 |
function AnimationManager:AnimationManager(anims) |
| 856 |
self.anim_length_cache = {} |
| 857 |
self.anims = anims |
| 858 |
end |
| 859 |
|
| 860 |
--! For overriding animations which have builtin repeats or excess frames |
| 861 |
function AnimationManager:setAnimLength(anim, length) |
| 862 |
self.anim_length_cache[anim] = length |
| 863 |
end |
| 864 |
|
| 865 |
function AnimationManager:getAnimLength(anim) |
| 866 |
if not self.anim_length_cache[anim] then |
| 867 |
local length = 0 |
| 868 |
local seen = {} |
| 869 |
local frame = self.anims:getFirstFrame(anim) |
| 870 |
while not seen[frame] do |
| 871 |
seen[frame] = true |
| 872 |
length = length + 1 |
| 873 |
frame = self.anims:getNextFrame(frame) |
| 874 |
end |
| 875 |
self.anim_length_cache[anim] = length |
| 876 |
end |
| 877 |
return self.anim_length_cache[anim] |
| 878 |
end |
| 879 |
|
| 880 |
--[[ A marker position can be used to indicate the position of en entity in the |
| 881 |
frame of an animation relative to the position of drawing the animation. |
| 882 |
|
| 883 |
The marker position is used to display a humanoid in the circular area of its |
| 884 |
window. It is also used to position mood icons or smoke of a machine above |
| 885 |
the entity. |
| 886 |
|
| 887 |
There are two marker positions associated with each frame of an animation. |
| 888 |
The primary marker is reserved for patients and machines. The secondary |
| 889 |
marker is used for staff and other officials such as VIPs and inspectors. |
| 890 |
|
| 891 |
Markers are set as part of the animation definitions of entities with code like |
| 892 |
|
| 893 |
-- Sets the primary markers of all frames of an animation: |
| 894 |
local anim_mgr = TheApp.animation_manager |
| 895 |
anim_mgr:setPatientMarker(...) |
| 896 |
|
| 897 |
-- Sets the secondary markers of all frames of an animation: |
| 898 |
local anim_mgr = TheApp.animation_manager |
| 899 |
anim_mgr:setStaffMarker(...) |
| 900 |
|
| 901 |
Markers can be set using a variety of different arguments: |
| 902 |
* set{Patient/Staff}Marker(anim_number, position) |
| 903 |
* set{Patient/Staff}Marker(anim_number, {position1, position2, ..., positionN}) |
| 904 |
* set{Patient/Staff}Marker(anim_number, start_position, end_position) |
| 905 |
* set{Patient/Staff}Marker(anim_number, keyframe_1, keyframe_1_position, keyframe_2, ...) |
| 906 |
|
| 907 |
The 'position' should be a table; An {x, y} pair for a tile position or |
| 908 |
{x, y, "px"} for a pixel position. In both cases the position should be the |
| 909 |
humanoid centre at floor level, with the centre of the (0, 0) tile being the |
| 910 |
origin in both cases. |
| 911 |
|
| 912 |
The first variant of set{Patient,Staff}Marker sets the same marker for each frame. |
| 913 |
The second variant of has a unique position for each frame. 'nil' can be used to |
| 914 |
repeat the previous position. |
| 915 |
The third variant does linear interpolation of the two positions between the first |
| 916 |
frame and the last frame. |
| 917 |
The fourth variant does linear interpolation between keyframes, and then the final |
| 918 |
position for frames after the last keyframe. The keyframe arguments should be |
| 919 |
0-based integers, as in the animation viewer. |
| 920 |
|
| 921 |
To set the markers for multiple animations at once, the anim_number argument |
| 922 |
can be a table, in which case the marker is set for all values in the table. |
| 923 |
Alternatively, the values function (defined in utility.lua) can be used in |
| 924 |
conjection with a for loop to set markers for multiple things. |
| 925 |
|
| 926 |
The AnimView program can be used to easily obtain position information in frames. |
| 927 |
--]] |
| 928 |
|
| 929 |
--! Define the centre of a patient at floor level in an animation. |
| 930 |
function AnimationManager:setPatientMarker(anim, ...) |
| 931 |
self:_unfoldAnims(anim, "setFramePrimaryMarker", ...) |
| 932 |
end |
| 933 |
|
| 934 |
--! Define the centre of a staff member at floor level in an animation. |
| 935 |
function AnimationManager:setStaffMarker(anim, ...) |
| 936 |
self:_unfoldAnims(anim, "setFrameSecondaryMarker", ...) |
| 937 |
end |
| 938 |
|
| 939 |
--! Convert a humanoid position to a pixel offset. |
| 940 |
--!param pos (table) Humanoid center position to convert. Is either a tile |
| 941 |
-- position of the form {x, y} with floating point numbers, or a pixel |
| 942 |
-- position of the form {x, y, "px"} with integer numbers. In both cases, |
| 943 |
-- the origin is at the center of tile (0, 0), at floor level. |
| 944 |
--!return (int, int) The computed offset wrt to the origin in pixels. |
| 945 |
local function positionToXy(pos) |
| 946 |
if pos[3] == "px" then |
| 947 |
return pos[1], pos[2] |
| 948 |
else |
| 949 |
local x, y = Map:WorldToScreen(pos[1] + 1, pos[2] + 1) |
| 950 |
return math.floor(x), math.floor(y) |
| 951 |
end |
| 952 |
end |
| 953 |
|
| 954 |
--! Unfold tables containing animation numbers. |
| 955 |
function AnimationManager:_unfoldAnims(anim, fn, ...) |
| 956 |
if not anim then return end |
| 957 |
|
| 958 |
if type(anim) == "table" then |
| 959 |
for _, val in pairs(anim) do |
| 960 |
self:_unfoldAnims(val, fn, ...) |
| 961 |
end |
| 962 |
else |
| 963 |
self:setMarkerRaw(anim, fn, ...) |
| 964 |
end |
| 965 |
end |
| 966 |
|
| 967 |
function AnimationManager:setMarkerRaw(anim, fn, arg1, arg2, ...) |
| 968 |
local anim_length = self:getAnimLength(anim) |
| 969 |
local frame = self.anims:getFirstFrame(anim) |
| 970 |
|
| 971 |
--! Set marker position for frame |
| 972 |
--!param x1y1 (boolean) use values given by x1 and y1 only if true, otherwise use |
| 973 |
-- linear interpolation between x1/y1 and x2/y2 |
| 974 |
--!param x1, y1, x2 (optional), y2 (optional) pixel positions |
| 975 |
--!param n fraction between start and end frame of linear interpolation |
| 976 |
local function setMarkerFramePosition(x1y1, x1, y1, x2, y2, n) |
| 977 |
assert(type(x1) == "number" and type(y1) == "number", |
| 978 |
"Anim " .. anim .. ", frame " .. frame .. " has missing marker position") |
| 979 |
local pos_x = x1y1 and x1 or math.floor((x2 - x1) * n + x1) |
| 980 |
local pos_y = x1y1 and y1 or math.floor((y2 - y1) * n + y1) |
| 981 |
self.anims[fn](self.anims, frame, pos_x, pos_y) |
| 982 |
end |
| 983 |
|
| 984 |
local tp_arg1 = type(arg1) |
| 985 |
if tp_arg1 == "table" then |
| 986 |
if arg2 then |
| 987 |
-- Linear-interpolation positions |
| 988 |
local x1, y1 = positionToXy(arg1) |
| 989 |
local x2, y2 = positionToXy(arg2) |
| 990 |
for i = 0, anim_length - 1 do |
| 991 |
local n = i / (anim_length - 1) |
| 992 |
setMarkerFramePosition(false, x1, y1, x2, y2, n) |
| 993 |
frame = self.anims:getNextFrame(frame) |
| 994 |
end |
| 995 |
elseif type(arg1[1]) == "table" then |
| 996 |
-- A position for each frame. |
| 997 |
local x, y |
| 998 |
for rel_frame = 1, anim_length do |
| 999 |
if arg1[rel_frame] then |
| 1000 |
x, y = positionToXy(arg1[rel_frame]) |
| 1001 |
end |
| 1002 |
setMarkerFramePosition(true, x, y) |
| 1003 |
frame = self.anims:getNextFrame(frame) |
| 1004 |
end |
| 1005 |
else |
| 1006 |
-- Static position |
| 1007 |
local x, y = positionToXy(arg1) |
| 1008 |
for _ = 1, anim_length do |
| 1009 |
setMarkerFramePosition(true, x, y) |
| 1010 |
frame = self.anims:getNextFrame(frame) |
| 1011 |
end |
| 1012 |
end |
| 1013 |
elseif tp_arg1 == "number" then |
| 1014 |
-- Keyframe positions, using liner-interpolation |
| 1015 |
local f1, x1, y1 = 0, 0, 0 |
| 1016 |
local args |
| 1017 |
if arg1 == 0 then |
| 1018 |
x1, y1 = positionToXy(arg2) |
| 1019 |
args = {...} |
| 1020 |
else |
| 1021 |
args = {arg1, arg2, ...} |
| 1022 |
end |
| 1023 |
local f2, x2, y2 |
| 1024 |
local args_i = 1 |
| 1025 |
for f = 0, anim_length - 1 do |
| 1026 |
if f2 and f == f2 then |
| 1027 |
f1, x1, y1 = f2, x2, y2 |
| 1028 |
f2, x2, y2 = nil, nil, nil |
| 1029 |
end |
| 1030 |
if not f2 then |
| 1031 |
f2 = args[args_i] |
| 1032 |
if f2 then |
| 1033 |
x2, y2 = positionToXy(args[args_i + 1]) |
| 1034 |
args_i = args_i + 2 |
| 1035 |
end |
| 1036 |
end |
| 1037 |
if f2 then |
| 1038 |
local n = (f - f1) / (f2 - f1) |
| 1039 |
setMarkerFramePosition(false, x1, y1, x2, y2, n) |
| 1040 |
else |
| 1041 |
setMarkerFramePosition(true, x1, y1) |
| 1042 |
end |
| 1043 |
frame = self.anims:getNextFrame(frame) |
| 1044 |
end |
| 1045 |
else |
| 1046 |
error("Invalid arguments to setMarker", 2) |
| 1047 |
end |
| 1048 |
end |
| 1049 |
|
| 1050 |
-- Kept for load compatibility |
| 1051 |
function Graphics:loadPalette(_, name) |
| 1052 |
-- Was named PREF01V.PAL in ui.lua until version 240 |
| 1053 |
if name == "PREF01V.PAL" then |
| 1054 |
name = "Pref01V.pal" |
| 1055 |
end |
| 1056 |
return self:getPalette(name) |
| 1057 |
end |