-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditSlices.lua
1947 lines (1682 loc) · 64.6 KB
/
editSlices.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[Slices have an internal reference to the frame on which they were
created. This reference cannot be accessed via Lua script.
]]
local pivotOptions <const> = {
"TOP_LEFT",
"TOP_CENTER",
"TOP_RIGHT",
"CENTER_LEFT",
"CENTER",
"CENTER_RIGHT",
"BOTTOM_LEFT",
"BOTTOM_CENTER",
"BOTTOM_RIGHT",
}
---@param layer Layer
---@param array Layer[]
---@return Layer[]
local function appendLeaves(layer, array)
if layer.isVisible then
if layer.isGroup then
local childLayers <const> = layer.layers --[=[@as Layer[]]=]
local lenChildLayers <const> = #childLayers
local i = 0
while i < lenChildLayers do
i = i + 1
appendLeaves(childLayers[i], array)
end
elseif (not layer.isReference) then
array[#array + 1] = layer
end
end
return array
end
---@param orig number
---@param dest number
---@param t number
---@param range number
---@return number
local function lerpAngleCcw(orig, dest, t, range)
local rangeVerif <const> = range or 360.0
local o <const> = orig % rangeVerif
local d <const> = dest % rangeVerif
local diff <const> = d - o
if diff == 0.0 then return o end
local u <const> = 1.0 - t
if o > d then
return (u * o + t * (d + rangeVerif)) % rangeVerif
else
return u * o + t * d
end
end
---@param pivotCombo string
---@param w integer
---@param h integer
---@return Point
local function pivotFromPreset(pivotCombo, w, h)
if pivotCombo == "TOP_LEFT" then
return Point(0, 0)
elseif pivotCombo == "TOP_CENTER" then
return Point(w // 2, 0)
elseif pivotCombo == "TOP_RIGHT" then
return Point(w - 1, 0)
elseif pivotCombo == "CENTER_LEFT" then
return Point(0, h // 2)
elseif pivotCombo == "CENTER" then
return Point(w // 2, h // 2)
elseif pivotCombo == "CENTER_RIGHT" then
return Point(w - 1, h // 2)
elseif pivotCombo == "BOTTOM_LEFT" then
return Point(0, h - 1)
elseif pivotCombo == "BOTTOM_CENTER" then
return Point(w // 2, h - 1)
elseif pivotCombo == "BOTTOM_RIGHT" then
return Point(w - 1, h - 1)
else
return Point(w // 2, h // 2)
end
end
---@param x number
---@return integer
local function round(x)
local ix <const>, fx <const> = math.modf(x)
if ix <= 0 and fx <= -0.5 then
return ix - 1
elseif ix >= 0 and fx >= 0.5 then
return ix + 1
end
return ix
end
---@param left Slice
---@param right Slice
---@return boolean
local function tlComparator(left, right)
local aBounds <const> = left.bounds
local bBounds <const> = right.bounds
if aBounds and bBounds then
local ay <const> = aBounds.y
local by <const> = bBounds.y
if ay == by then
local ax <const> = aBounds.x
local bx <const> = bBounds.x
if ax == bx then
return left.name < right.name
end
return ax < bx
end
return ay < by
end
return left.name < right.name
end
---@param dx integer
---@param dy integer
---@param moveBounds boolean
---@param movePivot boolean
---@param moveInset boolean
---@param pivotCombo string
---@param insetAmount integer
local function translateSlices(
dx, dy,
moveBounds, movePivot, moveInset,
pivotCombo, insetAmount)
local sprite <const> = app.sprite
if not sprite then return end
local oldTool <const> = app.tool.id
app.tool = "slice"
local range <const> = app.range
if range.sprite ~= sprite then
app.tool = oldTool
return
end
local slices <const> = range.slices
local lenSlices <const> = #slices
if lenSlices < 1 then
app.tool = oldTool
return
end
local abs <const> = math.abs
local max <const> = math.max
local actFrObj <const> = app.frame or sprite.frames[1]
app.frame = sprite.frames[1]
if moveBounds then
local dxNonZero <const> = dx ~= 0
local dyNonZero <const> = dy ~= 0
local wSprite <const> = sprite.width
local hSprite <const> = sprite.height
local xGrOff = 0
local yGrOff = 0
local xGrScl = 1
local yGrScl = 1
local appPrefs <const> = app.preferences
if appPrefs then
local docPrefs <const> = appPrefs.document(sprite)
if docPrefs then
local gridPrefs <const> = docPrefs.grid
if gridPrefs then
local useSnap <const> = gridPrefs.snap --[[@as boolean]]
if useSnap then
local grid <const> = sprite.gridBounds
xGrOff = grid.x
yGrOff = grid.y
xGrScl = math.max(1, math.abs(grid.width))
yGrScl = math.max(1, math.abs(grid.height))
end
end
end
end
local trsName <const> = string.format("Nudge Slices (%d, %d)", dx, dy)
app.transaction(trsName, function()
local i = 0
while i < lenSlices do
i = i + 1
local slice <const> = slices[i]
local bounds <const> = slice.bounds
if bounds then
local xSrc <const> = bounds.x
local ySrc <const> = bounds.y
local xTrg = xSrc + dx
if dxNonZero then
local xGrid <const> = round((xSrc - xGrOff) / xGrScl)
xTrg = xGrOff + (xGrid + dx) * xGrScl
end
local yTrg = ySrc + dy
if dyNonZero then
local yGrid <const> = round((ySrc - yGrOff) / yGrScl)
yTrg = yGrOff + (yGrid + dy) * yGrScl
end
local wTrg <const> = max(1, abs(bounds.width))
local hTrg <const> = max(1, abs(bounds.height))
local xBrTrg <const> = xTrg + wTrg - 1
local yBrTrg <const> = yTrg + hTrg - 1
if xTrg >= 0 and yTrg >= 0
and xBrTrg < wSprite and yBrTrg < hSprite then
slice.bounds = Rectangle(xTrg, yTrg, wTrg, hTrg)
-- TODO: Option to move content of slice as well? Maybe
-- place this in a separate loop before bounds are moved?
-- sprite.selection = Selection(Rectangle(xSrc, ySrc, wTrg, hTrg))
-- app.command.MoveMask {
-- direction = "left",
-- amount = 1,
-- target = "content",
-- units = "pixel",
-- wrap = false
-- }
end -- End bounds contained by sprite
end -- End bounds not nil
end -- End slices loop
end) -- End transaction
end -- End move bounds check
if movePivot then
local trsName <const> = string.format("Nudge Pivots (%d, %d)", dx, dy)
app.transaction(trsName, function()
local j = 0
while j < lenSlices do
j = j + 1
local slice <const> = slices[j]
local xSrcPiv = 0
local ySrcPiv = 0
local srcPivot <const> = slice.pivot
if srcPivot then
xSrcPiv = srcPivot.x
ySrcPiv = srcPivot.y
else
local sliceBounds <const> = slice.bounds
if sliceBounds then
local wSlice <const> = sliceBounds.width
local hSlice <const> = sliceBounds.height
local pivPreset <const> = pivotFromPreset(
pivotCombo, wSlice, hSlice)
xSrcPiv = pivPreset.x
ySrcPiv = pivPreset.y
end
end
local xTrgPiv <const> = xSrcPiv + dx
local yTrgPiv <const> = ySrcPiv + dy
slice.pivot = Point(xTrgPiv, yTrgPiv)
end -- End slices loop
end) -- End transaction
end -- End move pivot check
if moveInset then
local insVerif <const> = math.abs(insetAmount)
local insVerif2 <const> = insVerif + insVerif
local trsName <const> = string.format("Nudge Insets (%d, %d)", dx, dy)
app.transaction(trsName, function()
local k = 0
while k < lenSlices do
k = k + 1
local slice <const> = slices[k]
local bounds <const> = slice.bounds
if bounds then
local wBounds <const> = max(1, abs(bounds.width))
local hBounds <const> = max(1, abs(bounds.height))
local xtlSrcInset = insVerif
local ytlSrcInset = insVerif
local wSrcInset = wBounds - insVerif2
local hSrcInset = hBounds - insVerif2
local srcInset <const> = slice.center
if srcInset then
xtlSrcInset = srcInset.x
ytlSrcInset = srcInset.y
wSrcInset = max(1, abs(srcInset.width))
hSrcInset = max(1, abs(srcInset.height))
end
local xtlTrgInset <const> = xtlSrcInset + dx
local ytlTrgInset <const> = ytlSrcInset + dy
local xbrTrgInset <const> = xtlTrgInset + wSrcInset - 1
local ybrTrgInset <const> = ytlTrgInset + hSrcInset - 1
if xtlTrgInset >= 0 and xtlTrgInset < wBounds
and ytlTrgInset >= 0 and ytlTrgInset < hBounds
and xbrTrgInset >= 0 and xbrTrgInset < wBounds
and ybrTrgInset >= 0 and ybrTrgInset < hBounds
and xtlTrgInset <= xbrTrgInset
and ytlTrgInset <= ybrTrgInset then
local wTrgInset <const> = 1 + xbrTrgInset - xtlTrgInset
local hTrgInset <const> = 1 + ybrTrgInset - ytlTrgInset
slice.center = Rectangle(
xtlTrgInset, ytlTrgInset,
wTrgInset, hTrgInset)
end -- End inset contained by bounds
end -- End bounds not nil
end -- End slices loop
end) -- End transaction
end -- End move inset check
app.frame = actFrObj
app.tool = oldTool
app.refresh()
end
local wSet = 24
local hSet = 24
local pivotSet = "TOP_LEFT"
local nudgeStep <const> = 1
local displayMoveChecks <const> = true
local wSliceMin <const> = 3
local hSliceMin <const> = 3
local enableCopyWarning <const> = false
local useColorInvert <const> = true
if app.preferences then
local newFilePrefs <const> = app.preferences.new_file
if newFilePrefs then
local wNewSprite <const> = newFilePrefs.width --[[@as integer]]
local hNewSprite <const> = newFilePrefs.height --[[@as integer]]
if wNewSprite and (wNewSprite // 10) > 0 then
wSet = wNewSprite // 10
end
if hNewSprite and (hNewSprite // 10) > 0 then
hSet = hNewSprite // 10
end
end
local maskPrefs <const> = app.preferences.selection
if maskPrefs then
local maskIndex <const> = maskPrefs.pivot_position --[[@as integer]]
pivotSet = pivotOptions[1 + maskIndex]
end
end
local dlg <const> = Dialog { title = "Edit Slices" }
dlg:button {
id = "selectAllButton",
text = "A&LL",
label = "Select:",
focus = true,
visible = true,
onclick = function()
-- Aseprite UI already contains function for this, but slice context
-- bar may not be visible.
local sprite <const> = app.sprite
if not sprite then return end
local spriteSlices <const> = sprite.slices
local lenSpriteSlices <const> = #spriteSlices
if lenSpriteSlices < 1 then return end
local oldTool <const> = app.tool.id
app.tool = "slice"
local range <const> = app.range
if range.sprite ~= sprite then
app.tool = oldTool
return
end
local actFrObj <const> = app.frame or sprite.frames[1]
app.frame = sprite.frames[1]
---@type Slice[]
local assignSlices <const> = {}
local i = 0
while i < lenSpriteSlices do
i = i + 1
assignSlices[i] = spriteSlices[i]
end
range.slices = assignSlices
app.frame = actFrObj
app.tool = oldTool
app.refresh()
end
}
dlg:button {
id = "selectMaskButton",
text = "&MASK",
focus = false,
visible = true,
onclick = function()
local sprite <const> = app.sprite
if not sprite then return end
local spriteSlices <const> = sprite.slices
local lenSpriteSlices <const> = #spriteSlices
if lenSpriteSlices < 1 then return end
local oldTool <const> = app.tool.id
app.tool = "slice"
local range <const> = app.range
if range.sprite ~= sprite then
app.tool = oldTool
return
end
local actFrObj <const> = app.frame or sprite.frames[1]
app.frame = sprite.frames[1]
---@type Slice[]
local containedSlices <const> = {}
-- This prevents errors when mask is in a transform preview state.
app.command.InvertMask()
app.command.InvertMask()
-- If no mask, then select everything.
local mask <const> = sprite.selection
if mask == nil or mask.isEmpty then
local h = 0
while h < lenSpriteSlices do
h = h + 1
containedSlices[h] = spriteSlices[h]
end
else
local abs <const> = math.abs
local max <const> = math.max
local i = 0
while i < lenSpriteSlices do
i = i + 1
local slice <const> = spriteSlices[i]
local sliceBounds <const> = slice.bounds
if sliceBounds then
local xtlSlice <const> = sliceBounds.x
local ytlSlice <const> = sliceBounds.y
local wSlice <const> = max(1, abs(sliceBounds.width))
local hSlice <const> = max(1, abs(sliceBounds.height))
local areaSlice <const> = wSlice * hSlice
local isContained = true
local j = 0
while isContained and j < areaSlice do
local xSample <const> = xtlSlice + j % wSlice
local ySample <const> = ytlSlice + j // wSlice
isContained = isContained
and mask:contains(Point(xSample, ySample))
j = j + 1
end
if isContained then
containedSlices[#containedSlices + 1] = slice
end
end
end
end
range.slices = containedSlices
app.frame = actFrObj
app.tool = oldTool
app.refresh()
end
}
dlg:button {
id = "deselectButton",
text = "&NONE",
focus = false,
visible = true,
onclick = function()
-- Aseprite UI already contains function for this, but slice context
-- bar may not be visible.
local sprite <const> = app.sprite
if not sprite then return end
local oldTool <const> = app.tool.id
app.tool = "slice"
local actFrObj <const> = app.frame or sprite.frames[1]
app.frame = sprite.frames[1]
local range <const> = app.range
if range.sprite ~= sprite then
app.tool = oldTool
return
end
range.slices = {}
app.frame = actFrObj
app.tool = oldTool
app.refresh()
end
}
dlg:newrow { always = false }
dlg:button {
id = "copyButton",
label = "Edit:",
text = "COP&Y",
focus = false,
onclick = function()
local sprite <const> = app.sprite
if not sprite then return end
local oldTool <const> = app.tool.id
app.tool = "slice"
local range <const> = app.range
if range.sprite ~= sprite then
app.tool = oldTool
return
end
if enableCopyWarning then
local response <const> = app.alert {
title = "Warning",
text = {
"Are you sure you want to copy these slices?",
"Custom data and properties will NOT be copied.",
"A slice's frame data cannot be copied."
},
buttons = { "&YES", "&NO" }
}
if response == 2 then
app.tool = oldTool
return
end
end
local actFrObj <const> = app.frame or sprite.frames[1]
local actFrIdx <const> = actFrObj.frameNumber
app.frame = sprite.frames[1]
local slices <const> = range.slices
local lenSlices <const> = #slices
if lenSlices < 1 then
app.frame = actFrObj
app.tool = oldTool
app.alert {
title = "Error",
text = "No slices were selected."
}
return
end
---@type Slice[]
local slicesToDupe <const> = {}
local i = 0
while i < lenSlices do
i = i + 1
slicesToDupe[i] = slices[i]
end
table.sort(slicesToDupe, tlComparator)
local abs <const> = math.abs
local min <const> = math.min
local max <const> = math.max
local defaultColor = Color { r = 0, g = 0, b = 255, a = 255 }
local appPrefs <const> = app.preferences
if appPrefs then
local slicePrefs <const> = appPrefs.slices
if slicePrefs then
local prefsColor <const> = slicePrefs.default_color --[[@as Color]]
if prefsColor then
if prefsColor.alpha > 0 then
defaultColor = Color {
r = min(max(prefsColor.red, 0), 255),
g = min(max(prefsColor.green, 0), 255),
b = min(max(prefsColor.blue, 0), 255),
a = min(max(prefsColor.alpha, 0), 255)
}
end
end
end
end
---@type Slice[]
local duplicates <const> = {}
app.transaction("Copy Slices", function()
local j = 0
while j < lenSlices do
j = j + 1
local srcSlice <const> = slicesToDupe[j]
local srcBounds <const> = srcSlice.bounds
if srcBounds then
local xBounds <const> = srcBounds.x
local yBounds <const> = srcBounds.y
local wBounds <const> = max(1, abs(srcBounds.width))
local hBounds <const> = max(1, abs(srcBounds.height))
local trgBounds <const> = Rectangle(
xBounds, yBounds, wBounds, hBounds)
local trgSlice <const> = sprite:newSlice(trgBounds)
duplicates[#duplicates + 1] = trgSlice
local fromFrIdx = actFrIdx - 1
if srcSlice.properties["fromFrame"] then
fromFrIdx = srcSlice.properties["fromFrame"] --[[@as integer]]
end
local toFrIdx = actFrIdx - 1
if srcSlice.properties["toFrame"] then
fromFrIdx = srcSlice.properties["toFrame"] --[[@as integer]]
end
-- Swap invalid from and to frames.
if toFrIdx < fromFrIdx then
fromFrIdx, toFrIdx = toFrIdx, fromFrIdx
end
trgSlice.properties["fromFrame"] = fromFrIdx
trgSlice.properties["toFrame"] = toFrIdx
local srcCenter <const> = srcSlice.center
if srcCenter and srcCenter ~= nil then
local xCenter <const> = srcCenter.x
local yCenter <const> = srcCenter.y
local wCenter <const> = max(1, abs(srcCenter.width))
local hCenter <const> = max(1, abs(srcCenter.height))
trgSlice.center = Rectangle(xCenter, yCenter,
wCenter, hCenter)
end
local trgColor = Color {
r = defaultColor.red,
g = defaultColor.green,
b = defaultColor.blue,
a = defaultColor.alpha
}
local srcColor <const> = srcSlice.color
if srcColor then
if srcColor.alpha > 0 then
local rSrc <const> = min(max(srcColor.red, 0), 255)
local gSrc <const> = min(max(srcColor.green, 0), 255)
local bSrc <const> = min(max(srcColor.blue, 0), 255)
local aSrc <const> = min(max(srcColor.alpha, 0), 255)
local rTrg = rSrc
local gTrg = gSrc
local bTrg = bSrc
local aTrg <const> = aSrc
if useColorInvert then
rTrg = 255 - rTrg
gTrg = 255 - gTrg
bTrg = 255 - bTrg
end
trgColor = Color {
r = rTrg,
g = gTrg,
b = bTrg,
a = aTrg
}
end
end
trgSlice.color = trgColor
local trgName = "Slice (Copy)"
local srcName <const> = srcSlice.name
if srcName then
if #srcName > 0 then
trgName = srcName .. " (Copy)"
end
end
trgSlice.name = trgName
local srcPivot <const> = srcSlice.pivot
if srcPivot and srcPivot ~= nil then
trgSlice.pivot = Point(srcPivot.x, srcPivot.y)
end
end
end
end)
app.frame = actFrObj
range.slices = duplicates
app.tool = oldTool
app.refresh()
end
}
dlg:button {
id = "deleteButton",
text = "DELE&TE",
focus = false,
onclick = function()
local sprite <const> = app.sprite
if not sprite then return end
local oldTool <const> = app.tool.id
app.tool = "slice"
local range <const> = app.range
if range.sprite ~= sprite then
app.tool = oldTool
return
end
local response <const> = app.alert {
title = "Warning",
text = "Are you sure you want to delete these slices?",
buttons = { "&YES", "&NO" }
}
if response == 2 then
app.tool = oldTool
return
end
local actFrObj <const> = app.frame or sprite.frames[1]
local slices <const> = range.slices
local lenSlices <const> = #slices
if lenSlices < 1 then
app.frame = actFrObj
app.tool = oldTool
app.alert {
title = "Error",
text = "No slices were selected."
}
return
end
---@type Slice[]
local slicesToRemove <const> = {}
local i = 0
while i < lenSlices do
i = i + 1
slicesToRemove[i] = slices[i]
end
-- Empty range prior to deleting.
range.slices = {}
app.transaction("Delete Slices", function()
local j = lenSlices + 1
while j > 1 do
j = j - 1
local slice <const> = slicesToRemove[j]
sprite:deleteSlice(slice)
end
end)
app.frame = actFrObj
app.tool = oldTool
app.refresh()
end
}
dlg:newrow { always = false }
dlg:button {
id = "fromFramesButton",
label = "Convert:",
text = "&FRAME",
focus = false,
onclick = function()
local sprite <const> = app.sprite
if not sprite then return end
local actFrObj <const> = app.frame or sprite.frames[1]
local actFrIdx <const> = actFrObj.frameNumber
local tlHidden = true
local trgColor = Color { r = 0, g = 0, b = 255, a = 255 }
local appPrefs <const> = app.preferences
if appPrefs then
local gnrlPrefs <const> = appPrefs.general
if gnrlPrefs then
local visTimeline <const> = gnrlPrefs.visible_timeline --[[@as boolean]]
if visTimeline and visTimeline == true then
tlHidden = false
end
end
local slicePrefs <const> = appPrefs.slices
if slicePrefs then
local prefsColor <const> = slicePrefs.default_color --[[@as Color]]
if prefsColor and prefsColor.alpha > 0 then
trgColor = Color {
r = math.min(math.max(prefsColor.red, 0), 255),
g = math.min(math.max(prefsColor.green, 0), 255),
b = math.min(math.max(prefsColor.blue, 0), 255),
a = math.min(math.max(prefsColor.alpha, 0), 255)
}
end
end
end
if tlHidden then
app.command.Timeline { open = true }
end
---@type Layer[]
local chosenLayers = {}
local range <const> = app.range
if range.sprite == sprite then
if range.isEmpty or range.type == RangeType.FRAMES then
---@type Layer[]
local leaves <const> = {}
local spriteLayers <const> = sprite.layers
local lenSpriteLayers <const> = #spriteLayers
local i = 0
while i < lenSpriteLayers do
i = i + 1
appendLeaves(spriteLayers[i], leaves)
end
chosenLayers = leaves
else
local rangeLayers <const> = range.layers
local lenRangeLayers <const> = #rangeLayers
local h = 0
while h < lenRangeLayers do
h = h + 1
local rangeLayer <const> = rangeLayers[h]
if rangeLayer.isVisible
and (not rangeLayer.isGroup)
and (not rangeLayer.isReference) then
chosenLayers[#chosenLayers + 1] = rangeLayer
end
end
end
end
if tlHidden then
app.command.Timeline { close = true }
end
local lenChosenLayers <const> = #chosenLayers
if lenChosenLayers < 1 then
app.alert {
title = "Error",
text = "No visible layers selected at this frame."
}
return
end
table.sort(chosenLayers, function(a, b)
if a.stackIndex == b.stackIndex then
return a.name < b.name
end
return a.stackIndex < b.stackIndex
end)
local oldTool <const> = app.tool.id
app.tool = "slice"
local args <const> = dlg.data
local inset <const> = args.insetAmount --[[@as integer]]
local pivotCombo <const> = args.pivotCombo --[[@as string]]
local newName <const> = args.nameEntry --[[@as string]]
local newNameVrf = "Slice"
if newName and #newName > 0 then
newNameVrf = newName
end
local wSprite <const> = sprite.width
local hSprite <const> = sprite.height
local alphaIndex <const> = sprite.transparentColor
local colorMode <const> = sprite.colorMode
local bkgHex = 0
app.command.SwitchColors()
local bkgColor <const> = app.fgColor
if bkgColor.alpha > 0 then
if colorMode == ColorMode.GRAY then
local sr <const> = bkgColor.red
local sg <const> = bkgColor.green
local sb <const> = bkgColor.blue
local gray <const> = (sr * 2126 + sg * 7152 + sb * 722) // 10000
bkgHex = (bkgColor.alpha << 0x08) | gray
elseif colorMode == ColorMode.INDEXED then
bkgHex = bkgColor.index
elseif colorMode == ColorMode.RGB then
bkgHex = bkgColor.rgbaPixel
end
end
app.command.SwitchColors()
-- The problem with offsetting by the frame base index is that it could
-- be negative.
local frIdxDisplay = actFrIdx - 1
local insVerif = math.abs(inset)
local xtlInset <const> = insVerif
local ytlInset <const> = insVerif
-- This could layer name instead of or in addition to number, but the
-- problem is that layer names are not unique identifiers.
local format <const> = "%s Fr%d No%d"
local strfmt <const> = string.format
local max <const> = math.max
local min <const> = math.min
---@type Slice[]
local newSlices <const> = {}
local lenNewSlices = 0
app.frame = sprite.frames[1]
app.transaction("New Slices From Frame", function()
local i = 0
while i < lenChosenLayers do
i = i + 1
local layer <const> = chosenLayers[i]
local cel <const> = layer:cel(actFrIdx)
if cel then
local xtlCel = 0
local ytlCel = 0
local wCel = 0
local hCel = 0
if layer.isTilemap then
-- Shrink bounds does not work with tile maps.
local celBounds <const> = cel.bounds
xtlCel = celBounds.x
ytlCel = celBounds.y
wCel = celBounds.width
hCel = celBounds.height
else
-- Cel image may not be trimmed of alpha.
-- Empty images will return zero size rectangle.
local celPos <const> = cel.position
local celImage <const> = cel.image
local ref <const> = layer.isBackground
and bkgHex or alphaIndex
local trimRect <const> = celImage:shrinkBounds(ref)
xtlCel = celPos.x + trimRect.x
ytlCel = celPos.y + trimRect.y
wCel = trimRect.width
hCel = trimRect.height
end
if wCel > 0 and hCel > 0 then
-- Cel may be out of bounds, so it must be intersected
-- with sprite canvas.
local xbrCelCl <const> = min(wSprite - 1, xtlCel + wCel - 1)
local ybrCelCl <const> = min(hSprite - 1, ytlCel + hCel - 1)
local xtlCelCl <const> = max(0, xtlCel)
local ytlCelCl <const> = max(0, ytlCel)
if xtlCelCl <= xbrCelCl and ytlCelCl <= ybrCelCl then
local wSlice <const> = 1 + xbrCelCl - xtlCelCl
local hSlice <const> = 1 + ybrCelCl - ytlCelCl
if wSlice >= wSliceMin and hSlice >= hSliceMin then
local slice <const> = sprite:newSlice(Rectangle(
xtlCelCl, ytlCelCl, wSlice, hSlice))
lenNewSlices = lenNewSlices + 1
newSlices[lenNewSlices] = slice
slice.color = trgColor
slice.name = strfmt(format, newNameVrf,
frIdxDisplay, lenNewSlices)
slice.pivot = pivotFromPreset(pivotCombo,
wSlice, hSlice)
slice.properties["fromFrame"] = actFrIdx - 1
slice.properties["toFrame"] = actFrIdx - 1
local xbrInset <const> = (wSlice - 1) - insVerif
local ybrInset <const> = (hSlice - 1) - insVerif
if xtlInset <= xbrInset and ytlInset <= ybrInset then
local wInset <const> = 1 + xbrInset - xtlInset
local hInset <const> = 1 + ybrInset - ytlInset
slice.center = Rectangle(
xtlInset, ytlInset, wInset, hInset)
end -- End inset corners are valid.
end -- End slice size is gteq minimum.
end -- End bounds corners are valid.
end -- End cel nonzero bounds.
end -- End cel exists.
end -- End cels loop.
end)
range.slices = newSlices
app.frame = actFrObj
app.tool = oldTool
app.refresh()