home *** CD-ROM | disk | FTP | other *** search
Wrap
package com.livebrush.graphics { import com.livebrush.data.Settings; import com.livebrush.data.Storable; import com.livebrush.graphics.canvas.Canvas; import com.livebrush.styles.DecoAsset; import com.livebrush.transform.SyncPoint; import flash.display.DisplayObjectContainer; import flash.events.EventDispatcher; import flash.geom.Point; public class Edge extends EventDispatcher implements Storable { public var deltaAngleRads:Number = 0; public var centerPt:Point; public var lineIndex:int; public var decoGroup:DecoGroup; public var points:Array; public var alpha:Number; public var length:Number; public var color:uint; private var _angleRads:Number; public var lines:int; public function Edge(x:Number, y:Number, length:Number, angle:Number, lines:int = 2, color:uint = 16777215, alpha:Number = 1, decoGroup:DecoGroup = null) { super(); this.points = []; this.centerPt = new Point(x,y); this.length = length; this.angle = angle; this.lines = lines; this.color = color; this.alpha = alpha; this.decoGroup = decoGroup; this.init(); } public static function interpolate(edge1:Edge, edge2:Edge, f:Number) : Edge { var iPt:Point = Point.interpolate(edge1.centerPt,edge2.centerPt,f); var edge:Edge = new Edge(iPt.x,iPt.y,(edge1.length + edge2.length) * f,(edge1.angle + edge2.angle) * f,edge1.lines,edge1.color,edge1.alpha,null); var points:Array = []; for(var i:int = 0; i < edge1.lines; i++) { points.push(Point.interpolate(edge1.points[i],edge2.points[i],f)); } edge.points = points; return edge; } public static function xmlToEdge(xml:String) : Edge { var decoGroup:DecoGroup = null; var deco:XML = null; var edgeXML:XML = new XML(xml); var edge:Edge = new Edge(Number(edgeXML.@x),Number(edgeXML.@y),Number(edgeXML.@width),Number(edgeXML.@angle),Number(edgeXML.@lines),Number(edgeXML.@color),Number(edgeXML.@alpha)); if(edgeXML.@decorate == "true") { decoGroup = new DecoGroup(); for each(deco in edgeXML.*) { decoGroup.addDecoObj(Deco.xmlToDeco(deco.toXMLString())); } edge.decoGroup = decoGroup; } return edge; } public function die() : void { } public function get c() : Point { return this.centerPt; } public function set y(n:Number) : void { this.centerPt.y = n; } public function set width(n:Number) : void { this.length = n; } private function init() : void { this.applyProps(); } public function modifyEdge(c:Point, angleRads:Number, width:Number, fromScope:DisplayObjectContainer = null) : void { fromScope = fromScope != null ? fromScope : Canvas.GLOBAL_CANVAS; c = SyncPoint.localToLocal(c,fromScope,Canvas.GLOBAL_CANVAS.comp); this.pos = c; this.width = width; this.deltaAngleRads = angleRads - this.angleRads; this.angleRads = angleRads; this.applyProps(); } public function applyProps() : void { var lineFraction:Number = NaN; var divPt:Point = null; var i:int = 0; this.points = []; var halfWidth:Number = this.length / 2; var t:Number = Math.PI / 2; var widthPt1:Point = this.centerPt.clone(); widthPt1.offset(halfWidth * Math.cos(this.angleRads - t),halfWidth * Math.sin(this.angleRads - t)); var widthPt2:Point = this.centerPt.clone(); widthPt2.offset(halfWidth * Math.cos(this.angleRads + t),halfWidth * Math.sin(this.angleRads + t)); for(i = 0; i < Math.max(2,this.lines); i++) { lineFraction = i / (Math.max(2,this.lines) - 1); lineFraction = isNaN(lineFraction) ? 0 : lineFraction; divPt = Point.interpolate(widthPt1,widthPt2,lineFraction); this.points.push(divPt); } } public function set angle(n:Number) : void { this.angleRads = n * Math.PI / 180; } public function set pos(pt:Point) : void { this.x = pt.x; this.y = pt.y; } public function setXMLDecoBool(xml:String, setDecoGroup:Boolean = false) : void { var deco:XML = null; var edgeXML:XML = new XML(xml); this.centerPt = new Point(Number(edgeXML.@x),Number(edgeXML.@y)); this.length = Number(edgeXML.@width); this.angle = Number(edgeXML.@angle); this.lines = Number(edgeXML.@lines); this.color = uint(edgeXML.@color); this.alpha = Number(edgeXML.@alpha); if(setDecoGroup) { if(this.decoGroup != null) { this.decoGroup.removeAllDecos(); } else { this.decoGroup = new DecoGroup(); } if(edgeXML.@decorate == "true") { for each(deco in edgeXML.*) { this.decoGroup.addDecoObj(Deco.xmlToDeco(deco.toXMLString())); } } } this.applyProps(); } public function midPoint(pos:Number) : Point { return Point.interpolate(this.a,this.b,pos); } public function modify(c:Point, angleRads:Number, width:Number, fromScope:DisplayObjectContainer = null) : void { this.modifyEdge(c,angleRads,width,fromScope); } public function removeDecos() : void { if(this.decoGroup != null) { this.decoGroup.removeAllDecos(); } } public function transformEdge(c:Point, a:Point, b:Point) : void { var xDist:Number = b.x - a.x; var yDist:Number = b.y - a.y; var angleRads:Number = Math.atan2(yDist,xDist) + Math.PI / 2; this.pos = c; this.width = Point.distance(a,b); this.deltaAngleRads += angleRads - this.angleRads; this.angleRads = angleRads; this.applyProps(); } public function get angle() : Number { return this.angleRads * 180 / Math.PI; } public function get pos() : Point { return this.centerPt; } public function invert() : void { var b:uint = this.angleRads; this.angleRads += Math.PI; this.applyProps(); } public function setXML(xml:String) : void { var deco:XML = null; var edgeXML:XML = new XML(xml); this.centerPt = new Point(Number(edgeXML.@x),Number(edgeXML.@y)); this.length = edgeXML.@width; this.angle = edgeXML.@angle; this.lines = edgeXML.@lines; this.color = edgeXML.@color; this.alpha = edgeXML.@alpha; this.decoGroup.removeAllDecos(); if(edgeXML.@decorate == "true") { for each(deco in edgeXML.*) { this.decoGroup.addDecoObj(Deco.xmlToDeco(deco.toXMLString())); } } this.applyProps(); } public function get width() : Number { return this.length; } public function getXML() : XML { var i:int = 0; var edgeXML:XML = new XML(<stroke x={this.x} y={this.y} width={this.length} color={this.color} alpha={this.alpha} angle={this.angle} decorate={this.hasDecos}/>); if(this.hasDecos) { for(i = 0; i < this.decoGroup.length; i++) { edgeXML.appendChild(this.decoGroup.getDeco(i).getXML()); } } return edgeXML; } public function get b() : Point { return this.points[this.points.length - 1]; } public function get a() : Point { return this.points[0]; } public function set angleRads(n:Number) : void { this._angleRads = n; } public function get hasDecos() : Boolean { return this.decoGroup != null; } public function addDeco(decoAsset:DecoAsset, initObj:Settings) : void { if(this.decoGroup == null) { this.decoGroup = new DecoGroup(); } this.decoGroup.addDeco(decoAsset,initObj); } public function get angleRads() : Number { return this._angleRads; } override public function toString() : String { return "Edge: x=" + this.x + ", y=" + this.y + ", length=" + this.length + ", angle=" + this.angle; } public function copy(includeDecos:Boolean = true, basic:Boolean = false) : Edge { var newEdge:Edge = null; if(basic) { newEdge = new Edge(this.centerPt.x,this.centerPt.y,this.length,this.angle,2,this.color,1,this.decoGroup != null && includeDecos ? this.decoGroup.copy() : null); } else { newEdge = new Edge(this.centerPt.x,this.centerPt.y,this.length,this.angle,this.lines,this.color,this.alpha,this.decoGroup != null && includeDecos ? this.decoGroup.copy() : null); } return newEdge; } public function set x(n:Number) : void { this.centerPt.x = n; } public function get x() : Number { return this.centerPt.x; } public function get y() : Number { return this.centerPt.y; } public function get deltaAngle() : Number { return this.deltaAngleRads * 180 / Math.PI; } } }