
//______________________________________________________________________
/*

      ___            __                    
    /'___\          /\ \
   /\ \__/   __     \_\ \     __    ___  
   \ \ ,__\/'__`\   /'_` \  /'__`\/' _ `\
    \ \ \_/\ \L\.\_/\ \L\ \/\  __//\ \/\ \
     \ \_\\ \__/.\_\ \___,_\ \____\ \_\ \_\
      \/_/ \/__/\/_/\/__,_ /\/____/\/_/\/_/


    ____                               __ 
   /\  _`\                            /\ \  __                  
   \ \ \L\_\  _ __    __     __  __   \_\ \/\_\
    \ \ \L_L /\`'__\/'__`\  /\ \/\ \  /'_` \/\ \
     \ \ \/, \ \ \//\ \L\.\_\ \ \_\ \/\ \L\ \ \ \
      \ \____/\ \_\\ \__/.\_\\/`____ \ \___,_\ \_\
       \/___/  \/_/ \/__/\/_/ `/___/> \/__,_ /\/_/
                                 /\___/                         
                                 \/__/                          
    ____                   ___ 
   /\  _`\                /\_ \                   __
   \ \ \L\_\  __  _  _____\//\ \     ___     ____/\_\    ___     ___    
    \ \  _\L /\ \/'\/\ '__`\\ \ \   / __`\  /',__\/\ \  / __`\ /' _ `\
     \ \ \L\ \/>  </\ \ \L\ \\_\ \_/\ \L\ \/\__, `\ \ \/\ \L\ \/\ \/\ \
      \ \____//\_/\_\\ \ ,__//\____\ \____/\/\____/\ \_\ \____/\ \_\ \_\
       \/___/ \//\/_/ \ \ \/ \/____/\/___/  \/___/  \/_/\/___/  \/_/\/_/
                       \ \_\
                        \/_/
    ____                 __  
   /\  _`\              /\ \
   \ \ \L\ \ __      ___\ \ \/'\
    \ \ ,__/'__`\   /'___\ \ , < 
     \ \ \/\ \L\.\_/\ \__/\ \ \\`\ 
      \ \_\ \__/.\_\ \____\\ \_\ \_\
       \/_/\/__/\/_/\/____/ \/_/\/_/


    __ __       __      __      __  
   /\ \\ \    /'__`\  /'__`\  /'__`\      __
   \ \ \\ \  /\ \/\ \/\ \/\ \/\ \/\ \    /\_\    ____
    \ \ \\ \_\ \ \ \ \ \ \ \ \ \ \ \ \   \/\ \  /',__\
     \ \__ ,__\ \ \_\ \ \ \_\ \ \ \_\ \__ \ \ \/\__, `\
      \/_/\_\_/\ \____/\ \____/\ \____/\_\_\ \ \/\____/
         \/_/   \/___/  \/___/  \/___/\/_/\ \_\ \/___/ 
                                         \ \____/ 
                                          \/___/

*/   
//____ 4 scriptographer 2.0.025 ________________________________________
// created by the mighty graf salamander

/*
  what it does:
the script takes the selected objects and blows them up with a detonation.
the detonation starts at the documents zeropoint and can be adjusted by 
moving that point or the selected vectors around.

  how to use:
select a few objects or several or also quite a lot and press the 
play button. a dialog will open with some options to tweak the result. 
tweak them and hit "lets boom!!!"

if the objects don't explode, they're most likely still a group. 
ungroup them! use free objects, free the objects!! freedom for objects!! 
down wif teh grups!!!

the detonation uses four "circles" to create the explosion. 
they can be modified in the script options to achieve different looks.
try out different values and different selected objects. 
also don't forget to play with the scale of the selected objects
in relation to the document size.

  notes:
great things come from explosions. 
but also great evil, so choose your objects wisely!

  todo:
use pathfinder to make a splitter-mode. heelp needed ->
http://www.scriptographer.com/Forum/Help/how-to-use-pathfinder/

*/

// set initial values
var initDetonationRadius = 100;

var initShakeFactor1 = 20;
var initShakeFactor2 = 6;
var initShakeFactor3 = 3;
var initShakeFactor4 = 1;

var initTwistFactor1 = 20;
var initTwistFactor2 = 18;
var initTwistFactor3 = 13;
var initTwistFactor4 = 10;

