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