-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.lua
More file actions
324 lines (240 loc) · 5.8 KB
/
script.lua
File metadata and controls
324 lines (240 loc) · 5.8 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
function IsSprite( )
return app.sprite
end
function IsActiveBox( )
return app.layer and app.layer.data == "boxeres" and not app.layer.isGroup
end
function IsActiveCel( )
return app.cel and app.cel.data == "boxeres"
end
function ColorToTable( color )
return { color.red, color.green, color.blue, color.alpha }
end
function RectToTable( rect )
return { rect.x, rect.y, rect.w, rect.h }
end
function GetBoxGroup( )
for i, v in ipairs( app.sprite.layers ) do
if v.isGroup and v.data == "boxeres" then
return v
end
end
return nil
end
function DrawRect( rect, color )
local toolAttrs = {
tool = "rectangle",
brush = Brush( 1 ),
points = { { rect.x, rect.y }, { rect.x + rect.w - 1, rect.y + rect.h - 1 } }
}
app.fgColor = color
app.useTool( toolAttrs )
end
function DrawBoxRects( color )
if not IsActiveCel( ) then
return
end
toolAttrs = {
tool = "rectangle",
brush = Brush( 1 ),
}
app.fgColor = color
for i = 1, app.cel.properties.rectCount, 1 do
local rect = app.cel.properties[ "rect" .. i ]
local points = { { rect.x, rect.y }, { rect.x + rect.w - 1, rect.y + rect.h - 1 } }
toolAttrs.points = points
app.useTool( toolAttrs )
end
end
function AddBox( )
if not app.sprite then
return
end
local boxGroup = GetBoxGroup( )
if not boxGroup then
boxGroup = app.sprite:newGroup( )
boxGroup.data = "boxeres"
boxGroup.isEditable = false
end
local box = app.sprite:newLayer( )
box.color = Color{ r = 0, g = 0, b = 0, a = 255 }
box.data = "boxeres"
box.parent = boxGroup
end
function AddRect( )
if not IsActiveBox( ) then
return
end
local rect = app.sprite.selection.bounds
if rect.isEmpty then
return
end
DrawRect( rect, app.layer.color )
app.refresh( )
if app.cel.data ~= "boxeres" then
app.cel.data = "boxeres"
app.cel.properties.rectCount = 0
end
app.cel.properties.rectCount = app.cel.properties.rectCount + 1
app.cel.properties[ "rect" .. app.cel.properties.rectCount ] = rect
end
function RemoveRects( )
if not IsActiveBox( ) or app.cel.data ~= "boxeres" then
return
end
local rect = app.sprite.selection.bounds
if rect.isEmpty then
return
end
app.sprite.selection:selectAll( )
local offset = 0
for i = 1, app.cel.properties.rectCount, 1 do
local celRect = app.cel.properties[ "rect" .. i ]
if rect:intersects( celRect ) then
app.cel.properties[ "rect" .. i ] = nil
DrawRect( celRect, Color{ r = 0, g = 0, b = 0, a = 0 } )
offset = offset + 1
else
local temp = app.cel.properties[ "rect" .. i ]
app.cel.properties[ "rect" .. i ] = nil
app.cel.properties[ "rect" .. i - offset ] = temp
end
end
if not app.cel then
return
app.refresh( )
end
app.cel.properties.rectCount = app.cel.properties.rectCount - offset
DrawBoxRects( app.layer.color )
app.refresh( )
end
function ShowRects( )
if not IsActiveBox( ) or not app.cel or app.cel.data ~= "boxeres" then
return
end
celProperties = app.cel.properties
properties = { rectCount = celProperties.rectCount }
for i = 1, properties.rectCount, 1 do
properties[ "rect" .. i ] = celProperties[ "rect" .. i ]
end
app.command.ClearCel( )
for i = 1, properties.rectCount, 1 do
DrawRect( properties[ "rect" .. i ] )
app.cel.properties[ "rect" .. i ] = properties[ "rect" .. i ]
end
app.cel.data = "boxeres"
app.cel.properties.rectCount = properties.rectCount
end
function SaveAs( filepath )
local boxGroup = GetBoxGroup( )
if not boxGroup then
return
end
local data = { boxes = { }, tags = { } }
for i, layer in ipairs( boxGroup.layers ) do
data.boxes[ layer.name ] = { color = ColorToTable( layer.color ), rects = { } }
for j = 1, #app.sprite.frames do
cel = layer:cel( j )
if not cel then
data.boxes[ layer.name ].rects[ j ] = 0
else
data.boxes[ layer.name ].rects[ j ] = { }
for k = 1, cel.properties.rectCount, 1 do
data.boxes[ layer.name ].rects[ j ][ k ] = RectToTable( cel.properties[ "rect" .. k ] )
end
end
end
end
for i, tag in ipairs( app.sprite.tags ) do
data.tags[ tag.name ] = { tag.fromFrame.frameNumber - 1, tag.frames }
end
local file = io.open( filepath, "w" )
io.output( file )
io.write( json.encode( data ) )
io.close( )
end
function init( plugin )
print( "Initializing..." )
local boxeresGroupID = "boxeres_id"
plugin:newMenuSeparator {
group = "layer_merge"
}
plugin:newMenuGroup {
id = boxeresGroupID,
title = "Boxeres",
group = "layer_merge"
}
plugin:newCommand {
id = "new_box_id",
title = "New Box",
group = boxeresGroupID,
onclick = AddBox,
onenabled = IsSprite
}
plugin:newMenuSeparator {
group = boxeresGroupID
}
plugin:newCommand {
id = "add_rect_id",
title = "Add Rect",
group = boxeresGroupID,
onclick = AddRect,
onenabled = IsSprite
}
plugin:newCommand {
id = "remove_rects_id",
title = "Remove Rects",
group = boxeresGroupID,
onclick = RemoveRects,
onenabled = IsSprite
}
plugin:newMenuSeparator {
group = boxeresGroupID
}
plugin:newCommand {
id = "show_rects_id",
title = "Show Rects",
group = boxeresGroupID,
onclick = ShowRects,
onenabled = IsSprite
}
plugin:newMenuSeparator {
group = boxeresGroupID
}
plugin:newCommand {
id = "save_as_id",
title = "Save As",
group = boxeresGroupID,
onclick = function( )
local dlg = Dialog( "Save" )
dlg:file {
id = "file_path",
label = "This is a label:",
title = "This is a title",
save = true,
filetypes = { "json" }
}
dlg:button {
id = "save",
text = "Save"
}
dlg:button {
id = "cancel",
text = "Cancel"
}
dlg:show( )
local data = dlg.data
if data.file_path == "" then
return
end
if data.save then
SaveAs( data.file_path )
end
end,
onenabled = IsSprite
}
print( "Initialized." )
end
function exit( plugin )
print( "Quit." )
end