Improve selection for background objects and tidy ControllerState layer handling
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWayNode.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.geom.Point;
4         import flash.ui.Keyboard;
5         
6         import net.systemeD.halcyon.AttentionEvent;
7         import net.systemeD.halcyon.WayUI;
8         import net.systemeD.halcyon.connection.*;
9         import net.systemeD.halcyon.connection.actions.*;
10         import net.systemeD.potlatch2.tools.Quadrilateralise;
11
12     public class SelectedWayNode extends ControllerState {
13                 private var parentWay:Way;
14                 private var initIndex:int;
15                 private var selectedIndex:int;
16         
17         public function SelectedWayNode(way:Way,index:int) {
18             parentWay = way;
19                         initIndex = index;
20         }
21  
22         protected function selectNode(way:Way,index:int):void {
23                         var node:Node=way.getNode(index);
24             if ( way == parentWay && node == firstSelected )
25                 return;
26
27             clearSelection(this);
28             layer.setHighlight(way, { hover: false });
29             layer.setHighlight(node, { selected: true });
30             layer.setHighlightOnNodes(way, { selectedway: true });
31             selection = [node]; parentWay = way;
32             controller.updateSelectionUI();
33                         selectedIndex = index; initIndex = index;
34         }
35                 
36         protected function clearSelection(newState:ControllerState):void {
37             if ( selectCount ) {
38                 layer.setHighlight(parentWay, { selected: false });
39                                 layer.setHighlight(firstSelected, { selected: false });
40                                 layer.setHighlightOnNodes(parentWay, { selectedway: false });
41                                 selection = [];
42                 if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
43             }
44         }
45         
46         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
47                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
48             var focus:Entity = getTopLevelFocusEntity(entity);
49
50             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
51                                 // start new way
52                 var way:Way = entity.connection.createWay({}, [entity],
53                     MainUndoStack.getGlobalStack().addAction);
54                 return new DrawWay(way, true, false);
55                         } else if ( event.type == MouseEvent.MOUSE_UP && entity is Node && focus == parentWay ) {
56                                 // select node within way
57                                 return selectOrEdit(parentWay, getNodeIndex(parentWay,Node(entity)));
58             } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==parentWay && event.shiftKey) {
59                                 // insert node within way (shift-click)
60                         var d:DragWayNode=new DragWayNode(parentWay, -1, event, true);
61                                 d.forceDragStart();
62                                 return d;
63                         }
64                         var cs:ControllerState = sharedMouseEvents(event, entity);
65                         return cs ? cs : this;
66         }
67
68                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
69                         switch (event.keyCode) {
70                                 case 189:                                       return removeNode();                                    // '-'
71                                 case 88:                                        return splitWay();                                              // 'X'
72                 case 81:  /* Q */           Quadrilateralise.quadrilateralise(parentWay, MainUndoStack.getGlobalStack().addAction); return this;
73                                 case 82:                                        repeatTags(firstSelected); return this; // 'R'
74                                 case 87:                                        return new SelectedWay(parentWay);              // 'W'
75                                 case 191:                                       return cycleWays();                                             // '/'
76                 case 74:                    if (event.shiftKey) { return unjoin() }; return join();// 'J'
77                                 case Keyboard.BACKSPACE:        return deleteNode();
78                                 case Keyboard.DELETE:           return deleteNode();
79                                 case 188: /* , */           return stepNode(-1);
80                                 case 190: /* . */           return stepNode(+1);           
81                         }
82                         var cs:ControllerState = sharedKeyboardEvents(event);
83                         return cs ? cs : this;
84                 }
85
86                 override public function get selectedWay():Way {
87                         return parentWay;
88                 }
89
90         public function get selectedNode():Node {
91             return parentWay.getNode(selectedIndex);
92         }
93         
94                 private function cycleWays():ControllerState {
95                         var wayList:Array=firstSelected.parentWays;
96                         if (wayList.length==1) { return this; }
97                         wayList.splice(wayList.indexOf(parentWay),1);
98             // find index of this node in the newly selected way, to maintain state for keyboard navigation
99             var newindex:int = Way(wayList[0]).indexOfNode(parentWay.getNode(initIndex));
100                         return new SelectedWay(wayList[0],
101                                                new Point(controller.map.lon2coord(Node(firstSelected).lon),
102                                                          controller.map.latp2coord(Node(firstSelected).latp)),
103                                                wayList.concat(parentWay),
104                                                newindex);
105                 }
106
107                 override public function enterState():void {
108             selectNode(parentWay,initIndex);
109                         layer.setPurgable(selection,false);
110         }
111                 override public function exitState(newState:ControllerState):void {
112             if (firstSelected.hasTags()) {
113               controller.clipboards['node']=firstSelected.getTagsCopy();
114             }
115                         layer.setPurgable(selection,true);
116             clearSelection(newState);
117         }
118
119         override public function toString():String {
120             return "SelectedWayNode";
121         }
122
123                 public static function selectOrEdit(selectedWay:Way, index:int):ControllerState {
124                         var isFirst:Boolean = false;
125                         var isLast:Boolean = false;
126                         var node:Node = selectedWay.getNode(index);
127                         isFirst = selectedWay.getNode(0) == node;
128                         isLast = selectedWay.getLastNode() == node;
129                         if ( isFirst == isLast )    // both == looped, none == central node 
130                             return new SelectedWayNode(selectedWay, index);
131                         else
132                             return new DrawWay(selectedWay, isLast, true);
133         }
134
135                 /** Splits a way into two separate ways, at the currently selected node. Handles simple loops and P-shapes. Untested for anything funkier. */
136                 public function splitWay():ControllerState {
137                         var n:Node=firstSelected as Node;
138                         var ni:uint = parentWay.indexOfNode(n);
139                         // abort if start or end
140                         if (parentWay.isPShape() && !parentWay.hasOnceOnly(n)) {
141                                 // If P-shaped, we want to split at the midway point on the stem, not at the end of the loop
142                                 ni = parentWay.getPJunctionNodeIndex();
143                                 
144                         } else {
145                             if (parentWay.getNode(0)    == n) { return this; }
146                             if (parentWay.getLastNode() == n) { return this; }
147                         }
148
149                         layer.setHighlightOnNodes(parentWay, { selectedway: false } );
150                         layer.setPurgable([parentWay],true);
151             MainUndoStack.getGlobalStack().addAction(new SplitWayAction(parentWay, ni));
152
153                         return new SelectedWay(parentWay);
154                 }
155                 
156                 public function removeNode():ControllerState {
157                         if (firstSelected.numParentWays==1 && parentWay.hasOnceOnly(firstSelected as Node) && !(firstSelected as Node).hasInterestingTags()) {
158                                 return deleteNode();
159                         }
160                         parentWay.removeNodeByIndex(selectedIndex, MainUndoStack.getGlobalStack().addAction);
161                         return new SelectedWay(parentWay);
162                 }
163                 
164                 public function deleteNode():ControllerState {
165                         layer.setPurgable(selection,true);
166                         firstSelected.remove(MainUndoStack.getGlobalStack().addAction);
167                         return new SelectedWay(parentWay);
168                 }
169
170         public function unjoin():ControllerState {
171             Node(firstSelected).unjoin(parentWay, MainUndoStack.getGlobalStack().addAction);
172             return this;
173         }
174
175         /** Attempt to either merge the currently selected node with another very nearby node, or failing that,
176         *   attach it mid-way along a very nearby way. */
177                 // FIXME: why are we only merging one node at once? after all, shift-click to insert a node adds into all ways
178         public function join():ControllerState {
179                         var p:Point = new Point(controller.map.lon2coord(Node(firstSelected).lon),
180                                                 controller.map.latp2coord(Node(firstSelected).latp));
181             var q:Point = map.localToGlobal(p);
182
183             // First, look for POI nodes in 20x20 pixel box around the current node
184                         // FIXME: why aren't we using a hitTest for this?
185             var hitnodes:Array = layer.connection.getObjectsByBbox(
186                 map.coord2lon(p.x-10),
187                 map.coord2lon(p.x+10),
188                 map.coord2lat(p.y-10),
189                 map.coord2lat(p.y+10)).poisInside;
190             
191             for each (var n: Node in hitnodes) {
192                 if (!n.hasParent(selectedWay)) { 
193                    return doMergeNodes(n);
194                 }
195             }
196             
197                         var ways:Array=layer.findWaysAtPoint(q.x, q.y, selectedWay);
198                         for each (var w:Way in ways) {
199                 // hit a way, now let's see if we hit a specific node
200                 for (var i:uint = 0; i < w.length; i++) {
201                                         n = w.getNode(i);
202                                         var x:Number = map.lon2coord(n.lon);
203                                         var y:Number = map.latp2coord(n.latp);
204                                         if (n != selectedNode && Math.abs(x-p.x) + Math.abs(y-p.y) < 10) {
205                                                 return doMergeNodes(n);
206                                         }
207                                 }
208             }
209
210             // No nodes hit, so join our node onto any overlapping ways.
211             Node(firstSelected).join(ways,MainUndoStack.getGlobalStack().addAction);
212             return this;
213         }
214         
215         private function doMergeNodes(n:Node): ControllerState {
216                 n.mergeWith(Node(firstSelected), MainUndoStack.getGlobalStack().addAction);
217             // only merge one node at a time - too confusing otherwise?
218             var msg:String = "Nodes merged."
219             if (MergeNodesAction.lastProblemTags) {
220                                 // FIXME: ugh, just ugh.
221                 msg += " *Warning* The following tags conflicted and need attention: " + MergeNodesAction.lastProblemTags;
222             }
223             controller.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, msg));
224             return new SelectedWayNode(n.parentWays[0], Way(n.parentWays[0]).indexOfNode(n));
225         }
226         
227         /** Move the selection one node further up or down this way, looping if necessary. */
228         public function stepNode(delta:int):ControllerState {
229             var ni:int = (selectedIndex + delta + parentWay.length) %  parentWay.length
230             controller.map.scrollIfNeeded(parentWay.getNode(ni).lat,parentWay.getNode(ni).lon);
231             return new SelectedWayNode(parentWay, ni);
232         }
233
234     }
235     
236     
237 }
238