2011-05-03 04:22:02 +00:00
|
|
|
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;
|
2011-05-03 16:20:28 +00:00
|
|
|
|
|
|
|
public static const DRAW_BEGIN:String = "draw-begin";
|
|
|
|
public static const DRAW_END:String = "draw-end";
|
|
|
|
|
2011-05-03 04:22:02 +00:00
|
|
|
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
|
|
|
|
{
|
2011-05-03 16:20:28 +00:00
|
|
|
removeEventListener(Event.ADDED_TO_STAGE, init);
|
2011-05-03 04:22:02 +00:00
|
|
|
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)
|
|
|
|
{
|
2011-05-03 16:20:28 +00:00
|
|
|
dispatchEvent(new Event(DRAW_END));
|
2011-05-03 04:22:02 +00:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (shapeLine === null)
|
|
|
|
{
|
|
|
|
shapeLine = new Shape();
|
|
|
|
addChild(shapeLine);
|
2011-05-03 16:20:28 +00:00
|
|
|
dispatchEvent(new Event(DRAW_BEGIN));
|
2011-05-03 04:22:02 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|