Give "create multipolygon" a toolbox icon
[potlatch2.git] / net / systemeD / potlatch2 / controller / ControllerState.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.display.*;
4     import net.systemeD.halcyon.Map;
5     import net.systemeD.halcyon.MapPaint;
6     import net.systemeD.halcyon.connection.*;
7     import net.systemeD.potlatch2.collections.Imagery;
8     import net.systemeD.potlatch2.EditController;
9         import net.systemeD.potlatch2.save.SaveManager;
10         import net.systemeD.potlatch2.utils.SnapshotConnection;
11         import flash.ui.Keyboard;
12         import mx.controls.Alert;
13         import mx.events.CloseEvent;
14         
15     /** Represents a particular state of the controller, such as "dragging a way" or "nothing selected". Key methods are 
16     * processKeyboardEvent and processMouseEvent which take some action, and return a new state for the controller. 
17     * 
18     * This abstract class has some behaviour that applies in most states, and lots of 'null' behaviour. 
19     * */
20     public class ControllerState {
21
22         protected var controller:EditController;
23                 public var layer:MapPaint;
24         protected var previousState:ControllerState;
25
26                 protected var _selection:Array=[];
27
28         public function ControllerState() {}
29
30         public function setController(controller:EditController):void {
31             this.controller=controller;
32             if (!layer) layer=controller.map.editableLayer;
33         }
34
35         public function setPreviousState(previousState:ControllerState):void {
36             if ( this.previousState == null )
37                 this.previousState = previousState;
38         }
39
40                 public function isSelectionState():Boolean {
41                         return true;
42                 }
43
44         /** When triggered by a mouse action such as a click, perform an action on the given entity, then move to a new state. */
45         public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
46             return this;
47         }
48                 
49                 /** When triggered by a keypress, perform an action on the given entity, then move to a new state. */
50         public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
51             return this;
52         }
53
54         /** Retrieves the map associated with the current EditController */
55                 public function get map():Map {
56                         return controller.map;
57                 }
58
59         /** This is called when the EditController sets this ControllerState as the active state.
60         * Override this with whatever is needed, such as adding highlights to entities
61         */
62         public function enterState():void {}
63
64         /** This is called by the EditController as the current controllerstate is exiting.
65         * Override this with whatever cleanup is needed, such as removing highlights from entities
66         */
67         public function exitState(newState:ControllerState):void {}
68
69                 /** Represent the state in text for debugging. */
70                 public function toString():String {
71                         return "(No state)";
72                 }
73                 /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
74                 protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
75                         var editableLayer:MapPaint=controller.map.editableLayer;                                                                // shorthand for this method
76                         switch (event.keyCode) {
77                                 case 66:        setSourceTag(); break;                                                                                                  // B - set source tag for current object
78                                 case 67:        editableLayer.connection.closeChangeset(); break;                                               // C - close changeset
79                                 case 68:        editableLayer.alpha=1.3-editableLayer.alpha; return null;                               // D - dim
80                                 case 83:        SaveManager.saveChanges(editableLayer.connection); break;                               // S - save
81                                 case 84:        controller.tagViewer.togglePanel(); return null;                                                // T - toggle tags panel
82                                 case 90:        if (!event.shiftKey) { MainUndoStack.getGlobalStack().undo(); return null;}// Z - undo
83                                             else { MainUndoStack.getGlobalStack().redo(); return null;  }           // Shift-Z - redo                                           
84                                 case Keyboard.ESCAPE:   revertSelection(); break;                                                                       // ESC - revert to server version
85                                 case Keyboard.NUMPAD_ADD:                                                                                                                       // + - add tag
86                                 case 187:       controller.tagViewer.selectAdvancedPanel();                                                             //   |
87                                                         controller.tagViewer.addNewTag(); return null;                                                  //   |
88                         }
89                         return null;
90                 }
91
92                 /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
93                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
94                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
95             var focus:Entity = getTopLevelFocusEntity(entity);
96
97                         if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
98                                 map.mouseUpHandler();   // in case the end-drag is over an EntityUI
99                         } else if ( event.type == MouseEvent.ROLL_OVER && paint && paint.interactive ) {
100                                 paint.setHighlight(focus, { hover: true });
101                         } else if ( event.type == MouseEvent.MOUSE_OUT && paint && paint.interactive ) {
102                                 paint.setHighlight(focus, { hover: false });
103                         } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
104                                 if      (event.delta > 0) { map.zoomIn(); }
105                                 else if (event.delta < 0) { map.zoomOut(); }
106                         }
107
108                         if ( paint && paint.isBackground ) {
109                                 if (event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
110                                         // alt-click to pull data out of vector background layer
111                                         var newSelection:Array=[];
112                                         if (selection.indexOf(entity)==-1) { selection=[entity]; }
113                                         for each (var entity:Entity in selection) {
114                                                 paint.setHighlight(entity, { hover:false, selected: false });
115                                                 if (entity is Way) paint.setHighlightOnNodes(Way(entity), { selectedway: false });
116                                                 newSelection.push(paint.pullThrough(entity,controller.map.editableLayer));
117                                         }
118                                         return controller.findStateForSelection(newSelection);
119                                 } else if (!paint.interactive) {
120                                         return null;
121                                 } else if (event.type == MouseEvent.MOUSE_DOWN && paint.interactive) {
122                                         if      (entity is Way   ) { return new SelectedWay(entity as Way, paint); }
123                                         else if (entity is Node  ) { if (!entity.hasParentWays) return new SelectedPOINode(entity as Node, paint); }
124                                         else if (entity is Marker) { return new SelectedMarker(entity as Marker, paint); }
125                                 } else if ( event.type == MouseEvent.MOUSE_UP && !event.ctrlKey) {
126                                         return (this is NoSelection) ? null : new NoSelection();
127                                 } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING && !event.ctrlKey) {
128                                         return (this is NoSelection) ? null : new NoSelection();
129                                 }
130                                         
131                         } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
132                                 if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
133                                         // select node within this way
134                                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
135                                 } else if ( controller.keyDown(Keyboard.SPACE) ) {
136                                         // drag the background imagery to compensate for poor alignment
137                                         return new DragBackground(event);
138                                 } else if (entity && selection.indexOf(entity)>-1) {
139                                         return new DragSelection(selection, event);
140                                 } else if (entity) {
141                                         return controller.findStateForSelection([entity]);
142                                 } else if (event.ctrlKey && !layer.isBackground) {
143                                         return new SelectArea(event.localX,event.localY,selection);
144                                 }
145
146             } else if ( event.type==MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING && !event.ctrlKey) {
147                 return (this is NoSelection) ? null : new NoSelection();
148             }
149                         return null;
150                 }
151
152                 /** Gets the way that the selected node is part of, if that makes sense. If not, return the node, or the way, or nothing. */
153                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
154                         if ( entity is Node ) {
155                                 for each (var parent:Entity in entity.parentWays) {
156                                         return parent;
157                                 }
158                                 return entity;
159                         } else if ( entity is Way ) {
160                                 return entity;
161                         } else {
162                                 return null;
163                         }
164                 }
165
166                 /** Find the MapPaint object that this DisplayObject belongs to. */
167                 protected function getMapPaint(d:DisplayObject):MapPaint {
168                         while (d) {
169                                 if (d is MapPaint) { return MapPaint(d); }
170                                 d=d.parent;
171                         }
172                         return null;
173                 }
174
175                 protected function getNodeIndex(way:Way,node:Node):uint {
176                         for (var i:uint=0; i<way.length; i++) {
177                                 if (way.getNode(i)==node) { return i; }
178                         }
179                         return null;
180                 }
181
182                 /** Create a "repeat tags" action on the current entity, if possible. */
183                 protected function repeatTags(object:Entity):void {
184                         if (!controller.clipboards[object.getType()]) { return; }
185                         object.suspend();
186
187                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
188                         for (var k:String in controller.clipboards[object.getType()]) {
189                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
190                         }
191                         MainUndoStack.getGlobalStack().addAction(undo);
192                         controller.updateSelectionUI();
193                         object.resume();
194
195
196                 }
197
198                 /** Create an action to add "source=*" tag to current entity based on background imagery. This is a convenient shorthand for users. */
199                 protected function setSourceTag():void {
200                         if (selectCount!=1) { return; }
201                         if (Imagery.instance().selected && Imagery.instance().selected.sourcetag) {
202                                 if ("sourcekey" in Imagery.instance().selected)
203                                     firstSelected.setTag(Imagery.instance().selected.sourcekey,Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
204                                 else
205                                     firstSelected.setTag('source',Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
206                         }
207                         controller.updateSelectionUI();
208                 }
209
210                 /** Revert all selected items to previously saved state, via a dialog box. */
211                 protected function revertSelection():void {
212                         if (selectCount==0) return;
213                         Alert.show("Revert selected items to the last saved version, discarding your changes?","Are you sure?",Alert.YES | Alert.CANCEL,null,revertHandler);
214                 }
215                 protected function revertHandler(event:CloseEvent):void {
216                         if (event.detail==Alert.CANCEL) return;
217                         for each (var item:Entity in _selection) {
218                                 item.connection.loadEntity(item);
219                         }
220                 }
221
222                 // Selection getters
223
224                 public function get selectCount():uint {
225                         return _selection.length;
226                 }
227
228                 public function get selection():Array {
229                         return _selection;
230                 }
231
232                 public function get firstSelected():Entity {
233                         if (_selection.length==0) { return null; }
234                         return _selection[0];
235                 }
236
237                 public function get selectedWay():Way {
238                         if (firstSelected is Way) { return firstSelected as Way; }
239                         return null;
240                 }
241
242                 public function get selectedWays():Array {
243                         var selectedWays:Array=[];
244                         for each (var item:Entity in _selection) {
245                                 if (item is Way) { selectedWays.push(item); }
246                         }
247                         return selectedWays;
248                 }
249
250         public function get selectedNodes():Array {
251             var selectedNodes:Array=[];
252             for each (var item:Entity in _selection) {
253                 if (item is Node) { selectedNodes.push(item); }
254             }
255             return selectedNodes;
256         }
257
258                 public function hasSelectedWays():Boolean {
259                         for each (var item:Entity in _selection) {
260                                 if (item is Way) { return true; }
261                         }
262                         return false;
263                 }
264
265                 public function hasSelectedAreas():Boolean {
266                         for each (var item:Entity in _selection) {
267                                 if (item is Way && Way(item).isArea()) { return true; }
268                         }
269                         return false;
270                 }
271
272                 public function hasSelectedUnclosedWays():Boolean {
273                         for each (var item:Entity in _selection) {
274                                 if (item is Way && !Way(item).isArea()) { return true; }
275                         }
276                         return false;
277                 }
278
279         /** Determine whether or not any nodes are selected, and if so whether any of them belong to areas. */
280         public function hasSelectedWayNodesInAreas():Boolean {
281             for each (var item:Entity in _selection) {
282                 if (item is Node) {
283                     var parentWays:Array = Node(item).parentWays;
284                     for each (var way:Entity in parentWays) {
285                         if (Way(way).isArea()) { return true; }
286                     }
287                 }
288             }
289             return false;
290         }
291
292                 public function hasAdjoiningWays():Boolean {
293                         if (_selection.length<2) { return false; }
294                         var endNodes:Object={};
295                         for each (var item:Entity in _selection) {
296                                 if (item is Way && !Way(item).isArea()) {
297                                         if (endNodes[Way(item).getNode(0).id]) return true;
298                                         if (endNodes[Way(item).getLastNode().id]) return true;
299                                         endNodes[Way(item).getNode(0).id]=true;
300                                         endNodes[Way(item).getLastNode().id]=true;
301                                 }
302                         }
303                         return false;
304                 }
305
306                 /** Identify the inners and outer from the current selection for making a multipolygon. */
307                 
308                 public function multipolygonMembers():Object {
309                         if (_selection.length<2) { return {}; }
310
311                         var entity:Entity;
312                         var relation:Relation;
313                         var outer:Way;
314                         var inners:Array=[];
315
316                         // If there's an existing outer in the selection, use that
317                         for each (entity in selection) {
318                                 if (!entity is Way) return {};
319                                 var r:Array=entity.findParentRelationsOfType('multipolygon','outer');
320                                 if (r.length) { outer=Way(entity); relation=r[0]; }
321                         }
322
323                         // Otherwise, find the way with the biggest area
324                         var largest:Number=0;
325                         if (!outer) {
326                                 for each (entity in selection) {
327                                         if (!entity is Way) return {};
328                                         if (!Way(entity).isArea()) return {};
329                                         var props:Object=layer.wayUIProperties(entity as Way);
330                                         if (props.patharea>largest) { outer=Way(entity); largest=props.patharea; }
331                                 }
332                         }
333                         if (!outer) return {};
334                         
335                         // Identify the inners
336                         for each (entity in selection) {
337                                 if (entity==outer) continue;
338                                 if (!entity is Way) return {};
339                                 if (!Way(entity).isArea()) return {};
340                                 var node:Node=Way(entity).getFirstNode();
341                                 if (outer.pointWithin(node.lon,node.lat)) inners.push(entity);
342                         }
343                         if (inners.length==0) return {};
344                         
345                         return { outer: outer,
346                                  inners: inners,
347                                  relation: relation }
348                 }
349
350
351                 // Selection setters
352
353                 public function set selection(items:Array):void {
354                         _selection=items;
355                 }
356
357                 public function addToSelection(items:Array):void {
358                         for each (var item:Entity in items) {
359                                 if (_selection.indexOf(item)==-1) { _selection.push(item); }
360                         }
361                 }
362
363                 public function removeFromSelection(items:Array):void {
364                         for each (var item:Entity in items) {
365                                 if (_selection.indexOf(item)>-1) {
366                                         _selection.splice(_selection.indexOf(item),1);
367                                 }
368                         }
369                 }
370
371                 public function toggleSelection(item:Entity):Boolean {
372                         if (_selection.indexOf(item)==-1) {
373                                 _selection.push(item); return true;
374                         }
375                         _selection.splice(_selection.indexOf(item),1); return false;
376                 }
377     }
378 }