Advertisement
rodrigolopezpeker

DrawingTool - Eliminar ultimo lineTo

Apr 7th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Code by rodrigolopezpeker (aka 7interactive™) on 4/7/14 12:16 PM.
  3.  */
  4. package labs {
  5. import flash.display.GraphicsPath;
  6. import flash.display.IGraphicsData;
  7. import flash.display.Shape;
  8. import flash.display.Sprite;
  9. import flash.display.StageAlign;
  10. import flash.display.StageScaleMode;
  11. import flash.events.Event;
  12. import flash.events.KeyboardEvent;
  13. import flash.events.MouseEvent;
  14. import flash.ui.Keyboard;
  15.  
  16. [SWF(width="800", height="600", backgroundColor="#323232", frameRate="60")]
  17. public class TestDrawing extends Sprite {
  18.  
  19.     private var _shape:Shape;
  20.     private var isPressing:Boolean;
  21.  
  22.     public function TestDrawing() {
  23.         addEventListener(Event.ADDED_TO_STAGE, init);
  24.     }
  25.  
  26.     private function init(event:Event):void {
  27.         removeEventListener(Event.ADDED_TO_STAGE, init);
  28.         stage.scaleMode = StageScaleMode.NO_SCALE;
  29.         stage.align = StageAlign.TOP_LEFT;
  30.         stage.showDefaultContextMenu = false;
  31.         _shape = new Shape();
  32.         addChild(_shape);
  33.         stage.addEventListener(MouseEvent.MOUSE_DOWN, handleMousePress);
  34.         stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKey);
  35.     }
  36.  
  37.     private function handleKey(event:KeyboardEvent):void {
  38.         if (isPressing) return;
  39.         if (event.keyCode == Keyboard.BACKSPACE) {
  40.             removeLastLineTo();
  41.         }
  42.     }
  43.  
  44.     private function removeLastLineTo():void {
  45.         var gd:Vector.<IGraphicsData> = _shape.graphics.readGraphicsData(false);
  46.         var len:int = gd.length;
  47.         var lastPath:GraphicsPath;
  48.         for (var i:int = len - 1; i >= 0; i--) {
  49.             if (gd[i] is GraphicsPath) {
  50.                 lastPath = gd[i] as GraphicsPath;
  51.                 break;
  52.             }
  53.         }
  54.         if (!lastPath) return;
  55.         var lastLineToIndex:int = lastPath.commands.lastIndexOf(2);
  56.         if (lastLineToIndex > -1) {
  57.             lastPath.commands.splice(lastLineToIndex, 1);
  58.             lastPath.data.splice(lastPath.data.length - 2, 2);
  59.             // actualiza graphicData.
  60.             _shape.graphics.clear();
  61.             _shape.graphics.drawGraphicsData(gd);
  62.         }
  63.     }
  64.  
  65.     private function handleMousePress(event:MouseEvent):void {
  66.         isPressing = true;
  67.         stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseRelease);
  68.         stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
  69.         _shape.graphics.lineStyle(0, 0xff0000);
  70.         _shape.graphics.moveTo(mouseX, mouseY);
  71.     }
  72.  
  73.     private function handleMouseMove(event:MouseEvent):void {
  74.         _shape.graphics.lineTo(mouseX, mouseY);
  75.         event.updateAfterEvent();
  76.     }
  77.  
  78.     private function handleMouseRelease(event:MouseEvent):void {
  79.         isPressing = false;
  80.         stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseRelease);
  81.         stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
  82.     }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement