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 / Tool.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  7.7 KB  |  254 lines

  1. package com.livebrush.tools
  2. {
  3.    import com.livebrush.data.Settings;
  4.    import com.livebrush.data.StateManager;
  5.    import com.livebrush.events.CanvasEvent;
  6.    import com.livebrush.graphics.GraphicUpdate;
  7.    import com.livebrush.graphics.canvas.Canvas;
  8.    import com.livebrush.graphics.canvas.CanvasManager;
  9.    import com.livebrush.graphics.canvas.Layer;
  10.    import com.livebrush.styles.StyleManager;
  11.    import com.livebrush.ui.UI;
  12.    import com.livebrush.utils.Model;
  13.    import com.livebrush.utils.Update;
  14.    import flash.events.Event;
  15.    import flash.events.MouseEvent;
  16.    import flash.utils.Timer;
  17.    
  18.    public class Tool extends Model
  19.    {
  20.       public static const REALTIME_EDITMODE:String = "realtimeMode";
  21.       
  22.       public static const WIREFRAME_EDITMODE:String = "wireframeMode";
  23.       
  24.       public static const AUTO_EDITMODE:String = "autoEditMode";
  25.       
  26.       public static const ACTIVE_STATE:String = "activeState";
  27.       
  28.       public static const IDLE_STATE:String = "idleState";
  29.       
  30.       public static const RUN_STATE:String = "runState";
  31.       
  32.       public var toolManager:ToolManager;
  33.       
  34.       public var name:String;
  35.       
  36.       public var state:String = "idleState";
  37.       
  38.       private var updateDelay:Timer;
  39.       
  40.       public function Tool(toolManager:ToolManager)
  41.       {
  42.          super();
  43.          this.toolManager = toolManager;
  44.          this.init();
  45.       }
  46.       
  47.       public static function isTransformTool(t:Tool) : Boolean
  48.       {
  49.          return t is TransformTool;
  50.       }
  51.       
  52.       override protected function die() : void
  53.       {
  54.          super.die();
  55.       }
  56.       
  57.       public function toolUpdate(type:Update = null) : void
  58.       {
  59.          if(type == null)
  60.          {
  61.             this.clearWireframes();
  62.             update(GraphicUpdate.toolUpdate(),true,false);
  63.          }
  64.          else
  65.          {
  66.             update(type,true,true);
  67.          }
  68.       }
  69.       
  70.       protected function stageMouseUp(e:MouseEvent) : void
  71.       {
  72.       }
  73.       
  74.       protected function openCanvasMouseDown(e:MouseEvent) : void
  75.       {
  76.       }
  77.       
  78.       public function set settings(data:Settings) : void
  79.       {
  80.       }
  81.       
  82.       public function selectionUpdate(layers:Array) : void
  83.       {
  84.          update(Update.selectionUpdate({"layers":layers},false),true,true);
  85.       }
  86.       
  87.       protected function setSelection(e:CanvasEvent) : void
  88.       {
  89.       }
  90.       
  91.       public function get canvasManager() : CanvasManager
  92.       {
  93.          return this.toolManager.canvasManager;
  94.       }
  95.       
  96.       public function reset() : void
  97.       {
  98.          this.state = IDLE_STATE;
  99.          this.canvasManager.removeEventListener(CanvasEvent.MOUSE_EVENT,this.canvasMouseEvent);
  100.          this.canvasManager.removeEventListener(CanvasEvent.SELECTION_EVENT,this.setSelection);
  101.          this.canvasManager.removeEventListener(CanvasEvent.KEY_EVENT,this.canvasKeyEvent);
  102.          Canvas.CONTROLS.removeEventListener(MouseEvent.MOUSE_DOWN,this.controlsMouseHandler);
  103.          Canvas.CONTROLS.removeEventListener(MouseEvent.MOUSE_UP,this.controlsMouseHandler);
  104.          Canvas.STAGE.removeEventListener(Event.ENTER_FRAME,this.enterFrameHandler);
  105.          this.canvas.stage.removeEventListener(MouseEvent.MOUSE_UP,this.stageMouseUp);
  106.       }
  107.       
  108.       public function get isActive() : Boolean
  109.       {
  110.          return this.state == RUN_STATE || this.state == ACTIVE_STATE;
  111.       }
  112.       
  113.       public function layerUpdate(delay:Boolean = true) : void
  114.       {
  115.          this.clearWireframes();
  116.          update(GraphicUpdate.layerUpdate({"layers":this.activeLayers},delay),true,true);
  117.       }
  118.       
  119.       public function finish(delay:Boolean = true, saveState:Boolean = true) : void
  120.       {
  121.          if(saveState)
  122.          {
  123.             StateManager.addItem(function(state:Object):void
  124.             {
  125.                canvasManager.setObjectsXML(state.data.beginObjectsXML);
  126.             },function(state:Object):void
  127.             {
  128.                canvasManager.setObjectsXML(state.data.finishObjectsXML);
  129.             },-1,{
  130.                "beginObjectsXML":this.canvasManager._selectedObjects.slice(),
  131.                "finishObjectsXML":this.canvasManager.storeSelectedObjects().slice()
  132.             },-1,"Tool: finish");
  133.             StateManager.closeState();
  134.          }
  135.          this.state = Tool.ACTIVE_STATE;
  136.          this.toolUpdate(Update.finishUpdate(null,delay));
  137.       }
  138.       
  139.       public function get ui() : UI
  140.       {
  141.          return this.toolManager.ui;
  142.       }
  143.       
  144.       protected function enterFrameHandler(e:Event) : void
  145.       {
  146.       }
  147.       
  148.       private function init() : void
  149.       {
  150.       }
  151.       
  152.       public function get canvas() : Canvas
  153.       {
  154.          return this.canvasManager.canvas;
  155.       }
  156.       
  157.       protected function clearWireframes() : void
  158.       {
  159.          Canvas.WIREFRAME.graphics.clear();
  160.       }
  161.       
  162.       protected function canvasKeyEvent(e:CanvasEvent) : void
  163.       {
  164.       }
  165.       
  166.       public function get settings() : Settings
  167.       {
  168.          return new Settings();
  169.       }
  170.       
  171.       public function get activeLayers() : Array
  172.       {
  173.          return this.canvasManager.activeLayers;
  174.       }
  175.       
  176.       public function begin() : void
  177.       {
  178.          if(this.state != Tool.RUN_STATE)
  179.          {
  180.             StateManager.openState();
  181.             this.canvasManager._selectedObjects = this.canvasManager.storeSelectedObjects();
  182.             this.state = Tool.RUN_STATE;
  183.             this.toolUpdate(Update.beginUpdate());
  184.          }
  185.       }
  186.       
  187.       public function get activeLayer() : Layer
  188.       {
  189.          return this.canvasManager.activeLayer;
  190.       }
  191.       
  192.       protected function canvasMouseEvent(e:CanvasEvent) : void
  193.       {
  194.          var clickedContent:Boolean = false;
  195.          var mouseEvent:MouseEvent = e.triggerEvent as MouseEvent;
  196.          if(mouseEvent.type == MouseEvent.MOUSE_DOWN)
  197.          {
  198.             clickedContent = this.activeLayer.hitTest(mouseEvent.stageX,mouseEvent.stageY);
  199.             if(clickedContent)
  200.             {
  201.                this.contentMouseDown(mouseEvent);
  202.             }
  203.             else
  204.             {
  205.                this.openCanvasMouseDown(mouseEvent);
  206.             }
  207.          }
  208.          else if(mouseEvent.type == MouseEvent.MOUSE_UP)
  209.          {
  210.             this.canvasMouseUp(mouseEvent);
  211.          }
  212.       }
  213.       
  214.       protected function contentMouseDown(e:MouseEvent) : void
  215.       {
  216.       }
  217.       
  218.       protected function canvasMouseUp(e:MouseEvent) : void
  219.       {
  220.       }
  221.       
  222.       public function pullProps() : void
  223.       {
  224.       }
  225.       
  226.       public function get isRunning() : Boolean
  227.       {
  228.          return this.state == RUN_STATE;
  229.       }
  230.       
  231.       protected function controlsMouseHandler(e:MouseEvent) : void
  232.       {
  233.       }
  234.       
  235.       public function get styleManager() : StyleManager
  236.       {
  237.          return this.toolManager.styleManager;
  238.       }
  239.       
  240.       public function setup() : void
  241.       {
  242.          this.state = ACTIVE_STATE;
  243.          this.canvasManager.addEventListener(CanvasEvent.MOUSE_EVENT,this.canvasMouseEvent);
  244.          this.canvasManager.addEventListener(CanvasEvent.SELECTION_EVENT,this.setSelection);
  245.          this.canvasManager.addEventListener(CanvasEvent.KEY_EVENT,this.canvasKeyEvent);
  246.          Canvas.CONTROLS.addEventListener(MouseEvent.MOUSE_DOWN,this.controlsMouseHandler);
  247.          Canvas.CONTROLS.addEventListener(MouseEvent.MOUSE_UP,this.controlsMouseHandler);
  248.          Canvas.STAGE.addEventListener(Event.ENTER_FRAME,this.enterFrameHandler);
  249.          this.canvas.stage.addEventListener(MouseEvent.MOUSE_UP,this.stageMouseUp);
  250.       }
  251.    }
  252. }
  253.  
  254.