Large refactor and code cleanup.

This commit is contained in:
Anna Rose 2024-07-30 22:58:23 -04:00
parent 8b4cc23bb0
commit c92490fc91
7 changed files with 66 additions and 45 deletions

View File

@ -1,3 +1,7 @@
-- functions for modifying a layer's color
local p = require "abase-properties"
local BASE_COLOR = Color {
r = 0,
g = 0,
@ -27,31 +31,24 @@ local MERGE_SUBCOLOR = Color {
local function safeSetColor(layer, color)
pixelValue = layer.color.rgbaPixel
--[[ print("safeSetColor: " .. pixelValue)
print(BASE_COLOR.rgbaPixel)
print(IGNORE_COLOR.rgbaPixel)
print(IGNORE_SUBCOLOR.rgbaPixel)
print(MERGE_COLOR.rgbaPixel)
print(MERGE_SUBCOLOR.rgbaPixel) ]]
if (pixelValue ~= BASE_COLOR.rgbaPixel and
pixelValue ~= IGNORE_COLOR.rgbaPixel and
pixelValue ~= IGNORE_SUBCOLOR.rgbaPixel and
pixelValue ~= MERGE_COLOR.rgbaPixel and
pixelValue ~= MERGE_SUBCOLOR.rgbaPixel) then
--[[ print("DEBUG: not setting color")
]] return
return
end
layer.color = color
end
-- set the color of a layer and its sublayers based on the extension properties
local function SetColor(layer, subColor)
if (layer.properties(extKey).ignored) then
if p.IsIgnored(layer) then
safeSetColor(layer, IGNORE_COLOR)
subColor = IGNORE_SUBCOLOR
elseif subColor == IGNORE_SUBCOLOR then
safeSetColor(layer, subColor)
elseif (layer.properties(extKey).exportedAsSprite) then
elseif p.IsMerged(layer) then
safeSetColor(layer, MERGE_COLOR)
subColor = MERGE_SUBCOLOR
elseif subColor == MERGE_SUBCOLOR then

View File

@ -1,6 +1,7 @@
-- New commands to be executed via Aseprite menus / keyboard shortcuts
local layerUtils = require "abase-layer"
local colorUtils = require "abase-color"
local l = require "abase-layer"
local c = require "abase-color"
local p = require "abase-properties"
local function ExportSpritesheetAdvanced()
if not app.sprite then
@ -9,9 +10,9 @@ local function ExportSpritesheetAdvanced()
local spr = Sprite(app.sprite)
layerUtils.DeleteLayers(spr, spr.layers)
layerUtils.FlattenLayers(spr.layers)
layerUtils.RevealLayers(spr.layers)
l.DeleteLayers(spr, spr.layers)
l.FlattenLayers(spr.layers)
l.RevealLayers(spr.layers)
app.command.ExportSpriteSheet {
splitLayers = true
@ -20,39 +21,35 @@ local function ExportSpritesheetAdvanced()
spr:close()
end
local function toggleIgnoreLayer(layer)
if (layer.properties(extKey).ignored) then
layer.properties(extKey).ignored = false
else
layer.properties(extKey).ignored = true
end
colorUtils.SetColorFromRoot(layer)
end
-- Toggle ignore for all selected layers
-- TODO: should this behavior change when selected layers start with mixed state?
local function ToggleIgnore()
-- Determine whether we are setting or clearing the flag
local ignore = false
for _, layer in ipairs(app.range.layers) do
toggleIgnoreLayer(layer)
if not p.IsIgnored(layer) then
ignore = true
end
end
end
local function toggleExportAsSpriteLayer(layer)
if not layer.isGroup then return end
if (layer.properties(extKey).exportedAsSprite) then
layer.properties(extKey).exportedAsSprite = false
else
layer.properties(extKey).exportedAsSprite = true
for _, layer in ipairs(app.range.layers) do
p.SetIgnored(layer, ignore)
c.SetColorFromRoot(layer)
end
colorUtils.SetColorFromRoot(layer)
end
-- Toggle Merge on Export for all selected group layers
-- TODO: should this behavior change when selected layers start with mixed state?
local function ToggleExportAsSprite()
-- Determine whether we are setting or clearing the flag
local merge = false
for _, layer in ipairs(app.range.layers) do
toggleExportAsSpriteLayer(layer)
if layer.isGroup and not p.IsMerged(layer) then
merge = true
end
end
for _, layer in ipairs(app.range.layers) do
p.SetMerged(layer, merge)
c.SetColorFromRoot(layer)
end
end

View File

@ -1,9 +1,11 @@
-- Functions for modifying a sprite's layers based on configuration flags
-- Functions for modifying layers recursively based on configuration flags
local p = require "abase-properties"
-- Deletes any layers with the 'ignored' property.
local function DeleteLayers(spr, layers)
for _, layer in ipairs(layers) do
if layer.properties(extKey).ignored then
if p.IsIgnored(layer) then
spr:deleteLayer(layer)
elseif layer.isGroup then
DeleteLayers(spr, layer.layers)
@ -19,7 +21,7 @@ local function FlattenLayers(layers)
goto continue
end
if layer.properties(extKey).exportedAsSprite then
if p.IsMerged(layer) then
app.range.layers = {layer}
app.command.FlattenLayers(false)
else

View File

@ -1,9 +1,9 @@
local colorUtils = require "abase-color"
local c = require "abase-color"
-- recolors all layers in the current sprite
local function RecolorLayers()
for i, layer in ipairs(app.sprite.layers) do
colorUtils.SetColor(layer)
c.SetColor(layer)
end
end

27
abase-properties.lua Normal file
View File

@ -0,0 +1,27 @@
-- functions for retrieving and modifying layer properties
extKey = "annabunches/abase"
local function IsIgnored(layer)
return layer.properties(extKey).ignored
end
local function IsMerged(layer)
return layer.isGroup and layer.properties(extKey).merged
end
local function SetIgnored(layer, ignore)
layer.properties(extKey).ignored = ignore
end
local function SetMerged(layer, merge)
if not layer.isGroup then return end
layer.properties(extKey).merged = merge
end
local export = {
IsIgnored = IsIgnored,
IsMerged = IsMerged,
SetIgnored = SetIgnored,
SetMerged = SetMerged,
}
return export

View File

@ -1,5 +1,3 @@
extKey = "annabunches/abase" -- this must come before we require 'abase-commands'
local cmd = require "abase-commands"
local listeners = require "abase-listeners"

View File

@ -2,7 +2,7 @@
"name": "advanced-spritesheets",
"displayName": "Advanced Spritesheets",
"description": "Improved spritesheet export functionality for Aseprite.",
"version": "0.0.11",
"version": "0.0.12",
"author": { "name": "Anna Wiggins",
"email": "annabunches@gmail.com",
"url": "https://annabunches.net" },