Merge pull request #47 from stevage/parking-types
[potlatch2.git] / net / systemeD / potlatch2 / controller / DragWayNode.as
index eb48f29cbd44044b062827c536ef233143d9aa62..b8c7911ef89aae28938a74aff29f378bf5d83d60 100644 (file)
@@ -2,7 +2,7 @@ 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 draggingNode:Node;
@@ -18,36 +18,50 @@ package net.systemeD.potlatch2.controller {
                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, index:int, event:MouseEvent, newNode:Boolean) {
-            selectedWay = way;
-                       draggingIndex = index;
-            draggingNode = way.getNode(index);
-                       originalLat = draggingNode.lat;
-                       originalLon = draggingNode.lon;
+                       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,draggingIndex);
-//                     return endDrag();
+                       return new SelectedWayNode(parentWay,draggingIndex);
                                } else if (event.shiftKey && !isNew) {
                                        // start new way
-                                       var way:Way = controller.connection.createWay({}, [entity],
+                                       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,draggingIndex);
+                       return new SelectedWayNode(parentWay,draggingIndex);
                                } else {
                                        // select node
                                        dragstate=NOT_DRAGGING;
-                       return SelectedWayNode.selectOrEdit(selectedWay, draggingIndex);
+                       return SelectedWayNode.selectOrEdit(parentWay, draggingIndex);
                                }
 
                        } else if ( event.type == MouseEvent.MOUSE_MOVE) {
@@ -69,15 +83,11 @@ package net.systemeD.potlatch2.controller {
                override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
                        if (event.keyCode==27) {
                                draggingNode.setLatLon( originalLat, originalLon, MainUndoStack.getGlobalStack().addAction );
-                       return new SelectedWayNode(selectedWay,draggingIndex);
+                       return new SelectedWayNode(parentWay,draggingIndex);
                        }
                        return this;
                }
 
-        private function endDrag():ControllerState {
-            return previousState;
-        }
-        
         private function dragTo(event:MouseEvent):ControllerState {
                        draggingNode.setLatLon( controller.map.coord2lat(event.localY),
                                     controller.map.coord2lon(event.localX),
@@ -88,14 +98,28 @@ package net.systemeD.potlatch2.controller {
                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(newState:ControllerState):void {
-            controller.map.setHighlight(selectedWay, { showNodes: false } );
-                       Globals.vars.root.addDebug("**** <- "+this);
+                       layer.unprotectWay(parentWay);
+                       layer.limitWayDrawing(parentWay);
+                       layer.setHighlightOnNodes(parentWay, { selectedway: false } );
+                       layer.setHighlight(draggingNode, { selected: false } );
         }
         override public function toString():String {
             return "DragWayNode";