var initScaleFactor1 = 400;
var initScaleFactor2 = 300;
var initScaleFactor3 = 200;
var initScaleFactor4 = 100;

var color1 = new RGBColor(1,1,0);
var color2 = new RGBColor(1,0.8,0);
var color3 = new RGBColor(1,0.5,0);
var color4 = new RGBColor(1,0.3,0);
var color5 = new RGBColor(1,0.1,0);

var doColor = false;

// dialog is not open atm
openDialog = false;

// first open the dialog
optionDialog(); // dialog definition at files bottom

// the detonate function (aka. the mighty boomcore-blast) invoked from dialog
function detonate()
{
	// grab all selected items
	var items = null; // be sure items is emtpy at first
	var items = document.selectedItems;
	var selectedItemsCount = document.selectedItems.length;
	
	// if no item is selected
	if (selectedItemsCount == 0)
	{
		// send errormessage to console
		print('no objects selected, plz select boom-victims!');
		// and go home... ktnxbai
		return;
	}
	
	// prepare arrays and groups
	var coreZone = [];
	var innerZone = [];
	var middleZone = [];
	var outerZone = [];
	var rest = [];
	
	var groupCoreZone = new Group();
	var groupInnerZone = new Group();
	var groupMiddleZone = new Group();
	var groupOuterZone = new Group();
	var groupRest = new Group();
	
	// go throug all selected items
	for(var i=0;i<selectedItemsCount;i++)
	{
		var obj = null;
		obj = items[i];
		
		// get the distance of the object to the zero point (document origin)
		var distToDetonation = obj.bounds.center.getDistance(document.rulerOrigin);
		
		// divide the objects into different arrays based on the distance to center
		if(distToDetonation <= detonationRadius){
			coreZone.push(obj);
		}else if(distToDetonation <= detonationRadius*2){
			innerZone.push(obj);
		}else if(distToDetonation <= detonationRadius*3){
			middleZone.push(obj);
		}else if(distToDetonation <= detonationRadius*4){
			outerZone.push(obj);
		}else{
			rest.push(obj);
		}
	}
	
	// blast the different objectgroups with matching values 
	blastObjects(coreZone,coreValues,groupCoreZone)
	blastObjects(innerZone,innerValues,groupInnerZone)
	blastObjects(middleZone,middleValues,groupMiddleZone)
	blastObjects(outerZone,outerValues,groupOuterZone)
	
	// add the remaining objects to a group and colorize if desired
	for(var i=0;i<rest.length;i++)
	{
		if (doColor){
			rest[i].fillColor = color5;
		}
		groupRest.appendTop(rest[i]);
	}
	
	// orange groups, core in the front, rest in the back
	groupCoreZone.moveAbove(groupInnerZone);
	groupCoreZone.moveAbove(groupMiddleZone);
	groupCoreZone.moveAbove(groupOuterZone);
	groupCoreZone.moveAbove(groupRest);
	
	groupInnerZone.moveAbove(groupMiddleZone);
	groupInnerZone.moveAbove(groupOuterZone);
	groupInnerZone.moveAbove(groupRest);
	
	groupMiddleZone.moveAbove(groupOuterZone);
	groupMiddleZone.moveAbove(groupRest);
	
	groupOuterZone.moveAbove(groupRest);
}

// the blast function invoked from the detonate function
function blastObjects(obj,value,group)
{
	// iterate over all objects
	for(var i=0;i<obj.length;i++)
	{
		 // create new matrix
		var boomMove = new Matrix();
		// calculate shake value
		var shake = (Math.random() / 10 * (value[0]) + (value[0] / 10)); 
		// get x and y coordinates of the object
		var posX = obj[i].bounds.x;
		var posY = obj[i].bounds.y;
		
		// do a switch every object (i modulo 4 -> 4 possibilites -> 4 cases)
		switch(i%4)
		{
			// add shear to the matrix
			case 1:
				boomMove.shear(Math.random()/10 * value[1], Math.random()/10 * value[1]); // x and y positive
			case 2:
				boomMove.shear((Math.random()/10 * value[1]) * -1,(Math.random()/10 * value[1])*-1); // x and y negative
			case 3:
				boomMove.shear((Math.random()/10 * value[1]),(Math.random()/10 * value[1]) * -1); // x positive y negaive
			default:
				boomMove.shear((Math.random()/10 * value[1]) * -1,(Math.random()/10 * value[1])); // x positive y negative
		}
		
		// add translate to the matrix, current position multiplied by shakevalue -> keep direction away from zeropoint
		boomMove.translate(posX * shake, posY * shake);
		// add scale to the matrix
		boomMove.scale(value[2]/100,value[2]/100);
		// if desired change color of the object
		if (doColor){
			obj[i].fillColor = value[3];
		}
		
		// apply matrix to the object
		print(obj[i].bounds);
		obj[i].transform(boomMove);
		print(obj[i].bounds);

		// add object to group
		group.appendTop(obj[i]);
	}
}


