/
Export Tags.lua
307 lines (210 loc) · 7.67 KB
/
Export Tags.lua
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
local s = app.activeSprite
if not s then app.alert("No Sprite") return end
--Tag Check
if #s.tags == 0 then app.alert("No Tags to Export") return end
local tag_options = {"All Tags"};
for i, tag in ipairs(s.tags) do tag_options[i+1] = tag.name; end
--
local dlg = Dialog("Export Tags")
local file_type_list = { "ase", "aseprite", "bmp", "gif", "jpeg", "jpg", "png", };
local export_options = { "Strips", "Animation"}
local strip_options = {"Horizontal", "Vertical"}
local Warning = {false, false}
local Note = {false}
--"Stand" Vars
local s_close = true
local s_save = true
local s_export_tag = tag_options[1]
local s_export_type = export_options[1]
local s_export_strip_type = strip_options[1]
--Methods
local function AddLayer(_sprite, _tag, _dest, _source)
if _source.isImage then
local new_Layer = _sprite:newLayer()
new_Layer.parent = _dest
new_Layer.name = _source.name
new_Layer.opacity = _source.opacity
new_Layer.blendMode = _source.blendMode
new_Layer.isVisible = _source.isVisible
new_Layer.isEditable = _source.isEditable
local m
for m = 1, _tag.frames do
local frame = s.frames[_tag.fromFrame.frameNumber + (m-1) ]
local new_frame = _sprite.frames[m]
new_frame.duration = frame.duration
local Cel = _source:cel(frame.frameNumber)
if Cel ~= nil then
local new_Cel = new_Layer.sprite:newCel(new_Layer, new_frame, Cel.image, Cel.position)
new_Cel.color = Cel.color
new_Cel.opacity = Cel.opacity
new_Cel.data = Cel.data
end
end
else
local new_Group = _sprite:newGroup()
new_Group.parent = _dest
new_Group.name = _source.name
new_Group.opacity = _source.opacity
new_Group.blendMode = _source.blendMode
new_Group.isVisible = _source.isVisible
new_Group.isEditable = _source.isEditable
for l, cur_Layer in ipairs(_source.layers) do AddLayer(_sprite, _tag, new_Group, cur_Layer) end
end
end
local function UpdateDialog()
local path, file, extension = string.match(dlg.data.dir, "(.-)([^\\]-([^\\%.]+))$")
--Update Vars
s_close = dlg.data.d_close
s_save = dlg.data.d_save
s_export_tag = dlg.data.d_export_tag
s_export_type = dlg.data.d_export_type
s_export_strip_type = dlg.data.d_export_strip_type
--
gif_export = (extension == "gif" )
can_anim_export = ( (extension == "ase" ) or (extension == "aseprite" ) or (extension == "gif" ) )
--Update Stuff
dlg
:modify{ id="d_export_strip_type", visible=(s_export_type == "Strips"), enabled=true}
--:modify{ id="d_close", visible=true, enabled=(s_export_type == "Animation")}
--:modify{ id="d_save", visible=true, enabled=(s_export_type == "Animation")}
--Warning
local _warning_active = false
Warning[1] = (gif_export and (s_export_type ~= "Animation") )
Warning[2] = ( (not can_anim_export) and (s_export_type == "Animation") )
for i = 1, #Warning do
dlg:modify{ id=("Warning_" .. tostring(i)), visible=Warning[i]}
if Warning[i] then _warning_active = true end
end
--Notes
Note[1] = s_export_type == "Animation"
for i = 1, #Note do
dlg:modify{ id=("Note_" .. tostring(i)), visible=Note[i]}
end
dlg:modify{ id="ok", enabled= (not _warning_active) }
-- :modify{ id="prefix", visible=(s_export_type == "Strips"), enabled=true}
end
local function RunProgram(data)
UpdateDialog()
if data.dir == "" then
app.alert("No Directory chosen")
dlg:close()
end
local path, file, extension = string.match(data.dir, "(.-)([^\\]-([^\\%.]+))$")
file = file:match("(.+)%..+")
if file == nil then file = ""
else file = (file .. "_")
end
extension = "." .. extension
local export_tag_list
if s_export_tag == "All Tags" then
export_tag_list = s.tags
else
for i,tag in ipairs(s.tags) do
if s_export_tag == tag.name then export_tag_list = {tag} end
end
end
if s_export_type == "Strips" then --Strip Packing
for i, tag in ipairs(export_tag_list) do
local new_img
if s_export_strip_type == "Horizontal" then new_img = Image(s.width*tag.frames, s.height)
else new_img = Image(s.width, s.height*tag.frames) end
for j = 0, tag.frames - 1 do
if s_export_strip_type == "Horizontal" then
new_img:drawSprite(s, tag.fromFrame.frameNumber + j , Point(s.width*j, 0) )
else
new_img:drawSprite(s, tag.fromFrame.frameNumber + j , Point(0, s.height*j) )
end
end
new_img:saveAs(path .. file .. tag.name .. "_strip" .. tag.frames .. extension)
end
return
end
if s_export_type == "Animation" then --Animation
if gif_export then
for i, tag in ipairs(export_tag_list) do
local new_spr = Sprite(s.spec)
local _layer = new_spr.layers[1]
for j = 0, tag.frames - 1 do
local new_img = Image(s.width, s.height)
new_img:drawSprite(s, tag.fromFrame.frameNumber + j )
local new_Frame = new_spr:newEmptyFrame()
new_spr:newCel(_layer, new_Frame, new_img )
end
new_spr:deleteFrame(1)
new_spr:saveAs(path .. file .. tag.name .. extension)
new_spr:close()
end
--]]
else
for i, tag in ipairs(export_tag_list) do
local new_spr = Sprite(s.spec)
for m = 1, tag.frames do new_spr:newFrame() end
new_spr:deleteFrame(1)
for l, cur_Layer in ipairs(s.layers) do AddLayer(new_spr, tag, new_spr, cur_Layer) end
new_spr:deleteLayer( new_spr.layers[1] )
new_spr:saveAs(path .. file .. tag.name .. extension)
new_spr:close()
end
--]]
end
return
end
if s_export_type == "Sprite Sheet" then --Sheet Packing
local sheet_w = s.width
local sheet_h = s.height * #export_tag_list
for i, tag in ipairs(export_tag_list) do
sheet_w = math.max(s.width * tag.frames, sheet_w)
end
local new_img = Image(sheet_w, sheet_h)
for i, tag in ipairs(export_tag_list) do
for j = 0, tag.frames - 1 do
new_img:drawSprite(s, tag.fromFrame.frameNumber + j , Point(s.width*j, s.height * (i-1)) )
end
end
local img_filename = path .. file .. "sheet" .. extension
new_img:saveAs(img_filename)
app.open(img_filename)
return
end
end
local function Main()
dlg
:combobox{ id="d_export_tag",
label="Tags",
options=tag_options,
onchange=function() UpdateDialog() end
}
:combobox{ id="d_export_type",
label="Export Type",
options=export_options,
onchange=function() UpdateDialog() end
}
:combobox{ id="d_export_strip_type", options=strip_options,}
:file{ id="dir",
label="Select Directory",
title="Output File",
open=false,
save=true,
filename=s.filename,
entry=true,
filetypes=file_type_list,
onchange=function() UpdateDialog() end
}
--:entry(id = "prefix", label:"File Prefix", text="spr_")s
--:check{ id="d_close", text="Close Files", selected=s_close}
--:check{ id="d_save", text="Save Files", selected=s_save}
--Warning Messages
:label{ id="Warning_1", label="", text="Alert: GIFs can only be exported as an Animation"}
:label{ id="Warning_2", label="", text="Alert: PNGs (and other formats) can not be exported as an Animation"}
:label{ id="Note_1", label="", text = "Note: Animation Export is going to fill your recent files list."}
--Footer Buttons
:button{id="ok",
text="&Export",
onclick=function() RunProgram(dlg.data) dlg:close() end
}
:button{text="&Cancel"}
:show{wait=false}
UpdateDialog(dlg)
end
--Execute
Main()