Guest User

Untitled

a guest
Feb 7th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var snap = true;
  2. var dashlength = 0;
  3. var dashspacing = 0;
  4.  
  5. var joinPoints = true;
  6. var done = true;
  7. var almostDone = 0;
  8.  
  9. //////////////////////////////////////////////
  10. // function configureTool
  11. // Flash callback function.
  12. // Each tool must have a configureTool function.
  13. // This function is called by Flash when the
  14. // application launches
  15. //////////////////////////////////////////////
  16. function configureTool()
  17. {
  18.     // Set the standard tool information
  19.     theTool = fl.tools.activeTool;
  20.     theTool.setToolName("pxLine");
  21.     theTool.setIcon("pxLine.png");
  22.     theTool.setMenuString("Pixel Line Tool");
  23.     theTool.setToolTip("Pixel Line Tool");
  24.     theTool.setOptionsFile( "pxLine.xml" );
  25.    
  26.     // This tool uses the Shape property inspector
  27.     theTool.setPI( "shape" );
  28. }
  29.  
  30. //////////////////////////////////////////////
  31. // function notifySettingsChanged
  32. // Flash callback function
  33. // Called when the tool options are
  34. // changed by the user
  35. //////////////////////////////////////////////
  36. function notifySettingsChanged()
  37. {
  38.     // the current values of the properties
  39.     // for this tool are held by the activeTool object.
  40.     theTool = fl.tools.activeTool;
  41.    
  42.     // update our local values of the properties
  43.     // from the active tool object.
  44.     // These properties are defined in the xml file for the tool.
  45.     // Minimum and maximum values for these properties are set in the
  46.     // xml file, so we accept the values without checking.
  47.     snap = theTool.snap;
  48.     dashlength = theTool.dashlength;
  49.     dashspacing = theTool.dashspacing;
  50. }
  51.  
  52. //////////////////////////////////////////////
  53. // function setCursor
  54. // Flash callback function
  55. // Set the cursor to display
  56. //////////////////////////////////////////////
  57. function setCursor()
  58. {
  59.     // cursor 0 is the '+' cursor
  60.     fl.tools.setCursor( 2 );
  61. }
  62.  
  63. //////////////////////////////////////////////
  64. // function activate
  65. // Flash callback function
  66. // Called by Flash whenever the
  67. // user selects the tool
  68. //////////////////////////////////////////////
  69. function activate()
  70. {
  71.     //Set the snap array
  72.     //  snapArray = [-3.142,-2.678,-2.356,-2.034,-1.571,-1.107,-0.785,-0.464,0,
  73.     //            0.464,0.785,1.107,1.571,2.034,2.356,2.678,3.142];
  74.     //Theses correcpond to 45 degrees multiples and 2:1 iso ratios
  75.     snapArray = [-3.142,-2.678,-2.356,-2.034,-1.571,-1.108,-0.785,-0.4634,0,
  76.                   0.4634,0.785,1.108,1.571,2.034,2.356,2.678,3.142];
  77.                  
  78.     fl.drawingLayer.beginDraw();
  79. }
  80.  
  81.  
  82. //////////////////////////////////////////////
  83. // function deactivate
  84. // Flash callback function
  85. // Called when the active tool switches from
  86. // this tool to another tool
  87. //////////////////////////////////////////////
  88. function deactivate()
  89. {
  90.     fl.drawingLayer.endDraw();
  91. }
  92.  
  93. //////////////////////////////////////////////
  94. // function mouseDown
  95. // Flash callback function
  96. // Called when the user presses the mouse button
  97. // in the workspace when this tool is active
  98. //////////////////////////////////////////////
  99. function mouseDown(){
  100.    
  101.     var stroke = fl.getDocumentDOM().getCustomStroke("toolbar");
  102.     thick = Math.max(1, Math.round(stroke.thickness));
  103.     stPoint = fl.tools.penDownLoc;
  104.     roundOff(stPoint);
  105. }
  106.  
  107.  
  108. //////////////////////////////////////////////
  109. // function mouseMove
  110. // Flash callback function
  111. // Called when the user moves the mouse
  112. //////////////////////////////////////////////
  113. function mouseMove(){
  114.     if(done)
  115.     {
  116.         if(fl.tools.mouseIsDown){
  117.                 fl.drawingLayer.beginFrame();
  118.                 var penPoint = fl.tools.penLoc;
  119.                 constrainPrincipal(stPoint, penPoint);
  120.                 var line = buildLine(stPoint, penPoint);
  121.                 drawLine(line);
  122.                 fl.drawingLayer.endFrame();
  123.         }
  124.         else
  125.         {
  126.             drawPreview();
  127.         }
  128.     }
  129.     else
  130.     {
  131.         fl.drawingLayer.beginFrame();
  132.         fl.drawingLayer.endFrame();
  133.     }
  134.    
  135.     if(almostDone > 0)
  136.     {
  137.         almostDone++;
  138.         if(almostDone > 5)
  139.         {
  140.             almostDone = 0;
  141.             done = true;
  142.         }
  143.     }
  144. }
  145.  
  146. function drawPixels()
  147. {
  148.     var v = fl.getDocumentDOM().viewMatrix;
  149.     var e = v.tx;
  150.     var f = v.ty;
  151.     for(i = 0; i < pixels.length; i++)
  152.     {
  153.         var p = pixels[i];
  154.         var a1 = p.x*v.a + e;
  155.         var b1 = p.y*v.c;
  156.         var c1 = p.x*v.b + f;
  157.         var d1 = p.y*v.d;
  158.        
  159.         var a2 = a1 + thick*v.a;
  160.         var b2 = b1 + thick*v.c;
  161.         var c2 = c1 + thick*v.b;
  162.         var d2 = d1 + thick*v.d;
  163.        
  164.         /*
  165.         // This one too is numerically unstable
  166.         var a2 = (p.x + thick)*v.a + e;
  167.         var b2 = (p.y + thick)*v.c;
  168.         var c2 = (p.x + thick)*v.b + f;
  169.         var d2 = (p.y + thick)*v.d;
  170.         */
  171.  
  172.         fl.drawingLayer.moveTo(a1+b1, c1+d1);
  173.         fl.drawingLayer.lineTo(a1+b2, c1+d2);
  174.         fl.drawingLayer.lineTo(a2+b2, c2+d2);
  175.         fl.drawingLayer.lineTo(a2+b1, c2+d1);
  176.         fl.drawingLayer.lineTo(a1+b1, c1+d1);
  177.        
  178.         fl.drawingLayer.moveTo(a1+b1, c1+d1);
  179.         fl.drawingLayer.lineTo(a2+b2, c2+d2);
  180.        
  181.         fl.drawingLayer.moveTo(a1+b2, c1+d2);
  182.         fl.drawingLayer.lineTo(a2+b1, c2+d1);
  183.     }
  184. }
  185.  
  186. function mouseUp(){
  187.    
  188.     done = false;
  189.     almostDone = 0;
  190.    
  191.     var endPoint = fl.tools.penLoc;
  192.     endPoint = fl.tools.snapPoint(endPoint);
  193.     constrainPrincipal(stPoint, endPoint);
  194.     var line = buildLine(stPoint, endPoint);
  195.     drawLineOnStage(line);
  196.    
  197.     almostDone = 1;
  198.    
  199.     fl.drawingLayer.beginFrame();
  200.     fl.drawingLayer.endFrame();
  201. }
  202.  
  203. function drawPreview()
  204. {  
  205.     thick = 1;
  206.  
  207.    
  208.    
  209.     var penPoint = fl.tools.penLoc;
  210.     roundOff(penPoint);
  211.        
  212.     var v = fl.getDocumentDOM().viewMatrix;
  213.     var e = v.tx;
  214.     var f = v.ty;
  215.  
  216.     var p = penPoint;
  217.    
  218.     var a1 = p.x*v.a + e;
  219.     var b1 = p.y*v.c;
  220.     var c1 = p.x*v.b + f;
  221.     var d1 = p.y*v.d;
  222.    
  223.     var a2 = a1 + thick*v.a;
  224.     var b2 = b1 + thick*v.c;
  225.     var c2 = c1 + thick*v.b;
  226.     var d2 = d1 + thick*v.d;
  227.    
  228.     fl.drawingLayer.beginFrame();
  229.    
  230.     fl.drawingLayer.moveTo(a1+b1, c1+d1);
  231.     fl.drawingLayer.lineTo(a1+b2, c1+d2);
  232.     fl.drawingLayer.lineTo(a2+b2, c2+d2);
  233.     fl.drawingLayer.lineTo(a2+b1, c2+d1);
  234.     fl.drawingLayer.lineTo(a1+b1, c1+d1);
  235.  
  236.     fl.drawingLayer.endFrame();
  237.     fl.tools.setCursor(2);
  238. }
  239.  
  240.  
  241. function constrainPrincipal(p1, p2){
  242.     var sn = snap;
  243.     if(fl.tools.shiftIsDown)
  244.     {
  245.         sn = !sn;
  246.     }
  247.    
  248.     if (sn)
  249.     {
  250.         var dx = (p2.x - p1.x);
  251.         var dy = (p2.y - p1.y);
  252.         closeI = findClosest(Math.atan2(dy,dx), snapArray);
  253.         closeAng = snapArray[closeI];
  254.         if(Math.abs(dy) > Math.abs(dx))
  255.         {
  256.             p2.x = p1.x + dy/Math.tan(closeAng);
  257.         }
  258.         else
  259.         {
  260.             p2.y = p1.y + dx*Math.tan(closeAng);
  261.         }
  262.     }
  263. }
  264.  
  265. function findClosest(val, arr)
  266. {
  267.     var d = 0;
  268.     var lowD = 5;
  269.     for(i = 0; i < arr.length; i++)
  270.     {
  271.         d = Math.abs(arr[i] - val);
  272.         if(d < lowD)
  273.         {
  274.             closest = i;
  275.             lowD = d;
  276.         }
  277.     }
  278.     return closest;
  279. }
  280.  
  281. function transformPoint(aPoint, matrix){
  282.     var x = aPoint.x * matrix.a + aPoint.y * matrix.c + matrix.tx;
  283.     var y = aPoint.x * matrix.b + aPoint.y * matrix.d + matrix.ty;
  284.     aPoint.x = x;
  285.     aPoint.y = y;
  286. }
  287.  
  288. function roundOff(point)
  289. {
  290.     point.x = Math.floor(point.x);
  291.     point.y = Math.floor(point.y);
  292. }
  293.  
  294. function buildLine(from, to)
  295. {
  296.     line = new Array();
  297.     x1 = from.x;
  298.     x2 = to.x;
  299.     y1 = from.y;
  300.     y2 = to.y;
  301.     dx = x2 - x1;
  302.     dy = y2 - y1;
  303.    
  304.     if(dy < 0)
  305.     {
  306.         y2 = Math.ceil(y2);
  307.     }
  308.     else
  309.     {
  310.         y2 = Math.floor(y2);
  311.     }
  312.    
  313.     if(dx < 0)
  314.     {
  315.         x2 = Math.ceil(x2);
  316.     }
  317.     else
  318.     {
  319.         x2 = Math.floor(x2);
  320.     }
  321.  
  322.     line.push({x:x1, y:y1});
  323.    
  324.     if (Math.abs(dx) > Math.abs(dy))
  325.     {                                           // slope < 1
  326.         m = dy/dx;      // compute slope
  327.         b = y1 - m*x1;
  328.         dx = (dx < 0) ? -1 : 1;
  329.         var counter = 1;
  330.         while (x1 != x2)
  331.         {
  332.             x1 += dx;
  333.             //New dash code is here
  334.             if(dashlength == 0 || ((counter % (dashlength + dashspacing)) < dashlength))
  335.             {
  336.                 line.push({x:x1, y:Math.round(m*x1 + b)});
  337.             }
  338.             counter++;
  339.         }
  340.     }
  341.     else if (dy != 0)
  342.     {                                           // slope >= 1
  343.         m = dx/dy;      // compute slope
  344.         b = x1 - m*y1;
  345.         dy = (dy < 0) ? -1 : 1;
  346.         var counter = 1;
  347.         while (y1 != y2)
  348.         {
  349.             y1 += dy;
  350.             //New dash code is here
  351.             if(dashlength == 0 || ((counter % (dashlength + dashspacing)) < dashlength))
  352.             {
  353.                 line.push( {x:Math.round(m*y1 + b), y:y1} );
  354.             }
  355.             counter++;
  356.         }
  357.     }
  358.     return line;
  359. }
  360.  
  361. function drawLine(line)
  362. {
  363.     var dy = line[line.length - 1].y - line[0].y;
  364.     var dx = line[line.length - 1].x - line[0].x;
  365.     var ang = Math.atan2(dy,dx);
  366.    
  367.     var a = thick;
  368.     var b = 1;
  369.     var c = thick;
  370.     var d = 1;
  371.    
  372.     var v = fl.getDocumentDOM().viewMatrix;
  373.     var e = v.tx;
  374.     var f = v.ty;
  375.     for(i = 0; i < line.length; i++)
  376.     {
  377.         var p = line[i];
  378.         p.x -= Math.floor(c/2);
  379.         p.y -= Math.floor(d/2);
  380.         var a1 = p.x*v.a + e;
  381.         var b1 = p.y*v.c;
  382.         var c1 = p.x*v.b + f;
  383.         var d1 = p.y*v.d;
  384.        
  385.         var a2 = a1 + a*v.a;
  386.         var b2 = b1 + b*v.c;
  387.         var c2 = c1 + c*v.b;
  388.         var d2 = d1 + d*v.d;
  389.  
  390.         fl.drawingLayer.moveTo(a1+b1, c1+d1);
  391.         fl.drawingLayer.lineTo(a1+b2, c1+d2);
  392.         fl.drawingLayer.lineTo(a2+b2, c2+d2);
  393.         fl.drawingLayer.lineTo(a2+b1, c2+d1);
  394.         fl.drawingLayer.lineTo(a1+b1, c1+d1);
  395.     }
  396. }
  397.  
  398. function drawLineOnStage(line)
  399. {
  400.     var path = fl.drawingLayer.newPath();
  401.  
  402.     var dy = line[line.length - 1].y - line[0].y;
  403.     var dx = line[line.length - 1].x - line[0].x;
  404.     var ang = Math.atan2(dy,dx);
  405.    
  406.    
  407.     if( (ang > Math.PI/4 && ang < 3*Math.PI/4) ||
  408.         (ang < -Math.PI/4 && ang > -3*Math.PI/4) )
  409.     {
  410.         a = thick;
  411.         b = 1;
  412.         c = Math.floor(thick/2);
  413.         d = 0;
  414.     }
  415.     else
  416.     {
  417.         a = 1;
  418.         b = thick;
  419.         c = 0;
  420.         d = Math.floor(thick/2);
  421.     }
  422.  
  423.     //In order to play nice with the history panel, a single path is created
  424.     //The problem is that depending on how the line is built, it happens that lines are drwan twice
  425.     //Hence erasing the path after drawing it. This is why there are two ways used to draw the lines that
  426.     //Start in a different corner
  427.     if((line[line.length - 1].x - line[0].x > 0 && line[line.length - 1].y - line[0].y < 0) ||
  428.        (line[line.length - 1].x - line[0].x < 0 && line[line.length - 1].y - line[0].y > 0))
  429.     {
  430.         for(i = 0; i < line.length; i++)
  431.         {
  432.             var p = line[i];
  433.            
  434.             p.x -= c;
  435.             p.y -= d;
  436.             path.addPoint(p.x, p.y+b);
  437.             path.addPoint(p.x + a, p.y+b);
  438.             path.addPoint(p.x + a, p.y );
  439.             path.addPoint(p.x, p.y );
  440.             path.addPoint(p.x, p.y +b);
  441.         }
  442.     }
  443.     else
  444.     {
  445.         for(i = 0; i < line.length; i++)
  446.         {
  447.             var p = line[i];
  448.             p.x -= c;
  449.             p.y -= d;
  450.             path.addPoint(p.x, p.y);
  451.             path.addPoint(p.x + a, p.y);
  452.             path.addPoint(p.x + a, p.y + b);
  453.             path.addPoint(p.x, p.y + b);
  454.             path.addPoint(p.x, p.y);
  455.         }
  456.     }
  457.     path.makeShape(false, true);
  458. }
Advertisement
Add Comment
Please, Sign In to add comment