diff --git a/abase-commands.lua b/abase-commands.lua new file mode 100644 index 0000000..0d30b87 --- /dev/null +++ b/abase-commands.lua @@ -0,0 +1,45 @@ +-- New commands to be executed via Aseprite menus / keyboard shortcuts +local sprt = require "abase-sprite" + +local function ExportSpritesheetAdvanced() + if not app.sprite then + return app.alert "Must have a sprite open to export." + end + + local spr = Sprite(app.sprite) + + sprt.deleteLayers(spr, spr.layers) + sprt.flattenLayers(spr.layers) + sprt.revealLayers(spr.layers) + + app.command.ExportSpriteSheet { + splitLayers = true + } + + spr:close() +end + +local function ToggleIgnore() + local layer = app.layer + if (layer.properties(extKey).ignored) then + layer.properties(extKey).ignored = false + else + layer.properties(extKey).ignored = true + end +end + +local function ToggleExportAsSprite() + local layer = app.layer + if (layer.properties(extKey).exportedAsSprite) then + layer.properties(extKey).exportedAsSprite = false + else + layer.properties(extKey).exportedAsSprite = true + end +end + +local export = { + ExportSpritesheetAdvanced = ExportSpritesheetAdvanced, + ToggleIgnore = ToggleIgnore, + ToggleExportAsSprite = ToggleExportAsSprite +} +return export \ No newline at end of file diff --git a/abase-sprite.lua b/abase-sprite.lua new file mode 100644 index 0000000..5656eff --- /dev/null +++ b/abase-sprite.lua @@ -0,0 +1,52 @@ +-- Functions for modifying a sprite +-- Deletes any layers with the 'ignored' property. +local function deleteLayers(spr, layers) + for i, layer in ipairs(layers) do + if layer.properties(extKey).ignored then + spr:deleteLayer(layer) + elseif layer.isGroup then + deleteLayers(spr, layer.layers) + end + end +end + +-- Flattens any layers that have the 'exportedAsSprite' property. +-- Should be called after deleteLayers. +local function flattenLayers(layers) + for i, layer in ipairs(layers) do + if not layer.isGroup then + goto continue + end + + if layer.properties(extKey).exportedAsSprite then + app.range.layers = {layer} + app.command.FlattenLayers(false) + else + -- recurse + flattenLayers(layer.layers) + end + + ::continue:: + end +end + +-- Makes all layers visible. +-- This should be called after deleteLayers and flattenLayers +local function revealLayers(layers) + for i, layer in ipairs(layers) do + if layer.isGroup then + revealLayers(layer.layers) + end + + if not layer.isVisible then + layer.isVisible = true + end + end +end + +local export = { + deleteLayers = deleteLayers, + flattenLayers = flattenLayers, + revealLayers = revealLayers +} +return export \ No newline at end of file diff --git a/advanced-spritesheet-export.lua b/advanced-spritesheet-export.lua index 947d33d..395e3d4 100644 --- a/advanced-spritesheet-export.lua +++ b/advanced-spritesheet-export.lua @@ -1,99 +1,42 @@ -local extKey = "annabunches/AtlasExporter" +extKey = "annabunches/abase" -- this must come before we require 'abase-commands' + +local cmd = require "abase-commands" function init(plugin) plugin:newCommand{ id="AnnabunchesASEExportSpritesheetAdvanced", title="Export Spritesheet (Advanced)", - group="file_export", - onclick=exportSpritesheetAdvanced + group="file_export_1", + onclick=function() + cmd.ExportSpritesheetAdvanced() + end, + onenabled=function() + return app.activeSprite ~= nil + end + } + + plugin:newMenuSeparator{ + group="layer_popup_merge" } plugin:newCommand{ id="AnnabunchesASEToggleIgnoreLayer", title="Ignore on Export", - group="layer_popup_menu", - onclick=toggleIgnore + group="layer_popup_merge", + onclick=function() + cmd.ToggleIgnore() + end } plugin:newCommand{ id="AnnabunchesASEToggleExportAsSprite", title="Export as Sprite", - group="layer_popup_menu", - onclick=toggleExportAsSprite + group="layer_popup_merge", + onclick=function() + cmd.ToggleExportAsSprite() + end, + onnenabled=function() + return app.layer.isGroup + end } -end - -local function revealLayers(layers) - for i, layer in ipairs(layers) do - if layer.isGroup then - revealLayers(layer.layers) - end - - if not layer.isVisible then - layer.isVisible = true - layer.properties(extKey).revealed = true - end - end -end - -local function restoreLayers(layers) - for i, layer in ipairs(layers) do - if layer.isGroup then - restoreLayers(layer.layers) - end - - if layer.properties(extKey).revealed then - layer.isVisible = false --- layer.properties(extKey).revealed = nil - end - end -end - -local function deleteLayers(sprite, layers) - for i, layer in ipairs(layers) do - if layer.name:find("%+i") then - sprite:deleteLayer(layer) - elseif layer.isGroup then - deleteLayers(sprite, layer.layers) - end - end -end - -local function flattenLayers(layers) - for i, layer in ipairs(layers) do - if not layer.isGroup then - goto continue - end - - -- Layers "tagged" with +f get flattened - if layer.name:find("%+f") then - app.range.layers = { layer } - app.command.FlattenLayers(false) - else - -- recurse - flattenLayers(layer.layers) - end - - ::continue:: - end -end - -local function exportSpritesheetAdvanced() - local sprite = app.activeSprite - if not sprite then - return app.alert "Must have a sprite open to export." - end - - app.transaction(function() - deleteLayers(sprite, sprite.layers) - flattenLayers(sprite.layers) - revealLayers(sprite.layers) - end) - app.command.ExportSpriteSheet { - splitLayers=true, - } - restoreLayers(sprite.layers) - app.undo() -end - -main() +end \ No newline at end of file