Merge pull request #47 from stevage/parking-types
[potlatch2.git] / net / systemeD / potlatch2 / controller / DragWayNode.as
index abf22e8e076a4ad24dac7ab0db007024c68b6624..b8c7911ef89aae28938a74aff29f378bf5d83d60 100644 (file)
@@ -2,46 +2,66 @@ package net.systemeD.potlatch2.controller {
        import flash.events.*;
     import net.systemeD.potlatch2.EditController;
     import net.systemeD.halcyon.connection.*;
-       import net.systemeD.halcyon.Globals;
+       import net.systemeD.halcyon.WayUI;
 
     public class DragWayNode extends ControllerState {
-        private var selectedWay:Way;
         private var draggingNode:Node;
+               private var draggingIndex:int;
         private var isDraggingStarted:Boolean = false;
                private var isNew:Boolean = false;
 
         private var downX:Number;
         private var downY:Number;
+               private var originalLat:Number;
+               private var originalLon:Number;
                private var dragstate:uint=NOT_MOVED;
                private const NOT_DRAGGING:uint=0;
                private const NOT_MOVED:uint=1;
                private const DRAGGING:uint=2;
+               
+               private var parentWay:Way;
+               private var initEvent:MouseEvent;
         
-        public function DragWayNode(way:Way, node:Node, event:MouseEvent, newNode:Boolean) {
-            selectedWay = way;
-            draggingNode = node;
+        public function DragWayNode(way:Way, index:int, event:MouseEvent, newNode:Boolean) {
+                       parentWay=way;
+                       draggingIndex=index;
             downX = event.localX;
             downY = event.localY;
                        isNew = newNode;
+                       initEvent=event;
+                       // the rest of the init will be done during enterState, because we need the controller to be initialised
         }
+
+        private function addNode(selectedWay:Way,event:MouseEvent):int {
+                       var ways:Array = layer.findWaysAtPoint(event.stageX, event.stageY, selectedWay);
+            var lat:Number = controller.map.coord2lat(event.localY);
+            var lon:Number = controller.map.coord2lon(event.localX);
+            var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
+            var node:Node = selectedWay.connection.createNode({}, lat, lon, undo.push);
+            var index:int = selectedWay.insertNodeAtClosestPosition(node, true, undo.push);
+                       for each (var w:Way in ways) { w.insertNodeAtClosestPosition(node, true, undo.push); }
+            MainUndoStack.getGlobalStack().addAction(undo);
+                       return index;
+        }
+
  
        override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
 
             if (event.type==MouseEvent.MOUSE_UP) {
                                if (dragstate==DRAGGING) {
                                        // mouse-up while dragging, so end drag
-                       return new SelectedWayNode(selectedWay,draggingNode);
-//                     return endDrag();
+                       return new SelectedWayNode(parentWay,draggingIndex);
                                } else if (event.shiftKey && !isNew) {
                                        // start new way
-                                       var way:Way = controller.connection.createWay({}, [entity, entity]);
-                                       return new DrawWay(way, true);
+                                       var way:Way = entity.connection.createWay({}, [entity],
+                                           MainUndoStack.getGlobalStack().addAction);
+                                       return new DrawWay(way, true, false);
                                } else if (event.shiftKey && isNew) {
-                       return new SelectedWayNode(selectedWay,draggingNode);
+                       return new SelectedWayNode(parentWay,draggingIndex);
                                } else {
                                        // select node
                                        dragstate=NOT_DRAGGING;
-                       return new SelectedWayNode(selectedWay,draggingNode);
+                       return SelectedWayNode.selectOrEdit(parentWay, draggingIndex);
                                }
 
                        } else if ( event.type == MouseEvent.MOUSE_MOVE) {
@@ -60,27 +80,46 @@ package net.systemeD.potlatch2.controller {
                        }
         }
 
-        private function endDrag():ControllerState {
-            return previousState;
-        }
-        
+               override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
+                       if (event.keyCode==27) {
+                               draggingNode.setLatLon( originalLat, originalLon, MainUndoStack.getGlobalStack().addAction );
+                       return new SelectedWayNode(parentWay,draggingIndex);
+                       }
+                       return this;
+               }
+
         private function dragTo(event:MouseEvent):ControllerState {
-            draggingNode.lat = controller.map.coord2lat(event.localY);
-            draggingNode.lon = controller.map.coord2lon(event.localX);
+                       draggingNode.setLatLon( controller.map.coord2lat(event.localY),
+                                    controller.map.coord2lon(event.localX),
+                                    MainUndoStack.getGlobalStack().addAction );
             return this;
         }
         
                public function forceDragStart():void {
                        dragstate=NOT_MOVED;
                }
+               
+               override public function get selectedWay():Way {
+                       return parentWay;
+               }
 
         override public function enterState():void {
-            controller.map.setHighlight(selectedWay, { showNodes: true } );
-                       Globals.vars.root.addDebug("**** -> "+this);
+                       if (isNew && draggingIndex==-1) { draggingIndex=addNode(parentWay,initEvent); }
+            draggingNode = parentWay.getNode(draggingIndex);
+                       originalLat = draggingNode.lat;
+                       originalLon = draggingNode.lon;
+
+                       layer.setHighlightOnNodes(parentWay, { selectedway: true } );
+                       layer.limitWayDrawing(parentWay, draggingIndex);
+                       layer.setHighlight(draggingNode, { selected: true } );
+                       layer.protectWay(parentWay);
+                       layer.limitWayDrawing(parentWay, NaN, draggingIndex);
         }
-        override public function exitState():void {
-            controller.map.setHighlight(selectedWay, { showNodes: false } );
-                       Globals.vars.root.addDebug("**** <- "+this);
+        override public function exitState(newState:ControllerState):void {
+                       layer.unprotectWay(parentWay);
+                       layer.limitWayDrawing(parentWay);
+                       layer.setHighlightOnNodes(parentWay, { selectedway: false } );
+                       layer.setHighlight(draggingNode, { selected: false } );
         }
         override public function toString():String {
             return "DragWayNode";