home *** CD-ROM | disk | FTP | other *** search
- package com.livebrush.tools
- {
- import com.livebrush.events.CanvasEvent;
- import com.livebrush.events.StateEvent;
- import com.livebrush.events.UpdateEvent;
- import com.livebrush.graphics.canvas.Canvas;
- import com.livebrush.graphics.canvas.CanvasManager;
- import com.livebrush.styles.StyleManager;
- import com.livebrush.ui.UI;
- import flash.events.EventDispatcher;
- import flash.events.KeyboardEvent;
- import flash.events.MouseEvent;
- import flash.ui.Keyboard;
- import flash.utils.clearTimeout;
- import flash.utils.setTimeout;
-
- public class ToolManager extends EventDispatcher
- {
- public var brushTool:BrushTool;
-
- public var ui:UI;
-
- public var transformTool:TransformTool;
-
- public var quickTool:Tool = null;
-
- public var handTool:HandTool;
-
- public var canvasManager:CanvasManager;
-
- public var updateDelay:int = 1000;
-
- public var activeTool:Tool;
-
- public var styleManager:StyleManager;
-
- private var updateEvent:UpdateEvent;
-
- public var lastTool:Tool;
-
- public var penTool:PenTool;
-
- private var tools:Array;
-
- private var _toolKeyChars:String = "";
-
- private var timeout:int;
-
- public var sampleTool:SampleTool;
-
- public var colorLayerTool:ColorLayerTool;
-
- public var helpTool:HelpTool;
-
- public function ToolManager(ui:UI, canvasManager:CanvasManager, styleManager:StyleManager)
- {
- this.tools = [];
- super();
- this.ui = ui;
- this.canvasManager = canvasManager;
- this.styleManager = styleManager;
- this.init();
- }
-
- public function resetTool() : void
- {
- this.activeTool.reset();
- this.activeTool.setup();
- }
-
- private function init() : void
- {
- this.initTools();
- this.canvasManager.addEventListener(CanvasEvent.LAYER_SELECT,this.layerSelectHandler);
- this.canvasManager.addEventListener(CanvasEvent.LAYER_DELETE,this.layerDeleteHandler);
- this.canvasManager.addEventListener(CanvasEvent.MOUSE_EVENT,this.canvasMouseEvent);
- this.canvasManager.addEventListener(CanvasEvent.KEY_EVENT,this.canvasKeyEvent);
- }
-
- private function stageMouseEvent(e:MouseEvent) : void
- {
- if(e.type == MouseEvent.MOUSE_DOWN)
- {
- this.canvas.startDrag();
- }
- else if(e.type == MouseEvent.MOUSE_UP)
- {
- this.canvas.stopDrag();
- }
- }
-
- public function setTool(tool:Tool) : void
- {
- if(this.activeTool.state == Tool.RUN_STATE)
- {
- this.activeTool.finish(false);
- }
- this.resetUpdateTimeout(null,false);
- if(this.activeTool.isActive)
- {
- this.activeTool.reset();
- }
- this.lastTool = this.activeTool;
- this.activeTool = tool;
- this.activeTool.setup();
- this.ui.selectTool(this.activeTool.name);
- if(this.lastTool == this.transformTool || this.lastTool == this.penTool)
- {
- this.canvasManager.refreshLayers();
- }
- }
-
- public function stateChange(e:StateEvent = null) : void
- {
- this.activeTool.reset();
- this.activeTool.setup();
- }
-
- private function initTools() : void
- {
- this.brushTool = this.registerTool(new BrushTool(this)) as BrushTool;
- this.colorLayerTool = this.registerTool(new ColorLayerTool(this)) as ColorLayerTool;
- this.sampleTool = this.registerTool(new SampleTool(this)) as SampleTool;
- this.transformTool = this.registerTool(new TransformTool(this)) as TransformTool;
- this.helpTool = this.registerTool(new HelpTool(this)) as HelpTool;
- this.penTool = this.registerTool(new PenTool(this)) as PenTool;
- this.handTool = this.registerTool(new HandTool(this)) as HandTool;
- this.activeTool = new Tool(this);
- this.lastTool = new Tool(this);
- this._toolKeyChars = "BTPGE";
- this.setTool(this.brushTool);
- }
-
- private function getToolByName(toolName:String) : Tool
- {
- var tool:Tool = null;
- switch(toolName)
- {
- case BrushTool.NAME:
- tool = this.brushTool;
- break;
- case ColorLayerTool.NAME:
- tool = this.colorLayerTool;
- break;
- case SampleTool.NAME:
- tool = this.sampleTool;
- break;
- case TransformTool.NAME:
- tool = this.transformTool;
- break;
- case HelpTool.NAME:
- tool = this.helpTool;
- break;
- case PenTool.NAME:
- tool = this.penTool;
- break;
- case HandTool.NAME:
- tool = this.handTool;
- }
- return tool;
- }
-
- private function getToolByKey(char:String) : Tool
- {
- var tool:Tool = null;
- switch(char)
- {
- case BrushTool.KEY:
- tool = this.brushTool;
- break;
- case ColorLayerTool.KEY:
- tool = this.colorLayerTool;
- break;
- case SampleTool.KEY:
- tool = this.sampleTool;
- break;
- case TransformTool.KEY:
- tool = this.transformTool;
- break;
- case HelpTool.KEY:
- tool = this.helpTool;
- break;
- case PenTool.KEY:
- tool = this.penTool;
- break;
- case HandTool.KEY:
- tool = this.handTool;
- }
- return tool;
- }
-
- public function setToolByName(toolName:String) : void
- {
- this.setTool(this.getToolByName(toolName));
- }
-
- private function updateHandler(e:UpdateEvent) : void
- {
- if(e.delay || this.updateDelay == 0)
- {
- this.updateEvent = e;
- this.resetUpdateTimeout(e,true);
- }
- else
- {
- this.resetUpdateTimeout(e,false);
- this.dispatchUpdate(e);
- }
- }
-
- public function setQuickToolByName(toolName:String = "transformTool") : *
- {
- this.setQuickTool(this.getToolByName(toolName));
- }
-
- private function resetUpdateTimeout(e:UpdateEvent, restart:Boolean = true) : void
- {
- clearTimeout(this.timeout);
- if(restart)
- {
- this.timeout = setTimeout(this.dispatchUpdate,this.updateDelay,e);
- }
- }
-
- public function finishQuickTool() : void
- {
- this.quickTool.finish(false);
- if(this.quickTool != this.activeTool)
- {
- this.quickTool.reset();
- this.activeTool.reset();
- this.activeTool.setup();
- this.quickTool = null;
- }
- }
-
- private function layerSelectHandler(e:CanvasEvent) : void
- {
- if(this.activeTool.state == Tool.RUN_STATE)
- {
- this.activeTool.finish(false);
- }
- if(this.activeTool.isActive)
- {
- this.activeTool.reset();
- }
- this.setTool(this.activeTool);
- }
-
- public function get canvas() : Canvas
- {
- return this.canvasManager.canvas;
- }
-
- public function setQuickTool(tool:Tool = null) : void
- {
- this.quickTool = tool == null ? this.transformTool : tool;
- if(this.quickTool != this.activeTool)
- {
- this.quickTool.setup();
- }
- this.quickTool.begin();
- }
-
- private function canvasMouseEvent(e:CanvasEvent) : void
- {
- }
-
- private function layerDeleteHandler(e:CanvasEvent) : void
- {
- if(this.activeTool.isActive)
- {
- this.activeTool.reset();
- }
- }
-
- private function registerTool(newTool:Tool) : Tool
- {
- newTool.addEventListener(UpdateEvent.LAYER,this.updateHandler);
- newTool.addEventListener(UpdateEvent.SELECTION,this.updateHandler);
- newTool.addEventListener(UpdateEvent.BEGIN,this.updateHandler);
- newTool.addEventListener(UpdateEvent.FINISH,this.updateHandler);
- this.tools.push(newTool);
- return newTool;
- }
-
- private function canvasKeyEvent(e:CanvasEvent) : void
- {
- var keyEvent:KeyboardEvent = KeyboardEvent(e.triggerEvent);
- var char:String = String.fromCharCode(keyEvent.charCode).toUpperCase();
- if(this.activeTool != this.handTool && keyEvent.type == KeyboardEvent.KEY_DOWN && !this.canvas.locked && this.activeTool.state != Tool.RUN_STATE && !this.canvasManager.stylePreviewLayer.visible)
- {
- if(keyEvent.keyCode == Keyboard.SPACE)
- {
- this.canvas.lockContent();
- this.canvas.stage.addEventListener(MouseEvent.MOUSE_DOWN,this.stageMouseEvent);
- this.canvas.stage.addEventListener(MouseEvent.MOUSE_UP,this.stageMouseEvent);
- }
- else if(this._toolKeyChars.indexOf(char) > -1)
- {
- }
- }
- else if(this.activeTool != this.handTool && keyEvent.type == KeyboardEvent.KEY_UP && this.canvas.locked && this.activeTool.state != Tool.RUN_STATE)
- {
- if(keyEvent.keyCode == Keyboard.SPACE)
- {
- this.canvas.unlockContent();
- this.canvas.stopDrag();
- this.canvas.stage.removeEventListener(MouseEvent.MOUSE_DOWN,this.stageMouseEvent);
- this.canvas.stage.removeEventListener(MouseEvent.MOUSE_UP,this.stageMouseEvent);
- }
- }
- }
-
- private function dispatchUpdate(e:UpdateEvent) : void
- {
- if(this.activeTool is TransformTool)
- {
- this.ui.pushTransformProps(this.transformTool.settings);
- }
- if(e.type == UpdateEvent.FINISH)
- {
- this.activeTool.state = Tool.ACTIVE_STATE;
- }
- this.updateEvent = null;
- dispatchEvent(new UpdateEvent(e.type,false,false,e.data));
- }
- }
- }
-
-