-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgdk_slices.lua
More file actions
227 lines (187 loc) · 6.66 KB
/
sgdk_slices.lua
File metadata and controls
227 lines (187 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
-- Export Slices to Text File.lua
-- Saves all slice information (name, bounds, 9-patch, pivot) to a .txt file
-- Snap Size Dialog.lua
-- Opens a dialog to input Width & Height (8-256), snapped to multiples of 8
local sprite = app.activeSprite
local function snapToMultiple(value, multiple)
return math.floor((value + multiple / 2) / multiple) * multiple
end
local function saveSlices(w, h, s)
-- Choose output file (same folder as .aseprite, named after the file)
local baseName = app.fs.fileTitle(sprite.filename)
local defaultPath = app.fs.joinPath(app.fs.filePath(sprite.filename), baseName .. "_slices.txt")
local file = io.open(defaultPath, "w")
if not file then
app.alert("Error: Could not write to file!")
return
end
file:write(string.format("# Sprite: %s\n", sprite.filename))
file:write(string.format("# Resolution: %dx%d\n", sprite.width, sprite.height))
-- file:write(string.format("Total Slices: %d\n", #sprite.slices))
-- file:write(string.rep("=", 60) .. "\n\n")
-- Collect and group slices by pivot (x then y)
local pivotGroups = {}
for _, slice in ipairs(sprite.slices) do
local b = slice.bounds
local pivot_y = b.x//w
local pivot_x = b.y//h
--if pivot then
local px = pivot_x
local py = pivot_y
-- Create nested table: pivotGroups[x][y] = {slices}
if not pivotGroups[px] then
pivotGroups[px] = {}
end
if not pivotGroups[px][py] then
pivotGroups[px][py] = {}
end
table.insert(pivotGroups[px][py], slice)
-- else
-- -- Optional: keep track of slices with no pivot
-- if not pivotGroups["__no_pivot"] then
-- pivotGroups["__no_pivot"] = { ["__no_pivot"] = {} }
-- end
-- table.insert(pivotGroups["__no_pivot"]["__no_pivot"], slice)
-- end
end
-- Sort pivot X coordinates
local pivotXKeys = {}
for x, _ in pairs(pivotGroups) do
if x ~= "__no_pivot" then
table.insert(pivotXKeys, x)
end
end
table.sort(pivotXKeys)
-- 1. Slices with no pivot
if pivotGroups["__no_pivot"] then
file:write("# NO PIVOT DEFINED\n")
file:write(string.rep("─", 50) .. "\n")
for _, slice in ipairs(pivotGroups["__no_pivot"]["__no_pivot"]) do
local b = slice.bounds
file:write(string.format(" # %-30s %4d,%-4d %3dx%3d\n",
slice.name or "(no name)", b.x, b.y, b.width, b.height))
end
file:write("\n")
end
-- 2. Grouped by pivot X, then Y
for _, px in ipairs(pivotXKeys) do
local yTable = pivotGroups[px]
-- Header for each X coordinate
file:write(string.format("[ANIMATION %d]\n", px))
local pyKeys = {}
for y, _ in pairs(yTable) do table.insert(pyKeys, y) end
table.sort(pyKeys)
for i, py in ipairs(pyKeys) do
local slices = yTable[py]
-- Sub-header for each Y coordinate (with nice delimiter)
if i > 1 then file:write("\n") end -- extra line between Y groups
file:write(string.format("FRAME %d\n", py))
-- Sort slices in this exact pivot point by name
table.sort(slices, function(a,b) return (a.name or "") < (b.name or "") end)
for _, slice in ipairs(slices) do
local b = slice.bounds
local sw = b.width
local sh = b.height
if s then
sw = snapToMultiple(sw, 8);
sh = snapToMultiple(sh, 8);
end
local line = string.format("%d %d %d %d", b.x-(py*w), b.y-(px*h), sw, sh)
-- local center = slice.center
-- if center and not center.isEmpty then
-- line = line .. string.format(" [9-patch center %d,%d %dx%d]",
-- center.x, center.y, center.width, center.height)
-- end
file:write(line .. "\n")
end
end
file:write("\n") -- blank line between different X groups
end
file:write("#Generated by Aseprite - Export Slices to Text File\n")
file:close()
app.alert{
title = "Slices Exported",
text = string.format("Successfully saved %d slices to:\n%s", #sprite.slices, defaultPath),
buttons = "OK"
}
end
local function createDialog()
local dlg = Dialog{ title = "Snap Size to Multiple of 8" }
local w = 64 -- default values (will be snapped on open)
local h = 64
dlg:label{
text = "Enter Frame size (will snap to nearest multiple of 8):",
align = "center"
}
dlg:separator()
dlg:slider{
id = "width",
label = "Width :",
min=8,
max=256,
value=16,
focus = true,
onchange = function()
local val = dlg.data.width
if val and val >= 1 then
local snapped = snapToMultiple(val, 8)
snapped = math.max(8, math.min(256, snapped))
if snapped ~= val then
dlg:modify{ id = "width", value = snapped }
end
end
end
}
dlg:slider{
id = "height",
label = "Height:",
min=8,
max=256,
value=16,
onchange = function()
local val = dlg.data.height
if val and val >= 1 then
local snapped = snapToMultiple(val, 8)
snapped = math.max(8, math.min(256, snapped))
if snapped ~= val then
dlg:modify{ id = "height", value = snapped }
end
end
end
}
dlg:check {
id="chk_size",
label="Set Slices W/H to Mult. of 8",
}
dlg:button{
text = "Cancel",
onclick = function()
dlg:close()
end
}
dlg:button{
text = "Save",
focus = true,
onclick = function()
-- Final values are already snapped
-- app.alert{
-- title = "Size Selected",
-- text = string.format("Width: %d\nHeight: %d", w, h),
-- buttons = "OK"
-- }
-- HERE: Do whatever you want with the values!
-- For example: store them globally or use in another script
app.transaction(function()
saveSlices(w, h, dlg.data.chk_size)
end)
dlg:close()
end
}
dlg:show{ wait = true }
end
if not sprite then
app.alert("Error: No active sprite!")
return
else
createDialog()
end