home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 2010 Software/Programs / PCGuia_programas.iso / Software / Utils / Livebrush / Install-LivebrushLite.air / livebrush.swf / scripts / com / livebrush / graphics / Edge.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  10.3 KB  |  339 lines

  1. package com.livebrush.graphics
  2. {
  3.    import com.livebrush.data.Settings;
  4.    import com.livebrush.data.Storable;
  5.    import com.livebrush.graphics.canvas.Canvas;
  6.    import com.livebrush.styles.DecoAsset;
  7.    import com.livebrush.transform.SyncPoint;
  8.    import flash.display.DisplayObjectContainer;
  9.    import flash.events.EventDispatcher;
  10.    import flash.geom.Point;
  11.    
  12.    public class Edge extends EventDispatcher implements Storable
  13.    {
  14.       public var deltaAngleRads:Number = 0;
  15.       
  16.       public var centerPt:Point;
  17.       
  18.       public var lineIndex:int;
  19.       
  20.       public var decoGroup:DecoGroup;
  21.       
  22.       public var points:Array;
  23.       
  24.       public var alpha:Number;
  25.       
  26.       public var length:Number;
  27.       
  28.       public var color:uint;
  29.       
  30.       private var _angleRads:Number;
  31.       
  32.       public var lines:int;
  33.       
  34.       public function Edge(x:Number, y:Number, length:Number, angle:Number, lines:int = 2, color:uint = 16777215, alpha:Number = 1, decoGroup:DecoGroup = null)
  35.       {
  36.          super();
  37.          this.points = [];
  38.          this.centerPt = new Point(x,y);
  39.          this.length = length;
  40.          this.angle = angle;
  41.          this.lines = lines;
  42.          this.color = color;
  43.          this.alpha = alpha;
  44.          this.decoGroup = decoGroup;
  45.          this.init();
  46.       }
  47.       
  48.       public static function interpolate(edge1:Edge, edge2:Edge, f:Number) : Edge
  49.       {
  50.          var iPt:Point = Point.interpolate(edge1.centerPt,edge2.centerPt,f);
  51.          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);
  52.          var points:Array = [];
  53.          for(var i:int = 0; i < edge1.lines; i++)
  54.          {
  55.             points.push(Point.interpolate(edge1.points[i],edge2.points[i],f));
  56.          }
  57.          edge.points = points;
  58.          return edge;
  59.       }
  60.       
  61.       public static function xmlToEdge(xml:String) : Edge
  62.       {
  63.          var decoGroup:DecoGroup = null;
  64.          var deco:XML = null;
  65.          var edgeXML:XML = new XML(xml);
  66.          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));
  67.          if(edgeXML.@decorate == "true")
  68.          {
  69.             decoGroup = new DecoGroup();
  70.             for each(deco in edgeXML.*)
  71.             {
  72.                decoGroup.addDecoObj(Deco.xmlToDeco(deco.toXMLString()));
  73.             }
  74.             edge.decoGroup = decoGroup;
  75.          }
  76.          return edge;
  77.       }
  78.       
  79.       public function die() : void
  80.       {
  81.       }
  82.       
  83.       public function get c() : Point
  84.       {
  85.          return this.centerPt;
  86.       }
  87.       
  88.       public function set y(n:Number) : void
  89.       {
  90.          this.centerPt.y = n;
  91.       }
  92.       
  93.       public function set width(n:Number) : void
  94.       {
  95.          this.length = n;
  96.       }
  97.       
  98.       private function init() : void
  99.       {
  100.          this.applyProps();
  101.       }
  102.       
  103.       public function modifyEdge(c:Point, angleRads:Number, width:Number, fromScope:DisplayObjectContainer = null) : void
  104.       {
  105.          fromScope = fromScope != null ? fromScope : Canvas.GLOBAL_CANVAS;
  106.          c = SyncPoint.localToLocal(c,fromScope,Canvas.GLOBAL_CANVAS.comp);
  107.          this.pos = c;
  108.          this.width = width;
  109.          this.deltaAngleRads = angleRads - this.angleRads;
  110.          this.angleRads = angleRads;
  111.          this.applyProps();
  112.       }
  113.       
  114.       public function applyProps() : void
  115.       {
  116.          var lineFraction:Number = NaN;
  117.          var divPt:Point = null;
  118.          var i:int = 0;
  119.          this.points = [];
  120.          var halfWidth:Number = this.length / 2;
  121.          var t:Number = Math.PI / 2;
  122.          var widthPt1:Point = this.centerPt.clone();
  123.          widthPt1.offset(halfWidth * Math.cos(this.angleRads - t),halfWidth * Math.sin(this.angleRads - t));
  124.          var widthPt2:Point = this.centerPt.clone();
  125.          widthPt2.offset(halfWidth * Math.cos(this.angleRads + t),halfWidth * Math.sin(this.angleRads + t));
  126.          for(i = 0; i < Math.max(2,this.lines); i++)
  127.          {
  128.             lineFraction = i / (Math.max(2,this.lines) - 1);
  129.             lineFraction = isNaN(lineFraction) ? 0 : lineFraction;
  130.             divPt = Point.interpolate(widthPt1,widthPt2,lineFraction);
  131.             this.points.push(divPt);
  132.          }
  133.       }
  134.       
  135.       public function set angle(n:Number) : void
  136.       {
  137.          this.angleRads = n * Math.PI / 180;
  138.       }
  139.       
  140.       public function set pos(pt:Point) : void
  141.       {
  142.          this.x = pt.x;
  143.          this.y = pt.y;
  144.       }
  145.       
  146.       public function setXMLDecoBool(xml:String, setDecoGroup:Boolean = false) : void
  147.       {
  148.          var deco:XML = null;
  149.          var edgeXML:XML = new XML(xml);
  150.          this.centerPt = new Point(Number(edgeXML.@x),Number(edgeXML.@y));
  151.          this.length = Number(edgeXML.@width);
  152.          this.angle = Number(edgeXML.@angle);
  153.          this.lines = Number(edgeXML.@lines);
  154.          this.color = uint(edgeXML.@color);
  155.          this.alpha = Number(edgeXML.@alpha);
  156.          if(setDecoGroup)
  157.          {
  158.             if(this.decoGroup != null)
  159.             {
  160.                this.decoGroup.removeAllDecos();
  161.             }
  162.             else
  163.             {
  164.                this.decoGroup = new DecoGroup();
  165.             }
  166.             if(edgeXML.@decorate == "true")
  167.             {
  168.                for each(deco in edgeXML.*)
  169.                {
  170.                   this.decoGroup.addDecoObj(Deco.xmlToDeco(deco.toXMLString()));
  171.                }
  172.             }
  173.          }
  174.          this.applyProps();
  175.       }
  176.       
  177.       public function midPoint(pos:Number) : Point
  178.       {
  179.          return Point.interpolate(this.a,this.b,pos);
  180.       }
  181.       
  182.       public function modify(c:Point, angleRads:Number, width:Number, fromScope:DisplayObjectContainer = null) : void
  183.       {
  184.          this.modifyEdge(c,angleRads,width,fromScope);
  185.       }
  186.       
  187.       public function removeDecos() : void
  188.       {
  189.          if(this.decoGroup != null)
  190.          {
  191.             this.decoGroup.removeAllDecos();
  192.          }
  193.       }
  194.       
  195.       public function transformEdge(c:Point, a:Point, b:Point) : void
  196.       {
  197.          var xDist:Number = b.x - a.x;
  198.          var yDist:Number = b.y - a.y;
  199.          var angleRads:Number = Math.atan2(yDist,xDist) + Math.PI / 2;
  200.          this.pos = c;
  201.          this.width = Point.distance(a,b);
  202.          this.deltaAngleRads += angleRads - this.angleRads;
  203.          this.angleRads = angleRads;
  204.          this.applyProps();
  205.       }
  206.       
  207.       public function get angle() : Number
  208.       {
  209.          return this.angleRads * 180 / Math.PI;
  210.       }
  211.       
  212.       public function get pos() : Point
  213.       {
  214.          return this.centerPt;
  215.       }
  216.       
  217.       public function invert() : void
  218.       {
  219.          var b:uint = this.angleRads;
  220.          this.angleRads += Math.PI;
  221.          this.applyProps();
  222.       }
  223.       
  224.       public function setXML(xml:String) : void
  225.       {
  226.          var deco:XML = null;
  227.          var edgeXML:XML = new XML(xml);
  228.          this.centerPt = new Point(Number(edgeXML.@x),Number(edgeXML.@y));
  229.          this.length = edgeXML.@width;
  230.          this.angle = edgeXML.@angle;
  231.          this.lines = edgeXML.@lines;
  232.          this.color = edgeXML.@color;
  233.          this.alpha = edgeXML.@alpha;
  234.          this.decoGroup.removeAllDecos();
  235.          if(edgeXML.@decorate == "true")
  236.          {
  237.             for each(deco in edgeXML.*)
  238.             {
  239.                this.decoGroup.addDecoObj(Deco.xmlToDeco(deco.toXMLString()));
  240.             }
  241.          }
  242.          this.applyProps();
  243.       }
  244.       
  245.       public function get width() : Number
  246.       {
  247.          return this.length;
  248.       }
  249.       
  250.       public function getXML() : XML
  251.       {
  252.          var i:int = 0;
  253.          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}/>);
  254.          if(this.hasDecos)
  255.          {
  256.             for(i = 0; i < this.decoGroup.length; i++)
  257.             {
  258.                edgeXML.appendChild(this.decoGroup.getDeco(i).getXML());
  259.             }
  260.          }
  261.          return edgeXML;
  262.       }
  263.       
  264.       public function get b() : Point
  265.       {
  266.          return this.points[this.points.length - 1];
  267.       }
  268.       
  269.       public function get a() : Point
  270.       {
  271.          return this.points[0];
  272.       }
  273.       
  274.       public function set angleRads(n:Number) : void
  275.       {
  276.          this._angleRads = n;
  277.       }
  278.       
  279.       public function get hasDecos() : Boolean
  280.       {
  281.          return this.decoGroup != null;
  282.       }
  283.       
  284.       public function addDeco(decoAsset:DecoAsset, initObj:Settings) : void
  285.       {
  286.          if(this.decoGroup == null)
  287.          {
  288.             this.decoGroup = new DecoGroup();
  289.          }
  290.          this.decoGroup.addDeco(decoAsset,initObj);
  291.       }
  292.       
  293.       public function get angleRads() : Number
  294.       {
  295.          return this._angleRads;
  296.       }
  297.       
  298.       override public function toString() : String
  299.       {
  300.          return "Edge: x=" + this.x + ", y=" + this.y + ", length=" + this.length + ", angle=" + this.angle;
  301.       }
  302.       
  303.       public function copy(includeDecos:Boolean = true, basic:Boolean = false) : Edge
  304.       {
  305.          var newEdge:Edge = null;
  306.          if(basic)
  307.          {
  308.             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);
  309.          }
  310.          else
  311.          {
  312.             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);
  313.          }
  314.          return newEdge;
  315.       }
  316.       
  317.       public function set x(n:Number) : void
  318.       {
  319.          this.centerPt.x = n;
  320.       }
  321.       
  322.       public function get x() : Number
  323.       {
  324.          return this.centerPt.x;
  325.       }
  326.       
  327.       public function get y() : Number
  328.       {
  329.          return this.centerPt.y;
  330.       }
  331.       
  332.       public function get deltaAngle() : Number
  333.       {
  334.          return this.deltaAngleRads * 180 / Math.PI;
  335.       }
  336.    }
  337. }
  338.  
  339.