Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Code by rodrigolopezpeker (aka 7interactive™) on 4/7/14 12:16 PM.
- */
- package labs {
- import flash.display.GraphicsPath;
- import flash.display.IGraphicsData;
- import flash.display.Shape;
- import flash.display.Sprite;
- import flash.display.StageAlign;
- import flash.display.StageScaleMode;
- import flash.events.Event;
- import flash.events.KeyboardEvent;
- import flash.events.MouseEvent;
- import flash.ui.Keyboard;
- [SWF(width="800", height="600", backgroundColor="#323232", frameRate="60")]
- public class TestDrawing extends Sprite {
- private var _shape:Shape;
- private var isPressing:Boolean;
- public function TestDrawing() {
- addEventListener(Event.ADDED_TO_STAGE, init);
- }
- private function init(event:Event):void {
- removeEventListener(Event.ADDED_TO_STAGE, init);
- stage.scaleMode = StageScaleMode.NO_SCALE;
- stage.align = StageAlign.TOP_LEFT;
- stage.showDefaultContextMenu = false;
- _shape = new Shape();
- addChild(_shape);
- stage.addEventListener(MouseEvent.MOUSE_DOWN, handleMousePress);
- stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKey);
- }
- private function handleKey(event:KeyboardEvent):void {
- if (isPressing) return;
- if (event.keyCode == Keyboard.BACKSPACE) {
- removeLastLineTo();
- }
- }
- private function removeLastLineTo():void {
- var gd:Vector.<IGraphicsData> = _shape.graphics.readGraphicsData(false);
- var len:int = gd.length;
- var lastPath:GraphicsPath;
- for (var i:int = len - 1; i >= 0; i--) {
- if (gd[i] is GraphicsPath) {
- lastPath = gd[i] as GraphicsPath;
- break;
- }
- }
- if (!lastPath) return;
- var lastLineToIndex:int = lastPath.commands.lastIndexOf(2);
- if (lastLineToIndex > -1) {
- lastPath.commands.splice(lastLineToIndex, 1);
- lastPath.data.splice(lastPath.data.length - 2, 2);
- // actualiza graphicData.
- _shape.graphics.clear();
- _shape.graphics.drawGraphicsData(gd);
- }
- }
- private function handleMousePress(event:MouseEvent):void {
- isPressing = true;
- stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseRelease);
- stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
- _shape.graphics.lineStyle(0, 0xff0000);
- _shape.graphics.moveTo(mouseX, mouseY);
- }
- private function handleMouseMove(event:MouseEvent):void {
- _shape.graphics.lineTo(mouseX, mouseY);
- event.updateAfterEvent();
- }
- private function handleMouseRelease(event:MouseEvent):void {
- isPressing = false;
- stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseRelease);
- stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement