﻿/*
	1.0		Release
	1.01 	Fixed mask bug when scaling fractions
	1.02	Fixed mask bug that clipped path strokes. Changed from bounds to strokeBounds.
	2.0      Rewrote the script in jsx for the newer versions of Illustrator.
*/

var sversion = '2.0',
	scriptName = 'dViz: Stacks' + ' ' + sversion;

var doc;

var val = 1,
    r = 1,
    c = 1,
    v_gutter = 5,
    h_gutter = 5,
    fraction_style = 'Top/Down' ,
    draw_mode = 'Rows';

var styleList = ['Scale', 'Top/Down', 'Bottom/Up', 'Left/Right', 'Right/Left', 'Round Up', 'Round Down'],
    drawList = ['Rows', 'Columns'];

if (version.split(".")[0] >= 15) {
    if (app.documents.length > 0) {
        if (app.activeDocument != null) {
            doc = app.activeDocument;

            var dlg = new Window('dialog', scriptName); 
            
            dlg.rowcolInputGroup = dlg.add('group', undefined, 'Rows and columns');        
            (dlg.rowcolInputGroup.rInput = dlg.rowcolInputGroup.add('edittext', [100,15,160,35], r));
            dlg.rowcolInputGroup.add('statictext', undefined, 'Rows');
            (dlg.rowcolInputGroup.cInput = dlg.rowcolInputGroup.add('edittext', [100,15,160,35], c));
            dlg.rowcolInputGroup.add('statictext', undefined, 'Columns');
            (dlg.rowcolInputGroup.valueInput = dlg.rowcolInputGroup.add('edittext', [100,15,160,35], val));
            (dlg.rowcolInputGroup.percent = dlg.rowcolInputGroup.add('statictext', undefined, 'Value'));
            dlg.rowcolInputGroup.valueInput.onChange = function() {
                val = parseFloat(dlg.rowcolInputGroup.valueInput.text.replace(/[^\d.-]/g, ''));
                if (val > r*c) {
                    val = r*c;
                    dlg.rowcolInputGroup.valueInput.text = val;
                }
            }
            dlg.rowcolInputGroup.orientation = 'row';
            
            dlg.rowcolInputGroup.rInput.onChange = function() {
                r = parseInt(dlg.rowcolInputGroup.rInput.text.replace(/[^\d.-]/g, ''));
                dlg.rowcolInputGroup.rInput.text = r;
            }
        
            dlg.rowcolInputGroup.cInput.onChange = function() {
                c = parseInt(dlg.rowcolInputGroup.cInput.text.replace(/[^\d.-]/g, ''));
                dlg.rowcolInputGroup.cInput.text = c;
            }
            
            
            dlg.guttersInputGroup = dlg.add('group', undefined, 'Gutters');
            (dlg.guttersInputGroup.vInput = dlg.guttersInputGroup.add('edittext', [100,15,160,35], v_gutter));
            (dlg.guttersInputGroup.percent = dlg.guttersInputGroup.add('statictext', undefined, 'Vertical Gutter'));
            (dlg.guttersInputGroup.hInput = dlg.guttersInputGroup.add('edittext', [100,15,160,35], h_gutter));
            (dlg.guttersInputGroup.percent = dlg.guttersInputGroup.add('statictext', undefined, 'Horizontal Gutter'));
            dlg.guttersInputGroup.orientation = 'row';
            
            dlg.styleInputGroup = dlg.add('group', undefined, 'Fraction style');
            (dlg.styleInputGroup.percent = dlg.styleInputGroup.add('statictext', undefined, 'Fraction style:'));
            (dlg.styleInputGroup.styleList = dlg.styleInputGroup.add('dropdownlist', undefined, styleList));
            dlg.styleInputGroup.styleList.selection = 0;
            dlg.styleInputGroup.orientation = 'row';
            
            dlg.drawInputGroup = dlg.add('group', undefined, 'Draw mode');
            (dlg.drawInputGroup.percent = dlg.drawInputGroup.add('statictext', undefined, 'Draw mode:'));
            (dlg.drawInputGroup.drawList = dlg.drawInputGroup.add('dropdownlist', undefined, drawList));
            dlg.drawInputGroup.drawList.selection = 0;
            dlg.drawInputGroup.orientation = 'row';
            
            dlg.buttonGroup = dlg.add('group', undefined, 'Button Group');
            dlg.buttonGroup.cancelBtn = dlg.buttonGroup.add('button', undefined, 'Cancel', {name:'cancel'});
            dlg.buttonGroup.cancelBtn.onClick = function() {dlg.close();};
            dlg.buttonGroup.okBtn = dlg.buttonGroup.add('button', undefined, 'OK', {name:'ok'});
            dlg.buttonGroup.okBtn.onClick = function() {
                var selectedObjects = doc.selection;
                
                if (selectedObjects.length != 0) {
                
                    var selectionGroup;
                    // If the selection is not a group we make it a group
                    if (selectedObjects == 1) {
                        selectionGroup = selectedObjects[0];
                    } else {
                        // Create the group thing
                        selectionGroup = app.activeDocument.groupItems.add();
                        for (var s = 0; s < selectedObjects.length; s++ ) { 
                            var tmpItem = selectedObjects[s].duplicate();
                            tmpItem.moveToBeginning(selectionGroup);
                        }
                    }
                
                    for (var s = 0; s < selectedObjects.length; s++ ) {
                        selectedObjects[s].remove();
                    }
                      
                    storeValues();                    
                    if (val != 0) {
                        val--;
                        for (var y = 0; y < r; y++) {
                        for (var x = 0; x < c; x++) {
                                if (x==0 && y==0) continue;
                                if (val) {
                                    var newItem = selectionGroup.duplicate(doc);
                                    if (draw_mode == 'Rows') {
                                        newItem.left = selectionGroup.left + x * (selectionGroup.width + h_gutter);
                                        newItem.top = selectionGroup.top - y * (selectionGroup.height + v_gutter);  
                                    } else if (draw_mode == 'Columns') {
                                        newItem.left = selectionGroup.left + y * (selectionGroup.width + h_gutter);
                                        newItem.top = selectionGroup.top - x * (selectionGroup.height + v_gutter);  
                                    }
                                    
                                    if (val < 1) {
                                        if (fraction_style == "Scale") {
                                            newItem.resize(val*100, val*100);
                                        } else if (fraction_style == "Top/Down") {
                                            newItem.pathItems.rectangle(newItem.top - (newItem.height - newItem.height*val), newItem.left, newItem.width, newItem.height*val);
                                            newItem.clipped = true;
                                        } else if (fraction_style == "Bottom/Up") {
                                            newItem.pathItems.rectangle(newItem.top, newItem.left, newItem.width, newItem.height*val);
                                            newItem.clipped = true;
                                        } else if (fraction_style == "Left/Right") {
                                            newItem.pathItems.rectangle(newItem.top, newItem.left, newItem.width*val, newItem.height);
                                            newItem.clipped = true;
                                        } else if (fraction_style == "Right/Left") {
                                            newItem.pathItems.rectangle(newItem.top, newItem.left + (newItem.width - newItem.width*val), newItem.width*val, newItem.height);
                                            newItem.clipped = true;
                                        } else if (fraction_style == "Round Down") {
                                            newItem.remove();
                                        }
                                        val-=val;
                                    } else val--; 
                                }
                            }
                        }
                    } else { Window.alert("The value must not be 0."); }
                    dlg.close();
                } else { Window.alert("The selection is empty."); }
            }
            dlg.buttonGroup.orientation = 'row';
            dlg.show();
        } else { Window.alert("You must have an active document to run this script."); }
    } else { Window.alert("You must open at least one document to run this script."); }
} else { Window.alert("Your version of Adobe illustrator is too old to run this script."); }

function storeValues() {
    val = parseFloat(dlg.rowcolInputGroup.valueInput.text.replace(/[^\d.-]/g, ''));
    r = parseInt(dlg.rowcolInputGroup.rInput.text.replace(/[^\d.-]/g, ''));
    c = parseInt(dlg.rowcolInputGroup.cInput.text.replace(/[^\d.-]/g, ''));
    v_gutter = parseInt(dlg.guttersInputGroup.vInput.text.replace(/[^\d.-]/g, ''));
    h_gutter = parseInt(dlg.guttersInputGroup.hInput.text.replace(/[^\d.-]/g, ''));
    fraction_style = dlg.drawInputGroup.drawList.text;
    draw_mode = new String(dlg.drawInputGroup.drawList.selection);
    fraction_style = new String(dlg.styleInputGroup.styleList.selection);
}
