Created
April 19, 2015 04:40
-
-
Save hadidjah/c971d5c8b85a2d5769bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Only run in Photoshop | |
#target photoshop | |
//warn if no documents are open | |
if (documents.length == 0) | |
{ | |
alert("No documents are open! Please open the file you want to make a flipbook out of and run again!", "No File Found"); | |
} | |
//Pull some intial variables | |
var frameWidth = app.activeDocument.width; | |
var frameHeight = app.activeDocument.height; | |
var cellW = app.activeDocument.width; | |
var cellH = app.activeDocument.height; | |
var layerCount = activeDocument.layers.length; | |
//Check if canvas size is power of two | |
if ((cellW != 4 && cellW != 8 && cellW != 16 && cellW != 32 && cellW != 64 && cellW != 128 && cellW != 256 && cellW != 512 && cellW != 1024 && cellW != 2048) || (cellH != 4 && cellH != 8 && cellH != 16 && cellH != 32 && cellH != 64 && cellH != 128 && cellH != 256 && cellH != 512 && cellH != 1024 && cellH != 2048)) | |
{ | |
var badFrameSize = true; | |
} else { | |
var badFrameSize = false; | |
} | |
var suggestedHeight = 8192; | |
var suggestedWidth = 8192; | |
//Image resize function in case the dimensions are crap | |
function SizeEstimate() | |
{ | |
while (frameWidth != suggestedWidth && suggestedWidth > 15) | |
{ | |
if (frameWidth > (suggestedWidth - (suggestedWidth * 0.1))) //Allows 10% wiggle room on resize | |
{ | |
frameWidth = suggestedWidth; | |
} else { | |
suggestedWidth = suggestedWidth * 0.5; | |
} | |
if ((frameWidth != suggestedWidth) && (suggestedWidth < 16)) | |
{ | |
frameWidth = 16; | |
} | |
} | |
while (frameHeight != suggestedHeight && suggestedHeight > 15) | |
{ | |
if (frameHeight > (suggestedHeight - (suggestedHeight * 0.1))) //Allows 10% wiggle room on resize | |
{ | |
frameHeight = suggestedHeight; | |
} else { | |
suggestedHeight = suggestedHeight * 0.5; | |
} | |
if ((frameHeight != suggestedHeight) && (suggestedHeight < 16)) | |
{ | |
frameHeight = 16; | |
} | |
} | |
} | |
if (badFrameSize) | |
{ | |
SizeEstimate(); | |
} | |
//Check if framecount is power of two | |
var badLayerCount = false; | |
if (layerCount != 2 && layerCount != 4 && layerCount != 8 && layerCount != 16 && layerCount != 32 && layerCount != 64 && layerCount != 128) | |
{ | |
badLayerCount = true; | |
} | |
//Quick and dirty suggestions for number of rows and columns | |
var suggestedRows = 4; | |
var suggestedColumns = 4; | |
function GridEstimate () | |
{ | |
if (layerCount == 1) | |
{ | |
suggestedRows = 1; | |
suggestedColumns = 1; | |
} | |
else if (layerCount == 2) | |
{ | |
suggestedRows = 1; | |
suggestedColumns = 2; | |
} | |
else if (layerCount == 4) | |
{ | |
suggestedRows = 2; | |
suggestedColumns = 2; | |
} | |
else if (layerCount == 8) | |
{ | |
suggestedRows = 2; | |
suggestedColumns = 4; | |
} | |
else if (layerCount == 16) | |
{ | |
suggestedRows = 4; | |
suggestedColumns = 4; | |
} | |
else if (layerCount == 32) | |
{ | |
suggestedRows = 4; | |
suggestedColumns = 8; | |
} | |
else if (layerCount == 64) | |
{ | |
suggestedRows = 8; | |
suggestedColumns = 8; | |
} | |
else if (layerCount == 128) | |
{ | |
suggestedRows = 16; | |
suggestedColumns = 8; | |
} | |
else if (layerCount == 256) | |
{ | |
suggestedRows = 16; | |
suggestedColumns = 16; | |
} | |
//Heathens | |
else if (layerCount == 12) | |
{ | |
suggestedRows = 2; | |
suggestedColumns = 6; | |
} | |
else if (layerCount == 24) | |
{ | |
suggestedRows = 4; | |
suggestedColumns = 6; | |
} | |
else if (layerCount == 36) | |
{ | |
suggestedRows = 6; | |
suggestedColumns = 6; | |
} | |
else if (layerCount == 48) | |
{ | |
suggestedRows = 6; | |
suggestedColumns = 8; | |
} | |
else if (layerCount == 9) | |
{ | |
suggestedRows = 3; | |
suggestedColumns = 3; | |
} | |
else if (layerCount == 25) | |
{ | |
suggestedRows = 5; | |
suggestedColumns = 5; | |
} | |
//These will throw a warning anyways, but it seems polite to guess along | |
else if (layerCount < 4) | |
{ | |
suggestedRows = 2; | |
suggestedColumns = 2; | |
} | |
else if (layerCount < 8) | |
{ | |
suggestedRows = 2; | |
suggestedColumns = 4; | |
} | |
else if (layerCount < 16) | |
{ | |
suggestedRows = 4; | |
suggestedColumns = 4; | |
} | |
else if (layerCount < 32) | |
{ | |
suggestedRows = 4; | |
suggestedColumns = 8; | |
} | |
else if (layerCount < 64) | |
{ | |
suggestedRows = 8; | |
suggestedColumns = 8; | |
} | |
else if (layerCount < 128) | |
{ | |
suggestedRows = 16; | |
suggestedColumns = 8; | |
} | |
else if (layerCount < 256) | |
{ | |
suggestedRows = 16; | |
suggestedColumns = 16; | |
} | |
else if (layerCount > 256) | |
{ | |
suggestedRows = 16; | |
suggestedColumns = 16; | |
} | |
} | |
GridEstimate(); | |
//BUILD THE WINDOW | |
var dlg = new Window('dialog', 'Flipbooker',undefined); | |
dlg.preferredSize = [350, 130]; | |
dlg.orientation = 'column'; | |
//Build the GRID WARNING if the number of frames is not a power of two | |
if (badLayerCount) | |
{ | |
dlg.gridWarningPanel = dlg.add('panel', undefined, 'WARNING'); | |
dlg.gridWarningPanel.preferredSize = [300, 50]; | |
dlg.gridWarningPanel.bodyText = dlg.gridWarningPanel.add('statictext', undefined, "The number of frames in your flipbook is not a power of two. This can negatively impact mipping and performance.", {multiline: true}); | |
dlg.gridWarningPanel.bodyText2 = dlg.gridWarningPanel.add('statictext', undefined, "Consider adjusting your animation to use a number of frames such as 4, 8, 16, 32, etc.", {multiline: true}); | |
} | |
//Build the GRID PANEL to set rows and columns | |
dlg.gridPanel = dlg.add('panel', undefined, 'Grid Size:'); | |
dlg.gridPanel.preferredSize = [300, 50]; | |
dlg.gridPanel.orientation = 'row'; | |
//rows | |
dlg.gridPanel.rowCountName = dlg.gridPanel.add('statictext', undefined, 'Rows: ', {multiline: false}); | |
dlg.gridPanel.rowCount = dlg.gridPanel.add('edittext', undefined, suggestedRows, {multiline: false}); | |
dlg.gridPanel.rowCount.characters = 4; | |
//columns | |
dlg.gridPanel.columnCountName = dlg.gridPanel.add('statictext', undefined, 'Columns: ', {multiline: false}); | |
dlg.gridPanel.columnCount = dlg.gridPanel.add('edittext', undefined, suggestedColumns, {multiline: false}); | |
dlg.gridPanel.columnCount.characters = 4; | |
//Build the FRAME WARNING if the size of the frames is not a power of two | |
if (badFrameSize) | |
{ | |
dlg.frameWarningPanel = dlg.add('panel', undefined, 'WARNING'); | |
dlg.frameWarningPanel.preferredSize = [300, 50]; | |
dlg.frameWarningPanel.bodyText = dlg.frameWarningPanel.add('statictext', undefined, "Your frame dimensions are not a power of two. This can negatively impact mipping and performance.", {multiline: true}); | |
dlg.frameWarningPanel.bodyText = dlg.frameWarningPanel.add('statictext', undefined, "Consider resizing your frame dimensions to powers of two, such as 64, 128, 256, etc.", {multiline: true}); | |
dlg.frameWarningPanel.checkResize = dlg.frameWarningPanel.add('checkbox', undefined, "Resize frames?"); | |
dlg.frameWarningPanel.checkResize.value = true; | |
//Build the FRAME WINDOW if things should be resized | |
dlg.framePanel = dlg.add('panel', undefined, 'Frame Size:'); | |
dlg.framePanel.preferredSize = [300, 50]; | |
dlg.framePanel.orientation = 'row'; | |
//height | |
dlg.framePanel.frameHeightName = dlg.framePanel.add('statictext', undefined, 'Height: ', {multiline: false}); | |
dlg.framePanel.frameHeight = dlg.framePanel.add('edittext', undefined, suggestedHeight, {multiline: false}); | |
dlg.framePanel.frameHeight.characters = 4; | |
//width | |
dlg.framePanel.frameWidthName = dlg.framePanel.add('statictext', undefined, 'Width: ', {multiline: false}); | |
dlg.framePanel.frameWidth = dlg.framePanel.add('edittext', undefined, suggestedWidth, {multiline: false}); | |
dlg.framePanel.frameWidth.characters = 4; | |
} | |
//Build the BUTTON PANEL for running things | |
dlg.buttonPanel = dlg.add('group', undefined); | |
dlg.buttonPanel.orientation = 'row'; | |
//run script button | |
dlg.buttonPanel.applyButton = dlg.buttonPanel.add('button', undefined, 'Build Me a Flipbook!', {name:'ok'}); | |
dlg.buttonPanel.applyButton.preferredSize = [200, 24]; | |
//cancel button | |
dlg.buttonPanel.cancelButton = dlg.buttonPanel.add('button', undefined, 'Exit Script', {name:'cancel'}); | |
dlg.buttonPanel.cancelButton.preferredSize = [120, 24]; | |
//resizers only if frames are a funky size | |
if (badFrameSize) | |
{ | |
var frameWidth = dlg.framePanel.frameWidth.text; | |
var frameHeight = dlg.framePanel.frameHeight.text; | |
} | |
//----SET SOME VURRIBLES----- | |
var rowCount = dlg.gridPanel.rowCount.text; | |
var columnCount = dlg.gridPanel.columnCount.text; | |
var currentLayer = app.activeDocument.layers[layerCount-1]; | |
var currentLayerPlaceholder= layerCount-1; | |
var layersMoved = 0; | |
var currentRow = 0; | |
var currentColumn = 0; | |
dlg.buttonPanel.cancelButton = function() { | |
return dlg.close(); | |
}; | |
dlg.buttonPanel.applyButton.onClick = function() | |
{ | |
//RESIZE IMAGE if requested and frames are a funky size | |
if (badFrameSize) | |
{ | |
if (dlg.frameWarningPanel.checkResize.value == true) | |
{ | |
app.activeDocument.resizeImage (UnitValue(frameWidth,'px'), UnitValue(frameHeight,'px')); | |
cellW = frameWidth; | |
cellH = frameHeight; | |
} | |
} | |
//Resize canvas to fit all the tiles | |
app.activeDocument.resizeCanvas((cellW*columnCount), (cellH*rowCount), AnchorPosition.TOPLEFT); | |
//ARRANGE SOME FLIPBOOKS | |
while (layersMoved < layerCount-1) | |
{ | |
//Move frame layer | |
while ((currentRow < rowCount) && (layersMoved < layerCount)) | |
{ | |
currentLayer.translate((cellW*currentColumn), (cellH*currentRow)); | |
currentColumn += 1; | |
currentLayerPlaceholder -= 1; | |
if (currentLayerPlaceholder < 0) | |
{ | |
currentLayerPlaceholder = 0; | |
} | |
currentLayer = app.activeDocument.layers[currentLayerPlaceholder]; | |
layersMoved++; | |
//Update current row and column as needed | |
if (currentColumn == columnCount) | |
{ | |
currentRow += 1; | |
currentColumn = 0; | |
} | |
} | |
} | |
dlg.close(); | |
} | |
//Moooom, are we building yet? | |
dlg.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment