
#include "reflection_tessellation.jsx"

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

var gLastReflectionRectangles;

{
    var omd = newOminoDialog("K-Tessellation: Kepler");

	omd.boxedText(5,"This Illustrator script tesselates one Kepler's tilings with a reflected image.");
    omd.separator();
	omd.checkbox("Draw Images","drawImages",true,"draw");
	omd.openFile("Image","f","");

	omd.number("Scale","scale",1.0);

	omd.checkbox("Draw Edges","drawEdges",true,"draw");
	omd.number("Hex Size","hexSize",100);
    omd.number("Hexes Across","hexesAcross",7);
    omd.number("Down","hexesDown",7);

	omd.set(gLastReflectionRectangles);
    var r = omd.run();
	
	if(r != null)
	{
		gLastReflectionRectangles = r;

		// work on a new layer of the current doc.
		var doc = documents[0];
		var layer = doc.layers.add();
		layer.name = "tessellated_rectangles";
		
		var docCx = doc.width / 2;
		var docCy = doc.height / 2;
		
		var lowX = 0;
		var lowY = 0; // todo: center the whole image. maybe after laying the points down.

		var t = tNew();
		
		var s3 = r.hexSize * Math.sqrt(3);
		var ppr = r.hexesAcross * 6;
		// lay down the point mesh.
		// for each hex across, we lay down 6 points.
		for(var j = 0; j < r.hexesDown; j++)
		{
			var rowIdx = ppr * j;
			var left = lowX - j * (s3 + r.hexSize) / 2;
			var y = lowY + j * (s3 + 3 * r.hexSize) / 2;
			for(var i = 0; i < r.hexesAcross; i++)
			{
				var curIdx = rowIdx + 6 * i;
				/* points numbered:
					*     2
					*  1   3
					*  0   4
					*    5
					*/
				var x = left + i * (s3 + r.hexSize);
				var p0 = tAddPoint(t,x,y);
				var p1 = tAddPoint(t,x,y + r.hexSize);
				var p2 = tAddPoint(t,x + s3 / 2,y + 1.5 * r.hexSize);
				var p3 = tAddPoint(t,x + s3,y + r.hexSize);
				var p4 = tAddPoint(t,x + s3,y);
				var p5 = tAddPoint(t,x + s3 / 2,y - 0.5 * r.hexSize);
				
				tAddPoly(t,[p0,p1,p2,p3,p4,p5]);
				// todo: join rectangles & triangles to the one up, and left, of us

				var prevXIdx = curIdx - 6;
				var prevYIdx = curIdx - ppr;
				var prevXYIdx = curIdx - ppr - 6;
				if(i > 0) // are we not the leftmost?
				{
					// square to the left
					tAddPoly(t,[p0,p1,prevXIdx + 3,prevXIdx + 4]);
				}
				if(j > 0) // are e not the bottommost
				{
					// square to the below-right-ish
					tAddPoly(t,[p4,p5,prevYIdx + 1,prevYIdx + 2]);
				}
				if(i > 0 && j > 0) // are e not the cornermost
				{
					// square to the below-right-ish
					tAddPoly(t,[p5,p0,prevXYIdx + 2,prevXYIdx + 3]);
					// and two triangles, finishes this off.
					tAddPoly(t,[p0,prevXIdx + 4,prevXYIdx + 2]);
					tAddPoly(t,[p5,prevXYIdx + 3,prevYIdx + 1]);
				}
			}
		}

	tCenterOnto(t,docCx,docCy);


		var po = tFindCenterPoly(t)

		// seed the center and draw
		if(r.drawImages)
		{
			var g = layer.groupItems.add();
			g.name = "images";
			var cx = Math.floor(r.cellsAcross / 2);
			var cy = Math.floor(r.cellsDown / 2);


			var pa = tNewPatch(po.centerX,po.centerY,0,0);
			pa.scale = r.scale;
			po.pa = pa; // stash the patch in the poly

			tDrawKTessellation(g,t,r.f,po);
		}
		
		// draw the edges
		if(r.drawEdges)
		{
			var g = layer.groupItems.add();
			g.name = "edges";
			tDrawAllEdges(g,t);
		}
		
		// draw the center one
		tDrawPoly(layer,po,1,0,0,.3).name = "center_poly";

	}
}