Merge branch 'master' into history
[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 flash.ui.Keyboard;
14         import mx.controls.Alert;
15         import mx.events.CloseEvent;
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 66:        setSourceTag(); break;                                                                                                  // B - set source tag for current object
80                                 case 67:        editableLayer.connection.closeChangeset(); break;                                               // C - close changeset
81                                 case 68:        editableLayer.alpha=1.3-editableLayer.alpha; return null;                               // D - dim
82                 case 72:    showHistory(); break;                                                   // H - History
83                                 case 83:        SaveManager.saveChanges(editableLayer.connection); break;                               // S - save
84                                 case 84:        controller.tagViewer.togglePanel(); return null;                                                // T - toggle tags panel
85                                 case 90:        if (!event.shiftKey) { MainUndoStack.getGlobalStack().undo(); return null;}// Z - undo
86                                             else { MainUndoStack.getGlobalStack().redo(); return null;  }           // Shift-Z - redo                                           
87                                 case Keyboard.ESCAPE:   revertSelection(); break;                                                                       // ESC - revert to server version
88                                 case Keyboard.NUMPAD_ADD:                                                                                                                       // + - add tag
89                                 case 187:       controller.tagViewer.selectAdvancedPanel();                                                             //   |
90                                                         controller.tagViewer.addNewTag(); return null;                                                  //   |
91                         }
92                         return null;
93                 }
94
95                 /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
96                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
97                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
98             var focus:Entity = getTopLevelFocusEntity(entity);
99
100                         if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
101                                 map.mouseUpHandler();   // in case the end-drag is over an EntityUI
102                         } else if ( event.type == MouseEvent.ROLL_OVER && paint && paint.interactive ) {
103                                 paint.setHighlight(focus, { hover: true });
104                         } else if ( event.type == MouseEvent.MOUSE_OUT && paint && paint.interactive ) {
105                                 paint.setHighlight(focus, { hover: false });
106                         } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
107                                 if      (event.delta > 0) { map.zoomIn(); }
108                                 else if (event.delta < 0) { map.zoomOut(); }
109                         }
110
111                         if ( paint && paint.isBackground ) {
112                                 if (event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
113                                         // alt-click to pull data out of vector background layer
114                                         var newSelection:Array=[];
115                                         if (selection.indexOf(entity)==-1) { selection=[entity]; }
116                                         for each (var entity:Entity in selection) {
117                                                 paint.setHighlight(entity, { hover:false, selected: false });
118                                                 if (entity is Way) paint.setHighlightOnNodes(Way(entity), { selectedway: false });
119                                                 newSelection.push(paint.pullThrough(entity,controller.map.editableLayer));
120                                         }
121                                         return controller.findStateForSelection(newSelection);
122                                 } else if (!paint.interactive) {
123                                         return null;
124                                 } else if (event.type == MouseEvent.MOUSE_DOWN && paint.interactive) {
125                                         if      (entity is Way   ) { return new SelectedWay(entity as Way, paint); }
126                                         else if (entity is Node  ) { if (!entity.hasParentWays) return new SelectedPOINode(entity as Node, paint); }
127                                         else if (entity is Marker) { return new SelectedMarker(entity as Marker, paint); }
128                                 } else if ( event.type == MouseEvent.MOUSE_UP && !event.ctrlKey) {
129                                         return (this is NoSelection) ? null : new NoSelection();
130                                 } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING && !event.ctrlKey) {
131                                         return (this is NoSelection) ? null : new NoSelection();
132                                 }
133                                         
134                         } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
135                                 if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
136                                         // select node within this way
137                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
138                                 } else if ( entity is Node && focus is Way ) {
139                                         // select way node
140                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
141                                 } else if ( controller.keyDown(Keyboard.SPACE) ) {
142                                         // drag the background imagery to compensate for poor alignment
143                                         return new DragBackground(event);
144                                 } else if (entity && selection.indexOf(entity)>-1) {
145                                         return new DragSelection(selection, event);
146                                 } else if (entity) {
147                                         return new DragSelection([entity], event);
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         /** Show the history dialog, if only one object is selected. */
203         protected function showHistory():void {
204             if (selectCount == 1) {
205                 new HistoryDialog().init(firstSelected);
206             } else if (selectCount == 0) {
207                 map.connection.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Can't show history, nothing selected"));
208             } else {
209                 map.connection.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Can't show history, multiple objects selected"));
210             }
211         }
212
213                 /** Create an action to add "source=*" tag to current entity based on background imagery. This is a convenient shorthand for users. */
214                 protected function setSourceTag():void {
215                         if (selectCount!=1) { return; }
216                         if (Imagery.instance().selected && Imagery.instance().selected.sourcetag) {
217                                 if ("sourcekey" in Imagery.instance().selected)
218                                     firstSelected.setTag(Imagery.instance().selected.sourcekey,Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
219                                 else
220                                     firstSelected.setTag('source',Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
221                         }
222                         controller.updateSelectionUI();
223                 }
224
225                 /** Revert all selected items to previously saved state, via a dialog box. */
226                 protected function revertSelection():void {
227                         if (selectCount==0) return;
228                         Alert.show("Revert selected items to the last saved version, discarding your changes?","Are you sure?",Alert.YES | Alert.CANCEL,null,revertHandler);
229                 }
230                 protected function revertHandler(event:CloseEvent):void {
231                         if (event.detail==Alert.CANCEL) return;
232                         for each (var item:Entity in _selection) {
233                                 item.connection.loadEntity(item);
234                         }
235                 }
236
237                 // Selection getters
238
239                 public function get selectCount():uint {
240                         return _selection.length;
241                 }
242
243                 public function get selection():Array {
244                         return _selection;
245                 }
246
247                 public function get firstSelected():Entity {
248                         if (_selection.length==0) { return null; }
249                         return _selection[0];
250                 }
251
252                 public function get selectedWay():Way {
253                         if (firstSelected is Way) { return firstSelected as Way; }
254                         return null;
255                 }
256
257                 public function get selectedWays():Array {
258                         var selectedWays:Array=[];
259                         for each (var item:Entity in _selection) {
260                                 if (item is Way) { selectedWays.push(item); }
261                         }
262                         return selectedWays;
263                 }
264
265         public function get selectedNodes():Array {
266             var selectedNodes:Array=[];
267             for each (var item:Entity in _selection) {
268                 if (item is Node) { selectedNodes.push(item); }
269             }
270             return selectedNodes;
271         }
272
273                 public function hasSelectedWays():Boolean {
274                         for each (var item:Entity in _selection) {
275                                 if (item is Way) { return true; }
276                         }
277                         return false;
278                 }
279
280                 public function hasSelectedAreas():Boolean {
281                         for each (var item:Entity in _selection) {
282                                 if (item is Way && Way(item).isArea()) { return true; }
283                         }
284                         return false;
285                 }
286
287                 public function hasSelectedUnclosedWays():Boolean {
288                         for each (var item:Entity in _selection) {
289                                 if (item is Way && !Way(item).isArea()) { return true; }
290                         }
291                         return false;
292                 }
293
294         /** Determine whether or not any nodes are selected, and if so whether any of them belong to areas. */
295         public function hasSelectedWayNodesInAreas():Boolean {
296             for each (var item:Entity in _selection) {
297                 if (item is Node) {
298                     var parentWays:Array = Node(item).parentWays;
299                     for each (var way:Entity in parentWays) {
300                         if (Way(way).isArea()) { return true; }
301                     }
302                 }
303             }
304             return false;
305         }
306
307                 public function hasAdjoiningWays():Boolean {
308                         if (_selection.length<2) { return false; }
309                         var endNodes:Object={};
310                         for each (var item:Entity in _selection) {
311                                 if (item is Way && !Way(item).isArea()) {
312                                         if (endNodes[Way(item).getNode(0).id]) return true;
313                                         if (endNodes[Way(item).getLastNode().id]) return true;
314                                         endNodes[Way(item).getNode(0).id]=true;
315                                         endNodes[Way(item).getLastNode().id]=true;
316                                 }
317                         }
318                         return false;
319                 }
320
321                 // Selection setters
322
323                 public function set selection(items:Array):void {
324                         _selection=items;
325                 }
326
327                 public function addToSelection(items:Array):void {
328                         for each (var item:Entity in items) {
329                                 if (_selection.indexOf(item)==-1) { _selection.push(item); }
330                         }
331                 }
332
333                 public function removeFromSelection(items:Array):void {
334                         for each (var item:Entity in items) {
335                                 if (_selection.indexOf(item)>-1) {
336                                         _selection.splice(_selection.indexOf(item),1);
337                                 }
338                         }
339                 }
340
341                 public function toggleSelection(item:Entity):Boolean {
342                         if (_selection.indexOf(item)==-1) {
343                                 _selection.push(item); return true;
344                         }
345                         _selection.splice(_selection.indexOf(item),1); return false;
346                 }
347     }
348 }