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