Permalink
Browse files

adding ability to add margins between sprites and use different layouts.

  • Loading branch information...
gmurphey committed Aug 7, 2011
1 parent bd43546 commit 52b0353f5e41f0b863937217504113052c23dce7
Showing with 100 additions and 24 deletions.
  1. +100 −24 simple-sprites.jsx
View
@@ -1,7 +1,13 @@
-var settings, CONSTANTS, defaults, spriteDocument, inputFolder, inputDocuments, currentDocument;
+var settings, CONSTANTS, defaults, dialogLayout, dlg, margin, spriteDocument, inputFolder, inputDocuments, currentDocument, bounds;
settings = {
folderDialog: "Select a folder to process",
+ optionsDialog: {
+ title: "Simple Sprites Options",
+ errors: {
+ marginNaN: "Margin is not a number."
+ }
+ },
fileFilter: "*",
document: {
resolution: 72,
@@ -30,6 +36,21 @@ defaults = {
displayDialogs: app.displayDialogs
};
+dialogLayout = "dialog { " +
+ "marginGroup: Group { orientation: 'row', alignChildren: 'right', " +
+ "marginLabel: StaticText { text: 'Margin:' }, " +
+ "marginText: EditText { text: '0', characters: 5 } " +
+ "}, " +
+ "orientationPanel: Panel { orientation: 'row', alignChildren: 'left', text: 'Sprite Orientation', " +
+ "horizontalRadio: RadioButton { text: 'Horizontal', value: true }, " +
+ "verticalRadio: RadioButton { text: 'Vertical', value: false }" +
+ "}, " +
+ "buttonGroup: Group { orientation: 'row', alignChildren: 'right', " +
+ "okButton: Button { text: 'Ok', properties: { name: 'ok' } }, " +
+ "cancelButton: Button { text: 'Cancel', properties: { name: 'cancel'} }" +
+ "}" +
+"}";
+
// save current preferences for later
app.preferences.rulerUnits = settings.preferences.rulerUnits;
app.preferences.typeUnits = TypeUnits.PIXELS;
@@ -41,35 +62,90 @@ if (inputFolder === null) {
throw CONSTANTS.EXCEPTIONS.NO_INPUT;
}
-inputDocuments = inputFolder.getFiles(settings.fileFilter);
+dlg = new Window(dialogLayout, settings.optionsDialog.title);
-for (var i = 0; i < inputDocuments.length; i++) {
- if ((inputDocuments[i] instanceof File) && (!inputDocuments[i].hidden)) {
- currentDocument = open(inputDocuments[i]);
+dlg.buttonGroup.okButton.onClick = function () {
+ if (isNaN(this.window.marginGroup.marginText.text)) {
+ alert(settings.optionsDialog.errors.marginNaN);
+ return false;
+ } else {
+ dlg.close(1);
+ }
+}
- if (currentDocument === null) {
- throw CONSTANTS.EXCEPTIONS.BAD_DOC;
- }
+dlg.center();
- try {
- currentDocument.mergeVisibleLayers();
- } catch (e) {}
+if (dlg.show() === 1) {
+ margin = parseInt(dlg.marginGroup.marginText.text);
+ orientation = (dlg.orientationPanel.horizontalRadio.value) ? 'horizontal' : 'vertical';
+
+ inputDocuments = inputFolder.getFiles(settings.fileFilter);
+
+ for (var i = 0; i < inputDocuments.length; i++) {
+ if ((inputDocuments[i] instanceof File) && (!inputDocuments[i].hidden)) {
+ currentDocument = open(inputDocuments[i]);
+
+ if (currentDocument === null) {
+ throw CONSTANTS.EXCEPTIONS.BAD_DOC;
+ }
+
+ try {
+ currentDocument.mergeVisibleLayers();
+ } catch (e) {}
- currentDocument.selection.selectAll();
- currentDocument.selection.copy();
-
- // if sprite document doesn't exist, create it. If already existing, resize canvas to accomadate current document
- if (typeof(spriteDocument) != 'undefined') {
- app.activeDocument = spriteDocument;
- spriteDocument.resizeCanvas((spriteDocument.width + currentDocument.width), ((spriteDocument.height >= currentDocument.height) ? spriteDocument.height : currentDocument.height), AnchorPosition.TOPLEFT);
- } else {
- spriteDocument = app.documents.add(currentDocument.width, currentDocument.height, settings.document.resolution, settings.document.name, settings.document.mode, settings.document.initialFill, settings.document.pixelAspectRatio);
- }
+ currentDocument.selection.selectAll();
+ currentDocument.selection.copy();
+
+ // if sprite document doesn't exist, create it. If already existing, resize canvas to accomadate current document
+ if (typeof(spriteDocument) != 'undefined') {
+ app.activeDocument = spriteDocument;
+
+ if (orientation === 'vertical') {
+ spriteDocument.resizeCanvas(
+ ((spriteDocument.width >= currentDocument.width) ? spriteDocument.width : currentDocument.width),
+ (spriteDocument.height + currentDocument.height + margin),
+ AnchorPosition.TOPLEFT
+ );
+ } else {
+ spriteDocument.resizeCanvas(
+ (spriteDocument.width + currentDocument.width + margin),
+ ((spriteDocument.height >= currentDocument.height) ? spriteDocument.height : currentDocument.height),
+ AnchorPosition.TOPLEFT
+ );
+ }
+ } else {
+ spriteDocument = app.documents.add(
+ currentDocument.width,
+ currentDocument.height,
+ settings.document.resolution,
+ settings.document.name,
+ settings.document.mode,
+ settings.document.initialFill,
+ settings.document.pixelAspectRatio
+ );
+ }
- spriteDocument.selection.select([[(spriteDocument.width - currentDocument.width), 0], [spriteDocument.width, 0], [spriteDocument.width, currentDocument.height], [(spriteDocument.width - currentDocument.width), currentDocument.height]]);
- spriteDocument.paste(true);
+ if (orientation === 'vertical') {
+ bounds = [
+ [0, (spriteDocument.height - currentDocument.height)],
+ [spriteDocument.width, (spriteDocument.height - currentDocument.height)],
+ [spriteDocument.width, spriteDocument.height],
+ [0, spriteDocument.height]
+ ];
+ } else {
+ bounds = [
+ [(spriteDocument.width - currentDocument.width), 0],
+ [spriteDocument.width, 0],
+ [spriteDocument.width, currentDocument.height],
+ [(spriteDocument.width - currentDocument.width), currentDocument.height]
+ ];
+ }
+
+ spriteDocument.selection.select(bounds);
+ spriteDocument.paste(true);
- currentDocument.close(SaveOptions.DONOTSAVECHANGES);
+ currentDocument.close(SaveOptions.DONOTSAVECHANGES);
+ }
}
}

0 comments on commit 52b0353

Please sign in to comment.