| 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 |
-- This is a poor replacement for the sprite viewing facility of AnimView, but |
| 22 |
-- when you don't have a copy of AnimView, or the means to compile it, then a |
| 23 |
-- crude sprite viewer is better than no sprite viewer. |
| 24 |
|
| 25 |
local is_open = false |
| 26 |
local gfx = TheApp.gfx |
| 27 |
local font -- Set on open |
| 28 |
local need_draw = true |
| 29 |
local sprite_table_paths = {} |
| 30 |
local palettes = {} |
| 31 |
local palette_index = 1 |
| 32 |
local palette_name |
| 33 |
local sprite_table_index |
| 34 |
local sprite_table |
| 35 |
local is_complex = false |
| 36 |
local wdown = false |
| 37 |
local sdown = false |
| 38 |
local scale = 1 |
| 39 |
local bg_colour_index = 1 |
| 40 |
local y_off |
| 41 |
local old_event_handlers |
| 42 |
local old_gfx_cache = {} |
| 43 |
local sprite_viewer_gfx_cache = {} |
| 44 |
local mpalette_index |
| 45 |
|
| 46 |
local background_colours = { |
| 47 |
Colours.Black, |
| 48 |
Colours.White, |
| 49 |
Colours.Magenta, |
| 50 |
Colours.Cyan, |
| 51 |
Colours.Yellow, |
| 52 |
} |
| 53 |
|
| 54 |
for _, dir in ipairs({"Data", "QData", "DataM", "QDataM"}) do |
| 55 |
for item in pairs(TheApp.fs:listFiles(dir) or {}) do |
| 56 |
if item:match("%.TAB$") then |
| 57 |
local basename = item:sub(1, -5) |
| 58 |
if TheApp.fs:fileExists(dir, basename .. ".DAT") then |
| 59 |
sprite_table_paths[#sprite_table_paths + 1] = {dir, item:sub(1, -5)} |
| 60 |
else |
| 61 |
print("Sprite table " .. dir .. "/" .. item .. " has no matching .DAT file. Skipping.") |
| 62 |
end |
| 63 |
end |
| 64 |
end |
| 65 |
end |
| 66 |
table.sort(sprite_table_paths, function(lhs, rhs) |
| 67 |
return lhs[1] < rhs[1] or (lhs[1] == rhs[1] and lhs[2] < rhs[2]) |
| 68 |
end) |
| 69 |
|
| 70 |
for name, _ in pairs(TheApp.gfx:allPalettes()) do |
| 71 |
palettes[#palettes + 1] = name |
| 72 |
end |
| 73 |
table.sort(palettes) |
| 74 |
|
| 75 |
for i, name in ipairs(palettes) do |
| 76 |
if name == "MPalette.dat" then |
| 77 |
mpalette_index = i |
| 78 |
break |
| 79 |
end |
| 80 |
end |
| 81 |
|
| 82 |
-- Find the palette with the longest common prefix with the sprite table path, |
| 83 |
-- as this is likely the correct palette to use for the sprite table. |
| 84 |
-- Avoids hardcoding a mapping of sprites to palettes and accounts for THs |
| 85 |
-- naming e.g. AWARD01.PAL and AWARD02.PAL used for AWARD03.TAB |
| 86 |
-- |
| 87 |
--!param sprite_table_name (string) The name of the sprite table, without the |
| 88 |
-- .TAB extension |
| 89 |
--!return (number) The index of the closest matching palette in the palettes |
| 90 |
local function closest_matching_palette(sprite_table_name) |
| 91 |
local best_score = 0 |
| 92 |
local best_index |
| 93 |
local lower_st = sprite_table_name:lower() |
| 94 |
|
| 95 |
for i, pal_name in ipairs(palettes) do |
| 96 |
local score = 0 |
| 97 |
local lower_pal = pal_name:lower() |
| 98 |
|
| 99 |
for j = 3, math.min(#lower_pal, #lower_st) do |
| 100 |
if lower_pal:sub(1, j) == lower_st:sub(1, j) then |
| 101 |
score = j |
| 102 |
else |
| 103 |
break |
| 104 |
end |
| 105 |
end |
| 106 |
if score > best_score then |
| 107 |
best_score = score |
| 108 |
best_index = i |
| 109 |
end |
| 110 |
end |
| 111 |
|
| 112 |
return best_index |
| 113 |
end |
| 114 |
|
| 115 |
local function LoadTable(n, complex, detect_palette) |
| 116 |
sprite_table_index = n |
| 117 |
is_complex = complex |
| 118 |
local path = sprite_table_paths[n] |
| 119 |
local pal |
| 120 |
if detect_palette then |
| 121 |
palette_index = closest_matching_palette(path[2]) or mpalette_index |
| 122 |
end |
| 123 |
palette_name = palettes[palette_index] |
| 124 |
pal = gfx:getPalette(palette_name) |
| 125 |
sprite_table = gfx:loadSpriteTable(path[1], path[2], complex, pal) |
| 126 |
need_draw = true |
| 127 |
y_off = 0 |
| 128 |
end |
| 129 |
LoadTable(1, false, true) |
| 130 |
|
| 131 |
local function DoKey(_, rawchar) |
| 132 |
local key = rawchar:lower() |
| 133 |
if key == "c" then |
| 134 |
gfx.cache.tabled = {} |
| 135 |
LoadTable(sprite_table_index, not is_complex, false) |
| 136 |
elseif key == "a" then |
| 137 |
if sprite_table_index > 1 then |
| 138 |
LoadTable(sprite_table_index - 1, is_complex, true) |
| 139 |
end |
| 140 |
elseif key == "d" then |
| 141 |
if sprite_table_index < #sprite_table_paths then |
| 142 |
LoadTable(sprite_table_index + 1, is_complex, true) |
| 143 |
end |
| 144 |
elseif key == "p" then |
| 145 |
palette_index = (palette_index or 1) % #palettes + 1 |
| 146 |
gfx.cache.tabled = {} |
| 147 |
LoadTable(sprite_table_index, is_complex, false) |
| 148 |
elseif key == "w" then |
| 149 |
wdown = true |
| 150 |
need_draw = true |
| 151 |
elseif key == "s" then |
| 152 |
sdown = true |
| 153 |
need_draw = true |
| 154 |
elseif key == "-" then |
| 155 |
if scale > 1 then |
| 156 |
scale = scale - 1 |
| 157 |
need_draw = true |
| 158 |
end |
| 159 |
elseif key == "=" then |
| 160 |
if scale < 8 then |
| 161 |
scale = scale + 1 |
| 162 |
need_draw = true |
| 163 |
end |
| 164 |
elseif key == "b" then |
| 165 |
bg_colour_index = bg_colour_index % #background_colours + 1 |
| 166 |
need_draw = true |
| 167 |
end |
| 168 |
return need_draw |
| 169 |
end |
| 170 |
|
| 171 |
local function DoKeyUp(_, rawchar) |
| 172 |
local key = rawchar:lower() |
| 173 |
if key == "q" then -- Exit sprite viewer |
| 174 |
TheApp.eventHandlers = old_event_handlers |
| 175 |
sprite_viewer_gfx_cache = gfx.cache.tabled |
| 176 |
gfx.cache.tabled = old_gfx_cache |
| 177 |
is_open = false |
| 178 |
return |
| 179 |
end |
| 180 |
if key == "w" then |
| 181 |
wdown = false |
| 182 |
end |
| 183 |
if key == "s" then |
| 184 |
sdown = false |
| 185 |
end |
| 186 |
end |
| 187 |
|
| 188 |
local function DoTextInput() |
| 189 |
return false |
| 190 |
end |
| 191 |
|
| 192 |
local function Render(canvas) |
| 193 |
local encoding = is_complex and " (Complex) " or " (Simple) " |
| 194 |
local msg = table.concat(sprite_table_paths[sprite_table_index], package.config:sub(1, 1)) .. encoding .. palette_name |
| 195 |
local _, fonth = font:sizeOf(msg) |
| 196 |
local sep = 2 |
| 197 |
local y = y_off |
| 198 |
font:draw(canvas, "CorsixTH Debug Sprite Viewer", 0, y) |
| 199 |
y = y + (fonth + sep) |
| 200 |
font:draw(canvas, "W/A/S/D to navigate, C to change mode, P to switch palette, -/= to scale, B to change background, Q to quit", 0, y) |
| 201 |
y = y + (fonth + sep) |
| 202 |
font:draw(canvas, msg, 0, y) |
| 203 |
y = y + fonth + sep |
| 204 |
local x = 0 |
| 205 |
local sw, sh = TheApp.config.width, TheApp.config.height |
| 206 |
local tallest = 0 |
| 207 |
for i = 0, #sprite_table - 1 do |
| 208 |
local w, h = sprite_table:size(i) |
| 209 |
w = w * scale |
| 210 |
h = h * scale |
| 211 |
local lbl = "#" .. i .. " (" .. w .. "x" .. h .. ")" |
| 212 |
local lw = font:sizeOf(lbl) |
| 213 |
if lw > w then w = lw end |
| 214 |
h = h + fonth + sep |
| 215 |
if x + w > sw then |
| 216 |
x = 0 |
| 217 |
y = y + tallest |
| 218 |
if y > sh then |
| 219 |
break |
| 220 |
end |
| 221 |
end |
| 222 |
if h > tallest then tallest = h end |
| 223 |
font:draw(canvas, lbl, x, y) |
| 224 |
sprite_table:draw( |
| 225 |
canvas, |
| 226 |
i, |
| 227 |
x, |
| 228 |
y + fonth, |
| 229 |
{ scaleFactor = scale }) |
| 230 |
x = x + w + sep |
| 231 |
end |
| 232 |
end |
| 233 |
|
| 234 |
local function DoFrame(app) |
| 235 |
local canvas = app.video |
| 236 |
canvas:startFrame() |
| 237 |
if need_draw then |
| 238 |
need_draw = app.config.track_fps |
| 239 |
canvas:fillColour(background_colours[bg_colour_index]) |
| 240 |
Render(canvas) |
| 241 |
end |
| 242 |
canvas:endFrame() |
| 243 |
end |
| 244 |
|
| 245 |
local function DoTimer() |
| 246 |
if wdown then |
| 247 |
y_off = y_off + 32 |
| 248 |
need_draw = true |
| 249 |
end |
| 250 |
if sdown then |
| 251 |
y_off = y_off - 32 |
| 252 |
need_draw = true |
| 253 |
end |
| 254 |
return need_draw |
| 255 |
end |
| 256 |
|
| 257 |
local function open() |
| 258 |
if is_open then |
| 259 |
return |
| 260 |
end |
| 261 |
is_open = true |
| 262 |
old_gfx_cache = gfx.cache.tabled |
| 263 |
gfx.cache.tabled = sprite_viewer_gfx_cache |
| 264 |
font = gfx:loadFontAndSpriteTable("QData", "Font00V") |
| 265 |
|
| 266 |
old_event_handlers = TheApp.eventHandlers |
| 267 |
TheApp.eventHandlers = { |
| 268 |
frame = DoFrame, |
| 269 |
keydown = DoKey, |
| 270 |
keyup = DoKeyUp, |
| 271 |
timer = DoTimer, |
| 272 |
textinput = DoTextInput |
| 273 |
} |
| 274 |
|
| 275 |
need_draw = true |
| 276 |
end |
| 277 |
|
| 278 |
return open |