//****function which creates the option dialog****
function optionDialog() 
{
		//////////////////////CONFIG//////////////////////////
		// dialog config stuff
		var dialogSize = new Size(315, 260);
		var dialogTitle = "fadenGraydiExplosionPack4000";
		
		openDialog = true; // set dialog status to true/open
		
		// create dialog
		dialog = new FloatingDialog('tabbed') {
			title: dialogTitle,
			size: dialogSize,
			visible: true,
			onClose: function() {
			        this.destroy();
					openDialog = false;
			}
		};
		
		// *start shakeFactor1 value
		var shakeFactor1Edit = new SpinEdit(dialog) {
			visible: true,
			position: [80, 25],
			size: [50, 20],
			value: initShakeFactor1
		};
		
		var shakeFactor1Text = new TextPane(dialog) {
			text: 'core:',
			position: [20, 25]
		};
		
		//------
		
		// *start shakeFactor2 value
		var shakeFactor2Edit = new SpinEdit(dialog) {
			visible: true,
			position: [80, 50],
			size: [50, 20],
			value: initShakeFactor2
		};
		
		var shakeFactor2Text = new TextPane(dialog) {
			text: 'inner:',
			position: [20, 50]
		};
		//------
		
		// *start shakeFactor3 value
		var shakeFactor3Edit = new SpinEdit(dialog) {
			visible: true,
			position: [80, 75],
			size: [50, 20],
			value: initShakeFactor3
		};
		
		var shakeFactor3Text = new TextPane(dialog) {
			text: 'middle:',
			position: [20, 75]
		};
		//------
		
		// *start shakeFactor4 value
		var shakeFactor4Edit = new SpinEdit(dialog) {
			visible: true,
			position: [80, 100],
			size: [50, 20],
			value: initShakeFactor4
		};
		
		var shakeFactor4Text = new TextPane(dialog) {
			text: 'outer:',
			position: [20, 100]
		};
		//------
		
		// *start twistFactor1 value
		var twistFactor1Edit = new SpinEdit(dialog){
			visible: true,
			position: [80, 150],
			size: [50, 20],
			value: initTwistFactor1
		};
		
		var twistFactor1Text = new TextPane(dialog) {
			text: 'core:',
			position: [20, 150]
		};
		//------
		
		// *start twistFactor2 value
		var twistFactor2Edit = new SpinEdit(dialog) {
			visible: true,
			position: [80, 175],
			size: [50, 20],
			value: initTwistFactor2
		};
		
		var twistFactor2Text = new TextPane(dialog) {
			text: 'inner:',
			position: [20, 175]
		};
		//------
		
		// *start twistFactor3 value
		var twistFactor3Edit = new SpinEdit(dialog) {
			visible: true,
			position: [80, 200],
			size: [50, 20],
			value: initTwistFactor3
		};
		
		var twistFactor3Text = new TextPane(dialog) {
			text: 'middle:',
			position: [20, 200]
		};
		//------
		
		// *start twistFactor4 value
		var twistFactor4Edit = new SpinEdit(dialog) {
			visible: true,
			position: [80, 225],
			size: [50, 20],
			value: initTwistFactor4
		};
		
		var twistFactor4Text = new TextPane(dialog) {
			text: 'outer:',
			position: [20, 225]
		};
		//------
		
		// *start scaleFactor1 value
		var scaleFactor1Edit = new SpinEdit(dialog) {
			visible: true,
			position: [235, 25],
			size: [50, 20],
			value: initScaleFactor1
		};
		
		var scaleFactor1Text = new TextPane(dialog) {
			text: 'core:',
			position: [175, 25]
		};
		
		var scalePercent1Text = new TextPane(dialog) {
			text: '%',
			position: [290, 25]
		};
		//------
		
		// *start scaleFactor2 value
		var scaleFactor2Edit = new SpinEdit(dialog) {
			visible: true,
			position: [235, 50],
			size: [50, 20],
			value: initScaleFactor2
		};
		
		var scaleFactor2Text = new TextPane(dialog) {
			text: 'inner',
			position: [175, 50]
		};
		
		var scalePercent2Text = new TextPane(dialog) {
			text: '%',
			position: [290, 50]
		};
		//------
		
		// *start scaleFactor3 value
		var scaleFactor3Edit = new SpinEdit(dialog) {
			visible: true,
			position: [235, 75],
			size: [50, 20],
			value: initScaleFactor3
		};
		
		var scaleFactor3Text = new TextPane(dialog) {
			text: 'middle',
			position: [175, 75]
		};
		
		var scalePercent3Text = new TextPane(dialog) {
			text: '%',
			position: [290, 75]
		};
		//------
		
		// *start scaleFactor4 value
		var scaleFactor4Edit = new SpinEdit(dialog) {
			visible: true,
			position: [235, 100],
			size: [50, 20],
			value: initScaleFactor4
		};
		
		var scaleFactor4Text = new TextPane(dialog) {
			text: 'outer',
			position: [175, 100]
		};
		
		var scalePercent4Text = new TextPane(dialog) {
			text: '%',
			position: [290, 100]
		};
		//------
		
		// *start detonationRadius value
		var detonationRadiusEdit = new SpinEdit(dialog) {
			visible: true,
			position: [235, 150],
			size: [50, 20],
			value: initDetonationRadius
		};
		
		var detonationRadiusText = new TextPane(dialog) {
			text: 'radius:',
			position: [175, 150]
		};
		//------
		
		// ***** start color checkbox *****
		var colorCheck = new CheckBox(dialog) {
			position: [235, 175],
			size: [20, 15],
			visible: true,
			checked: false,
			onClick: function () {
				doColor = colorCheck.checked;
			}
		};

		var colorText = new TextPane(dialog) {
			text: 'colorize:',
			position: [175, 175]
		};
		// ----- end color checkbox -----
		
		// *start goButton
		var submitButton = new Button(dialog) {
			text: 'lets boom!!!',
			size: [145,20],
			position: [165, 220],
			onClick: function () { // set all option on click

				detonationRadius = detonationRadiusEdit.value;

				coreValues = [];
				innerValues = [];
				middleValues = [];
				outerValues = [];

				coreValues[0] = shakeFactor1Edit.value;
				innerValues[0] = shakeFactor2Edit.value;
				middleValues[0] = shakeFactor3Edit.value;
				outerValues[0] = shakeFactor4Edit.value;

				coreValues[1] = twistFactor1Edit.value;
				innerValues[1] = twistFactor2Edit.value;
				middleValues[1] = twistFactor3Edit.value;
				outerValues[1] = twistFactor4Edit.value;

				coreValues[2] = scaleFactor1Edit.value;
				innerValues[2] = scaleFactor2Edit.value;
				middleValues[2] = scaleFactor3Edit.value;
				outerValues[2] = scaleFactor4Edit.value;

				coreValues[3] = color1;
				innerValues[3] = color2;
				middleValues[3] = color3;
				outerValues[3] = color4;

				doColor = colorCheck.checked;

				/// go for it!!!
				detonate();
			}
		};

		//-------
		
		// *start shakeFrame 
		var shakeFrame = new Frame(dialog) {
			text: 'boom shake zone',
			font: 'palette-italic',
			size: [140, 125],
			position: [5, 5]
		};
		//---------
		
		// *start twistFrame 
		var twistFrame = new Frame(dialog) {
			text: 'boom twist zone',
			font: 'palette-italic',
			size: [140, 125],
			position: [5, 130]
		};
		//---------
		
		// *start scaleFrame 
		var scaleFrame = new Frame(dialog) {
			text: 'boom scale zone',
			font: 'palette-italic',
			size: [150, 125],
			position: [160, 5]
		};
		//---------
		
		// *start otherFrame 
		var otherFrame = new Frame(dialog) {
			text: 'boom misc',
			font: 'palette-italic',
			size: [150, 70],
			position: [160, 130]
		};

		//**********************************
}


