﻿/*
 * point-align
  Description: translate objects until two points align
 * dvb2017
 */

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


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

{
	
	
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 pointSelectionKind(k)
{
    if(k == PathPointSelection.ANCHORPOINT)
      return "P";
    if(k == PathPointSelection.LEFTDIRECTION)
      return "L";
    if(k == PathPointSelection.RIGHTDIRECTION)
      return "R";
      
    return "?";
}

function firstSelectedAnchor(aPath)
{
      var selectedPathPoints = aPath.selectedPathPoints;
      for(var jx = 0; jx < selectedPathPoints.length; jx++)
      {
          var po = selectedPathPoints[jx];
          if(po.selected == PathPointSelection.ANCHORPOINT)
            return po;
      }
      return null;
}

function translatePath(pa, xDelta, yDelta)
{
    var oldPoints = [];
    var k = pa.pathPoints.length
    for(var ix = 0; ix < k; ix++)
    {
        var po = pa.pathPoints[ix];
        oldPoints.push(po);
    }

 //   pa.pathPoints.removeAll();
    
    for(var ix = 0; ix < k; ix++)
    {
        var poN = pa.pathPoints.add();
        var poO = oldPoints[ix];
        
        poN.anchor = Array(poO.anchor[0] + xDelta, poO.anchor[1] + yDelta);
        poN.leftDirection = Array(poO.leftDirection[0] + xDelta, poO.leftDirection[1] + yDelta);
        poN.rightDirection = Array(poO.rightDirection[0] + xDelta, poO.rightDirection[1] + yDelta);
        poN.pointType = poO.pointType;
        
        poO.remove();
    }
    

}
function doAlignment(docRef)
{
    var s = "";
    var selectedPaths = docRef.selection;
    var anchorPoints = [];
    var anchorPaths = []; // indexed same as the points.
    

    for(var ix = 0; ix < selectedPaths.length; ix++)
    {
      var pa = selectedPaths[ix];
      var po = firstSelectedAnchor(pa);
      if(po != null)
      {
        anchorPoints.push(po);
        anchorPaths.push(pa);
      }
    }

    if(selectedPaths.length < 2)
    {
        alert("Nope\nPlease select one point on each of 2 or more paths.");
        return;
    }

    var pointMain = anchorPoints[0];
    for(ix = 1; ix < anchorPoints.length; ix++)
    {
        var po = anchorPoints[ix];
        var xDelta = po.anchor[0] - pointMain.anchor[0];
        var yDelta = po.anchor[1] - pointMain.anchor[1];
        translatePath(anchorPaths[ix], -xDelta, -yDelta);
    }

}

function main()
{
	var docRef = documents[0];
	var docHeight = docRef.height;
	var docWidth = docRef.width;

    // default values
	var x = 10;

    var omd = newOminoDialog("Align 1");

	omd.boxedText(5,"This Illustrator script moves paths so the corresponding selected points come into alignment.");
    omd.separator();
	
	omd.set(gLastAlign1Settings);
    var result = omd.run();
	
	if(result)
		gLastAlign1Settings = result;

	if(!result)
		return; // canceled

    doAlignment(docRef);
}

main();
}

