fix backspacing issue on self-intersecting ways
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWayNode.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.ui.Keyboard;
4     import net.systemeD.potlatch2.EditController;
5     import net.systemeD.halcyon.connection.*;
6     import net.systemeD.halcyon.connection.actions.*;
7         import net.systemeD.halcyon.Globals;
8
9     public class SelectedWayNode extends SelectedWay {
10                 protected var selectedIndex:int;
11                 protected var initIndex:int;
12         
13         public function SelectedWayNode(way:Way,index:int) {
14                         super (way);
15                         initIndex = index;
16         }
17  
18         protected function selectNode(way:Way,index:int):void {
19                         var node:Node=way.getNode(index);
20             if ( way == selectedWay && node == selectedNode )
21                 return;
22
23             clearSelection(this);
24             controller.setSelectedEntity(node);
25             controller.map.setHighlight(way, { hover: false, showNodes: true, nodeSelected: node.id });
26             selectedWay = way; initWay = way;
27                         selectedIndex = index; initIndex = index;
28             selectedNode = node;
29         }
30                 
31         override protected function clearSelection(newState:ControllerState):void {
32             if ( selectedNode != null ) {
33                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, nodeSelected: null });
34                 if (!newState.isSelectionState()) { controller.setSelectedEntity(null); }
35                 selectedNode = null;
36                                 selectedWay = null;
37             }
38         }
39         
40         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
41                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
42             var focus:Entity = getTopLevelFocusEntity(entity);
43
44             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
45                                 // start new way
46                 var way:Way = controller.connection.createWay({}, [entity],
47                     MainUndoStack.getGlobalStack().addAction);
48                 return new DrawWay(way, true, false);
49                         } else if ( event.type == MouseEvent.MOUSE_UP && entity is Node && focus == selectedWay ) {
50                                 // select node within way
51                                 return selectOrEdit(selectedWay, getNodeIndex(selectedWay,Node(entity)));
52             } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==selectedWay && event.shiftKey) {
53                                 // insert node within way (shift-click)
54                         var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
55                                 d.forceDragStart();
56                                 return d;
57                         }
58                         var cs:ControllerState = sharedMouseEvents(event, entity);
59                         return cs ? cs : this;
60         }
61
62                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
63                         switch (event.keyCode) {
64                                 case 189:                                       return removeNode();                                    // '-'
65                                 case 88:                                        return splitWay();                                              // 'X'
66                                 case 82:                                        repeatTags(selectedNode); return this;  // 'R'
67                                 case Keyboard.BACKSPACE:        return deleteNode();
68                                 case Keyboard.DELETE:           return deleteNode();
69                         }
70                         var cs:ControllerState = sharedKeyboardEvents(event);
71                         return cs ? cs : this;
72                 }
73                 
74                 override public function enterState():void {
75             selectNode(initWay,initIndex);
76                         Globals.vars.root.addDebug("**** -> "+this);
77         }
78                 override public function exitState(newState:ControllerState):void {
79                         controller.clipboards['node']=selectedNode.getTagsCopy();
80             clearSelection(newState);
81                         Globals.vars.root.addDebug("**** <- "+this);
82         }
83
84         override public function toString():String {
85             return "SelectedWayNode";
86         }
87
88         public static function selectOrEdit(selectedWay:Way, index:int):ControllerState {
89                 var isFirst:Boolean = false;
90                         var isLast:Boolean = false;
91                         var node:Node = selectedWay.getNode(index);
92                         isFirst = selectedWay.getNode(0) == node;
93                         isLast = selectedWay.getNode(selectedWay.length - 1) == node;
94                         if ( isFirst == isLast )    // both == looped, none == central node 
95                             return new SelectedWayNode(selectedWay, index);
96                         else
97                             return new DrawWay(selectedWay, isLast, true);
98         }
99
100                 public function splitWay():ControllerState {
101                         // abort if start or end
102                         if (selectedWay.getNode(0                   ) == selectedNode) { return this; }
103                         if (selectedWay.getNode(selectedWay.length-1) == selectedNode) { return this; }
104
105             MainUndoStack.getGlobalStack().addAction(new SplitWayAction(selectedWay, selectedNode));
106
107                         return new SelectedWay(selectedWay);
108                 }
109                 
110                 public function removeNode():ControllerState {
111                         if (selectedNode.numParentWays==1 && selectedWay.hasOnceOnly(selectedNode)) {
112                                 return deleteNode();
113                         }
114                         selectedWay.removeNodeByIndex(selectedIndex, MainUndoStack.getGlobalStack().addAction);
115                         return new SelectedWay(selectedWay);
116                 }
117                 
118                 public function deleteNode():ControllerState {
119                         selectedNode.remove(MainUndoStack.getGlobalStack().addAction);
120                         return new SelectedWay(selectedWay);
121                 }
122
123     }
124 }