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