Created
October 3, 2019 15:47
-
-
Save trezy/e43e2462cbc43a45319b44a366e6f38b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Get current sprite | |
local sourceSprite = app.activeSprite | |
-- Exit if we're not currently looking at a sprite | |
if not sourceSprite then | |
return app.alert("There is no active sprite") | |
end | |
-- Initiate the important variables | |
local executionDialog = Dialog("AwesomeExport is creating your sprite... ") | |
local layerCount = 0 | |
local outputHeight = 0 | |
local outputSprite = Sprite(sourceSprite.width, sourceSprite.height, sourceSprite.colorMode) | |
local outputWidth = 0 | |
local originalLayerStates = {} | |
local tagCount = 0 | |
-- Cache original layer visibility states, then hide all layers | |
-- Also count how many top level layers there are | |
for i, layer in ipairs(sourceSprite.layers) do | |
originalLayerStates[i] = layer.isVisible | |
layer.isVisible = false | |
layerCount = layerCount + 1 | |
end | |
-- Open the execution dialog to indicate that we're busy | |
executionDialog:show{ wait=false } | |
-- Set the output width to the width of the source * the length of the longest tag | |
-- Also count how many tags there are | |
for i, tag in ipairs(sourceSprite.tags) do | |
local newWidth = tag.frames * sourceSprite.width | |
if newWidth < outputSprite.width then | |
newWidth = outputSprite.width | |
end | |
outputWidth = newWidth | |
tagCount = tagCount + 1 | |
end | |
-- Update the output height based on the number of layers and tags | |
outputHeight = sourceSprite.height * (layerCount * tagCount) | |
-- Resize the output | |
outputSprite:resize(outputWidth, outputHeight) | |
-- Loop over every frame once for every layer, copying the content of that frame into the output | |
for i, layer in ipairs(sourceSprite.layers) do | |
layer.isVisible = true | |
for j, frame in ipairs(sourceSprite.frames) do | |
local x = sourceSprite.width * (j - 1) | |
local y = sourceSprite.height * (i - 1) | |
local cel = outputSprite:newCel(outputSprite.layers[1], 1) | |
cel.image:drawSprite(sourceSprite, frame) | |
end | |
layer.isVisible = false | |
end | |
-- Reset the layers in the source sprite to their original states | |
for i, layer in ipairs(sourceSprite.layers) do | |
layer.isVisible = originalLayerStates[i] | |
end | |
-- TODO: Create flipped copies of layers | |
-- TODO: Render to export file | |
-- outputSprite:close() | |
executionDialog:close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment