Facebook
From Nathan Sturdevant, 3 Weeks ago, written in Lua.
Embed
Download Paste or View Raw
Hits: 133
  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{ fromClipboard=true }
  54.  
  55.     -- Rename the layer
  56.     app.activeLayer.name = "Tile "..count
  57.  
  58.     spr.selection:deselect()
  59.   end
  60.  
  61.   --[[
  62.     Iterates over each tile in the sheet to paste it onto its own layer.
  63.     Sets the originally active/baselayer invisible once done.
  64.   ]]--
  65.   self.splitTiles=function()
  66.     local baseLayer = app.activeLayer
  67.  
  68.     local tileCount = 0
  69.  
  70.     for j = 0,rows-1 do
  71.       for i = 0,cols-1 do
  72.         tileCount = tileCount+1
  73.         self.copyTileToLayer(j,i,tileCount)
  74.       end
  75.     end
  76.  
  77.     baseLayer.isVisible = false
  78.   end
  79.  
  80.   return self
  81. end
  82.  
  83. -- Initializes the splitter for transforming the tilesheet to tiles
  84. local splitter=TileSplitter(data.tile_w, data.tile_h, spr)
  85.  
  86. -- Call method to split image to tiles as one transaction,
  87. --  allow a single undo
  88. app.transaction(
  89.   function()
  90.     splitter.splitTiles()
  91. end)
  92.