remove node from this way only, and some fixes
[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();
24             controller.setSelectedEntity(node);
25             controller.map.setHighlight(way, { 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():void {
32             if ( selectedNode != null ) {
33                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, nodeSelected: null });
34                 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                         if (cs) return cs;
60                         return this;
61         }
62
63                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
64                         switch (event.keyCode) {
65                                 case 189:                                       return removeNode();    // '-'
66                                 case 88:                                        return splitWay();              // 'X'
67                                 case Keyboard.BACKSPACE:        return deleteNode();
68                                 case Keyboard.DELETE:           return deleteNode();
69                         }
70                         return this;
71                 }
72                 
73                 override public function enterState():void {
74             selectNode(initWay,initIndex);
75                         Globals.vars.root.addDebug("**** -> "+this);
76         }
77                 override public function exitState():void {
78             clearSelection();
79                         Globals.vars.root.addDebug("**** <- "+this);
80         }
81
82         override public function toString():String {
83             return "SelectedWayNode";
84         }
85
86         public static function selectOrEdit(selectedWay:Way, index:int):ControllerState {
87                 var isFirst:Boolean = false;
88                         var isLast:Boolean = false;
89                         var node:Node = selectedWay.getNode(index);
90                         isFirst = selectedWay.getNode(0) == node;
91                         isLast = selectedWay.getNode(selectedWay.length - 1) == node;
92                         if ( isFirst == isLast )    // both == looped, none == central node 
93                             return new SelectedWayNode(selectedWay, index);
94                         else
95                             return new DrawWay(selectedWay, isLast, true);
96         }
97
98                 public function splitWay():ControllerState {
99                         // abort if start or end
100                         if (selectedWay.getNode(0                   ) == selectedNode) { return this; }
101                         if (selectedWay.getNode(selectedWay.length-1) == selectedNode) { return this; }
102
103             MainUndoStack.getGlobalStack().addAction(new SplitWayAction(selectedWay, selectedNode));
104
105                         return new SelectedWay(selectedWay);
106                 }
107                 
108                 public function removeNode():ControllerState {
109                         if (selectedNode.numParentWays==1) { return deleteNode(); }
110                         selectedWay.removeNodeByIndex(selectedIndex, MainUndoStack.getGlobalStack().addAction);
111                         return new SelectedWay(selectedWay);
112                 }
113                 
114                 public function deleteNode():ControllerState {
115                         selectedNode.remove(MainUndoStack.getGlobalStack().addAction);
116                         return new SelectedWay(selectedWay);
117                 }
118
119     }
120 }