forked from advename/Photoshop-Sprite-Sheet-Creator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite-Sheet-Creator-v2.jsx
188 lines (145 loc) · 7 KB
/
Sprite-Sheet-Creator-v2.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
/*
The MIT License (MIT)
Updateded Version : Copyright (c) 2017 Lars (https://www.advena.me)
Initial Version : Copyright (c) 2014 William Malone
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
<javascriptresource>
<name>Create Sprite Sheet from Layers v2</name>
</javascriptresource>
*/
// Duplicate current Document (Updated version 2017 Lars)
var aDoc = app.activeDocument;
aDoc.duplicate();
//Rest of script is William Malone's HTML5 Photoshop Sprite Sheet Creator
//https://github.com/williammalone/HTML5-Photoshop-Sprite-Sheet-Creator
var i, prefRulerUnits, prefTypeUnits, frames, frameWidth, frameHeight, mp, dialog, columns, rows,
exit = function () {
dialog.close();
},
onRowsChanged = function () {
rows = Number(dialog.msgPnl.rowsGrp.rows.text);
columns = Math.ceil(frames / rows);
dialog.msgPnl.columns.text = 'Columns: ' + columns;
},
createSprite = function () {
var i, columnIndex, rowIndex;
dialog.hide();
app.activeDocument.resizeCanvas(columns * frameWidth, rows * frameHeight, AnchorPosition.TOPLEFT);
columnIndex = 0;
rowIndex = 0;
for (i = 0; i < frames; i += 1) {
if (app.activeDocument.layers[i].visible) {
if (dialog.msgPnl.topDownCheckbox.value) {
activeDocument.layers[i].translate(frameWidth * columnIndex, frameHeight * rowIndex);
} else {
activeDocument.layers[i].translate((frames - 1) * frameWidth - frameWidth * columnIndex, frameHeight * rowIndex);
}
columnIndex += 1;
if (columnIndex >= columns) {
rowIndex += 1;
columnIndex = 0;
}
}
}
app.activeDocument.mergeVisibleLayers();
app.activeDocument.layers[0].name = "Sprite Sheet with " + frames + " Frames";
};
if (app && app.preferences) {
// Save the current preferences
prefRulerUnits = app.preferences.rulerUnits,
prefTypeUnits = app.preferences.typeUnits,
// Change the current preferences to pixel units
app.preferences.typeUnits = TypeUnits.PIXELS;
app.preferences.rulerUnits = Units.PIXELS;
}
try {
if (app && app.activeDocument) {
// Determine number of visible layers
frames = 0;
for (i = 0; i < app.activeDocument.layers.length; i += 1) {
if (app.activeDocument.layers[i].visible) {
frames += 1;
}
}
if (frames <= 1) {
alert("Error: Sprite sheet requires more than one visible layer");
} else {
// Create dialog
dialog = new Window('dialog', 'Sprite Sheet Creator');
dialog.msgPnl = dialog.add('panel', undefined, undefined);
dialog.msgPnl.frames = dialog.msgPnl.add('StaticText', undefined, 'Number of Frames:');
dialog.msgPnl.frames.alignment = 'left';
dialog.msgPnl.frames.text = "Frames: " + frames;
columns = frames;
dialog.msgPnl.columns = dialog.msgPnl.add('StaticText', undefined, 'Columns: ' + columns);
dialog.msgPnl.columns.alignment = 'left';
dialog.msgPnl.rowsGrp = dialog.msgPnl.add('group', undefined, undefined, {
orientation: 'column',
alignChildren: 'left'
});
dialog.msgPnl.rowsGrp.alignment = 'left';
dialog.msgPnl.rowsGrp.rowsLabel = dialog.msgPnl.rowsGrp.add('StaticText', undefined, 'Rows: ');
rows = 1;
dialog.msgPnl.rowsGrp.rows = dialog.msgPnl.rowsGrp.add('EditText', undefined, rows);
dialog.msgPnl.rowsGrp.rows.characters = 3;
dialog.msgPnl.rowsGrp.rows.enabled = false;
dialog.msgPnl.rowsGrp.rows.addEventListener('changing', onRowsChanged);
dialog.msgPnl.topDownCheckbox = dialog.msgPnl.add('checkbox', undefined, 'First Frame is Top Layer');
dialog.msgPnl.topDownCheckbox.value = true;
dialog.msgPnl.topDownCheckbox.enabled = false;
dialog.message = dialog.add('StaticText', undefined, 'Initializing...', {
multiline: true
});
dialog.message.text = "";
dialog.btnGrp = dialog.add('group');
dialog.btnGrp.cancelButton = dialog.btnGrp.add('button', undefined, 'Cancel');
dialog.btnGrp.cancelButton.onClick = exit;
dialog.btnGrp.cancelButton.alignment = ['left', 'bottom'];
dialog.btnGrp.createButton = dialog.btnGrp.add('button', undefined, 'Ok');
dialog.btnGrp.createButton.onClick = createSprite;
dialog.btnGrp.cancelButton.alignment = ['right', 'bottom'];
dialog.btnGrp.createButton.enabled = false;
// Determine the frame width based on the active document's dimensions
frameWidth = app.activeDocument.width;
frameHeight = app.activeDocument.height;
// Determine the MP to see if the image is too large to load on iOS devices
mp = frameHeight * frameWidth / 1024 / 1024;
if (mp * frames > 5) {
dialog.message.text = "Warning: The sprite sheet is too large to be loaded on iOS devices (see http://wmalone.com/maximage for details)";
} else if (mp * frames > 3) {
dialog.message.text = "Warning: The sprite sheet is too large to be loaded on legacy iOS devices (see http://wmalone.com/maximage for details)";
} else {
//dialog.msgPnl.message.text = "Ready to create sprite sheet...";
}
dialog.btnGrp.createButton.enabled = true;
dialog.msgPnl.topDownCheckbox.enabled = true;
dialog.msgPnl.rowsGrp.rows.enabled = true;
dialog.show();
}
} else {
alert("Error: No document open");
}
} catch (ex) {
alert("Error: No document open");
}
if (app && app.preferences) {
// Revert to the previous preferences
app.preferences.typeUnits = prefTypeUnits;
app.preferences.rulerUnits = prefRulerUnits;
}