﻿/*
 * circularGauge.jsx
 *
  Description: This Adobe Illustrator script draws a series of
  radial strokes, with a number at the end of each. Settings are
  entered through a simple dialog.
 
  Handy for concocting gauges. ::
 * dvb2007
 */

#include "../omino_adobe_script_suite/src/shared/ominoDialogMaker.jsx"


// this variable goes outside the main brackets to keep the
// most recent dialog values.
var gLastRoundRectSettings;

{
var docCount = documents.length;

if (docCount == 0)
{
   documents.add();
}


/*
 * Add a point to PathPoints,
 * straigh.
 */
function addP(points,x,y)
{
	var p = points.add();
	p.anchor = [x,y];
	p.leftDirection = p.anchor;
	p.rightDirection = p.anchor;
}
function addP2(group,x1,y1,x2,y2)
{
	var pathItem = group.pathItems.add();
	var points = pathItem.pathPoints; // brand new, empty
	addP(points,x1,y1);
	addP(points,x2,y2);
}

function addRadialTick(group,cx,cy,angle,r1,r2)
{
	var angleR = angle / 57.29577951;
	var x1 = cx + Math.cos(angleR) * r1;
	var y1 = cy + Math.sin(angleR) * r1;
	var x2 = cx + Math.cos(angleR) * r2;
	var y2 = cy + Math.sin(angleR) * r2;
	addP2(group,x1,y1,x2,y2);
}

function addRadialText(group,cx,cy,angle,r,text,textSize)
{
	// add the text... a little further out
	var angleR = angle / 57.29577951;
	var x2 = cx + Math.cos(angleR) * (r);
	var y2 = cy + Math.sin(angleR) * (r);
	var textAngle = angle - 90;
    //var t = Math.floor(x2) + "," + Math.floor(y2);
    //t = "" + angle;
	addText(group,text,
			x2,y2,textSize,textAngle);
}

function layerByName(layers,name)
{
	var i;
	for(i = 0; i < layers.length; i++)
	{
		var layer = layers[i];
		if(layer.name == name)
			return layer;
	}
	return null;
}

function newLayer(doc,baseName)
{
	var n = 0;
	var result = null;
	while(result == null)
	{
		n++;
		var aName = "" + baseName + n;
		var existingLayer = layerByName(doc.layers,aName);
		if(existingLayer == null)
		{
			result = doc.layers.add();
			result.name = aName;
		}
	}
	return result;
}

function addRadialTicks(layer,groupName,cx,cy,angleStart,angleEnd,divisionCount,r1,r2)
{
	var group = layer.groupItems.add();
	group.name = groupName;

    if(angleEnd % 360 == angleStart)
    {
        angleEnd -= 360 / divisionCount;
        divisionCount--;
    }

	for(var i = 0; i <= divisionCount; i++)
	{
		var theta = angleStart + (angleEnd - angleStart) * i / divisionCount;
		addRadialTick(group,cx,cy,theta,r1,r2);
	}
}

function addRadialTexts(layer,groupName,cx,cy,angleStart,angleEnd,divisionCount,valueLow,valueHigh,r)
{
	var group = layer.groupItems.add();
	group.name = groupName;
	var doc = app.documents[0];
	var newLayer = doc.layers.add();
	newLayer.name = name;

    if(angleEnd % 360 == angleStart)
    {
        angleEnd -= 360 / divisionCount;
		valueHigh = valueLow + (valueHigh - valueLow) * (divisionCount - 1) / divisionCount;
        divisionCount--;
    }

    var textSize;
    {
        var angleStep = (angleEnd - angleStart) / divisionCount;
        var approximateStepLength = angleStep * r / 57.29577951;
        textSize = approximateStepLength / 2; // consider n characters...
    }

	for(var i = 0; i <= divisionCount; i++)
	{
		var theta = angleStart + (angleEnd - angleStart) * i / divisionCount;
		
		var v = valueLow + (valueHigh - valueLow) * i / divisionCount;
		addRadialText(group,cx,cy,theta,r,v,textSize);
	}
}

function addText(group,text,x,y,size,angle)
{
	var textItem = group.textFrames.add();
	textItem.contents = text;


	textItem.textRange.characterAttributes.size = size;

	textItem.rotate(angle, undefined, undefined, undefined, undefined, Transformation.CENTER);

	var bounds = textItem.visibleBounds;
	
	var tW = Math.abs(bounds[2] - bounds[0]);
	var tH = Math.abs(bounds[3] - bounds[1]);

	// coords go from bottom to top...
	// but text position is left, top.
	var tX = x - tW/2;
	var tY = y + tH/2;

	textItem.position = [tX,tY];
}

function segment(s, g, top, left, color)
{
    // use settings s for the size, and top, left.
    // add to "g" that has pathitems.
    var edge = s.width;
    var hole = s.hole;
    var holeOffset = (edge - hole) / 2.0;
    var rad = s.radius;
    
    var coPa = g.compoundPathItems.add();
    var pa;
    pa = coPa.pathItems.roundedRectangle(top,left,edge, edge, rad, rad);
    pa.fillColor = color;
    pa = coPa.pathItems.roundedRectangle(top-holeOffset,left+holeOffset, hole, hole, rad, rad, 1);      
    pa.fillColor = color;
    return coPa;
}

function rr(low,high)
{
    var x = Math.random() * (high - low);
    x += low;
    return x;
}

function doIt(s)
{
    var doc = documents[0];
    var activeLayer = doc.activeLayer;
    var g = activeLayer.groupItems.add();
    g.name = "roundedRectangleShelf";

	var selects = app.selection;
    
    var co = new RGBColor();
    co.red = rr(128,200);
    co.green = rr(128,200);
    co.blue = rr(128,200);


    var s2 = {};
    for(var k in s)
    {
        s2[k] = s[k];
        if(k == "segments")
            continue;
        s2[k] *= 72;
    }
    s = s2;

    var joinerWidth = s.hole - 2 * s.radius;
    var joinerHeight = s.thickness;
    var joinerLeft = (s.width - joinerWidth) / 2;
    
    var y = 0;
    for(var ix = 0; ix < s.segments; ix++)
    {
        if(ix > 0)
        {
            var top = y + joinerHeight;
            var reI = g.pathItems.rectangle(top, joinerLeft, joinerWidth, joinerHeight);
            reI.fillColor = co;
        }
        var rrEI = segment(s, g, y, 0, co);
        y -= s.width + s.thickness;
    }
    
//~     var edge = s.width * 72;
//~     var hole = s.hole * 72;
//~     var holeOffset = (edge - hole) / 2.0;
//~     var rad = (edge - hole) / 4.0;
//~     var coPa = layer.compoundPathItems.add();
//~     coPa.pathItems.roundedRectangle(0,0,edge, edge, rad, rad);
//~     coPa.pathItems.roundedRectangle(-holeOffset,holeOffset, hole, hole, rad, rad, 1);      
}

function validate(s)
{
    var t = "Will it fit\n";
    t += "width: " + s.width + "\n";
    t += "thickness: " + s.thickness + "\n";
    t += "hole: " + s.hole + "\n";
    
    var holeDiag = s.hole * Math.sqrt(2) - 2 * (Math.sqrt(2) - 1) * s.radius;
    var need = s.width + s.thickness;
    t += "need: " + need + "\n";
    t += "got: " + holeDiag + "\n";
    alert(t);
    }
function main()
{
	var docRef = documents[0];
	var docHeight = -docRef.height;
	var docWidth = docRef.width;


    // default values
	var thickness = 0.75;
    var width = 12.0;
    var hole = 10.0;
    var radius = 1.0;
    var segments = 4;
    var doHole = 1;

    var omd = newOminoDialog("Round Rect Shelves");

	


	omd.boxedText(5,"This Illustrator script helps create round rect shelves."
        + " Great for Bentonville Domino.");
    omd.separator();
    omd.number("Thickness","thickness",thickness);
    omd.number("Width","width",width);
    omd.number("Hole","hole",hole);
    omd.number("Radius","radius",radius);

    omd.number("Segments","segments",segments);
    omd.gap();
	omd.checkbox("Do Hole","doHole",doHole);

    omd.gap();
    omd.button("Check Measurements", validate, gLastRounRectShelvesSettings);

	omd.set(gLastRoundRectSettings);
    var result = omd.run();
	
	if(result)
		gLastRoundRectSettings = result;

	if(!result)
		return; // canceled

    doIt(gLastRoundRectSettings);
}

main();
}

