home *** CD-ROM | disk | FTP | other *** search
- package com.livebrush.tools
- {
- import com.livebrush.data.StateManager;
- import com.livebrush.events.UpdateEvent;
- import com.livebrush.graphics.Edge;
- import com.livebrush.graphics.GraphicUpdate;
- import com.livebrush.graphics.Line;
- import com.livebrush.graphics.SmoothLine;
- import com.livebrush.graphics.canvas.Canvas;
- import com.livebrush.graphics.canvas.Layer;
- import com.livebrush.graphics.canvas.LineLayer;
- import com.livebrush.transform.EdgeView;
- import com.livebrush.transform.LineWireframeView;
- import com.livebrush.transform.SingleEdgeView;
- import com.livebrush.transform.SyncPoint;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.geom.Point;
- import flash.utils.clearTimeout;
- import flash.utils.setTimeout;
-
- public class PenTool extends Tool
- {
- public static const NAME:String = "penTool";
-
- public static const KEY:String = "P";
-
- public static const ADD_OPEN:String = "addOpen";
-
- public static const ADD_INSIDE:String = "addInside";
-
- public static const ADD_OUTSIDE:String = "addOutside";
-
- private var lastNewEdge:Edge;
-
- private var liveEdgeIndex:int = 0;
-
- private var wireframeView:LineWireframeView;
-
- private var lastSingleEdgeView:SingleEdgeView;
-
- private var timeout:int;
-
- private var addMode:String = "addOpen";
-
- private var lastNewEdgeAngleRads:Number;
-
- private var singleEdgeView:SingleEdgeView;
-
- private var updateNewEdge:Boolean = false;
-
- public var edgeViews:Array;
-
- public var updateDelay:int = 1000;
-
- private var newLine:Line;
-
- private var newEdge:Edge;
-
- public function PenTool(toolMan:ToolManager)
- {
- super(toolMan);
- this.edgeViews = [];
- this.init();
- }
-
- override public function setup() : void
- {
- var i:int = 0;
- super.setup();
- if(activeLayers.length == 1 && (activeLayer is LineLayer && !Layer.isBitmapLayer(activeLayer)) && LineLayer(activeLayer).line.length > 0)
- {
- for(i = 0; i < LineLayer(activeLayer).line.length; i++)
- {
- this.edgeViews.push(registerView(new EdgeView(this,LineLayer(activeLayer),i)));
- }
- this.wireframeView = LineWireframeView(registerView(new LineWireframeView(this,LineLayer(activeLayer).line)));
- }
- views.reverse();
- update(GraphicUpdate.layerUpdate());
- }
-
- override protected function openCanvasMouseDown(e:MouseEvent) : void
- {
- var layer:LineLayer = null;
- this.resetUpdateTimeout(false);
- begin();
- if(!Layer.isLineLayer(activeLayer) || Layer.isBitmapLayer(activeLayer))
- {
- layer = canvasManager.addLayer(Layer.newLineLayer(canvas)) as LineLayer;
- layer.line = Line.newLine(styleManager.activeStyle.lineStyle.smoothing,styleManager.activeStyle.strokeStyle.strokeType,styleManager.activeStyle.strokeStyle.lines,styleManager.activeStyle.strokeStyle.weight);
- this.setNewLine(false);
- this.addNewEdge(false);
- }
- else if(activeLayer is LineLayer)
- {
- layer = activeLayer as LineLayer;
- if(layer.line.length == 0)
- {
- this.setNewLine(true);
- this.addNewEdge(false);
- }
- else if(this.newLine != null)
- {
- this.addNewEdge(true);
- }
- else
- {
- this.setNewLine(true);
- this.liveEdgeIndex = 0;
- if(layer.line.length > 0)
- {
- this.liveEdgeIndex = this.getClosestEdgeIndex(Canvas.MOUSE_POINT);
- }
- this.addNewEdge(true,true);
- this.addNewEdge(true);
- }
- }
- if(this.wireframeView != null)
- {
- unregisterView(this.wireframeView);
- this.wireframeView.die();
- }
- this.wireframeView = LineWireframeView(registerView(new LineWireframeView(this,this.newLine)));
- this.singleEdgeView = SingleEdgeView(registerView(new SingleEdgeView(this,layer,this.newEdge,false)));
- this.updateNewEdge = true;
- }
-
- override protected function controlsMouseHandler(e:MouseEvent) : void
- {
- this.canvasMouseUp(e);
- }
-
- override protected function die() : void
- {
- super.die();
- clearTimeout(this.timeout);
- if(this.newLine != null)
- {
- this.newLine.die();
- this.newLine = null;
- this.newEdge = null;
- }
- this.edgeViews = [];
- }
-
- private function init() : void
- {
- name = NAME;
- }
-
- public function addEdgeAt(layer:LineLayer, index:int, pt:Point = null) : void
- {
- if(this.addMode == ADD_OPEN)
- {
- layer.line.addEdgeAt(index,pt);
- this.reset();
- this.setup();
- layerUpdate();
- StateManager.closeState();
- }
- }
-
- override protected function enterFrameHandler(e:Event) : void
- {
- var angleRads:Number = NaN;
- var newEdgeWidth:Number = NaN;
- var dy:Number = NaN;
- var dx:Number = NaN;
- var angleRads2:* = undefined;
- if(this.updateNewEdge)
- {
- angleRads = canvas.angleRads;
- newEdgeWidth = 25;
- if(this.newLine.length > 1)
- {
- dy = canvas.mousePt.y - this.lastNewEdge.c.y;
- dx = canvas.mousePt.x - this.lastNewEdge.c.x;
- angleRads = Math.atan2(dy,dx);
- newEdgeWidth = this.newEdge.width;
- this.newEdge.modify(canvas.mousePt,angleRads,newEdgeWidth);
- if(LineLayer(activeLayer).line.length == 0)
- {
- newEdgeWidth = this.lastNewEdge.width;
- this.lastNewEdge.modify(this.lastNewEdge.c,angleRads,newEdgeWidth);
- }
- else if(this.newLine.length > 2)
- {
- newEdgeWidth = this.lastNewEdge.width;
- dy = canvas.mousePt.y - this.newLine.edges[this.newLine.edges.length - 3].c.y;
- dx = canvas.mousePt.x - this.newLine.edges[this.newLine.edges.length - 3].c.x;
- angleRads2 = Math.atan2(dy,dx);
- this.lastNewEdge.modify(this.lastNewEdge.c,angleRads2,newEdgeWidth);
- }
- this.newLine.invalidateEdge(this.lastNewEdge);
- this.newLine.invalidateEdge(this.newEdge);
- }
- else
- {
- newEdgeWidth = this.newEdge.width;
- this.newEdge.modify(canvas.mousePt,Math.PI,newEdgeWidth);
- }
- this.newLine.applyProps();
- clearWireframes();
- if(this.newLine.length == 2 && LineLayer(activeLayer).line.length < 2)
- {
- this.lastNewEdge.modify(this.lastNewEdge.c,angleRads,this.lastNewEdge.width);
- }
- this.singleEdgeView.edge.modify(canvas.mousePt,angleRads,newEdgeWidth);
- toolUpdate();
- this.wireframeView.update(new GraphicUpdate(UpdateEvent.LINE));
- }
- }
-
- override public function reset() : void
- {
- super.reset();
- Canvas.WIREFRAME.graphics.clear();
- this.liveEdgeIndex = 0;
- this.addMode = ADD_OPEN;
- this.die();
- }
-
- public function transformEdge(upObj:Object, updateNow:Boolean = true) : void
- {
- var fromScope:Sprite = upObj.fromScope == null ? Canvas.WIREFRAME : upObj.fromScope;
- var layer:LineLayer = upObj.layer;
- var edgeIndex:int = int(upObj.index);
- var c:Object = upObj.c;
- var a:Object = upObj.a;
- var b:Object = upObj.b;
- if(fromScope != Canvas.WIREFRAME)
- {
- layer.line.modifyEdge(edgeIndex,SyncPoint.localToLocal(SyncPoint.objToPoint(c),fromScope,Canvas.WIREFRAME),SyncPoint.localToLocal(SyncPoint.objToPoint(a),fromScope,Canvas.WIREFRAME),SyncPoint.localToLocal(SyncPoint.objToPoint(b),fromScope,Canvas.WIREFRAME));
- this.wireframeView.wireLine.modifyEdge(edgeIndex,SyncPoint.localToLocal(SyncPoint.objToPoint(c),fromScope,Canvas.WIREFRAME),SyncPoint.localToLocal(SyncPoint.objToPoint(a),fromScope,Canvas.WIREFRAME),SyncPoint.localToLocal(SyncPoint.objToPoint(b),fromScope,Canvas.WIREFRAME));
- }
- else
- {
- layer.line.modifyEdge(edgeIndex,new Point(c.x,c.y),new Point(a.x,a.y),new Point(b.x,b.y));
- this.wireframeView.wireLine.modifyEdge(edgeIndex,new Point(c.x,c.y),new Point(a.x,a.y),new Point(b.x,b.y));
- }
- if(updateNow)
- {
- layerUpdate();
- }
- }
-
- private function addNewEdge(usePrevious:Boolean, copyFromRealLine:Boolean = false) : void
- {
- var dy:Number = NaN;
- var dx:Number = NaN;
- var angleRads:Number = NaN;
- if(usePrevious)
- {
- if(copyFromRealLine)
- {
- this.newEdge = LineLayer(activeLayer).line.edges[this.liveEdgeIndex].copy(false,true);
- if(this.liveEdgeIndex == 0)
- {
- this.newEdge.invert();
- }
- }
- else
- {
- this.lastNewEdge = this.newEdge;
- this.newEdge = this.lastNewEdge.copy();
- dy = Canvas.Y - this.lastNewEdge.c.y;
- dx = Canvas.X - this.lastNewEdge.c.x;
- angleRads = Math.atan2(dy,dx);
- }
- }
- else
- {
- this.newEdge = new Edge(Canvas.X,Canvas.Y,25,90,this.newLine.lines,16711680,this.newLine.weight,null);
- }
- this.newLine.addEdge(this.newEdge);
- }
-
- private function applyNewLine(delay:Boolean = false) : void
- {
- var edge:Edge = null;
- var existingLineEdge:Edge = null;
- var layer:LineLayer = activeLayer as LineLayer;
- var line:Line = layer.line;
- var lineXML:XML = line.getXML();
- if(line.length > 0)
- {
- existingLineEdge = line.edges[this.liveEdgeIndex];
- }
- else
- {
- existingLineEdge = this.newEdge;
- }
- for(var i:int = 0; i < this.newLine.length; i++)
- {
- edge = this.newLine.edges[i];
- edge.alpha = existingLineEdge.alpha;
- edge.lines = existingLineEdge.lines;
- edge.applyProps();
- }
- this.newLine.type = line.type;
- this.newLine.lines = line.lines;
- this.newLine.applyProps();
- if(line.length == 1)
- {
- line.edges[0].modify(line.edges[0].c,this.newLine.edges[0].angleRads,line.edges[0].length);
- this.newLine.edges.shift();
- }
- else if(line.length > 1)
- {
- this.newLine.edges.shift();
- }
- if(line.length > 1 && this.liveEdgeIndex == 0)
- {
- for(i = 0; i < this.newLine.edges.length; i++)
- {
- this.newLine.edges[i].invert();
- }
- this.newLine.edges.reverse();
- line.edges = this.newLine.edges.concat(line.edges);
- }
- else
- {
- line.edges = line.edges.concat(this.newLine.edges);
- }
- LineLayer(activeLayer).line.rebuild();
- this.reset();
- layerUpdate(delay);
- finish(delay);
- this.setup();
- }
-
- private function resetUpdateTimeout(restart:Boolean = true) : void
- {
- clearTimeout(this.timeout);
- if(restart)
- {
- this.timeout = setTimeout(this.applyNewLine,this.updateDelay);
- }
- }
-
- override protected function contentMouseDown(e:MouseEvent) : void
- {
- if(!Layer.isLineLayer(activeLayer))
- {
- this.openCanvasMouseDown(e);
- }
- }
-
- private function setNewLine(useExisting:Boolean) : void
- {
- var layer:LineLayer = activeLayer as LineLayer;
- if(useExisting)
- {
- this.newLine = Line.newLine(layer.line is SmoothLine,layer.line.type,layer.line.lines,layer.line.weight);
- }
- else
- {
- this.newLine = Line.newLine(styleManager.activeStyle.lineStyle.smoothing,styleManager.activeStyle.strokeStyle.strokeType,styleManager.activeStyle.strokeStyle.lines,styleManager.activeStyle.strokeStyle.weight);
- }
- this.addMode = ADD_OUTSIDE;
- }
-
- override protected function canvasMouseUp(e:MouseEvent) : void
- {
- if(this.updateNewEdge)
- {
- this.updateNewEdge = false;
- unregisterView(this.singleEdgeView);
- this.singleEdgeView.die();
- this.singleEdgeView = null;
- if(this.updateDelay == 0)
- {
- this.resetUpdateTimeout(false);
- this.applyNewLine();
- }
- else
- {
- this.resetUpdateTimeout(true);
- }
- if(LineLayer(activeLayer).line.length <= 0)
- {
- this.applyNewLine();
- }
- }
- }
-
- private function getClosestEdgeIndex(pt:Point) : int
- {
- var closeEdge:int = 0;
- var dist:Number = Point.distance(this.edgeViews[0].edge.c,pt);
- if(Point.distance(this.edgeViews[this.edgeViews.length - 1].edge.c,pt) < dist)
- {
- closeEdge = int(this.edgeViews.length - 1);
- }
- return closeEdge;
- }
- }
- }
-
-