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 / tools / PenTool.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  14.0 KB  |  402 lines

  1. package com.livebrush.tools
  2. {
  3.    import com.livebrush.data.StateManager;
  4.    import com.livebrush.events.UpdateEvent;
  5.    import com.livebrush.graphics.Edge;
  6.    import com.livebrush.graphics.GraphicUpdate;
  7.    import com.livebrush.graphics.Line;
  8.    import com.livebrush.graphics.SmoothLine;
  9.    import com.livebrush.graphics.canvas.Canvas;
  10.    import com.livebrush.graphics.canvas.Layer;
  11.    import com.livebrush.graphics.canvas.LineLayer;
  12.    import com.livebrush.transform.EdgeView;
  13.    import com.livebrush.transform.LineWireframeView;
  14.    import com.livebrush.transform.SingleEdgeView;
  15.    import com.livebrush.transform.SyncPoint;
  16.    import flash.display.Sprite;
  17.    import flash.events.Event;
  18.    import flash.events.MouseEvent;
  19.    import flash.geom.Point;
  20.    import flash.utils.clearTimeout;
  21.    import flash.utils.setTimeout;
  22.    
  23.    public class PenTool extends Tool
  24.    {
  25.       public static const NAME:String = "penTool";
  26.       
  27.       public static const KEY:String = "P";
  28.       
  29.       public static const ADD_OPEN:String = "addOpen";
  30.       
  31.       public static const ADD_INSIDE:String = "addInside";
  32.       
  33.       public static const ADD_OUTSIDE:String = "addOutside";
  34.       
  35.       private var lastNewEdge:Edge;
  36.       
  37.       private var liveEdgeIndex:int = 0;
  38.       
  39.       private var wireframeView:LineWireframeView;
  40.       
  41.       private var lastSingleEdgeView:SingleEdgeView;
  42.       
  43.       private var timeout:int;
  44.       
  45.       private var addMode:String = "addOpen";
  46.       
  47.       private var lastNewEdgeAngleRads:Number;
  48.       
  49.       private var singleEdgeView:SingleEdgeView;
  50.       
  51.       private var updateNewEdge:Boolean = false;
  52.       
  53.       public var edgeViews:Array;
  54.       
  55.       public var updateDelay:int = 1000;
  56.       
  57.       private var newLine:Line;
  58.       
  59.       private var newEdge:Edge;
  60.       
  61.       public function PenTool(toolMan:ToolManager)
  62.       {
  63.          super(toolMan);
  64.          this.edgeViews = [];
  65.          this.init();
  66.       }
  67.       
  68.       override public function setup() : void
  69.       {
  70.          var i:int = 0;
  71.          super.setup();
  72.          if(activeLayers.length == 1 && (activeLayer is LineLayer && !Layer.isBitmapLayer(activeLayer)) && LineLayer(activeLayer).line.length > 0)
  73.          {
  74.             for(i = 0; i < LineLayer(activeLayer).line.length; i++)
  75.             {
  76.                this.edgeViews.push(registerView(new EdgeView(this,LineLayer(activeLayer),i)));
  77.             }
  78.             this.wireframeView = LineWireframeView(registerView(new LineWireframeView(this,LineLayer(activeLayer).line)));
  79.          }
  80.          views.reverse();
  81.          update(GraphicUpdate.layerUpdate());
  82.       }
  83.       
  84.       override protected function openCanvasMouseDown(e:MouseEvent) : void
  85.       {
  86.          var layer:LineLayer = null;
  87.          this.resetUpdateTimeout(false);
  88.          begin();
  89.          if(!Layer.isLineLayer(activeLayer) || Layer.isBitmapLayer(activeLayer))
  90.          {
  91.             layer = canvasManager.addLayer(Layer.newLineLayer(canvas)) as LineLayer;
  92.             layer.line = Line.newLine(styleManager.activeStyle.lineStyle.smoothing,styleManager.activeStyle.strokeStyle.strokeType,styleManager.activeStyle.strokeStyle.lines,styleManager.activeStyle.strokeStyle.weight);
  93.             this.setNewLine(false);
  94.             this.addNewEdge(false);
  95.          }
  96.          else if(activeLayer is LineLayer)
  97.          {
  98.             layer = activeLayer as LineLayer;
  99.             if(layer.line.length == 0)
  100.             {
  101.                this.setNewLine(true);
  102.                this.addNewEdge(false);
  103.             }
  104.             else if(this.newLine != null)
  105.             {
  106.                this.addNewEdge(true);
  107.             }
  108.             else
  109.             {
  110.                this.setNewLine(true);
  111.                this.liveEdgeIndex = 0;
  112.                if(layer.line.length > 0)
  113.                {
  114.                   this.liveEdgeIndex = this.getClosestEdgeIndex(Canvas.MOUSE_POINT);
  115.                }
  116.                this.addNewEdge(true,true);
  117.                this.addNewEdge(true);
  118.             }
  119.          }
  120.          if(this.wireframeView != null)
  121.          {
  122.             unregisterView(this.wireframeView);
  123.             this.wireframeView.die();
  124.          }
  125.          this.wireframeView = LineWireframeView(registerView(new LineWireframeView(this,this.newLine)));
  126.          this.singleEdgeView = SingleEdgeView(registerView(new SingleEdgeView(this,layer,this.newEdge,false)));
  127.          this.updateNewEdge = true;
  128.       }
  129.       
  130.       override protected function controlsMouseHandler(e:MouseEvent) : void
  131.       {
  132.          this.canvasMouseUp(e);
  133.       }
  134.       
  135.       override protected function die() : void
  136.       {
  137.          super.die();
  138.          clearTimeout(this.timeout);
  139.          if(this.newLine != null)
  140.          {
  141.             this.newLine.die();
  142.             this.newLine = null;
  143.             this.newEdge = null;
  144.          }
  145.          this.edgeViews = [];
  146.       }
  147.       
  148.       private function init() : void
  149.       {
  150.          name = NAME;
  151.       }
  152.       
  153.       public function addEdgeAt(layer:LineLayer, index:int, pt:Point = null) : void
  154.       {
  155.          if(this.addMode == ADD_OPEN)
  156.          {
  157.             layer.line.addEdgeAt(index,pt);
  158.             this.reset();
  159.             this.setup();
  160.             layerUpdate();
  161.             StateManager.closeState();
  162.          }
  163.       }
  164.       
  165.       override protected function enterFrameHandler(e:Event) : void
  166.       {
  167.          var angleRads:Number = NaN;
  168.          var newEdgeWidth:Number = NaN;
  169.          var dy:Number = NaN;
  170.          var dx:Number = NaN;
  171.          var angleRads2:* = undefined;
  172.          if(this.updateNewEdge)
  173.          {
  174.             angleRads = canvas.angleRads;
  175.             newEdgeWidth = 25;
  176.             if(this.newLine.length > 1)
  177.             {
  178.                dy = canvas.mousePt.y - this.lastNewEdge.c.y;
  179.                dx = canvas.mousePt.x - this.lastNewEdge.c.x;
  180.                angleRads = Math.atan2(dy,dx);
  181.                newEdgeWidth = this.newEdge.width;
  182.                this.newEdge.modify(canvas.mousePt,angleRads,newEdgeWidth);
  183.                if(LineLayer(activeLayer).line.length == 0)
  184.                {
  185.                   newEdgeWidth = this.lastNewEdge.width;
  186.                   this.lastNewEdge.modify(this.lastNewEdge.c,angleRads,newEdgeWidth);
  187.                }
  188.                else if(this.newLine.length > 2)
  189.                {
  190.                   newEdgeWidth = this.lastNewEdge.width;
  191.                   dy = canvas.mousePt.y - this.newLine.edges[this.newLine.edges.length - 3].c.y;
  192.                   dx = canvas.mousePt.x - this.newLine.edges[this.newLine.edges.length - 3].c.x;
  193.                   angleRads2 = Math.atan2(dy,dx);
  194.                   this.lastNewEdge.modify(this.lastNewEdge.c,angleRads2,newEdgeWidth);
  195.                }
  196.                this.newLine.invalidateEdge(this.lastNewEdge);
  197.                this.newLine.invalidateEdge(this.newEdge);
  198.             }
  199.             else
  200.             {
  201.                newEdgeWidth = this.newEdge.width;
  202.                this.newEdge.modify(canvas.mousePt,Math.PI,newEdgeWidth);
  203.             }
  204.             this.newLine.applyProps();
  205.             clearWireframes();
  206.             if(this.newLine.length == 2 && LineLayer(activeLayer).line.length < 2)
  207.             {
  208.                this.lastNewEdge.modify(this.lastNewEdge.c,angleRads,this.lastNewEdge.width);
  209.             }
  210.             this.singleEdgeView.edge.modify(canvas.mousePt,angleRads,newEdgeWidth);
  211.             toolUpdate();
  212.             this.wireframeView.update(new GraphicUpdate(UpdateEvent.LINE));
  213.          }
  214.       }
  215.       
  216.       override public function reset() : void
  217.       {
  218.          super.reset();
  219.          Canvas.WIREFRAME.graphics.clear();
  220.          this.liveEdgeIndex = 0;
  221.          this.addMode = ADD_OPEN;
  222.          this.die();
  223.       }
  224.       
  225.       public function transformEdge(upObj:Object, updateNow:Boolean = true) : void
  226.       {
  227.          var fromScope:Sprite = upObj.fromScope == null ? Canvas.WIREFRAME : upObj.fromScope;
  228.          var layer:LineLayer = upObj.layer;
  229.          var edgeIndex:int = int(upObj.index);
  230.          var c:Object = upObj.c;
  231.          var a:Object = upObj.a;
  232.          var b:Object = upObj.b;
  233.          if(fromScope != Canvas.WIREFRAME)
  234.          {
  235.             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));
  236.             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));
  237.          }
  238.          else
  239.          {
  240.             layer.line.modifyEdge(edgeIndex,new Point(c.x,c.y),new Point(a.x,a.y),new Point(b.x,b.y));
  241.             this.wireframeView.wireLine.modifyEdge(edgeIndex,new Point(c.x,c.y),new Point(a.x,a.y),new Point(b.x,b.y));
  242.          }
  243.          if(updateNow)
  244.          {
  245.             layerUpdate();
  246.          }
  247.       }
  248.       
  249.       private function addNewEdge(usePrevious:Boolean, copyFromRealLine:Boolean = false) : void
  250.       {
  251.          var dy:Number = NaN;
  252.          var dx:Number = NaN;
  253.          var angleRads:Number = NaN;
  254.          if(usePrevious)
  255.          {
  256.             if(copyFromRealLine)
  257.             {
  258.                this.newEdge = LineLayer(activeLayer).line.edges[this.liveEdgeIndex].copy(false,true);
  259.                if(this.liveEdgeIndex == 0)
  260.                {
  261.                   this.newEdge.invert();
  262.                }
  263.             }
  264.             else
  265.             {
  266.                this.lastNewEdge = this.newEdge;
  267.                this.newEdge = this.lastNewEdge.copy();
  268.                dy = Canvas.Y - this.lastNewEdge.c.y;
  269.                dx = Canvas.X - this.lastNewEdge.c.x;
  270.                angleRads = Math.atan2(dy,dx);
  271.             }
  272.          }
  273.          else
  274.          {
  275.             this.newEdge = new Edge(Canvas.X,Canvas.Y,25,90,this.newLine.lines,16711680,this.newLine.weight,null);
  276.          }
  277.          this.newLine.addEdge(this.newEdge);
  278.       }
  279.       
  280.       private function applyNewLine(delay:Boolean = false) : void
  281.       {
  282.          var edge:Edge = null;
  283.          var existingLineEdge:Edge = null;
  284.          var layer:LineLayer = activeLayer as LineLayer;
  285.          var line:Line = layer.line;
  286.          var lineXML:XML = line.getXML();
  287.          if(line.length > 0)
  288.          {
  289.             existingLineEdge = line.edges[this.liveEdgeIndex];
  290.          }
  291.          else
  292.          {
  293.             existingLineEdge = this.newEdge;
  294.          }
  295.          for(var i:int = 0; i < this.newLine.length; i++)
  296.          {
  297.             edge = this.newLine.edges[i];
  298.             edge.alpha = existingLineEdge.alpha;
  299.             edge.lines = existingLineEdge.lines;
  300.             edge.applyProps();
  301.          }
  302.          this.newLine.type = line.type;
  303.          this.newLine.lines = line.lines;
  304.          this.newLine.applyProps();
  305.          if(line.length == 1)
  306.          {
  307.             line.edges[0].modify(line.edges[0].c,this.newLine.edges[0].angleRads,line.edges[0].length);
  308.             this.newLine.edges.shift();
  309.          }
  310.          else if(line.length > 1)
  311.          {
  312.             this.newLine.edges.shift();
  313.          }
  314.          if(line.length > 1 && this.liveEdgeIndex == 0)
  315.          {
  316.             for(i = 0; i < this.newLine.edges.length; i++)
  317.             {
  318.                this.newLine.edges[i].invert();
  319.             }
  320.             this.newLine.edges.reverse();
  321.             line.edges = this.newLine.edges.concat(line.edges);
  322.          }
  323.          else
  324.          {
  325.             line.edges = line.edges.concat(this.newLine.edges);
  326.          }
  327.          LineLayer(activeLayer).line.rebuild();
  328.          this.reset();
  329.          layerUpdate(delay);
  330.          finish(delay);
  331.          this.setup();
  332.       }
  333.       
  334.       private function resetUpdateTimeout(restart:Boolean = true) : void
  335.       {
  336.          clearTimeout(this.timeout);
  337.          if(restart)
  338.          {
  339.             this.timeout = setTimeout(this.applyNewLine,this.updateDelay);
  340.          }
  341.       }
  342.       
  343.       override protected function contentMouseDown(e:MouseEvent) : void
  344.       {
  345.          if(!Layer.isLineLayer(activeLayer))
  346.          {
  347.             this.openCanvasMouseDown(e);
  348.          }
  349.       }
  350.       
  351.       private function setNewLine(useExisting:Boolean) : void
  352.       {
  353.          var layer:LineLayer = activeLayer as LineLayer;
  354.          if(useExisting)
  355.          {
  356.             this.newLine = Line.newLine(layer.line is SmoothLine,layer.line.type,layer.line.lines,layer.line.weight);
  357.          }
  358.          else
  359.          {
  360.             this.newLine = Line.newLine(styleManager.activeStyle.lineStyle.smoothing,styleManager.activeStyle.strokeStyle.strokeType,styleManager.activeStyle.strokeStyle.lines,styleManager.activeStyle.strokeStyle.weight);
  361.          }
  362.          this.addMode = ADD_OUTSIDE;
  363.       }
  364.       
  365.       override protected function canvasMouseUp(e:MouseEvent) : void
  366.       {
  367.          if(this.updateNewEdge)
  368.          {
  369.             this.updateNewEdge = false;
  370.             unregisterView(this.singleEdgeView);
  371.             this.singleEdgeView.die();
  372.             this.singleEdgeView = null;
  373.             if(this.updateDelay == 0)
  374.             {
  375.                this.resetUpdateTimeout(false);
  376.                this.applyNewLine();
  377.             }
  378.             else
  379.             {
  380.                this.resetUpdateTimeout(true);
  381.             }
  382.             if(LineLayer(activeLayer).line.length <= 0)
  383.             {
  384.                this.applyNewLine();
  385.             }
  386.          }
  387.       }
  388.       
  389.       private function getClosestEdgeIndex(pt:Point) : int
  390.       {
  391.          var closeEdge:int = 0;
  392.          var dist:Number = Point.distance(this.edgeViews[0].edge.c,pt);
  393.          if(Point.distance(this.edgeViews[this.edgeViews.length - 1].edge.c,pt) < dist)
  394.          {
  395.             closeEdge = int(this.edgeViews.length - 1);
  396.          }
  397.          return closeEdge;
  398.       }
  399.    }
  400. }
  401.  
  402.