fix tab selection issue, and remove annoying flash of drag-and-drop panel when going...
[potlatch2.git] / net / systemeD / potlatch2 / EditController.as
index 3b7881a..099697c 100644 (file)
@@ -2,80 +2,121 @@ package net.systemeD.potlatch2 {
     import net.systemeD.halcyon.Map;
     import net.systemeD.halcyon.MapController;
     import net.systemeD.halcyon.connection.*;
+    import net.systemeD.potlatch2.controller.*;
+       import mx.managers.CursorManager;
        import flash.events.*;
        import flash.geom.*;
 
     public class EditController implements MapController {
 
-        private var map:Map;
+        private var _map:Map;
         private var tagViewer:TagViewer;
-        private var selectedEntity:Entity;
+               private var toolbox:Toolbox;
         
-        private var draggingNode:Node = null;
+        public var state:ControllerState;
+        private var _connection:Connection;
         
+               private var keys:Object={};
+               public var clipboards:Object={};
+               public var imagery:Array=[];
 
-        public function EditController(map:Map, tagViewer:TagViewer) {
-            this.map = map;
+               [Embed(source="../../../embedded/pen.png")]             public var pen:Class;
+               [Embed(source="../../../embedded/pen_x.png")]           public var pen_x:Class;
+               [Embed(source="../../../embedded/pen_o.png")]           public var pen_o:Class;
+               [Embed(source="../../../embedded/pen_so.png")]          public var pen_so:Class;
+               [Embed(source="../../../embedded/pen_plus.png")]        public var pen_plus:Class;
+               
+        public function EditController(map:Map, tagViewer:TagViewer, toolbox:Toolbox) {
+            this._map = map;
             this.tagViewer = tagViewer;
+                       this.toolbox = toolbox;
+                       this.toolbox.init(this);
+            setState(new NoSelection());
             
             map.parent.addEventListener(MouseEvent.MOUSE_MOVE, mapMouseEvent);
             map.parent.addEventListener(MouseEvent.MOUSE_UP, mapMouseEvent);
             map.parent.addEventListener(MouseEvent.MOUSE_DOWN, mapMouseEvent);
+            map.parent.addEventListener(MouseEvent.CLICK, mapMouseEvent);
+            map.parent.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
+            map.parent.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
         }
 
         public function setActive():void {
             map.setController(this);
+            _connection = map.connection;
         }
 
-        private function mapMouseEvent(event:MouseEvent):void {
-            if ( draggingNode != null ) {
-                var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
-                event.localX = mapLoc.x;
-                event.localY = mapLoc.y;
-
-                processNodeEvent(event, null);
-            }
+        public function get map():Map {
+            return _map;
         }
         
-        public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
-            if ( event.type == MouseEvent.MOUSE_DOWN )
-                event.stopPropagation();
+        public function get connection():Connection {
+            return _connection;
+        }
+        
+        public function setSelectedEntity(entity:Entity):void {
+            tagViewer.setEntity(entity);
+                       toolbox.setEntity(entity);
+        }
+        
+        private function keyDownHandler(event:KeyboardEvent):void {
+                       keys[event.keyCode]=true;
+               }
 
-            if ( entity is Node || draggingNode != null ) {
-                processNodeEvent(event, entity);
-                return;
-            }
+        private function keyUpHandler(event:KeyboardEvent):void {
+            trace("key code "+event.keyCode);
+                       if (keys[event.keyCode]) { delete keys[event.keyCode]; }
+            var newState:ControllerState = state.processKeyboardEvent(event);
+            setState(newState);            
+               }
+
+               public function keyDown(key:Number):Boolean {
+                       return Boolean(keys[key]);
+               }
+
+        private function mapMouseEvent(event:MouseEvent):void {
+            map.stage.focus = map.parent;
             
-            if ( event.type == MouseEvent.CLICK ) {
-                if ( selectedEntity != null ) {
-                    map.setHighlight(selectedEntity, "selected", false);
-                    map.setHighlight(selectedEntity, "showNodes", false);
-                }
-                tagViewer.setEntity(entity);
-                map.setHighlight(entity, "selected", true);
-                map.setHighlight(entity, "showNodes", true);
-                selectedEntity = entity;
-            } else if ( event.type == MouseEvent.MOUSE_OVER )
-                map.setHighlight(entity, "hover", true);
-            else if ( event.type == MouseEvent.MOUSE_OUT )
-                map.setHighlight(entity, "hover", false);
+            var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
+            event.localX = mapLoc.x;
+            event.localY = mapLoc.y;
 
+            var newState:ControllerState = state.processMouseEvent(event, null);
+            setState(newState);
         }
+        
+        public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
+            map.stage.focus = map.parent;
+            //if ( event.type == MouseEvent.MOUSE_DOWN )
+            event.stopPropagation();
+                
+            var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
+            event.localX = mapLoc.x;
+            event.localY = mapLoc.y;
 
-        private function processNodeEvent(event:MouseEvent, entity:Entity):void {
-            if ( draggingNode != null ) {
-                if ( event.type == MouseEvent.MOUSE_UP ) {
-                    draggingNode = null;
-                } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
-                    draggingNode.lat = map.coord2lat(event.localY);
-                    draggingNode.lon = map.coord2lon(event.localX);
-                }
-            } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
-                draggingNode = entity as Node;
-            }
+            var newState:ControllerState = state.processMouseEvent(event, entity);
+            setState(newState);
         }
         
+        public function setState(newState:ControllerState):void {
+            if ( newState == state )
+                return;
+                
+            if ( state != null )
+                state.exitState(newState);
+            newState.setController(this);
+            newState.setPreviousState(state);
+            state = newState;
+            state.enterState();
+        }
+
+               public function setCursor(cursor:Class):void {
+                       CursorManager.removeAllCursors();
+                       if (cursor) { CursorManager.setCursor(cursor,2,-4,0); }
+               }
+
     }
 
+    
 }