-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPuzzleScriptExport.lua
445 lines (396 loc) · 11.4 KB
/
PuzzleScriptExport.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
-- PuzzleScript Export
-- by pancelor, 2022-02-01
-- pancelor.com
-- handy link to the aseprite api docs:
-- https://www.aseprite.org/api/
--[[
note: this extenstion does not deal with making maps using the aseprite
"tilemap" feature I don't think it would fit into my personal workflow --
sometimes you want to make tweaks in the official editor, and how would
you import those back into aseprite? it's doable, but it sounds awkward
is it useful enough to add anyway? eh, maybe
the tilemap docs don't exist yet, but see this example:
https://github.com/dacap/export-aseprite-file/
(via https://github.com/aseprite/api/issues/66)
]]
--[[
# libpance
]]
local qindent = 0
local function qq(...)
local tbl = {...}
local s = ""
for i = 1,#tbl do
local o = tbl[i]
if type(o)=='table' then
s = s..'{\n'
qindent = qindent+2
assert(qindent<50)
local tab = string.rep(" ",qindent)
for k,v in pairs(o) do
s = s..tab..k..' = '..qq(v)..'\n'
end
qindent = qindent-2
s = s..string.rep(" ",qindent)..'} '
else
s = s..tostring(o).." "
end
end
return s
end
local function pq(...)
print(qq(...))
end
local function qa(l)
return "{"..table.concat(l,",").."}"
end
-- returns the first index in arr where arr[index]==target
local function find(arr,target)
for k,v in ipairs(arr) do
if v==target then return k,v end
end
end
-- returns any key in arr s.t fn(value) is truthy
local function findby(arr,fn)
for k,v in ipairs(arr) do
if fn(v) then return k,v end
end
end
local function add(tab,elem)
table.insert(tab,elem)
return elem
end
local function deli(tab, ix)
return table.remove(tab,ix)
end
local function mid(a,b,c)
c=c or 0
b=b or 1
local ab,bc,ac=a<b,b<c,a<c
if ab==bc then
return b
elseif bc==ac then
return a
else
return c
end
end
--[[
# helpers
]]
-- note: this counter doesn't reset between exports
-- (good, I think?)
local _id = -1
local function getId()
_id = _id+1
return _id
end
-- convert a name into a valid puzzlescript spritename
-- note: not very robust
-- mainly just intended to easy-fix filenames
local function sanitizeName(name)
return name:gsub("[ -.]","_")
-- return name:gsub("[%p%s]","_")
end
local function tilemapToImage(imgSrc, tileset, colorMode)
assert(imgSrc.colorMode==ColorMode.TILEMAP,"can only call on tilemap")
local size = tileset.grid.tileSize
local imgDstSpec = ImageSpec(imgSrc.spec)
imgDstSpec.colorMode = colorMode
imgDstSpec.width = imgSrc.width *size.width
imgDstSpec.height = imgSrc.height*size.height
local imgDst = Image(imgDstSpec)
for it in imgSrc:pixels() do
local tileimg = tileset:getTile(it())
imgDst:drawImage(tileimg,it.x*size.width,it.y*size.height)
end
return imgDst
end
local function weirdImagetoRgbImage(imgSrc)
assert(imgSrc.colorMode==ColorMode.INDEXED or imgSrc.colorMode==ColorMode.GRAYSCALE,"can only call on indexed or grayscale images")
local imgDstSpec = ImageSpec(imgSrc.spec)
imgDstSpec.colorMode = ColorMode.RGB
local imgDst = Image(imgDstSpec)
for it in imgSrc:pixels() do
if it()~=imgSrc.spec.transparentColor then
local col = Color(it())
-- pq("pixel",it(),col.red,col.blue,col.green,col.alpha)
imgDst:drawPixel(it.x,it.y,col)
end
end
return imgDst
end
-- converts a single "zone" into a string
local function exportZone(img,zone)
assert(img.colorMode==ColorMode.RGB)
-- pq("exportZone",zone)
local pal = {} -- array of colors
local body = ""
local opaque = true
for y = zone.bounds.y,zone.bounds.y+zone.bounds.height-1 do
for x = zone.bounds.x,zone.bounds.x+zone.bounds.width-1 do
-- pqt = x==0 and y==0 and pq or function()end
local inbounds = mid(0,img.width-1,x)==x and mid(0,img.height-1,y)==y
local col32 = inbounds and img:getPixel(x,y) or 0
local hexcode,trans do
-- note: col32 is a 32-bit int, even if app.activeSprite.colorMode is indexed
-- so Color(col32) does not work -- it will misinterpret indexed images
-- https://www.aseprite.org/api/color#color
local rr = app.pixelColor.rgbaR(col32)
local gg = app.pixelColor.rgbaG(col32)
local bb = app.pixelColor.rgbaB(col32)
local aa = app.pixelColor.rgbaA(col32)
hexcode = string.format("#%02x%02x%02x", rr, gg, bb)
trans = aa==0
-- pq(string.format("#%08x @ %d,%d",col32,x,y),"|",rr,gg,bb,aa)
end
local code
if trans then
code = "."
opaque = false
else
local index = find(pal,hexcode)
if not index then
add(pal,hexcode)
index = #pal
end
code = index-1
end
body = body..code
end
body = body.."\n"
end
if #pal>10 then
print("error: more than 10 colors in sprite at ("..zone.bounds.x..","..zone.bounds.y..")")
return ""
end
if opaque and #pal==1 then
return zone.name.."\n"..pal[1].."\n"
end
if #pal>0 then
return zone.name.."\n"..table.concat(pal," ").."\n"..body
end
return zone.name.."\ntransparent\n"
end
--[[
# script
]]
-- see gatherZones
local function _gatherZonesFromGrid(data)
for y = data.rect.y,data.rect.y+data.rect.height-1,data.gridheight do
for x = data.rect.x,data.rect.x+data.rect.width-1,data.gridwidth do
local id = getId()-(data.idOffset or 0)
add(data.zones,{
name = data.prefix..tostring(id),
bounds = Rectangle{
x = x,
y = y,
width = data.gridwidth,
height = data.gridheight,
},
})
end
end
end
-- returns a list of "zones" that fit into the current selection of the active sprite
-- a "zone" is a {name = <string>, bounds = <Rectangle>} object
-- sel may be non-rectangular (e.g. multiple rectangles)
-- but support isn't great (e.g. the grid anchor will not reset between
-- multiple selections)
local function gatherZones(data)
local prefix = data.prefix or "sprite"
local zones = {}
local sel = app.activeSprite.selection
-- rect: the subrectangle to export tiles from
local rect = sel.isEmpty and app.activeSprite.bounds or sel.bounds
if data.source=="grid" then
-- selection correction
-- if the selection isn't an exact multiples of 5x5,
-- the edge tiles will expand to full 5x5
local x1 = math.floor(rect.x/data.gridwidth)*data.gridwidth
local y1 = math.floor(rect.y/data.gridheight)*data.gridheight
local x2 = math.ceil((rect.x+rect.width)/data.gridwidth)*data.gridwidth
local y2 = math.ceil((rect.y+rect.height)/data.gridheight)*data.gridheight
-- pq(rect)
-- pq(x1,y1,x2,y2)
rect = Rectangle(x1,y1,x2-x1,y2-y1)
-- pq(rect)
_gatherZonesFromGrid{
zones=zones,
prefix=prefix,
rect=rect,
gridwidth=data.gridwidth,
gridheight=data.gridheight,
}
elseif data.source=="slices" then
for i,slice in ipairs(app.activeSprite.slices) do
if rect:contains(slice.bounds) then
if data.subdivide then
_gatherZonesFromGrid{
zones=zones,
prefix=slice.name,
rect=slice.bounds,
gridwidth=data.gridwidth,
gridheight=data.gridheight,
idOffset=_id+1, -- HACK: make ids start at 0 for each slice
}
else
add(zones,{
name = slice.name,
bounds = slice.bounds,
})
end
end
end
else
assert(nil,"bad source: "..tostring(data.source))
end
return zones
end
-- create an rgb image from the current sprite
-- properly handles tilemaps
-- properly handles indexed/grayscale images
local function spriteToRgbImage(layeronly)
local res
if app.activeImage.colorMode==ColorMode.TILEMAP and layeronly then
local ti,tileset = find(app.activeSprite.tilesets,app.activeLayer.tileset)
assert(tileset)
res = tilemapToImage(app.activeImage,tileset,app.activeSprite.colorMode)
else
res = Image(app.activeSprite.spec)
if layeronly then
res:drawImage(app.activeCel.image,app.activeCel.position)
else
res:drawSprite(app.activeSprite,app.activeFrame)
assert(res.colorMode~=ColorMode.TILEMAP)
end
end
if res.colorMode==ColorMode.INDEXED or res.colorMode==ColorMode.GRAY then
res = weirdImagetoRgbImage(res)
end
assert(res.colorMode==ColorMode.RGB)
return res
end
-- returns a list of strings
-- each string looks something like this:
-- tile12
-- #ff8000 #00ff80
-- ..010
-- .0010
-- 00110
-- 0110.
-- 110..
local function exportZones(img,zones)
local result = {}
for i,zone in ipairs(zones) do
local str = exportZone(img,zone)
if str then
add(result,str)
end
end
return result
end
--[[
# main
]]
local hasSlices = #app.activeSprite.slices>0
local dlg = Dialog("PuzzleScript Export")
-- TODO maybe this visibility stuff would be more understandable as
-- one tab for slices and one tab for grid. (and modify enabled instead of visible)
function updateDlgVisibility()
dlg:modify{id = 'gridwidth', visible = dlg.data.source=='grid' or dlg.data.subdivide}
dlg:modify{id = 'gridheight', visible = dlg.data.source=='grid' or dlg.data.subdivide}
dlg:modify{id = 'subdivide', visible = dlg.data.source=='slices'}
dlg:modify{id = 'prefix', visible = dlg.data.source=='grid'}
end
dlg:combobox{
id = 'source',
label = 'Source',
option = hasSlices and 'slices' or 'grid',
options = {'grid', 'slices'},
onchange = function()
updateDlgVisibility()
end,
}
dlg:check{
id = 'subdivide',
label = 'Subdivide slices',
onclick = function()
updateDlgVisibility()
end,
}
do
-- grid size
local gb = app.activeSprite.gridBounds
local default = gb.width==16 and gb.height==16
dlg:number{
id = 'gridwidth',
label = 'Grid size',
text = tostring(default and 5 or gb.width),
decimals = 0,
-- onchange = function()
-- if dlg.data.gridwidth<1 then
-- dlg:modify{id='gridwidth', text='1'}
-- end
-- end
}
dlg:number{
id = 'gridheight',
text = tostring(default and 5 or gb.height),
decimals = 0,
-- onchange = function()
-- if dlg.data.gridheight<1 then
-- dlg:modify{id='gridheight', text='1'}
-- end
-- end
}
end
dlg:entry{
id = 'prefix',
label = 'Name prefix',
text = sanitizeName(app.fs.fileTitle(app.activeSprite.filename or 'aseprite')),
-- onchange = function()
-- dlg:modify{
-- id = 'prefix',
-- text = sanitizeName(dlg.data.prefix),
-- }
-- end,
}
dlg:check{
id = 'layeronly',
label = 'Active layer only',
selected = true,
}
dlg:button{text = "&Export", onclick = function()
-- clear output, in case we're interrupted by errors
dlg:modify{id = "output", label = "", text = ""}
if not app.activeSprite then return app.alert("error: no sprite found") end
local zones = gatherZones{
source = dlg.data.source,
subdivide = dlg.data.subdivide,
gridwidth = dlg.data.gridwidth,
gridheight = dlg.data.gridheight,
prefix = dlg.data.prefix,
}
local imgRGB = spriteToRgbImage(dlg.data.layeronly)
local tiles = exportZones(imgRGB,zones)
-- set output
local label = string.format("Output (%d)",#tiles)
local text = table.concat(tiles,"\n")
dlg:modify{
id = "output",
label = label,
text = text,
focus = true,
visible = true,
}
end}
dlg:entry{
id = "output",
label = "Output",
text = "",
focus = false,
visible = false,
}
updateDlgVisibility()
dlg:show{wait = false}