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