refactoring and some bugfixes
[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 ControllerState {
10                 private var parentWay:Way;
11                 private var initIndex:int;
12                 private var selectedIndex:int;
13         
14         public function SelectedWayNode(way:Way,index:int) {
15             parentWay = way;
16                         initIndex = index;
17         }
18  
19         protected function selectNode(way:Way,index:int):void {
20                         var node:Node=way.getNode(index);
21             if ( way == parentWay && node == firstSelected )
22                 return;
23
24             clearSelection(this);
25             controller.map.setHighlight(way, { hover: false });
26             controller.map.setHighlight(node, { selected: true });
27             controller.map.setHighlightOnNodes(way, { selectedway: true });
28             selection = [node]; parentWay = way;
29             controller.updateSelectionUI();
30                         selectedIndex = index; initIndex = index;
31         }
32                 
33         protected function clearSelection(newState:ControllerState):void {
34             if ( selectCount ) {
35                 controller.map.setHighlight(parentWay, { selected: false });
36                                 controller.map.setHighlight(firstSelected, { selected: false });
37                                 controller.map.setHighlightOnNodes(parentWay, { selectedway: false });
38                                 selection = []; parentWay = null;
39                 if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
40             }
41         }
42         
43         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
44                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
45             var focus:Entity = getTopLevelFocusEntity(entity);
46
47             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
48                                 // start new way
49                 var way:Way = controller.connection.createWay({}, [entity],
50                     MainUndoStack.getGlobalStack().addAction);
51                 return new DrawWay(way, true, false);
52                         } else if ( event.type == MouseEvent.MOUSE_UP && entity is Node && focus == parentWay ) {
53                                 // select node within way
54                                 return selectOrEdit(parentWay, getNodeIndex(parentWay,Node(entity)));
55             } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==parentWay && event.shiftKey) {
56                                 // insert node within way (shift-click)
57                         var d:DragWayNode=new DragWayNode(parentWay, -1, event, true);
58                                 d.forceDragStart();
59                                 return d;
60                         }
61                         var cs:ControllerState = sharedMouseEvents(event, entity);
62                         return cs ? cs : this;
63         }
64
65                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
66                         switch (event.keyCode) {
67                                 case 189:                                       return removeNode();                                    // '-'
68                                 case 88:                                        return splitWay();                                              // 'X'
69                                 case 82:                                        repeatTags(firstSelected); return this; // 'R'
70                                 case 87:                                        return new SelectedWay(parentWay);              // 'W'
71                                 case Keyboard.BACKSPACE:        return deleteNode();
72                                 case Keyboard.DELETE:           return deleteNode();
73                         }
74                         var cs:ControllerState = sharedKeyboardEvents(event);
75                         return cs ? cs : this;
76                 }
77
78                 override public function get selectedWay():Way {
79                         return parentWay;
80                 }
81
82                 override public function enterState():void {
83             selectNode(parentWay,initIndex);
84                         controller.map.setPurgable(firstSelected,false);
85                         Globals.vars.root.addDebug("**** -> "+this);
86         }
87                 override public function exitState(newState:ControllerState):void {
88                         controller.clipboards['node']=firstSelected.getTagsCopy();
89                         controller.map.setPurgable(firstSelected,true);
90             clearSelection(newState);
91                         Globals.vars.root.addDebug("**** <- "+this);
92         }
93
94         override public function toString():String {
95             return "SelectedWayNode";
96         }
97
98                 public static function selectOrEdit(selectedWay:Way, index:int):ControllerState {
99                         var isFirst:Boolean = false;
100                         var isLast:Boolean = false;
101                         var node:Node = selectedWay.getNode(index);
102                         isFirst = selectedWay.getNode(0) == node;
103                         isLast = selectedWay.getLastNode() == node;
104                         if ( isFirst == isLast )    // both == looped, none == central node 
105                             return new SelectedWayNode(selectedWay, index);
106                         else
107                             return new DrawWay(selectedWay, isLast, true);
108         }
109
110                 public function splitWay():ControllerState {
111                         // abort if start or end
112                         if (parentWay.getNode(0)    == firstSelected) { return this; }
113                         if (parentWay.getLastNode() == firstSelected) { return this; }
114
115                         controller.map.setHighlightOnNodes(parentWay, { selectedway: false } );
116                         controller.map.setPurgable(parentWay,true);
117             MainUndoStack.getGlobalStack().addAction(new SplitWayAction(parentWay, firstSelected as Node));
118
119                         return new SelectedWay(parentWay);
120                 }
121                 
122                 public function removeNode():ControllerState {
123                         if (firstSelected.numParentWays==1 && parentWay.hasOnceOnly(firstSelected as Node)) {
124                                 return deleteNode();
125                         }
126                         parentWay.removeNodeByIndex(selectedIndex, MainUndoStack.getGlobalStack().addAction);
127                         return new SelectedWay(parentWay);
128                 }
129                 
130                 public function deleteNode():ControllerState {
131                         controller.map.setPurgable(firstSelected,true);
132                         firstSelected.remove(MainUndoStack.getGlobalStack().addAction);
133                         return new SelectedWay(parentWay);
134                 }
135
136     }
137 }