From 0757363573fe289d6e7ac565eec582e9460ce83e Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Mon, 2 May 2011 21:22:02 -0700 Subject: [PATCH] shape sketching --- src/ClickArea.as | 37 +++++++++++--------- src/Main.as | 10 +++++- src/SketchShape.as | 85 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 src/SketchShape.as diff --git a/src/ClickArea.as b/src/ClickArea.as index 6632150..807c5c7 100644 --- a/src/ClickArea.as +++ b/src/ClickArea.as @@ -16,30 +16,35 @@ package public function ClickArea(rgpoint: Array, color:uint, alpha:Number = 1) { super(); - shape = MakeShape(rgpoint, color, alpha); - shapeHidden = MakeShape(rgpoint, color, 0); + shape = drawShape(new Shape(), rgpoint, color, null, alpha); + shapeHidden = drawShape(new Shape(), rgpoint, color, null, 0); fHidden = false; addChild(shape); } - private function MakeShape(rgpoint:Array, color:uint, alpha:Number):Shape + public static function drawShape(shape:Shape, rgpoint:Array, color:uint, colorLine:* = null, alpha:Number = 1):Shape { - var shape:Shape = new Shape(); - shape.graphics.beginFill(color, alpha); - var fFirstPoint:Boolean = true; - for each (var point:Point in rgpoint) + shape.graphics.clear(); + if (rgpoint.length > 0) { - if (fFirstPoint) + if (colorLine != null) + shape.graphics.lineStyle(1, colorLine); + shape.graphics.beginFill(color, alpha); + var fFirstPoint:Boolean = true; + for each (var point:Point in rgpoint) { - shape.graphics.moveTo(point.x, point.y); - fFirstPoint = false; - } - else - { - shape.graphics.lineTo(point.x, point.y); + if (fFirstPoint) + { + shape.graphics.moveTo(point.x, point.y); + fFirstPoint = false; + } + else + { + shape.graphics.lineTo(point.x, point.y); + } } + shape.graphics.lineTo(rgpoint[0].x, rgpoint[0].y); + shape.graphics.endFill(); } - shape.graphics.lineTo(rgpoint[0].x, rgpoint[0].y); - shape.graphics.endFill(); return shape; } public function Show():void diff --git a/src/Main.as b/src/Main.as index bbabb85..9786159 100644 --- a/src/Main.as +++ b/src/Main.as @@ -12,9 +12,11 @@ package public class Main extends Sprite { private var clickarea:ClickArea; + private var sketchShape:SketchShape; public function Main():void { clickarea = new ClickArea([new Point(50, 50), new Point(100, 80), new Point(30, 120)], 0x555555); + sketchShape = new SketchShape(); if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } @@ -24,14 +26,20 @@ package removeEventListener(Event.ADDED_TO_STAGE, init); // entry point addChild(clickarea); + addChild(sketchShape); addEventListener(MouseEvent.CLICK, onClick); + sketchShape.addEventListener(Event.COMPLETE, onShapeComplete); } private function onClick(e: MouseEvent): void { if (clickarea.FHit(new Point(e.stageX, e.stageY))) clickarea.Toggle(); } - + private function onShapeComplete(e: Event): void + { + addChild(new ClickArea(sketchShape.rgpoint, 0x883388, 0.5)); + sketchShape.clear(); + } } } \ No newline at end of file diff --git a/src/SketchShape.as b/src/SketchShape.as new file mode 100644 index 0000000..380240a --- /dev/null +++ b/src/SketchShape.as @@ -0,0 +1,85 @@ +package +{ + import flash.display.Shape; + import flash.display.Sprite; + import flash.events.Event; + import flash.events.MouseEvent; + import flash.geom.Point; + import flash.utils.getTimer; + /** + * ... + * @author jjp + */ + public class SketchShape extends Sprite + { + public var rgpoint: Array; + private var shape: Shape; + private var shapeLine: Shape; + private var msClickLast: int; + public function SketchShape() + { + super(); + rgpoint = []; + shape = new Shape(); + shapeLine = null; + msClickLast = -1; + addChild(shape); + addEventListener(Event.ADDED_TO_STAGE, init); + } + private function init(e: Event):void + { + stage.doubleClickEnabled = true; + stage.addEventListener(MouseEvent.CLICK, onClick); + stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove); + } + public function clear():void + { + rgpoint = []; + if (shapeLine !== null) + { + removeChild(shapeLine); + shapeLine = null; + } + redrawShape(); + } + private function onClick(e: MouseEvent):void + { + var msClickNow:int = getTimer(); + var dmsClick:int = msClickNow - msClickLast; + if (dmsClick < 200) + { + dispatchEvent(new Event(Event.COMPLETE)); + clear(); + } + else + { + if (shapeLine === null) + { + shapeLine = new Shape(); + addChild(shapeLine); + } + rgpoint.push(new Point(e.stageX, e.stageY)); + redrawShape(); + } + msClickLast = msClickNow; + } + private function onMove(e: MouseEvent):void + { + if (shapeLine !== null) + redrawLine(new Point(e.stageX, e.stageY)); + } + private function redrawShape():void + { + ClickArea.drawShape(shape, rgpoint, 0x555555, 0x000000, 0.1); + } + private function redrawLine(ptEnd:Point):void + { + var ptStart:Point = rgpoint[rgpoint.length - 1]; + shapeLine.graphics.clear(); + shapeLine.graphics.lineStyle(1); + shapeLine.graphics.moveTo(ptStart.x, ptStart.y); + shapeLine.graphics.lineTo(ptEnd.x, ptEnd.y); + } + } + +} \ No newline at end of file