//=====================================================
//startDrag and stopDrag replacements
MovieClip.prototype.beginDrag = function(lock, l, t, r, b){
if (this.$dragMethod) this.endDrag();
this.$dragMethod = {MM:this.onMouseMove};
ASSetPropFlags(this,"$dragMethod",1,1);
this.addProperty("onMouseMove",arguments.callee.getMM,arguments.callee.setMM);
var my = this;
var constrain = (arguments.length > 1);
var off_x = 0, off_y = 0;
if (!lock){
off_x = my._parent._xmouse-my._x;
off_y = my._parent._ymouse-my._y;
}
this.$dragMethod.drag = function(){
my._x = my._parent._xmouse-off_x;
my._y = my._parent._ymouse-off_y;
if (constrain){
if (typeof l == "object"){
t = l.ymin;
r = l.xmax;
b = l.ymax;
l = l.xmin;
}
if (my._x < l) my._x = l;
else if (my._x > r) my._x = r;
if (my._y < t) my._y = t;
else if (my._y > b) my._y = b;
}
updateAfterEvent();
}
}
MovieClip.prototype.beginDrag.getMM = function(){
this.$dragMethod.drag();
return this.$dragMethod.MM;
}
MovieClip.prototype.beginDrag.setMM = function(f){
this.$dragMethod.MM = f;
}
MovieClip.prototype.endDrag = function(){
delete this.onMouseMove;
this.onMouseMove = this.$dragMethod.MM;
delete this.$dragMethod;
this.startDrag(); // for _droptarget
this.stopDrag();
}
// OBJECT SCOPES: ============================================================
// lines clip is where to draw lines
lines = this.createEmptyMovieClip("l",1);
// ornaments is for movement handles etc - above lines clip
ornaments = this.createEmptyMovieClip("o",2);
// VARIABLES: ================================================================
curLine = null; // currently selected line
// ^ as a 'global' variable, easily lets you be able to set the
// color or line thickness of the line while its selected
// FUNCTIONS: ================================================================
// drag functions used on movement handles
setDrag = function(){
this.dragging = true; // drag flag true (now dragging)
p3handle._visible = false;
}
unSetDrag = function(){
this.dragging = false; // drag flag false (now not dragging)
//readjust centre point
trace("new p1: (" + curLine.p1.x + "," + curLine.p1.y + ")");
trace("new p2: (" + curLine.p2.x + "," + curLine.p2.y + ")");
lineCentreXpos = (curLine.p2.x + curLine.p1.x)/2;
lineCentreYpos = (curLine.p2.y + curLine.p1.y)/2;
p3handle._x = lineCentreXpos;
p3handle._y = lineCentreYpos;
p3handle.pt = {x:lineCentreXpos, y:lineCentreYpos};
p3handle._visible = true;
trace("lines NEW coordinates: (" + lines._x + ", " + lines._y + ")");
}
onDrag = function(){
if (this.dragging){ // if being dragged
p3handle._visible = false;
this._x = this._parent._xmouse; // move to the mouse
this._y = this._parent._ymouse;
this.pt.x = this._x; // saved point, revereces moveTo or lineTo point
this.pt.y = this._y;
curLine.redraw(); // redraw the current line
}
}
unSetDragLine = function(){
this.endDrag();
curLine.endDrag();
p3OldXpos = this.pt.x;
p3OldYpos = this.pt.y;
this.pt.x = this._x; // saved point, revereces moveTo or lineTo point
this.pt.y = this._y;
p3NewOldXposDiff = this.pt.x - p3OldXpos;
p3NewOldYposDiff = this.pt.y - p3OldYpos;
trace("new p1: (" + curLine.p1.x + "," + curLine.p1.y + ")");
p1handle._x = curLine.p1.x + p3NewOldXposDiff;
p1handle._y = curLine.p1.y + p3NewOldYposDiff; // set position on screen
p1handle.pt = {x:curLine.p1.x + p3NewOldXposDiff, y:curLine.p1.y + p3NewOldYposDiff};
trace("new p2: (" + curLine.p2.x + "," + curLine.p2.y + ")");
p2handle._x = curLine.p2.x + p3NewOldXposDiff;
p2handle._y = curLine.p2.y + p3NewOldYposDiff; // set position on screen
p2handle.pt = {x:curLine.p2.x + p3NewOldXposDiff, y:curLine.p2.y + p3NewOldYposDiff}; // p2 ha
curLine.redraw();
p1handle._visible = p2handle._visible = true;//reactivate other points
trace("lines NEW coordinates: (" + lines._x + ", " + lines._y + ")");
//this.pt.x = this._x; // saved point, revereces moveTo or lineTo point
//this.pt.y = this._y;
}
onDragLine = function()
{
p1handle._visible = p2handle._visible = false//deactivate other points
//if (this.dragging){ // if being dragged
this.beginDrag(false);
curLine.beginDrag(false);
}
MovieClip.prototype.createCircHandle = function(x,y,d,col,alpha){
trace("node diameter " + d);
var r=d/2;
var c1=r*(Math.SQRT2-1);
var c2=r*Math.SQRT2/2;
this.lineStyle(1,0);
this.beginFill(col,alpha);
this.moveTo(x+r,y);
this.curveTo(x+r,y+c1,x+c2,y+c2);
this.curveTo(x+c1,y+r,x,y+r);
this.curveTo(x-c1,y+r,x-c2,y+c2);
this.curveTo(x-r,y+c1,x-r,y);
this.curveTo(x-r,y-c1,x-c2,y-c2);
this.curveTo(x-c1,y-r,x,y-r);
this.curveTo(x+c1,y-r,x+c2,y-c2);
this.curveTo(x+r,y-c1,x+r,y);
this.endFill();
return this;
}
// draw a box for the handle - lines and color hardcoded
MovieClip.prototype.drawHandle = function(x,y,r){
this.lineStyle(1,0);
this.beginFill(0xffff00,75);
this.moveTo(x-r,y-r);
this.lineTo(x+r,y-r);
this.lineTo(x+r,y+r);
this.lineTo(x-r,y+r);
this.lineTo(x-r,y-r);
this.endFill();
return this;
}
// sets a linestyle for a clip, if no arguments are passed
// a saved linestyle is set based on the last set style
// why? because if you clear, linestyle is lost, now you can
// easily reset to the last style
MovieClip.prototype.setLineStyle = function(thick, col, alpha){
if (arguments.length) this.definedLineStyle = [thick, col, alpha];
this.lineStyle.apply(this, this.definedLineStyle);
}
// a moveTo which saves the point moved to in a p1 object
MovieClip.prototype.moveToPoint = function(x,y){
trace("original p1: (" + x + "," + y + ")");
this.p1 = {x:x, y:y}
this.moveTo(x,y);
}
// a lineTo which saves the point moved to in a p2 object
MovieClip.prototype.lineToPoint = function(x,y){
trace("original p2: (" + x + "," + y + ")");
this.p2 = {x:x, y:y}
this.lineTo(x,y);
}
// redraws the clip based on the saved lineStyle, moveTo
// and lineTo variables
MovieClip.prototype.redraw = function(){
trace("function redraw()");
this.clear() // clear
this.setLineStyle(5,0x000000,100); // no argument so sets it to the saved style
//this.moveTo(this.p1.x, this.p1.y); // draw
//this.lineTo(this.p2.x, this.p2.y);
this.moveTo(this.p1.x, this.p1.y); // draw
this.lineTo(this.p2.x, this.p2.y);
}
MovieClip.prototype.moveLine = function(){
this.clear(); // clear
this.setLineStyle(2,0,20); // no argument so sets it to the saved style
//new p1 pos = current mouse pos - (old coodrinate - current mouse pos)
//works
this.p1.x = xMouse - (this.p1.x - xMouse);
this.p1.y = yMouse - (this.p1.y - yMouse);
this.p2.x = xMouse - (this.p2.x - xMouse);
this.p2.y = yMouse - (this.p2.y - yMouse);
updateAfterEvent();
this.moveTo(this.p1.x, this.p1.y); // draw
this.lineTo(this.p2.x, this.p2.y);
}
// adds 2 handles for each of the two points on a line. These
// are added to the ornaments clip so not to interfere with lines
MovieClip.prototype.addHandles = function(){
//===========================
//Add point adjustment handles
// created and draw first handle
curLine = this
p1handle = ornaments.createEmptyMovieClip("p1handle",1).drawHandle(0,0,5);
p1handle._x = curLine.p1.x;
p1handle._y = curLine.p1.y; // set position on screen
p1handle.pt = curLine.p1; // p1 handle's point is the p1 of this line
// assign drag functions
p1handle.onPress = setDrag;
p1handle.onMouseMove = onDrag
p1handle.onRelease = p1handle.onReleaseOutside = unSetDrag;
// created and draw second handle
p2handle = ornaments.createEmptyMovieClip("p2handle",2).drawHandle(0,0,5);
p2handle._x = curLine.p2.x; p2handle._y = curLine.p2.y; // set position on screen
p2handle.pt = curLine.p2; // p2 handle's point is the p2 of this line
// assign drag functions
p2handle.onPress = setDrag;
p2handle.onMouseMove = onDrag
p2handle.onRelease = p2handle.onReleaseOutside = unSetDrag;
//==========================
//Add drag line handle for dragging of whole line
p3handle = ornaments.createEmptyMovieClip("p3handle",3).createCircHandle(0,0,10,0xffff00,75);
//work out center of line to place handle
lineCentreXpos = (curLine.p2.x + curLine.p1.x)/2;
lineCentreYpos = (curLine.p2.y + curLine.p1.y)/2;
p3handle._x = lineCentreXpos;
p3handle._y = lineCentreYpos;
p3handle.pt = {x:lineCentreXpos, y:lineCentreYpos};//store handles coord's in obj
p3handle.onPress = onDragLine;
//p3handle.onMouseMove = onDragLine;
p3handle.onRelease = p3handle.onReleaseOutside = unSetDragLine;
}
// removes the handle clips from the ornaments layer
// (currently not used, but would be used to go back to
// a drawing tool or something of the such)
MovieClip.prototype.removeHandles = function(){
ornaments.p1handle.removeMovieClip();
ornaments.p2handle.removeMovieClip();
}
// MAIN: =====================================================================
// create two lines to play with, here, drawing is hardcoded.
l1 = lines.createEmptyMovieClip("line1",1);
trace("lines ORIGINAL coordinates: (" + lines._x + ", " + lines._y + ")");
//l1.p1 = {x:l1._x, y:l1._y};
l1.setLineStyle(5,0xff0000,100);
l1.moveToPoint(100,100);
l1.lineToPoint(200,200);
l1.onPress = function(){
this.addHandles();
}
/*
l2 = lines.createEmptyMovieClip("line2",2);
l2.setLineStyle(5,0,100);
l2.moveToPoint(100,0);
l2.lineToPoint(200,100);
l2.onPress = function(){
trace("l2****");
this.addHandles();
}
*/