home *** CD-ROM | disk | FTP | other *** search
- package com.livebrush.tools
- {
- import com.livebrush.data.Settings;
- import com.livebrush.data.StateManager;
- import com.livebrush.events.CanvasEvent;
- import com.livebrush.graphics.GraphicUpdate;
- import com.livebrush.graphics.canvas.Canvas;
- import com.livebrush.graphics.canvas.CanvasManager;
- import com.livebrush.graphics.canvas.Layer;
- import com.livebrush.styles.StyleManager;
- import com.livebrush.ui.UI;
- import com.livebrush.utils.Model;
- import com.livebrush.utils.Update;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.utils.Timer;
-
- public class Tool extends Model
- {
- public static const REALTIME_EDITMODE:String = "realtimeMode";
-
- public static const WIREFRAME_EDITMODE:String = "wireframeMode";
-
- public static const AUTO_EDITMODE:String = "autoEditMode";
-
- public static const ACTIVE_STATE:String = "activeState";
-
- public static const IDLE_STATE:String = "idleState";
-
- public static const RUN_STATE:String = "runState";
-
- public var toolManager:ToolManager;
-
- public var name:String;
-
- public var state:String = "idleState";
-
- private var updateDelay:Timer;
-
- public function Tool(toolManager:ToolManager)
- {
- super();
- this.toolManager = toolManager;
- this.init();
- }
-
- public static function isTransformTool(t:Tool) : Boolean
- {
- return t is TransformTool;
- }
-
- override protected function die() : void
- {
- super.die();
- }
-
- public function toolUpdate(type:Update = null) : void
- {
- if(type == null)
- {
- this.clearWireframes();
- update(GraphicUpdate.toolUpdate(),true,false);
- }
- else
- {
- update(type,true,true);
- }
- }
-
- protected function stageMouseUp(e:MouseEvent) : void
- {
- }
-
- protected function openCanvasMouseDown(e:MouseEvent) : void
- {
- }
-
- public function set settings(data:Settings) : void
- {
- }
-
- public function selectionUpdate(layers:Array) : void
- {
- update(Update.selectionUpdate({"layers":layers},false),true,true);
- }
-
- protected function setSelection(e:CanvasEvent) : void
- {
- }
-
- public function get canvasManager() : CanvasManager
- {
- return this.toolManager.canvasManager;
- }
-
- public function reset() : void
- {
- this.state = IDLE_STATE;
- this.canvasManager.removeEventListener(CanvasEvent.MOUSE_EVENT,this.canvasMouseEvent);
- this.canvasManager.removeEventListener(CanvasEvent.SELECTION_EVENT,this.setSelection);
- this.canvasManager.removeEventListener(CanvasEvent.KEY_EVENT,this.canvasKeyEvent);
- Canvas.CONTROLS.removeEventListener(MouseEvent.MOUSE_DOWN,this.controlsMouseHandler);
- Canvas.CONTROLS.removeEventListener(MouseEvent.MOUSE_UP,this.controlsMouseHandler);
- Canvas.STAGE.removeEventListener(Event.ENTER_FRAME,this.enterFrameHandler);
- this.canvas.stage.removeEventListener(MouseEvent.MOUSE_UP,this.stageMouseUp);
- }
-
- public function get isActive() : Boolean
- {
- return this.state == RUN_STATE || this.state == ACTIVE_STATE;
- }
-
- public function layerUpdate(delay:Boolean = true) : void
- {
- this.clearWireframes();
- update(GraphicUpdate.layerUpdate({"layers":this.activeLayers},delay),true,true);
- }
-
- public function finish(delay:Boolean = true, saveState:Boolean = true) : void
- {
- if(saveState)
- {
- StateManager.addItem(function(state:Object):void
- {
- canvasManager.setObjectsXML(state.data.beginObjectsXML);
- },function(state:Object):void
- {
- canvasManager.setObjectsXML(state.data.finishObjectsXML);
- },-1,{
- "beginObjectsXML":this.canvasManager._selectedObjects.slice(),
- "finishObjectsXML":this.canvasManager.storeSelectedObjects().slice()
- },-1,"Tool: finish");
- StateManager.closeState();
- }
- this.state = Tool.ACTIVE_STATE;
- this.toolUpdate(Update.finishUpdate(null,delay));
- }
-
- public function get ui() : UI
- {
- return this.toolManager.ui;
- }
-
- protected function enterFrameHandler(e:Event) : void
- {
- }
-
- private function init() : void
- {
- }
-
- public function get canvas() : Canvas
- {
- return this.canvasManager.canvas;
- }
-
- protected function clearWireframes() : void
- {
- Canvas.WIREFRAME.graphics.clear();
- }
-
- protected function canvasKeyEvent(e:CanvasEvent) : void
- {
- }
-
- public function get settings() : Settings
- {
- return new Settings();
- }
-
- public function get activeLayers() : Array
- {
- return this.canvasManager.activeLayers;
- }
-
- public function begin() : void
- {
- if(this.state != Tool.RUN_STATE)
- {
- StateManager.openState();
- this.canvasManager._selectedObjects = this.canvasManager.storeSelectedObjects();
- this.state = Tool.RUN_STATE;
- this.toolUpdate(Update.beginUpdate());
- }
- }
-
- public function get activeLayer() : Layer
- {
- return this.canvasManager.activeLayer;
- }
-
- protected function canvasMouseEvent(e:CanvasEvent) : void
- {
- var clickedContent:Boolean = false;
- var mouseEvent:MouseEvent = e.triggerEvent as MouseEvent;
- if(mouseEvent.type == MouseEvent.MOUSE_DOWN)
- {
- clickedContent = this.activeLayer.hitTest(mouseEvent.stageX,mouseEvent.stageY);
- if(clickedContent)
- {
- this.contentMouseDown(mouseEvent);
- }
- else
- {
- this.openCanvasMouseDown(mouseEvent);
- }
- }
- else if(mouseEvent.type == MouseEvent.MOUSE_UP)
- {
- this.canvasMouseUp(mouseEvent);
- }
- }
-
- protected function contentMouseDown(e:MouseEvent) : void
- {
- }
-
- protected function canvasMouseUp(e:MouseEvent) : void
- {
- }
-
- public function pullProps() : void
- {
- }
-
- public function get isRunning() : Boolean
- {
- return this.state == RUN_STATE;
- }
-
- protected function controlsMouseHandler(e:MouseEvent) : void
- {
- }
-
- public function get styleManager() : StyleManager
- {
- return this.toolManager.styleManager;
- }
-
- public function setup() : void
- {
- this.state = ACTIVE_STATE;
- this.canvasManager.addEventListener(CanvasEvent.MOUSE_EVENT,this.canvasMouseEvent);
- this.canvasManager.addEventListener(CanvasEvent.SELECTION_EVENT,this.setSelection);
- this.canvasManager.addEventListener(CanvasEvent.KEY_EVENT,this.canvasKeyEvent);
- Canvas.CONTROLS.addEventListener(MouseEvent.MOUSE_DOWN,this.controlsMouseHandler);
- Canvas.CONTROLS.addEventListener(MouseEvent.MOUSE_UP,this.controlsMouseHandler);
- Canvas.STAGE.addEventListener(Event.ENTER_FRAME,this.enterFrameHandler);
- this.canvas.stage.addEventListener(MouseEvent.MOUSE_UP,this.stageMouseUp);
- }
- }
- }
-
-