////////////////////////////////////////////////////////////////////////////////
// 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';

/**
 * Script:	Qwatch
 * Author:	Quentin T
 * URL:		http://toki-woki.net
*/
function onInit() {
	curvePrecision=10;
	try {
		if (w && !w.visible) {
			w.visible=true;
			return;
		}
	} catch (e) {}
	w=new FloatingDialog('tabbed') {
		title: 'Qwatch Help',
		size: [200, 100],
		onChange : function() {
			t.size = w.size;
		}
	};
	var t=new TextPane(w, 'multiline');
	t.text='Hi!\r\n';
	t.text+='You can use Qwatch in two ways:\r\n';
	t.text+=' - As a tool\r\n'
	t.text+=' - As a script\r\n\r\n';
	t.text+='To use it as a tool, select an image and a shape, then draw over the image.\r\nThe image\'s colors will be used as a live swatch and the shape will be your "brush".\r\n\r\n';
	t.text+='To use it as a script, select an image, a shape and a blank path (no fill, no stroke).\r\nThe image\'s colors will be used as a live swatch, the shape will be your "brush" and the blank path will be used as a draw path.\r\nDouble click on the tool button (in your toolbar) to set some preferences.\r\n\r\n';
	t.text+='Have fun.';
	w.onChange();
}
function onOptions() {
	var values = Dialog.prompt("Options:", [
		{ value: curvePrecision, description: "Curve precision (script mode only)"}
	]);
	if (values) {
		curvePrecision = values[0];
	}
}
function tidySelection() {
	var sel=document.selectedItems;
	raster=null;
	brush=null;
	path=null;
	for (var i=0; i<sel.length; i++) {
		var o=sel[i];
		if (o instanceof Raster || o instanceof PlacedFile) raster=handleRaster(o);
		if (o instanceof Path) {
			if (o.strokeColor==null && o.fillColor==null) path=o;
			else brush=o;
		}
	}
}
function onMouseDown(e) {
	dots=[];
	tidySelection();
	if (!raster || !brush) {
		Dialog.alert('In "tool mode" you must select an image and a shape!');
		return;
	}
	drawing=true;
}
function onMouseDrag(e) {
	if (!drawing) return;
	var dot=handlePoint(e.point.x, e.point.y, raster);
	if (dot) dots.push(dot);
}
function handlePoint(px, py, raster) {
	if (!(px>=raster.bounds.left && px<=raster.bounds.right && py<=raster.bounds.top && py>=raster.bounds.bottom)) return;
	var x=Math.round(px-raster.bounds.left);
	var y=Math.round(raster.bounds.top-py);
	dot=brush.clone();
	moveArtTo(dot, px, py);
	dot.fillColor=raster.getPixel(x, y);
	dot.strokeColor=null;
	return dot;
}
function moveArtTo(art, x, y) {
	art.translate(new Point(x, y) - art.bounds.center);
}
function onMouseUp(e) {
	if (dots.length>0) new Group(dots);
	drawing=false;
}
function hasBitmap() {
	var sel = document.selectedItems;
	if (sel.length!=1 || !sel[0] instanceof Raster) return false;
	var obj=sel[0];
	raster=handleRaster(obj);
	return true;
}
function handleRaster(o) {
	if (o instanceof PlacedFile && !o.eps) return o.embed(false);
	return o;
}
//
tidySelection();
if (raster && brush && path) {
	var g=new Group();
	var p=path.clone();
	p.curvesToPoints(curvePrecision);
	for (var i=0; i<p.segments.length; i++) {
		var seg=p.segments.get(i);
		g.appendTop(handlePoint(seg.point.x, seg.point.y, raster));
	}
	p.remove();
}
