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 / ToolManager.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  10.4 KB  |  331 lines

  1. package com.livebrush.tools
  2. {
  3.    import com.livebrush.events.CanvasEvent;
  4.    import com.livebrush.events.StateEvent;
  5.    import com.livebrush.events.UpdateEvent;
  6.    import com.livebrush.graphics.canvas.Canvas;
  7.    import com.livebrush.graphics.canvas.CanvasManager;
  8.    import com.livebrush.styles.StyleManager;
  9.    import com.livebrush.ui.UI;
  10.    import flash.events.EventDispatcher;
  11.    import flash.events.KeyboardEvent;
  12.    import flash.events.MouseEvent;
  13.    import flash.ui.Keyboard;
  14.    import flash.utils.clearTimeout;
  15.    import flash.utils.setTimeout;
  16.    
  17.    public class ToolManager extends EventDispatcher
  18.    {
  19.       public var brushTool:BrushTool;
  20.       
  21.       public var ui:UI;
  22.       
  23.       public var transformTool:TransformTool;
  24.       
  25.       public var quickTool:Tool = null;
  26.       
  27.       public var handTool:HandTool;
  28.       
  29.       public var canvasManager:CanvasManager;
  30.       
  31.       public var updateDelay:int = 1000;
  32.       
  33.       public var activeTool:Tool;
  34.       
  35.       public var styleManager:StyleManager;
  36.       
  37.       private var updateEvent:UpdateEvent;
  38.       
  39.       public var lastTool:Tool;
  40.       
  41.       public var penTool:PenTool;
  42.       
  43.       private var tools:Array;
  44.       
  45.       private var _toolKeyChars:String = "";
  46.       
  47.       private var timeout:int;
  48.       
  49.       public var sampleTool:SampleTool;
  50.       
  51.       public var colorLayerTool:ColorLayerTool;
  52.       
  53.       public var helpTool:HelpTool;
  54.       
  55.       public function ToolManager(ui:UI, canvasManager:CanvasManager, styleManager:StyleManager)
  56.       {
  57.          this.tools = [];
  58.          super();
  59.          this.ui = ui;
  60.          this.canvasManager = canvasManager;
  61.          this.styleManager = styleManager;
  62.          this.init();
  63.       }
  64.       
  65.       public function resetTool() : void
  66.       {
  67.          this.activeTool.reset();
  68.          this.activeTool.setup();
  69.       }
  70.       
  71.       private function init() : void
  72.       {
  73.          this.initTools();
  74.          this.canvasManager.addEventListener(CanvasEvent.LAYER_SELECT,this.layerSelectHandler);
  75.          this.canvasManager.addEventListener(CanvasEvent.LAYER_DELETE,this.layerDeleteHandler);
  76.          this.canvasManager.addEventListener(CanvasEvent.MOUSE_EVENT,this.canvasMouseEvent);
  77.          this.canvasManager.addEventListener(CanvasEvent.KEY_EVENT,this.canvasKeyEvent);
  78.       }
  79.       
  80.       private function stageMouseEvent(e:MouseEvent) : void
  81.       {
  82.          if(e.type == MouseEvent.MOUSE_DOWN)
  83.          {
  84.             this.canvas.startDrag();
  85.          }
  86.          else if(e.type == MouseEvent.MOUSE_UP)
  87.          {
  88.             this.canvas.stopDrag();
  89.          }
  90.       }
  91.       
  92.       public function setTool(tool:Tool) : void
  93.       {
  94.          if(this.activeTool.state == Tool.RUN_STATE)
  95.          {
  96.             this.activeTool.finish(false);
  97.          }
  98.          this.resetUpdateTimeout(null,false);
  99.          if(this.activeTool.isActive)
  100.          {
  101.             this.activeTool.reset();
  102.          }
  103.          this.lastTool = this.activeTool;
  104.          this.activeTool = tool;
  105.          this.activeTool.setup();
  106.          this.ui.selectTool(this.activeTool.name);
  107.          if(this.lastTool == this.transformTool || this.lastTool == this.penTool)
  108.          {
  109.             this.canvasManager.refreshLayers();
  110.          }
  111.       }
  112.       
  113.       public function stateChange(e:StateEvent = null) : void
  114.       {
  115.          this.activeTool.reset();
  116.          this.activeTool.setup();
  117.       }
  118.       
  119.       private function initTools() : void
  120.       {
  121.          this.brushTool = this.registerTool(new BrushTool(this)) as BrushTool;
  122.          this.colorLayerTool = this.registerTool(new ColorLayerTool(this)) as ColorLayerTool;
  123.          this.sampleTool = this.registerTool(new SampleTool(this)) as SampleTool;
  124.          this.transformTool = this.registerTool(new TransformTool(this)) as TransformTool;
  125.          this.helpTool = this.registerTool(new HelpTool(this)) as HelpTool;
  126.          this.penTool = this.registerTool(new PenTool(this)) as PenTool;
  127.          this.handTool = this.registerTool(new HandTool(this)) as HandTool;
  128.          this.activeTool = new Tool(this);
  129.          this.lastTool = new Tool(this);
  130.          this._toolKeyChars = "BTPGE";
  131.          this.setTool(this.brushTool);
  132.       }
  133.       
  134.       private function getToolByName(toolName:String) : Tool
  135.       {
  136.          var tool:Tool = null;
  137.          switch(toolName)
  138.          {
  139.             case BrushTool.NAME:
  140.                tool = this.brushTool;
  141.                break;
  142.             case ColorLayerTool.NAME:
  143.                tool = this.colorLayerTool;
  144.                break;
  145.             case SampleTool.NAME:
  146.                tool = this.sampleTool;
  147.                break;
  148.             case TransformTool.NAME:
  149.                tool = this.transformTool;
  150.                break;
  151.             case HelpTool.NAME:
  152.                tool = this.helpTool;
  153.                break;
  154.             case PenTool.NAME:
  155.                tool = this.penTool;
  156.                break;
  157.             case HandTool.NAME:
  158.                tool = this.handTool;
  159.          }
  160.          return tool;
  161.       }
  162.       
  163.       private function getToolByKey(char:String) : Tool
  164.       {
  165.          var tool:Tool = null;
  166.          switch(char)
  167.          {
  168.             case BrushTool.KEY:
  169.                tool = this.brushTool;
  170.                break;
  171.             case ColorLayerTool.KEY:
  172.                tool = this.colorLayerTool;
  173.                break;
  174.             case SampleTool.KEY:
  175.                tool = this.sampleTool;
  176.                break;
  177.             case TransformTool.KEY:
  178.                tool = this.transformTool;
  179.                break;
  180.             case HelpTool.KEY:
  181.                tool = this.helpTool;
  182.                break;
  183.             case PenTool.KEY:
  184.                tool = this.penTool;
  185.                break;
  186.             case HandTool.KEY:
  187.                tool = this.handTool;
  188.          }
  189.          return tool;
  190.       }
  191.       
  192.       public function setToolByName(toolName:String) : void
  193.       {
  194.          this.setTool(this.getToolByName(toolName));
  195.       }
  196.       
  197.       private function updateHandler(e:UpdateEvent) : void
  198.       {
  199.          if(e.delay || this.updateDelay == 0)
  200.          {
  201.             this.updateEvent = e;
  202.             this.resetUpdateTimeout(e,true);
  203.          }
  204.          else
  205.          {
  206.             this.resetUpdateTimeout(e,false);
  207.             this.dispatchUpdate(e);
  208.          }
  209.       }
  210.       
  211.       public function setQuickToolByName(toolName:String = "transformTool") : *
  212.       {
  213.          this.setQuickTool(this.getToolByName(toolName));
  214.       }
  215.       
  216.       private function resetUpdateTimeout(e:UpdateEvent, restart:Boolean = true) : void
  217.       {
  218.          clearTimeout(this.timeout);
  219.          if(restart)
  220.          {
  221.             this.timeout = setTimeout(this.dispatchUpdate,this.updateDelay,e);
  222.          }
  223.       }
  224.       
  225.       public function finishQuickTool() : void
  226.       {
  227.          this.quickTool.finish(false);
  228.          if(this.quickTool != this.activeTool)
  229.          {
  230.             this.quickTool.reset();
  231.             this.activeTool.reset();
  232.             this.activeTool.setup();
  233.             this.quickTool = null;
  234.          }
  235.       }
  236.       
  237.       private function layerSelectHandler(e:CanvasEvent) : void
  238.       {
  239.          if(this.activeTool.state == Tool.RUN_STATE)
  240.          {
  241.             this.activeTool.finish(false);
  242.          }
  243.          if(this.activeTool.isActive)
  244.          {
  245.             this.activeTool.reset();
  246.          }
  247.          this.setTool(this.activeTool);
  248.       }
  249.       
  250.       public function get canvas() : Canvas
  251.       {
  252.          return this.canvasManager.canvas;
  253.       }
  254.       
  255.       public function setQuickTool(tool:Tool = null) : void
  256.       {
  257.          this.quickTool = tool == null ? this.transformTool : tool;
  258.          if(this.quickTool != this.activeTool)
  259.          {
  260.             this.quickTool.setup();
  261.          }
  262.          this.quickTool.begin();
  263.       }
  264.       
  265.       private function canvasMouseEvent(e:CanvasEvent) : void
  266.       {
  267.       }
  268.       
  269.       private function layerDeleteHandler(e:CanvasEvent) : void
  270.       {
  271.          if(this.activeTool.isActive)
  272.          {
  273.             this.activeTool.reset();
  274.          }
  275.       }
  276.       
  277.       private function registerTool(newTool:Tool) : Tool
  278.       {
  279.          newTool.addEventListener(UpdateEvent.LAYER,this.updateHandler);
  280.          newTool.addEventListener(UpdateEvent.SELECTION,this.updateHandler);
  281.          newTool.addEventListener(UpdateEvent.BEGIN,this.updateHandler);
  282.          newTool.addEventListener(UpdateEvent.FINISH,this.updateHandler);
  283.          this.tools.push(newTool);
  284.          return newTool;
  285.       }
  286.       
  287.       private function canvasKeyEvent(e:CanvasEvent) : void
  288.       {
  289.          var keyEvent:KeyboardEvent = KeyboardEvent(e.triggerEvent);
  290.          var char:String = String.fromCharCode(keyEvent.charCode).toUpperCase();
  291.          if(this.activeTool != this.handTool && keyEvent.type == KeyboardEvent.KEY_DOWN && !this.canvas.locked && this.activeTool.state != Tool.RUN_STATE && !this.canvasManager.stylePreviewLayer.visible)
  292.          {
  293.             if(keyEvent.keyCode == Keyboard.SPACE)
  294.             {
  295.                this.canvas.lockContent();
  296.                this.canvas.stage.addEventListener(MouseEvent.MOUSE_DOWN,this.stageMouseEvent);
  297.                this.canvas.stage.addEventListener(MouseEvent.MOUSE_UP,this.stageMouseEvent);
  298.             }
  299.             else if(this._toolKeyChars.indexOf(char) > -1)
  300.             {
  301.             }
  302.          }
  303.          else if(this.activeTool != this.handTool && keyEvent.type == KeyboardEvent.KEY_UP && this.canvas.locked && this.activeTool.state != Tool.RUN_STATE)
  304.          {
  305.             if(keyEvent.keyCode == Keyboard.SPACE)
  306.             {
  307.                this.canvas.unlockContent();
  308.                this.canvas.stopDrag();
  309.                this.canvas.stage.removeEventListener(MouseEvent.MOUSE_DOWN,this.stageMouseEvent);
  310.                this.canvas.stage.removeEventListener(MouseEvent.MOUSE_UP,this.stageMouseEvent);
  311.             }
  312.          }
  313.       }
  314.       
  315.       private function dispatchUpdate(e:UpdateEvent) : void
  316.       {
  317.          if(this.activeTool is TransformTool)
  318.          {
  319.             this.ui.pushTransformProps(this.transformTool.settings);
  320.          }
  321.          if(e.type == UpdateEvent.FINISH)
  322.          {
  323.             this.activeTool.state = Tool.ACTIVE_STATE;
  324.          }
  325.          this.updateEvent = null;
  326.          dispatchEvent(new UpdateEvent(e.type,false,false,e.data));
  327.       }
  328.    }
  329. }
  330.  
  331.