shape sketching

This commit is contained in:
Jeremy Penner 2011-05-02 21:22:02 -07:00
parent 91b0caf974
commit 0757363573
3 changed files with 115 additions and 17 deletions

View file

@ -16,14 +16,18 @@ package
public function ClickArea(rgpoint: Array, color:uint, alpha:Number = 1) public function ClickArea(rgpoint: Array, color:uint, alpha:Number = 1)
{ {
super(); super();
shape = MakeShape(rgpoint, color, alpha); shape = drawShape(new Shape(), rgpoint, color, null, alpha);
shapeHidden = MakeShape(rgpoint, color, 0); shapeHidden = drawShape(new Shape(), rgpoint, color, null, 0);
fHidden = false; fHidden = false;
addChild(shape); 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.clear();
if (rgpoint.length > 0)
{
if (colorLine != null)
shape.graphics.lineStyle(1, colorLine);
shape.graphics.beginFill(color, alpha); shape.graphics.beginFill(color, alpha);
var fFirstPoint:Boolean = true; var fFirstPoint:Boolean = true;
for each (var point:Point in rgpoint) for each (var point:Point in rgpoint)
@ -40,6 +44,7 @@ package
} }
shape.graphics.lineTo(rgpoint[0].x, rgpoint[0].y); shape.graphics.lineTo(rgpoint[0].x, rgpoint[0].y);
shape.graphics.endFill(); shape.graphics.endFill();
}
return shape; return shape;
} }
public function Show():void public function Show():void

View file

@ -12,9 +12,11 @@ package
public class Main extends Sprite public class Main extends Sprite
{ {
private var clickarea:ClickArea; private var clickarea:ClickArea;
private var sketchShape:SketchShape;
public function Main():void public function Main():void
{ {
clickarea = new ClickArea([new Point(50, 50), new Point(100, 80), new Point(30, 120)], 0x555555); clickarea = new ClickArea([new Point(50, 50), new Point(100, 80), new Point(30, 120)], 0x555555);
sketchShape = new SketchShape();
if (stage) init(); if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init); else addEventListener(Event.ADDED_TO_STAGE, init);
} }
@ -24,14 +26,20 @@ package
removeEventListener(Event.ADDED_TO_STAGE, init); removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point // entry point
addChild(clickarea); addChild(clickarea);
addChild(sketchShape);
addEventListener(MouseEvent.CLICK, onClick); addEventListener(MouseEvent.CLICK, onClick);
sketchShape.addEventListener(Event.COMPLETE, onShapeComplete);
} }
private function onClick(e: MouseEvent): void private function onClick(e: MouseEvent): void
{ {
if (clickarea.FHit(new Point(e.stageX, e.stageY))) if (clickarea.FHit(new Point(e.stageX, e.stageY)))
clickarea.Toggle(); clickarea.Toggle();
} }
private function onShapeComplete(e: Event): void
{
addChild(new ClickArea(sketchShape.rgpoint, 0x883388, 0.5));
sketchShape.clear();
}
} }
} }

85
src/SketchShape.as Normal file
View file

@ -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);
}
}
}