Skip to content

Commit c66a5ae

Browse files
committed
adds script for converting a sheet to layered tiles
1 parent c072d92 commit c66a5ae

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Tiles to Layers.lua

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
----------------------------------------------------------------------
2+
-- Takes a sprite for a tile sheet and splits
3+
-- each tile onto its own layer.
4+
----------------------------------------------------------------------
5+
local spr = app.activeSprite
6+
7+
-- Checks for a valid sprite
8+
if not spr then
9+
app.alert("There is no sprite to export")
10+
return
11+
end
12+
13+
-- Dialog prompt to get dimensions for an individual sprite
14+
local d = Dialog("Split Tiles to Layers")
15+
d:label{ id="help", label="", text="Set the width and height to split tiles by:" }
16+
:number{ id="tile_w", label="Tile Width:", text="8", focus=true }
17+
:number{ id="tile_h", label="Tile Height:", text="8" }
18+
:button{ id="ok", text="&OK", focus=true }
19+
:button{ text="&Cancel" }
20+
:show()
21+
22+
-- Data validation
23+
local data = d.data
24+
if not data.ok then return end
25+
26+
--[[
27+
Tile Splitter Class
28+
@param {Number} tile_w The width in pixels for an individual tile
29+
@param {Number} tile_w The height in pixels for an individual tile
30+
@param {Sprite} spr The sprite sheet to split
31+
@return {Table} TileSplitter instance
32+
]]--
33+
function TileSplitter(tile_w,tile_h,spr)
34+
local self = {}
35+
self.tile_w = tile_w
36+
self.tile_h = tile_h
37+
self.spr = spr
38+
local rows = math.floor(self.spr.height/self.tile_h)
39+
local cols = math.floor(self.spr.width/self.tile_w)
40+
41+
--[[
42+
Copies a single tile to a new layer and names that layer
43+
@param {Number} row The row of the tile to copy
44+
@param {Number} col The column of the tile to copy
45+
@param {Number} count The overall tile number to be copies (used for naming)
46+
]]--
47+
self.copyTileToLayer=function(row, col, count)
48+
-- Select an area of the current sprite
49+
spr.selection:select(Rectangle(col*self.tile_w, row*self.tile_h, self.tile_w, self.tile_h))
50+
51+
-- Copy and paste the selection
52+
app.command.CopyMerged()
53+
app.command.NewLayer()
54+
app.command.Paste()
55+
56+
-- Rename the layer
57+
app.activeLayer.name = "Tile "..count
58+
59+
spr.selection:deselect()
60+
end
61+
62+
--[[
63+
Iterates over each tile in the sheet to paste it onto its own layer.
64+
Sets the originally active/baselayer invisible once done.
65+
]]--
66+
self.splitTiles=function()
67+
local baseLayer = app.activeLayer
68+
69+
local tileCount = 0
70+
71+
for j = 0,rows-1 do
72+
for i = 0,cols-1 do
73+
tileCount = tileCount+1
74+
self.copyTileToLayer(j,i,tileCount)
75+
end
76+
end
77+
78+
baseLayer.isVisible = false
79+
end
80+
81+
return self
82+
end
83+
84+
-- Initializes the splitter for transforming the tilesheet to tiles
85+
local splitter=TileSplitter(data.tile_w, data.tile_h, spr)
86+
87+
-- Call method to split image to tiles as one transaction,
88+
-- allow a single undo
89+
app.transaction(
90+
function()
91+
splitter.splitTiles()
92+
end)

0 commit comments

Comments
 (0)