slight refactoring of controller states
[potlatch2.git] / net / systemeD / potlatch2 / controller / DrawWay.as
index 29a3f7d9a544349ebad1deb7e4d1656a3bed211b..5517952892add180c2b9ad4ed5314556a88c19ca 100644 (file)
@@ -27,7 +27,7 @@ package net.systemeD.potlatch2.controller {
                override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
                        var mouse:Point;
                        var node:Node;
-                       var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
+                       var focus:Entity = getTopLevelFocusEntity(entity);
 
                        if ( event.type == MouseEvent.MOUSE_UP ) {
                                if ( entity == null ) {
@@ -47,15 +47,19 @@ package net.systemeD.potlatch2.controller {
                                                        return stopDrawing();
                                                }
                                        } else {
-                                               appendNode(entity as Node);
+                                               appendNode(entity as Node, MainUndoStack.getGlobalStack().addAction);
                                                controller.map.setHighlight(focus, { showNodesHover: false });
                                                controller.map.setHighlight(selectedWay, { showNodes: true });
                                                resetElastic(entity as Node);
                                                lastClick=entity;
+                                               if (selectedWay.getNode(0)==selectedWay.getNode(selectedWay.length-1)) {
+                                                       return new SelectedWay(selectedWay);
+                                               }
                                        }
                                } else if ( entity is Way ) {
                                        node = createAndAddNode(event);
-                                       Way(entity).insertNodeAtClosestPosition(node, true);
+                                       Way(entity).insertNodeAtClosestPosition(node, true,
+                                           MainUndoStack.getGlobalStack().addAction);
                                        resetElastic(node);
                                        lastClick=node;
                                }
@@ -65,7 +69,7 @@ package net.systemeD.potlatch2.controller {
                                                  controller.map.coord2lon(event.localX),
                                                  controller.map.coord2latp(event.localY));
                                elastic.end = mouse;
-                       } else if ( event.type == MouseEvent.MOUSE_OVER && focus!=selectedWay) {
+                       } else if ( event.type == MouseEvent.ROLL_OVER && focus!=selectedWay) {
                                controller.map.setHighlight(focus, { showNodesHover: true });
                        } else if ( event.type == MouseEvent.MOUSE_OUT  && focus!=selectedWay) {
                                controller.map.setHighlight(focus, { showNodesHover: false });
@@ -96,7 +100,7 @@ package net.systemeD.potlatch2.controller {
                protected function stopDrawing():ControllerState {
                        if ( selectedWay.length<2) {
                                controller.map.setHighlight(selectedWay, { showNodes: false });
-                               selectedWay.remove();
+                               selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
                                // delete controller.map.ways[selectedWay.id];
                                return new NoSelection();
                        } else if ( leaveNodeSelected ) {
@@ -108,18 +112,22 @@ package net.systemeD.potlatch2.controller {
                }
 
                public function createAndAddNode(event:MouseEvent):Node {
+                   var undo:CompositeUndoableAction = new CompositeUndoableAction("Add node");
+                   
                        var lat:Number = controller.map.coord2lat(event.localY);
                        var lon:Number = controller.map.coord2lon(event.localX);
-                       var node:Node = controller.connection.createNode({}, lat, lon);
-                       appendNode(node);
+                       var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
+                       appendNode(node, undo.push);
+                       
+                       MainUndoStack.getGlobalStack().addAction(undo);
                        return node;
                }
                
-               protected function appendNode(node:Node):void {
+               protected function appendNode(node:Node, performAction:Function):void {
                        if ( editEnd )
-                               selectedWay.appendNode(node);
+                               selectedWay.appendNode(node, performAction);
                        else
-                               selectedWay.insertNode(0, node);
+                               selectedWay.insertNode(0, node, performAction);
                }
                
                override public function enterState():void {