From b9388061abc2a164fed58399bdd8ea7a2adbb251 Mon Sep 17 00:00:00 2001 From: annabunches Date: Mon, 29 Jul 2024 23:22:57 -0400 Subject: [PATCH] Add new layer detection to keep colors mapped correctly. --- abase-commands.lua | 50 ++----------------------------- abase-listeners.lua | 14 +++++++++ abase-sprite.lua | 52 ++++++++++++++++++++++++++++++++- advanced-spritesheet-export.lua | 10 +++++++ package.json | 1 + 5 files changed, 78 insertions(+), 49 deletions(-) create mode 100644 abase-listeners.lua diff --git a/abase-commands.lua b/abase-commands.lua index c580d77..b324fe3 100644 --- a/abase-commands.lua +++ b/abase-commands.lua @@ -1,52 +1,6 @@ -- New commands to be executed via Aseprite menus / keyboard shortcuts local sprt = require "abase-sprite" -local BASE_COLOR = Color { - r = 0, - g = 0, - b = 0, - a = 0 -} -local IGNORE_COLOR = Color { - gray = 100 -} -local IGNORE_SUBCOLOR = Color { - gray = 150 -} -local MERGE_COLOR = Color { - r = 200, - g = 200, - b = 0 -} -local MERGE_SUBCOLOR = Color { - r = 200, - g = 200, - b = 128 -} - --- set the color of a layer and its sublayers -local function setColor(layer, subColor) - if (layer.properties(extKey).ignored) then - layer.color = IGNORE_COLOR - subColor = IGNORE_SUBCOLOR - elseif subColor == IGNORE_SUBCOLOR then - layer.color = subColor - elseif (layer.properties(extKey).exportedAsSprite) then - layer.color = MERGE_COLOR - subColor = MERGE_SUBCOLOR - elseif subColor == MERGE_SUBCOLOR then - layer.color = subColor - else - layer.color = BASE_COLOR - end - - if (layer.isGroup) then - for i, sublayer in ipairs(layer.layers) do - setColor(sublayer, subColor) - end - end -end - local function ExportSpritesheetAdvanced() if not app.sprite then return app.alert "Must have a sprite open to export." @@ -72,7 +26,7 @@ local function ToggleIgnore() else layer.properties(extKey).ignored = true end - setColor(layer) + sprt.setColor(layer) end local function ToggleExportAsSprite() @@ -82,7 +36,7 @@ local function ToggleExportAsSprite() else layer.properties(extKey).exportedAsSprite = true end - setColor(layer) + sprt.setColor(layer) end local export = { diff --git a/abase-listeners.lua b/abase-listeners.lua new file mode 100644 index 0000000..c87f767 --- /dev/null +++ b/abase-listeners.lua @@ -0,0 +1,14 @@ +local sprt = require "abase-sprite" + +-- recolors all layers in the current sprite +local function RecolorLayers() + for i, layer in ipairs(app.sprite.layers) do + sprt.setColor(layer) + end +end + + +local export = { + RecolorLayers = RecolorLayers, +} +return export \ No newline at end of file diff --git a/abase-sprite.lua b/abase-sprite.lua index 5656eff..34dc0b8 100644 --- a/abase-sprite.lua +++ b/abase-sprite.lua @@ -1,4 +1,28 @@ -- Functions for modifying a sprite + +local BASE_COLOR = Color { + r = 0, + g = 0, + b = 0, + a = 0 +} +local IGNORE_COLOR = Color { + gray = 100 +} +local IGNORE_SUBCOLOR = Color { + gray = 150 +} +local MERGE_COLOR = Color { + r = 200, + g = 200, + b = 0 +} +local MERGE_SUBCOLOR = Color { + r = 200, + g = 200, + b = 128 +} + -- Deletes any layers with the 'ignored' property. local function deleteLayers(spr, layers) for i, layer in ipairs(layers) do @@ -44,9 +68,35 @@ local function revealLayers(layers) end end +-- set the color of a layer and its sublayers +local function setColor(layer, subColor) + if (layer.properties(extKey).ignored) then + layer.color = IGNORE_COLOR + subColor = IGNORE_SUBCOLOR + elseif subColor == IGNORE_SUBCOLOR then + layer.color = subColor + elseif (layer.properties(extKey).exportedAsSprite) then + layer.color = MERGE_COLOR + subColor = MERGE_SUBCOLOR + elseif subColor == MERGE_SUBCOLOR then + layer.color = subColor + else + layer.color = BASE_COLOR + end + + if (layer.isGroup) then + for i, sublayer in ipairs(layer.layers) do + setColor(sublayer, subColor) + end + end +end + + + local export = { deleteLayers = deleteLayers, flattenLayers = flattenLayers, - revealLayers = revealLayers + revealLayers = revealLayers, + setColor = setColor, } return export \ No newline at end of file diff --git a/advanced-spritesheet-export.lua b/advanced-spritesheet-export.lua index 705cb36..590e21b 100644 --- a/advanced-spritesheet-export.lua +++ b/advanced-spritesheet-export.lua @@ -1,6 +1,7 @@ extKey = "annabunches/abase" -- this must come before we require 'abase-commands' local cmd = require "abase-commands" +local listeners = require "abase-listeners" function init(plugin) plugin:newCommand{ @@ -76,4 +77,13 @@ function init(plugin) return app.layer.isGroup end } + + app.events:on( + "aftercommand", + function(ev) + if (ev.name == "NewLayer") then + listeners.RecolorLayers() + end + end + ) end \ No newline at end of file diff --git a/package.json b/package.json index 2b5513a..96c3f5b 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "contributes": { "scripts": [ { "path": "./advanced-spritesheet-export.lua" }, + { "path": "./abase-listeners.lua" }, { "path": "./abase-commands.lua" }, { "path": "./abase-sprite.lua" } ]