-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRetro Sprite Generator.jsx
1086 lines (886 loc) · 43.6 KB
/
Retro Sprite Generator.jsx
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
/**
* Title: Retro Sprite Generator
* Author: @magicjar
* Url: https://github.com/magicjar
*/
/*
@@@BUILDINFO@@@ Retro Sprite Generator.jsx 2.0.0
*/
/*
// BEGIN__HARVEST_EXCEPTION_ZSTRING
<javascriptresource>
<name>$$$/JavaScripts/RetroSpriteGenerator/Menu=Retro Sprite Generator...</name>
<category>scriptexport</category>
<menu>export</menu>
<enableinfo>true</enableinfo>
<eventid>c1448398-d731-4691-9c60-2f5410fc703a</eventid>
</javascriptresource>
// END__HARVEST_EXCEPTION_ZSTRING
*/
#target photoshop
///////////////////////////////////////////////////////////////////////////////
// Globals
///////////////////////////////////////////////////////////////////////////////
var w,
tabIndex = 0,
frames = getFrameCount(),
currentDoc,
columns = 4,
rows = 4,
selectedScale = 0,
scaleNumber = 0,
selectedResample = 0,
resampleMethod = 0,
spriteWidth, // Original width
spriteHeight, // Original height
spriteResolution,
padding = 0,
offset = 0;
var jpegIndex = 0;
var pngIndex = 1;
var exportButtonID = 1;
var cancelButtonID = 2;
///////////////////////////////////////////////////////////////////////////////
// Dispatch
///////////////////////////////////////////////////////////////////////////////
init();
function init() {
if (frames == 0) {
tabIndex = 1;
}
var exportOptions = new Object();
initExportOptions(exportOptions);
// Get last used params via Photoshop registry
try {
var d = app.getCustomOptions("f987ff71-e289-49e3-9a5f-f35b106321e1");
descriptorToObject(exportOptions, d, "Retro Sprite Generator settings");
} catch (e) {
}
descriptorToObject(exportOptions, app.playbackParameters, "Retro Sprite Generator settings");
currentDoc = app.activeDocument;
spriteWidth = currentDoc.width;
spriteHeight = currentDoc.height;
spriteResolution = currentDoc.resolution;
calculateColRowVals();
if (DialogModes.ALL == app.playbackDisplayDialogs) {
if (cancelButtonID == createWindow(exportOptions)) {
return 'cancel';
}
}
switch (tabIndex) {
case 1:
createSingleImage(exportOptions);
break;
default:
createSpriteSheet(exportOptions);
break;
}
// Set last used params to Photoshop registry
var d = objectToDescriptor(exportOptions, "Retro Sprite Generator settings");
app.putCustomOptions("f987ff71-e289-49e3-9a5f-f35b106321e1", d);
var dd = objectToDescriptor(exportOptions, "Retro Sprite Generator settings");
app.playbackParameters = dd;
}
///////////////////////////////////////////////////////////////////////////////
// Function: createSingleImage
// Usage: single file document builder
// Input: export options
// Return: <none>, a file on disk
///////////////////////////////////////////////////////////////////////////////
function createSingleImage(exportOptions) {
try {
var duppedDoc = app.activeDocument.duplicate();
app.activeDocument = duppedDoc;
if (exportOptions.exportType == 1)
exportGroupRecursively(exportOptions, duppedDoc, currentDoc, duppedDoc);
else
exportLayerRecursively(exportOptions, duppedDoc, currentDoc, duppedDoc);
duppedDoc.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = currentDoc;
} catch (ex) {
alert("An error occured, please submit a bug report. Error: " + ex);
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: createSpriteSheet
// Usage: sprite-sheet document builder
// Input: export options
// Return: <none>, a file on disk
///////////////////////////////////////////////////////////////////////////////
function createSpriteSheet(exportOptions) {
try {
if (frames == 0) {
alert("No animation frames were found.\nThis script requires minimum of 1 frame animation to create a sprite sheet.");
return;
}
var savedPrefs = {
typeUnits: app.preferences.typeUnits,
rulerUnits: app.preferences.rulerUnits
};
app.preferences.typeUnits = TypeUnits.PIXELS;
app.preferences.rulerUnits = Units.PIXELS;
w.hide();
// Parse forms
columns = parseInt(w.tabGroup.spriteTab.columns.text);
rows = parseInt(w.tabGroup.spriteTab.rows.text);
padding = parseInt(w.tabGroup.spriteTab.padding.text);
offset = parseInt(w.tabGroup.spriteTab.offset.text);
var startFrame = parseInt(w.tabGroup.spriteTab.startFrame.text);
// Scaled width and height variable
var scaledWidth = spriteWidth * scaleNumber;
var scaledHeight = spriteHeight * scaleNumber;
// Create namming
var sheetName = exportOptions.fileNamePrefix + "_" + parseInt(scaledWidth) + "x" + parseInt(scaledHeight);
// Duplicate original Document
var duppedDoc = app.activeDocument.duplicate();
// Resize and Resample duplicated Document if scaling is true
if (scaleNumber > 1) {
duppedDoc.resizeImage(scaledWidth, scaledHeight, spriteResolution, resampleMethod, sheetName + "_dupped");
}
// Create temporary Document
var tempDoc = app.documents.add(scaledWidth, scaledHeight, spriteResolution, sheetName + "_tmp");
// Create sprite sheet Document
var spriteSheetDoc = app.documents.add((scaledWidth * columns) + (padding * (columns - 1)), (scaledHeight * rows) + (padding * (rows - 1)), spriteResolution, sheetName + "_spritesheet");
var cellSize = getSelectionShape(scaledWidth, 0, scaledHeight, 0);
if (w.tabGroup.spriteTab.startFromTop.value == true) {
var currentColumn = 0,
currentRow = 0;
} else {
var currentColumn = 0,
currentRow = rows - 1;
}
if (frames > 0) {
for (var i = 0; i < frames; i++) {
app.activeDocument = duppedDoc;
selectFrame(startFrame + i);
app.activeDocument.selection.select(cellSize);
// Only way at the moment to check for empty selection is to catch the exception
var selectionIsEmpty = false;
try {
app.activeDocument.selection.copy(true);
} catch (ex) {
selectionIsEmpty = true;
}
if (!selectionIsEmpty) {
app.activeDocument = tempDoc;
app.activeDocument.selection.select(cellSize);
// paste in place might not work in versions below CS5
pasteInPlace();
var layer = app.activeDocument.activeLayer.duplicate(spriteSheetDoc);
app.activeDocument = spriteSheetDoc;
layer.translate((currentColumn * scaledWidth) + (currentColumn * padding), (currentRow * scaledHeight) + (currentRow * padding));
}
currentColumn++;
if (currentColumn >= columns) {
if (w.tabGroup.spriteTab.startFromTop.value == true) {
currentRow++;
currentColumn = 0;
} else {
currentRow--;
currentColumn = 0;
}
}
}
app.activeDocument = tempDoc;
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = duppedDoc;
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = spriteSheetDoc;
// Adding offset
if (offset > 0)
app.activeDocument.resizeCanvas(spriteSheetDoc.width + offset * 2, spriteSheetDoc.height + offset * 2, AnchorPosition.MIDDLECENTER);
// Remove the default background layer
app.activeDocument.artLayers.getByName(app.activeDocument.backgroundLayer.name).remove();
exportOptions.pngInterlaced = w.tabGroup.spriteTab.optionsPanel.optionsGroup.interlaced.value;
exportOptions.pngTransparency = w.tabGroup.spriteTab.optionsPanel.optionsGroup.transparency.value;
exportOptions.png8 = w.tabGroup.spriteTab.optionsPanel.optionsGroup.smallbit.value;
saveFile(spriteSheetDoc, sheetName, exportOptions);
app.preferences.typeUnits = savedPrefs.typeUnits;
app.preferences.rulerUnits = savedPrefs.rulerUnits;
}
spriteSheetDoc.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = currentDoc;
} catch (ex) {
alert("An error occured, please submit a bug report. Error: " + ex);
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: exportLayerRecursively
// Usage: export all layer to a file, recursively
// Input: document or layerset
// Return: file(s)
///////////////////////////////////////////////////////////////////////////////
function exportLayerRecursively(exportOptions, dupObj, oriObj, dupDocRef) {
setInvisibleAllArtLayers(dupObj);
for (var k = 0; k < dupObj.artLayers.length; k++) {
if (exportOptions.visibleOnly)
if (!oriObj.artLayers[k].visible)
continue;
dupObj.artLayers[k].visible = true;
var tmpName = exportOptions.fileNamePrefix + "_" + dupObj.artLayers[k].name;
var duppedDocumentTmp = dupDocRef.duplicate();
if (exportOptions.fileType != pngIndex)
duppedDocumentTmp.flatten();
else
if (exportOptions.pngTrim)
app.activeDocument.trim(TrimType.TRANSPARENT);
saveFile(duppedDocumentTmp, tmpName, exportOptions);
duppedDocumentTmp.close(SaveOptions.DONOTSAVECHANGES);
dupObj.artLayers[k].visible = false;
}
// Recursive
for (var i = 0; i < dupObj.layerSets.length; i++) {
if (exportOptions.visibleOnly)
if (!oriObj.layerSets[i].visible)
continue;
exportLayerRecursively(exportOptions, dupObj.layerSets[i], oriObj.layerSets[i], dupDocRef); // recursive
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: exportGroupRecursively
// Usage: export all group of layer to a file
// Input: document or layerset
// Return: file(s)
///////////////////////////////////////////////////////////////////////////////
function exportGroupRecursively(exportOptions, dupObj, oriObj, dupDocRef) {
setInvisibleAllLayerSets(dupObj, false);
for (var i = 0; i < dupObj.layerSets.length; i++) {
if (exportOptions.visibleOnly)
if (!oriObj.layerSets[i].visible)
continue;
dupObj.layerSets[i].visible = true;
var tmpName = exportOptions.fileNamePrefix + "_" + dupObj.layerSets[i].name;
var duppedDocumentTmp = dupDocRef.duplicate();
if (exportOptions.fileType != pngIndex)
duppedDocumentTmp.flatten();
else
if (exportOptions.pngTrim)
app.activeDocument.trim(TrimType.TRANSPARENT);
saveFile(duppedDocumentTmp, tmpName, exportOptions);
duppedDocumentTmp.close(SaveOptions.DONOTSAVECHANGES);
dupObj.layerSets[i].visible = false;
}
// Recursive
// for (var i = 0; i < dupObj.layerSets.length; i++) {
// // if (visibleOnly) {
// // if (!oriObj.layerSets[i].visible) {
// // continue;
// exportGroupRecursively(exportOptions, dupObj, oriObj, fileName);
// }
}
///////////////////////////////////////////////////////////////////////////////
// Function: getFrameCount
// Usage: Count the number of frames in the timeline
// Input: none
// Return: integer
///////////////////////////////////////////////////////////////////////////////
function getFrameCount() {
for (var f = 1; f < 999; f++)
if (selectFrame(f) == false)
return f - 1;
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// Function: selectFrame
// Usage: Select animation frame
// Input: frame number
// Return: boolean and frame selected
///////////////////////////////////////////////////////////////////////////////
function selectFrame(number) {
try {
var desc = new ActionDescriptor();
var ref = new ActionReference();
var idslct = charIDToTypeID("slct");
var idnull = charIDToTypeID("null");
var idanimationFrameClass = stringIDToTypeID("animationFrameClass");
ref.putIndex(idanimationFrameClass, number);
desc.putReference(idnull, ref);
executeAction(idslct, desc, DialogModes.NO);
return true;
} catch (e) {
//
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
// Function: getSelectionShape
// Usage: Get selection
// Input: width, column, height, row
// Return: Shape
///////////////////////////////////////////////////////////////////////////////
function getSelectionShape(width, column, height, row) {
var shape = [
[column * width, row * height], // top left
[column * width, row * height + height], // bottom left
[column * width + width, row * height + height], // bottom right
[column * width + width, row * height] // top right
];
return shape;
}
///////////////////////////////////////////////////////////////////////////////
// Function: pasteInPlace
// Usage: Paste selected layer into document
// Input: none
// Return: Layer is pasted in place
///////////////////////////////////////////////////////////////////////////////
function pasteInPlace() {
var idpast = charIDToTypeID("past");
var desc4 = new ActionDescriptor();
var idinPlace = stringIDToTypeID("inPlace");
desc4.putBoolean(idinPlace, true);
var idAntA = charIDToTypeID("AntA");
var idAnnt = charIDToTypeID("Annt");
var idAnno = charIDToTypeID("Anno");
desc4.putEnumerated(idAntA, idAnnt, idAnno);
executeAction(idpast, desc4, DialogModes.NO);
}
///////////////////////////////////////////////////////////////////////////////
// Function: calculateColRowVals
// Usage: Calculate sprite-sheet rows and columns
// Input: none
// Return: Total rows and columns
///////////////////////////////////////////////////////////////////////////////
function calculateColRowVals() {
rows = Math.floor(Math.sqrt(frames));
columns = Math.ceil(frames / rows);
}
///////////////////////////////////////////////////////////////////////////////
// Function: onFramesChange
// Usage: Recalculate row and column when event is triggered
// Input: event
// Return: New rows and columns
///////////////////////////////////////////////////////////////////////////////
function onFramesChange(e) {
frames = parseInt(w.tabGroup.spriteTab.endFrame.text) - parseInt(w.tabGroup.spriteTab.startFrame.text) + 1;
calculateColRowVals();
w.tabGroup.spriteTab.row.text = rows;
w.tabGroup.spriteTab.columns.text = columns;
}
///////////////////////////////////////////////////////////////////////////////
// Function: setInvisibleAllArtLayers
// Usage: unlock and make invisible all art layers, recursively
// Input: document or layerset
// Return: all art layers are unlocked and invisible
///////////////////////////////////////////////////////////////////////////////
function setInvisibleAllArtLayers(obj) {
for (var i = 0; i < obj.artLayers.length; i++) {
obj.artLayers[i].allLocked = false;
obj.artLayers[i].visible = false;
}
for (var i = 0; i < obj.layerSets.length; i++) {
setInvisibleAllArtLayers(obj.layerSets[i]);
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: setInvisibleAllLayerSets
// Usage: unlock and make invisible all layer sets (layer group)
// Input: document or layerset and is recursive
// Return: all art layers are unlocked and invisible
///////////////////////////////////////////////////////////////////////////////
function setInvisibleAllLayerSets(obj, recursively) {
for (var i = 0; i < obj.layerSets.length; i++) {
obj.layerSets[i].visible = false;
}
if (!recursively)
return;
for (var i = 0; i < obj.layerSets.length; i++) {
setInvisibleAllLayerSets(obj.layerSets[i]);
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: removeAllInvisibleArtLayers
// Usage: remove all the invisible art layers, recursively
// Input: document or layer set
// Return: <none>, all layers that were invisible are now gone
///////////////////////////////////////////////////////////////////////////////
function removeAllInvisibleArtLayers(obj) {
for (var i = obj.artLayers.length - 1; 0 <= i; i--)
if (!obj.artLayers[i].visible)
obj.artLayers[i].remove();
for (var i = obj.layerSets.length - 1; 0 <= i; i--)
removeAllInvisibleArtLayers(obj.layerSets[i]);
}
///////////////////////////////////////////////////////////////////////////////
// Function: removeAllEmptyLayerSets
// Usage: find all empty layer sets and remove them, recursively
// Input: document or layer set
// Return: empty layer sets are now gone
///////////////////////////////////////////////////////////////////////////////
function removeAllEmptyLayerSets(obj) {
var foundEmpty = true;
for (var i = obj.layerSets.length - 1; 0 <= i; i--) {
if (removeAllEmptyLayerSets(obj.layerSets[i]))
obj.layerSets[i].remove();
else
foundEmpty = false;
}
if (obj.artLayers.length > 0)
foundEmpty = false;
return foundEmpty;
}
///////////////////////////////////////////////////////////////////////////////
// Function: zeroSuppress
// Usage: return a string padded to digit(s)
// Input: num to convert, digit count needed
// Return: string padded to digit length
///////////////////////////////////////////////////////////////////////////////
function removeAllInvisible(docRef) {
removeAllInvisibleArtLayers(docRef);
removeAllEmptyLayerSets(docRef);
}
///////////////////////////////////////////////////////////////////////////////
// Function: saveFile
// Usage: the worker routine, take our params and save the file accordingly
// Input: reference to the document, the name of the output file,
// export info object containing more information
// Return: <none>, a file on disk
///////////////////////////////////////////////////////////////////////////////
function saveFile(docRef, fileNameBody, exportOptions) {
switch (exportOptions.fileType) {
case jpegIndex:
var jpgSaveOptions = new JPEGSaveOptions();
var saveFile = new File(exportOptions.destination + "/" + fileNameBody + ".jpg");
jpgSaveOptions.embedColorProfile = exportOptions.icc;
jpgSaveOptions.quality = exportOptions.jpegQuality;
docRef.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
break;
case pngIndex:
var pngS4WOptions = new ExportOptionsSaveForWeb();
var saveFile = new File(exportOptions.destination + "/" + fileNameBody + ".png");
pngS4WOptions.format = SaveDocumentType.PNG;
pngS4WOptions.PNG8 = exportOptions.png8;
pngS4WOptions.transparency = exportOptions.pngTransparency;
pngS4WOptions.interlaced = exportOptions.pngInterlaced;
pngS4WOptions.includeProfile = exportOptions.icc;
pngS4WOptions.quality = 100;
docRef.exportDocument(saveFile, ExportType.SAVEFORWEB, pngS4WOptions);
break;
default:
if (DialogModes.NO != app.playbackDisplayDialogs) {
alert("Unexpected error");
}
break;
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: createWindow
// Usage: pop the ui and get user settings
// Input: exportOptions object containing our parameters
// Return: on ok, the dialog info is set to the exportOptions object
///////////////////////////////////////////////////////////////////////////////
function createWindow(exportOptions) {
w = new Window('dialog', 'Retro Sprite Generator', undefined, { closeButton: true });
w.tabGroup = w.add('tabbedpanel');
w.tabGroup.onChange = function () {
switch (w.tabGroup.selection.text) {
case "Spritesheet Export":
tabIndex = 0;
break;
default:
tabIndex = 1;
break;
}
}
drawSpritesheetGUI(exportOptions);
drawSingleImageGUI(exportOptions);
// Destination
w.destinationPanel = w.add('panel', undefined, "Export Destination");
// Destination Preferences
w.destinationPanel.destinationGroup = w.destinationPanel.add("group");
w.destinationPanel.destinationGroup.destinationForm = w.destinationPanel.destinationGroup.add("edittext", [0, 0, 305, 20], exportOptions.destination.toString());
w.destinationPanel.destinationGroup.destinationBrowse = w.destinationPanel.destinationGroup.add("button", [0, 0, 100, 20], "Browse");
w.destinationPanel.destinationGroup.destinationBrowse.onClick = function () {
var defaultFolder = w.destinationPanel.destinationGroup.destinationForm.text;
var testFolder = new Folder(w.destinationPanel.destinationGroup.destinationForm.text);
if (!testFolder.exists) {
defaultFolder = "~";
}
var selFolder = Folder.selectDialog("Select Destination", defaultFolder);
if (selFolder != null) {
w.destinationPanel.destinationGroup.destinationForm.text = selFolder.fsName;
}
w.defaultElement.active = true;
}
w.destinationPanel.destinationGroup.destinationReset = w.destinationPanel.destinationGroup.add("button", [0, 0, 50, 20], "Reset");
w.destinationPanel.destinationGroup.destinationReset.onClick = function () {
w.destinationPanel.destinationGroup.destinationForm.text = exportOptions.destination = Folder(app.activeDocument.fullName.parent).fsName;
}
// Filename
w.namePanel = w.add('panel', undefined, "File Name Prefix");
// Filename Preferences
w.namePanel.nameGroup = w.namePanel.add('group');
w.namePanel.nameGroup.nameText = w.namePanel.nameGroup.add("edittext", [0, 0, 415, 20], exportOptions.fileNamePrefix.toString());
w.namePanel.nameGroup.resetName = w.namePanel.nameGroup.add("button", [0, 0, 50, 20], "Reset");
w.namePanel.nameGroup.resetName.onClick = function () {
w.namePanel.nameGroup.nameText.text = exportOptions.fileNamePrefix = app.activeDocument.name.split('.')[0]
}
// Action Buttons
var buttons = w.add('group');
buttons.cancel = buttons.add('button', undefined, 'Cancel');
buttons.cancel.onClick = function () {
w.close(cancelButtonID);
};
buttons.export = buttons.add('button', undefined, 'Export');
buttons.export.onClick = function () {
var destination = w.destinationPanel.destinationGroup.destinationForm.text;
if (destination.length == 0) {
alert('Please specify destination.');
return;
}
var testFolder = new Folder(destination);
if (!testFolder.exists) {
alert('Destination does not exist.');
return;
}
w.close(exportButtonID);
};
w.tabGroup.selection = tabIndex;
var result = w.show();
if (cancelButtonID == result)
return result;
exportOptions.destination = w.destinationPanel.destinationGroup.destinationForm.text;
exportOptions.fileNamePrefix = w.namePanel.nameGroup.nameText.text;
exportOptions.exportType = w.tabGroup.singleTab.ddTypeIndex.selection.index;
exportOptions.visibleOnly = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.visibleOnly.value;
exportOptions.fileType = w.tabGroup.singleTab.exportTypePanel.exportTypeGroup.ddFileType.selection.index;
if (exportOptions.fileType == jpegIndex)
exportOptions.icc = w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.icc.value;
else
exportOptions.icc = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.icc.value;
exportOptions.pngTransparency = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.pngTransparency.value;
exportOptions.pngInterlaced = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.pngInterlaced.value;
exportOptions.pngTrim = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.pngTrim.value;
exportOptions.png8 = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.png8.value;
exportOptions.jpegQuality = w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup.jpegQualityText.text;
return result;
}
///////////////////////////////////////////////////////////////////////////////
// Function: drawSpritesheetGUI
// Usage: Draw sprite-sheet GUI
// Input: exportOptions object containing our parameters
// Return: none
///////////////////////////////////////////////////////////////////////////////
function drawSpritesheetGUI(exportOptions) {
w.tabGroup.spriteTab = w.tabGroup.add('tab', undefined, 'Spritesheet Export');
// Frames
w.tabGroup.spriteTab.framePanel = w.tabGroup.spriteTab.add('panel', undefined, "Frames");
// Frame Preferences
w.tabGroup.spriteTab.frameGroup = w.tabGroup.spriteTab.framePanel.add('group');
w.tabGroup.spriteTab.frameGroup.add('StaticText', [0, 0, 70, 20], 'Start frame:');
w.tabGroup.spriteTab.startFrame = w.tabGroup.spriteTab.frameGroup.add('EditText', [0, 0, 120, 20], 1);
w.tabGroup.spriteTab.startFrame.onChange = onFramesChange;
w.tabGroup.spriteTab.frameGroup.add('StaticText', [0, 0, 70, 20], 'End frame:');
w.tabGroup.spriteTab.endFrame = w.tabGroup.spriteTab.frameGroup.add('EditText', [0, 0, 120, 20], frames);
w.tabGroup.spriteTab.endFrame.onChange = onFramesChange;
// Sizes
w.tabGroup.spriteTab.dimensionsPanel = w.tabGroup.spriteTab.add('panel', undefined, "Sizes");
// Size Preferences
w.tabGroup.spriteTab.dimensionsGroup = w.tabGroup.spriteTab.dimensionsPanel.add('group');
w.tabGroup.spriteTab.dimensionsGroup.add('StaticText', [0, 0, 70, 20], 'Columns:');
w.tabGroup.spriteTab.columns = w.tabGroup.spriteTab.dimensionsGroup.add('EditText', [0, 0, 120, 20], columns);
w.tabGroup.spriteTab.columns.helpTip = 'Number of columns';
w.tabGroup.spriteTab.dimensionsGroup.add('StaticText', [0, 0, 70, 20], 'Rows:');
w.tabGroup.spriteTab.rows = w.tabGroup.spriteTab.dimensionsGroup.add('EditText', [0, 0, 120, 20], rows);
w.tabGroup.spriteTab.rows.helpTip = 'Number of rows';
// Image Scale
w.tabGroup.spriteTab.imageScalePanel = w.tabGroup.spriteTab.add('panel', undefined, "Image Scale");
// Image Scale Preferences
w.tabGroup.spriteTab.imageScaleGroup = w.tabGroup.spriteTab.imageScalePanel.add('group');
w.tabGroup.spriteTab.imageScaleGroup.add('StaticText', [0, 0, 70, 20], 'Scale:');
w.tabGroup.spriteTab.ddScaleNumber = w.tabGroup.spriteTab.imageScaleGroup.add("dropdownlist", [0, 0, 120, 20], ['Default', '@2x', '@3x']);
w.tabGroup.spriteTab.ddScaleNumber.onChange = function () {
// get scale number
selectedScale = this.selection.index + 1;
switch (selectedScale) {
case 1: // Default / no scale
scaleNumber = 1;
break;
case 2: // 2x scale
scaleNumber = 2;
break;
case 3: // 3x scale
scaleNumber = 3;
break;
}
}
// Resample Preferences
w.tabGroup.spriteTab.imageScaleGroup.add('StaticText', [0, 0, 70, 20], 'Resample:');
w.tabGroup.spriteTab.ddResampleMethod = w.tabGroup.spriteTab.imageScaleGroup.add("dropdownlist", [0, 0, 120, 20], ['Automatic', 'Bicubic', 'Bicubic Automatic', 'Bicubic Sharper', 'Bicubic Smoother', 'Bilinear', 'Nearest Neighbor', 'None', 'Preserve Details']);
w.tabGroup.spriteTab.ddResampleMethod.onChange = function () {
selectedResample = this.selection.index;
switch (selectedResample) {
case 0:
resampleMethod = ResampleMethod.AUTOMATIC;
break;
case 1:
resampleMethod = ResampleMethod.BICUBIC;
break;
case 2:
resampleMethod = ResampleMethod.BICUBICAUTOMATIC;
break;
case 3:
resampleMethod = ResampleMethod.BICUBICSHARPER;
break;
case 4:
resampleMethod = ResampleMethod.BICUBICSMOOTHER;
break;
case 5:
resampleMethod = ResampleMethod.BILINEAR;
break;
case 6:
resampleMethod = ResampleMethod.NEARESTNEIGHBOR;
break;
case 7:
resampleMethod = ResampleMethod.NONE;
break;
case 8:
resampleMethod = ResampleMethod.PRESERVEDETAILS;
break;
}
}
// Spacing
w.tabGroup.spriteTab.spacoffPanel = w.tabGroup.spriteTab.add('panel', undefined, "Spacing");
// Spacing Preferences
w.tabGroup.spriteTab.spacingGroup = w.tabGroup.spriteTab.spacoffPanel.add('group');
w.tabGroup.spriteTab.spacingGroup.add('StaticText', [0, 0, 70, 20], 'Offset:');
w.tabGroup.spriteTab.offset = w.tabGroup.spriteTab.spacingGroup.add('EditText', [0, 0, 120, 20], offset);
w.tabGroup.spriteTab.offset.helpTip = 'Outer space around sprite sheet';
w.tabGroup.spriteTab.spacingGroup.add('StaticText', [0, 0, 70, 20], 'Padding:');
w.tabGroup.spriteTab.padding = w.tabGroup.spriteTab.spacingGroup.add('EditText', [0, 0, 120, 20], padding);
w.tabGroup.spriteTab.padding.helpTip = 'Space between each images';
// Start From
w.tabGroup.spriteTab.startFromPanel = w.tabGroup.spriteTab.add('panel', undefined, "Start From");
// Option Preferences
w.tabGroup.spriteTab.startFromGroup = w.tabGroup.spriteTab.startFromPanel.add('group');
w.tabGroup.spriteTab.startFromTop = w.tabGroup.spriteTab.startFromGroup.add('radiobutton', [0, 0, 200, 20], 'Top');
w.tabGroup.spriteTab.startFromTop.value = true;
w.tabGroup.spriteTab.startFromBottom = w.tabGroup.spriteTab.startFromGroup.add('radiobutton', [0, 0, 200, 20], 'Bottom');
w.tabGroup.spriteTab.startFromBottom.value = false;
w.tabGroup.spriteTab.ddScaleNumber.items[selectedScale].selected = true;
w.tabGroup.spriteTab.ddResampleMethod.items[selectedResample].selected = true;
// Options
w.tabGroup.spriteTab.optionsPanel = w.tabGroup.spriteTab.add('panel', undefined, "Export Options");
w.tabGroup.spriteTab.optionsPanel.alignment = ['fill', 'fill'];
// Option Preferences
w.tabGroup.spriteTab.optionsPanel.optionsGroup = w.tabGroup.spriteTab.optionsPanel.add('group');
w.tabGroup.spriteTab.optionsPanel.optionsGroup.interlaced = w.tabGroup.spriteTab.optionsPanel.optionsGroup.add('checkbox', undefined, 'Interlaced');
w.tabGroup.spriteTab.optionsPanel.optionsGroup.interlaced.value = exportOptions.pngInterlaced;
w.tabGroup.spriteTab.optionsPanel.optionsGroup.transparency = w.tabGroup.spriteTab.optionsPanel.optionsGroup.add('checkbox', undefined, 'Transparency');
w.tabGroup.spriteTab.optionsPanel.optionsGroup.transparency.value = exportOptions.pngTransparency;
w.tabGroup.spriteTab.optionsPanel.optionsGroup.smallbit = w.tabGroup.spriteTab.optionsPanel.optionsGroup.add('checkbox', undefined, 'Smaller File (8-bit)');
w.tabGroup.spriteTab.optionsPanel.optionsGroup.smallbit.value = exportOptions.png8;
}
///////////////////////////////////////////////////////////////////////////////
// Function: drawSingleImageGUI
// Usage: Draw files export GUI
// Input: exportOptions object containing our parameters
// Return: none
///////////////////////////////////////////////////////////////////////////////
function drawSingleImageGUI(exportOptions) {
w.tabGroup.singleTab = w.tabGroup.add('tab', undefined, 'Files Export');
// Export Mode
w.tabGroup.singleTab.exportModePanel = w.tabGroup.singleTab.add('panel', undefined, "Export Mode");
// Export Mode Preferences
w.tabGroup.singleTab.exportModePanel.exportModeGroup = w.tabGroup.singleTab.exportModePanel.add('group');
w.tabGroup.singleTab.exportModePanel.exportModeGroup.add('StaticText', [0, 0, 70, 20], 'Type:');
w.tabGroup.singleTab.ddTypeIndex = w.tabGroup.singleTab.exportModePanel.exportModeGroup.add("dropdownlist", [0, 0, 330, 20], ['Layers', 'Groups']);
w.tabGroup.singleTab.ddTypeIndex.items[exportOptions.exportType].selected = true;
// Export Type
w.tabGroup.singleTab.exportTypePanel = w.tabGroup.singleTab.add('panel', undefined, "File Setting");
// Export Type Preferences
w.tabGroup.singleTab.exportTypePanel.exportTypeGroup = w.tabGroup.singleTab.exportTypePanel.add('group');
w.tabGroup.singleTab.exportTypePanel.exportTypeGroup.add('StaticText', [0, 0, 70, 20], 'Format:');
w.tabGroup.singleTab.exportTypePanel.exportTypeGroup.ddFileType = w.tabGroup.singleTab.exportTypePanel.exportTypeGroup.add("dropdownlist", [0, 0, 330, 20], ['JPEG', 'PNG']);
w.tabGroup.singleTab.exportTypePanel.exportTypeGroup.ddFileType.onChange = function () {
hideAllFileTypePanel(w);
switch (this.selection.index) {
case jpegIndex:
w.tabGroup.singleTab.optionsPanel.text = 'JPEG Options';
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.show();
break;
case pngIndex:
w.tabGroup.singleTab.optionsPanel.text = 'PNG Options';
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.show();
break;
}
}
// Options Panel
w.tabGroup.singleTab.optionsPanel = w.tabGroup.singleTab.add('panel', undefined, "Options");
w.tabGroup.singleTab.optionsPanel.orientation = 'stack';
w.tabGroup.singleTab.optionsPanel.alignment = 'fill';
w.tabGroup.singleTab.optionsPanel.alignChildren = 'fill';
// PNG Options
w.tabGroup.singleTab.optionsPanel.pngOptionGroup = w.tabGroup.singleTab.optionsPanel.add('group');
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.orientation = 'column';
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.visibleOnly = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.add("checkbox", [50, 0, 300, 20], "Visible Layer/Group Only");
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.visibleOnly.value = exportOptions.visibleOnly;
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.pngTransparency = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.add("checkbox", [50, 0, 300, 20], "Transparency");
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.pngTransparency.value = exportOptions.pngTransparency;
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.pngInterlaced = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.add("checkbox", [50, 0, 300, 20], "Interlaced");
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.pngInterlaced.value = exportOptions.pngInterlaced;
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.pngTrim = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.add("checkbox", [50, 0, 300, 20], "Trim");
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.pngTrim.value = exportOptions.pngTrim;
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.png8 = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.add("checkbox", [50, 0, 300, 20], "PNG-8");
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.png8.value = exportOptions.png8;
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.icc = w.tabGroup.singleTab.optionsPanel.pngOptionGroup.add("checkbox", [50, 0, 300, 20], "Embed Color Profile");
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.icc.value = exportOptions.icc;
w.tabGroup.singleTab.optionsPanel.pngOptionGroup.visible = (exportOptions.fileType == pngIndex);
// JPEG Options
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup = w.tabGroup.singleTab.optionsPanel.add('group');
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.orientation = 'column';
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.visibleOnly = w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.add("checkbox", [50, 0, 300, 20], "Visible Layer/Group Only");
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.visibleOnly.value = exportOptions.visibleOnly;
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.icc = w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.add("checkbox", [50, 0, 300, 20], "Embed Color Profile");
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.icc.value = exportOptions.icc;
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup = w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.add('group');
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup.jpegQualityLabel = w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup.add("statictext", [0, 0, 70, 20], 'Image Quality:');
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup.jpegQuality = w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup.add('slider', undefined, exportOptions.jpegQuality, 0, 12);
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup.jpegQuality.preferredSize = [300, -1];
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup.jpegQualityText = w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup.add("statictext", [0, 0, 20, 20], exportOptions.jpegQuality.toString());
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup.jpegQuality.onChanging = function () {
this.value = Math.round(this.value);
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.jpegQualityGroup.jpegQualityText.text = this.value;
}
w.tabGroup.singleTab.optionsPanel.jpegOptionGroup.visible = (exportOptions.fileType == jpegIndex);
w.tabGroup.singleTab.exportTypePanel.exportTypeGroup.ddFileType.items[exportOptions.fileType].selected = true;
}
///////////////////////////////////////////////////////////////////////////////
// Function: hideAllFileTypePanel
// Usage: hide all the panels in the common actions
// Input: window is the dialog for this script
// Return: <none>, all panels are now hidden
///////////////////////////////////////////////////////////////////////////////
function hideAllFileTypePanel(window) {
window.tabGroup.singleTab.optionsPanel.jpegOptionGroup.hide();
window.tabGroup.singleTab.optionsPanel.pngOptionGroup.hide();
}
///////////////////////////////////////////////////////////////////////////////
// Function: initExportOptions
// Usage: create our default parameters
// Input: a new Object
// Return: a new object with params set to default
///////////////////////////////////////////////////////////////////////////////
function initExportOptions(exportOptions) {
exportOptions.destination = new String("");
exportOptions.fileNamePrefix = new String("untitled_");
exportOptions.exportType = 0;
exportOptions.visibleOnly = true;
exportOptions.fileType = pngIndex;
exportOptions.icc = true;
exportOptions.pngTransparency = true;
exportOptions.pngInterlaced = false;
exportOptions.pngTrim = false;
exportOptions.png8 = false;
exportOptions.jpegQuality = 8;
try {
exportOptions.destination = Folder(app.activeDocument.fullName.parent).fsName; // destination folder
var tmp = app.activeDocument.fullName.name;
exportOptions.fileNamePrefix = decodeURI(tmp.substring(0, tmp.indexOf("."))); // filename body part
} catch (e) {
exportOptions.destination = new String("");
exportOptions.fileNamePrefix = app.activeDocument.name; // filename body part
}
}
///////////////////////////////////////////////////////////////////////////////
// Function: objectToDescriptor
// Usage: create an ActionDescriptor from a JavaScript Object
// Input: JavaScript Object (o)
// object unique string (s)
// Return: ActionDescriptor
// NOTE: Only boolean, string, number and UnitValue are supported.
// REUSE: This routine is used in other scripts. Please update those if you
// modify. I am not using include or eval statements as I want these
// scripts self contained.
///////////////////////////////////////////////////////////////////////////////
function objectToDescriptor(o, s) {
var d = new ActionDescriptor;
var l = o.reflect.properties.length;
d.putString(app.charIDToTypeID('Msge'), s);
for (var i = 0; i < l; i++) {
var k = o.reflect.properties[i].toString();
if (k == "__proto__" || k == "__count__" || k == "__class__" || k == "reflect")
continue;
var v = o[k];
k = app.stringIDToTypeID(k);
switch (typeof (v)) {