////////////////////////////////////////////////////////////////////////////////
// Note from the Scriptographer.org Team
//
// In Scriptographer 2.9, we switched to a top-down coordinate system and
// degrees for angle units as an easier alternative to radians.
// 
// For backward compatibility we offer the possibility to still use the old
// bottom-up coordinate system and radians for angle units, by setting the two
// values bellow. Read more about this transition on our website:
// http://scriptographer.org/news/version-2.9.064-arrived/

script.coordinateSystem = 'bottom-up';
script.angleUnits = 'radians';

/*
cylinder
  
Scriptographer 2.7 script
  
2006-11-23
2008-03-26 modified to work with Sg 2.0.025
2008-03-27 minor modification for dialog
2008-04-01  minor fix on myDialog_getValues
2009-?-?   official staff modified to work with Sg 2.5. Thanks!
2010-02-21 added some function for the dialog
2010-04-17 modified to work with Sg 2.7.040

Scriptographer is a plugin for Adobe Illustrator(TM)
created by Juerg Lehni
http://www.scriptographer.org/

This script was written by Hiroyuki Sato
http://lines-about-to-be-generated.blogspot.com/
*/

var hlen = 4 * (Math.sqrt(2) - 1) / 3;
var mpi = Math.PI;
var hpi = Math.PI / 2;
var wpi = Math.PI * 2;

// ----------------------------------------------
var values = { 
  diameter: 10,
  height:   18,
  interval: 13.5,
  jitter:   18
}; 
 
var items = { 
  diameter: { type: 'number', label: 'diameter', units: 'point' },
  height:   { type: 'number', label: 'height',   units: 'point' },
  interval: { type: 'number', label: 'interval', units: 'point' },
  jitter:   { type: 'number', label: 'jitter',   units: 'point' }
}; 

var palette = new Palette('cylinder', items, values);

// ----------------------------------------------
function onInit() {}
// ----------------------------------------------
function mm2pt(n){  return n * 2.83464567;  }
// ----------------------------------------------
function onSelect(){ palette.visible = true; }
// ----------------------------------------------
function onDeselect(){ palette.visible = false; }
// ----------------------------------------------
function onMouseDown(event){
    with(document.activeLayer){
        if(locked || hidden){
            Dialog.alert("please unlock and show the active layer");
            return; }
    }
    
    tool.distanceThreshold = values.interval;
    
    drawCylinder(event.point);
}
// ----------------------------------------------
function onMouseDrag(event){
    var jitter_vector = (event.point - event.lastPoint).normalize().rotate(hpi);
    
    drawCylinder(event.point + jitter_vector * (Math.random() - 0.5) * values.jitter);
}
// ----------------------------------------------
function onMouseUp(){}
// ----------------------------------------------
function drawCylinder(pnt){
    var r = values.diameter / 2.0;
    var h = values.height;

    var tv = (Math.random() * 0.8 + 0.1) * hpi;

    var v = new Point(1,0);

    var path = make_a_path(1, new GrayColor(0));
    path.closed = true;

    var gr = new Group();
    gr.appendChild(path);
    var seg = path.segments;
    seg.addAll([
        new Segment(v.rotate(hpi) * r * [1, Math.sin(tv)],
                    v * r * hlen, v * -r * hlen),
        new Segment(v.rotate(mpi) * r,
                    v.rotate(hpi) * r * hlen * [1,Math.sin(tv)],
                    new Point(0,0))
    ]);
    seg.addAll([
        new Segment(seg[1].point + [0, -h * Math.cos(tv)],
                    new Point(0,0),
                    -seg[1].handleIn),
        new Segment(seg[0].point + [0, -h * Math.cos(tv) -r * 2 * Math.sin(tv)],
                    seg[0].handleOut, seg[0].handleIn)
        ]);
    seg.addAll([
        new Segment(seg[2].point + [r * 2,0],
                    seg[2].handleOut, new Point(0, 0)),
        new Segment(seg[1].point + [r * 2,0],
                    new Point(0, 0), seg[1].handleIn)
        ]);
    
    path = make_a_path(0.5, null);
    var seg2 = path.segments; path.moveAbove(gr.firstChild);
    seg2.addAll([
        new Segment(seg[1].point, new Point(0,0), seg[2].handleOut),
        new Segment(new Point(0,-seg[0].point.y),
                    seg[0].handleOut, seg[0].handleIn),
        new Segment(seg[5].point, seg[4].handleIn, new Point(0,0))
        ]);
    
    gr.translate(new Point(0,h*Math.cos(tv)/2));
    gr.rotate( Math.random()*wpi );
    gr.translate(pnt);
}
// ----------------------------------------------
function make_a_path(w,c){
    var path = new Path() {
      closed: false,
      strokeWidth: w,
      strokeColor: new GrayColor(1),
      fillColor: c
    };
    return path;
}

