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