Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var snap = true;
- var dashlength = 0;
- var dashspacing = 0;
- var joinPoints = true;
- var done = true;
- var almostDone = 0;
- //////////////////////////////////////////////
- // function configureTool
- // Flash callback function.
- // Each tool must have a configureTool function.
- // This function is called by Flash when the
- // application launches
- //////////////////////////////////////////////
- function configureTool()
- {
- // Set the standard tool information
- theTool = fl.tools.activeTool;
- theTool.setToolName("pxLine");
- theTool.setIcon("pxLine.png");
- theTool.setMenuString("Pixel Line Tool");
- theTool.setToolTip("Pixel Line Tool");
- theTool.setOptionsFile( "pxLine.xml" );
- // This tool uses the Shape property inspector
- theTool.setPI( "shape" );
- }
- //////////////////////////////////////////////
- // function notifySettingsChanged
- // Flash callback function
- // Called when the tool options are
- // changed by the user
- //////////////////////////////////////////////
- function notifySettingsChanged()
- {
- // the current values of the properties
- // for this tool are held by the activeTool object.
- theTool = fl.tools.activeTool;
- // update our local values of the properties
- // from the active tool object.
- // These properties are defined in the xml file for the tool.
- // Minimum and maximum values for these properties are set in the
- // xml file, so we accept the values without checking.
- snap = theTool.snap;
- dashlength = theTool.dashlength;
- dashspacing = theTool.dashspacing;
- }
- //////////////////////////////////////////////
- // function setCursor
- // Flash callback function
- // Set the cursor to display
- //////////////////////////////////////////////
- function setCursor()
- {
- // cursor 0 is the '+' cursor
- fl.tools.setCursor( 2 );
- }
- //////////////////////////////////////////////
- // function activate
- // Flash callback function
- // Called by Flash whenever the
- // user selects the tool
- //////////////////////////////////////////////
- function activate()
- {
- //Set the snap array
- // snapArray = [-3.142,-2.678,-2.356,-2.034,-1.571,-1.107,-0.785,-0.464,0,
- // 0.464,0.785,1.107,1.571,2.034,2.356,2.678,3.142];
- //Theses correcpond to 45 degrees multiples and 2:1 iso ratios
- snapArray = [-3.142,-2.678,-2.356,-2.034,-1.571,-1.108,-0.785,-0.4634,0,
- 0.4634,0.785,1.108,1.571,2.034,2.356,2.678,3.142];
- fl.drawingLayer.beginDraw();
- }
- //////////////////////////////////////////////
- // function deactivate
- // Flash callback function
- // Called when the active tool switches from
- // this tool to another tool
- //////////////////////////////////////////////
- function deactivate()
- {
- fl.drawingLayer.endDraw();
- }
- //////////////////////////////////////////////
- // function mouseDown
- // Flash callback function
- // Called when the user presses the mouse button
- // in the workspace when this tool is active
- //////////////////////////////////////////////
- function mouseDown(){
- var stroke = fl.getDocumentDOM().getCustomStroke("toolbar");
- thick = Math.max(1, Math.round(stroke.thickness));
- stPoint = fl.tools.penDownLoc;
- roundOff(stPoint);
- }
- //////////////////////////////////////////////
- // function mouseMove
- // Flash callback function
- // Called when the user moves the mouse
- //////////////////////////////////////////////
- function mouseMove(){
- if(done)
- {
- if(fl.tools.mouseIsDown){
- fl.drawingLayer.beginFrame();
- var penPoint = fl.tools.penLoc;
- constrainPrincipal(stPoint, penPoint);
- var line = buildLine(stPoint, penPoint);
- drawLine(line);
- fl.drawingLayer.endFrame();
- }
- else
- {
- drawPreview();
- }
- }
- else
- {
- fl.drawingLayer.beginFrame();
- fl.drawingLayer.endFrame();
- }
- if(almostDone > 0)
- {
- almostDone++;
- if(almostDone > 5)
- {
- almostDone = 0;
- done = true;
- }
- }
- }
- function drawPixels()
- {
- var v = fl.getDocumentDOM().viewMatrix;
- var e = v.tx;
- var f = v.ty;
- for(i = 0; i < pixels.length; i++)
- {
- var p = pixels[i];
- var a1 = p.x*v.a + e;
- var b1 = p.y*v.c;
- var c1 = p.x*v.b + f;
- var d1 = p.y*v.d;
- var a2 = a1 + thick*v.a;
- var b2 = b1 + thick*v.c;
- var c2 = c1 + thick*v.b;
- var d2 = d1 + thick*v.d;
- /*
- // This one too is numerically unstable
- var a2 = (p.x + thick)*v.a + e;
- var b2 = (p.y + thick)*v.c;
- var c2 = (p.x + thick)*v.b + f;
- var d2 = (p.y + thick)*v.d;
- */
- fl.drawingLayer.moveTo(a1+b1, c1+d1);
- fl.drawingLayer.lineTo(a1+b2, c1+d2);
- fl.drawingLayer.lineTo(a2+b2, c2+d2);
- fl.drawingLayer.lineTo(a2+b1, c2+d1);
- fl.drawingLayer.lineTo(a1+b1, c1+d1);
- fl.drawingLayer.moveTo(a1+b1, c1+d1);
- fl.drawingLayer.lineTo(a2+b2, c2+d2);
- fl.drawingLayer.moveTo(a1+b2, c1+d2);
- fl.drawingLayer.lineTo(a2+b1, c2+d1);
- }
- }
- function mouseUp(){
- done = false;
- almostDone = 0;
- var endPoint = fl.tools.penLoc;
- endPoint = fl.tools.snapPoint(endPoint);
- constrainPrincipal(stPoint, endPoint);
- var line = buildLine(stPoint, endPoint);
- drawLineOnStage(line);
- almostDone = 1;
- fl.drawingLayer.beginFrame();
- fl.drawingLayer.endFrame();
- }
- function drawPreview()
- {
- thick = 1;
- var penPoint = fl.tools.penLoc;
- roundOff(penPoint);
- var v = fl.getDocumentDOM().viewMatrix;
- var e = v.tx;
- var f = v.ty;
- var p = penPoint;
- var a1 = p.x*v.a + e;
- var b1 = p.y*v.c;
- var c1 = p.x*v.b + f;
- var d1 = p.y*v.d;
- var a2 = a1 + thick*v.a;
- var b2 = b1 + thick*v.c;
- var c2 = c1 + thick*v.b;
- var d2 = d1 + thick*v.d;
- fl.drawingLayer.beginFrame();
- fl.drawingLayer.moveTo(a1+b1, c1+d1);
- fl.drawingLayer.lineTo(a1+b2, c1+d2);
- fl.drawingLayer.lineTo(a2+b2, c2+d2);
- fl.drawingLayer.lineTo(a2+b1, c2+d1);
- fl.drawingLayer.lineTo(a1+b1, c1+d1);
- fl.drawingLayer.endFrame();
- fl.tools.setCursor(2);
- }
- function constrainPrincipal(p1, p2){
- var sn = snap;
- if(fl.tools.shiftIsDown)
- {
- sn = !sn;
- }
- if (sn)
- {
- var dx = (p2.x - p1.x);
- var dy = (p2.y - p1.y);
- closeI = findClosest(Math.atan2(dy,dx), snapArray);
- closeAng = snapArray[closeI];
- if(Math.abs(dy) > Math.abs(dx))
- {
- p2.x = p1.x + dy/Math.tan(closeAng);
- }
- else
- {
- p2.y = p1.y + dx*Math.tan(closeAng);
- }
- }
- }
- function findClosest(val, arr)
- {
- var d = 0;
- var lowD = 5;
- for(i = 0; i < arr.length; i++)
- {
- d = Math.abs(arr[i] - val);
- if(d < lowD)
- {
- closest = i;
- lowD = d;
- }
- }
- return closest;
- }
- function transformPoint(aPoint, matrix){
- var x = aPoint.x * matrix.a + aPoint.y * matrix.c + matrix.tx;
- var y = aPoint.x * matrix.b + aPoint.y * matrix.d + matrix.ty;
- aPoint.x = x;
- aPoint.y = y;
- }
- function roundOff(point)
- {
- point.x = Math.floor(point.x);
- point.y = Math.floor(point.y);
- }
- function buildLine(from, to)
- {
- line = new Array();
- x1 = from.x;
- x2 = to.x;
- y1 = from.y;
- y2 = to.y;
- dx = x2 - x1;
- dy = y2 - y1;
- if(dy < 0)
- {
- y2 = Math.ceil(y2);
- }
- else
- {
- y2 = Math.floor(y2);
- }
- if(dx < 0)
- {
- x2 = Math.ceil(x2);
- }
- else
- {
- x2 = Math.floor(x2);
- }
- line.push({x:x1, y:y1});
- if (Math.abs(dx) > Math.abs(dy))
- { // slope < 1
- m = dy/dx; // compute slope
- b = y1 - m*x1;
- dx = (dx < 0) ? -1 : 1;
- var counter = 1;
- while (x1 != x2)
- {
- x1 += dx;
- //New dash code is here
- if(dashlength == 0 || ((counter % (dashlength + dashspacing)) < dashlength))
- {
- line.push({x:x1, y:Math.round(m*x1 + b)});
- }
- counter++;
- }
- }
- else if (dy != 0)
- { // slope >= 1
- m = dx/dy; // compute slope
- b = x1 - m*y1;
- dy = (dy < 0) ? -1 : 1;
- var counter = 1;
- while (y1 != y2)
- {
- y1 += dy;
- //New dash code is here
- if(dashlength == 0 || ((counter % (dashlength + dashspacing)) < dashlength))
- {
- line.push( {x:Math.round(m*y1 + b), y:y1} );
- }
- counter++;
- }
- }
- return line;
- }
- function drawLine(line)
- {
- var dy = line[line.length - 1].y - line[0].y;
- var dx = line[line.length - 1].x - line[0].x;
- var ang = Math.atan2(dy,dx);
- var a = thick;
- var b = 1;
- var c = thick;
- var d = 1;
- var v = fl.getDocumentDOM().viewMatrix;
- var e = v.tx;
- var f = v.ty;
- for(i = 0; i < line.length; i++)
- {
- var p = line[i];
- p.x -= Math.floor(c/2);
- p.y -= Math.floor(d/2);
- var a1 = p.x*v.a + e;
- var b1 = p.y*v.c;
- var c1 = p.x*v.b + f;
- var d1 = p.y*v.d;
- var a2 = a1 + a*v.a;
- var b2 = b1 + b*v.c;
- var c2 = c1 + c*v.b;
- var d2 = d1 + d*v.d;
- fl.drawingLayer.moveTo(a1+b1, c1+d1);
- fl.drawingLayer.lineTo(a1+b2, c1+d2);
- fl.drawingLayer.lineTo(a2+b2, c2+d2);
- fl.drawingLayer.lineTo(a2+b1, c2+d1);
- fl.drawingLayer.lineTo(a1+b1, c1+d1);
- }
- }
- function drawLineOnStage(line)
- {
- var path = fl.drawingLayer.newPath();
- var dy = line[line.length - 1].y - line[0].y;
- var dx = line[line.length - 1].x - line[0].x;
- var ang = Math.atan2(dy,dx);
- if( (ang > Math.PI/4 && ang < 3*Math.PI/4) ||
- (ang < -Math.PI/4 && ang > -3*Math.PI/4) )
- {
- a = thick;
- b = 1;
- c = Math.floor(thick/2);
- d = 0;
- }
- else
- {
- a = 1;
- b = thick;
- c = 0;
- d = Math.floor(thick/2);
- }
- //In order to play nice with the history panel, a single path is created
- //The problem is that depending on how the line is built, it happens that lines are drwan twice
- //Hence erasing the path after drawing it. This is why there are two ways used to draw the lines that
- //Start in a different corner
- if((line[line.length - 1].x - line[0].x > 0 && line[line.length - 1].y - line[0].y < 0) ||
- (line[line.length - 1].x - line[0].x < 0 && line[line.length - 1].y - line[0].y > 0))
- {
- for(i = 0; i < line.length; i++)
- {
- var p = line[i];
- p.x -= c;
- p.y -= d;
- path.addPoint(p.x, p.y+b);
- path.addPoint(p.x + a, p.y+b);
- path.addPoint(p.x + a, p.y );
- path.addPoint(p.x, p.y );
- path.addPoint(p.x, p.y +b);
- }
- }
- else
- {
- for(i = 0; i < line.length; i++)
- {
- var p = line[i];
- p.x -= c;
- p.y -= d;
- path.addPoint(p.x, p.y);
- path.addPoint(p.x + a, p.y);
- path.addPoint(p.x + a, p.y + b);
- path.addPoint(p.x, p.y + b);
- path.addPoint(p.x, p.y);
- }
- }
- path.makeShape(false, true);
- }
Advertisement
Add Comment
Please, Sign In to add comment