/*
cube
  
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-18 modified to work with Sg 2.6.036
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 mpi = Math.PI;
var hpi = Math.PI / 2;
var wpi = Math.PI * 2;

// ----------------------------------------------
var values = { 
  cubeSize: 10,
  interval: 15,
  jitter:   10
}; 
 
var items = { 
  cubeSize: { type: 'number', label: 'size',     units: 'point' },
  interval: { type: 'number', label: 'interval', units: 'point' },
  jitter:   { type: 'number', label: 'jitter',   units: 'point' }
}; 

var palette = new Palette('cube', items, values);

// ----------------------------------------------
function mm2pt(n){  return n * 2.83464567;  }
// ----------------------------------------------
function onInit() {}
// ----------------------------------------------
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;
    
    drawCube(values.cubeSize, event.point);
}
// ----------------------------------------------
function onMouseDrag(event){
    var jitter_vector = (event.point - event.lastPoint).normalize().rotate(hpi);

    drawCube(values.cubeSize,
             event.point + jitter_vector * (Math.random() - 0.5) * values.jitter);
}
// ----------------------------------------------
function onMouseUp(){}
// ----------------------------------------------
function points2segments(points){
    for(var i=0; i < points.length; i++){
        points[i] = new Segment(points[i]);
    }
    return points;
}
// ----------------------------------------------
function drawCube(s, pnt){
    //  s = Math.random()*s + s;

    var tv = (Math.random() * 0.7 + 0.15) * hpi;
    var th = (Math.random() - 0.5) * (hpi * 0.85);
    var r = s / Math.sqrt(2);
    var v = new Point(1,0);
    var v2 = v.rotate(th) * r;
    var sn = Math.sin(tv);
    
    var face_top = [v2 * new Point(1, sn),
                    v2.rotate(hpi) * new Point(1, sn),
                    v2.rotate(mpi) * new Point(1, sn),
                    v2.rotate(-hpi) * new Point(1,sn)];
    
    var s2 = s * Math.cos(tv);
    
    var face_side = [v2.rotate(mpi) * new Point(1,sn) + new Point(0,-s2),
                     v2.rotate(-hpi) * new Point(1,sn) + new Point(0,-s2),
                     v2 * new Point(1,sn) + new Point(0,-s2)];
    
    var path = make_a_path(1, new GrayColor(0));
    
    path.closed = true;
    path.strokeJoin = 'round';

    var gr = new Group();
    gr.appendTop(path);
    path.segments = points2segments(
        [face_top[0],  face_top[1],  face_top[2],
         face_side[0], face_side[1], face_side[2]]);

    
    path = make_a_path(0.5, null);
    gr.appendTop(path);
    path.segments = points2segments(
        [face_top[0], face_top[3]]);
    
    path = make_a_path(0.5, null);
    gr.appendTop(path);
    path.segments = points2segments(
        [face_top[2], face_top[3]]);
    
    path = make_a_path(0.5, null);
    gr.appendTop(path);
    path.segments = points2segments(
        [face_side[1], face_top[3]]);
    
    gr.position += new Point(0, s2 / 2);
    gr.rotate( Math.random() * wpi );
    gr.position += pnt;
}
// ----------------------------------------------
function make_a_path(w, c){
    var path = new Path() {
      closed: false,
      strokeWidth: w,
      strokeColor: new GrayColor(1),
      fillColor: c
    };
    return path;
}
