Skip to content

Commit 4e4ef61

Browse files
author
Anton Petrov
committed
fixed imagetable not allowing full image names
1 parent 0dd908e commit 4e4ef61

1 file changed

Lines changed: 56 additions & 31 deletions

File tree

playdate/imagetable.lua

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,73 @@ module.__index = meta
1010
-- TODO: overload the `[]` array index operator to return an image
1111
-- TODO: overload the `#` length operator to return the number of images
1212

13-
-- TODO: handle overloaded signature (count, cellsWide, cellSize)
14-
function module.new(path, cellsWide, cellsSize)
15-
@@ASSERT(cellsWide == nil, "[ERR] Parameter cellsWide is not yet implemented.")
16-
@@ASSERT(cellsSize == nil, "[ERR] Parameter cellsSize is not yet implemented.")
13+
local function splitPath(path)
14+
path = path:gsub("[/\\]+$", "")
1715

18-
local imagetable = setmetatable({}, meta)
19-
local folder = ""
20-
local pattern = path.."-table-"
21-
22-
-- no findLast() so reverse string first
23-
local start, ends = string.find(string.reverse(path), "/")
24-
if start and ends then
25-
folder = string.sub(path, 1, #path - ends)
26-
pattern = string.sub(path, #path - ends + 2).."-table-"
16+
local dir, tail = path:match("^(.*[/\\])([^/\\]+)$")
17+
if not dir then
18+
dir, tail = "", path
19+
end
20+
21+
local name, ext = tail:match("^(.*)%.([^%.]+)$")
22+
if name and name ~= "" and tail:sub(1,1) ~= "." then
23+
-- ok: "file.png" -> name="file", ext="png"
24+
-- ok: "file.tar.gz" -> name="file.tar", ext="gz"
25+
else
26+
name, ext = tail, nil
2727
end
2828

29-
-- escape dashes
30-
pattern = string.gsub(pattern, "%-", "%%%-")
31-
-- TODO: escape other magic chars?
32-
33-
-- TODO: support about a sequence of files (image1.png, image2.png, etc)
34-
local actualFilename = ""
35-
local files = love.filesystem.getDirectoryItems(folder)
36-
for i = 1, #files, 1 do
37-
local f = files[i]
38-
local s, e = string.find(f, pattern)
39-
if s and e then
40-
-- file found, remove extension
41-
actualFilename = string.sub(f, 1, #f - 4)
42-
break
29+
return dir, name, ext
30+
end
31+
32+
-- returns actualPath, frameWidth, frameHeight
33+
local function resolveImagePath(path)
34+
local folder, name, ext = splitPath(path)
35+
folder = folder or ""
36+
ext = ext or ""
37+
38+
-- if this is partial name then scan the folder
39+
if not string.find(name, "-table-", nil, true) then
40+
local pattern = name.."-table-"
41+
name = nil
42+
local files = love.filesystem.getDirectoryItems(folder)
43+
for i = 1, #files do
44+
local f = files[i]
45+
if string.find(f, pattern, nil, true) then
46+
local fd, fn, fe = splitPath(f)
47+
if fe == "png" then
48+
name = fn
49+
break
50+
end
51+
end
4352
end
53+
54+
if not name then return end
4455
end
4556

4657
-- parse frame width and height out of filename
47-
local matches = string.gmatch(actualFilename, "%-(%d+)")
58+
local matches = string.gmatch(name, "%-(%d+)")
4859
local frameWidth = tonumber(matches())
4960
local frameHeight = tonumber(matches())
50-
local actualPath = folder.."/"..actualFilename
61+
62+
return folder .. name .. ".png", frameWidth, frameHeight
63+
end
64+
65+
-- TODO: handle overloaded signature (count, cellsWide, cellSize)
66+
function module.new(path, cellsWide, cellsSize)
67+
@@ASSERT(cellsWide == nil, "[ERR] Parameter cellsWide is not yet implemented.")
68+
@@ASSERT(cellsSize == nil, "[ERR] Parameter cellsSize is not yet implemented.")
69+
70+
local actualPath, frameWidth, frameHeight = resolveImagePath(path)
71+
if not actualPath then
72+
return nil -- todo: error?
73+
end
74+
75+
local imagetable = setmetatable({}, meta)
5176

5277
-- load atlas
53-
local atlas = love.image.newImageData(actualPath..".png")
54-
78+
local atlas = love.image.newImageData(actualPath)
79+
5580
-- create a separate image for each frame
5681
local w = atlas:getWidth()
5782
local h = atlas:getHeight()

0 commit comments

Comments
 (0)