Add some documenation notes, and remove some traces
[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 flash.ui.Keyboard;
13         import mx.controls.Alert;
14         import mx.events.CloseEvent;
15         
16     /** Represents a particular state of the controller, such as "dragging a way" or "nothing selected". Key methods are 
17     * processKeyboardEvent and processMouseEvent which take some action, and return a new state for the controller. 
18     * 
19     * This abstract class has some behaviour that applies in most states, and lots of 'null' behaviour. 
20     * */
21     public class ControllerState {
22
23         protected var controller:EditController;
24         protected var previousState:ControllerState;
25
26                 protected var _selection:Array=[];
27
28         public function ControllerState() {}
29
30         public function setController(controller:EditController):void {
31             this.controller = controller;
32         }
33
34         public function setPreviousState(previousState:ControllerState):void {
35             if ( this.previousState == null )
36                 this.previousState = previousState;
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                 public function get map():Map {
54                         return controller.map;
55                 }
56
57         public function enterState():void {}
58         public function exitState(newState:ControllerState):void {}
59
60                 /** Represent the state in text for debugging. */
61                 public function toString():String {
62                         return "(No state)";
63                 }
64                 /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
65                 protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
66                         switch (event.keyCode) {
67                                 case 66:        setSourceTag(); break;                                                                                                  // B - set source tag for current object
68                                 case 67:        controller.connection.closeChangeset(); break;                                                  // C - close changeset
69                                 case 68:        controller.map.paint.alpha=1.3-controller.map.paint.alpha; return null; // D - dim
70                 case 72:    showHistory(); break;                                                   // H - History
71                                 case 83:        SaveManager.saveChanges(); break;                                                                               // S - save
72                                 case 84:        controller.tagViewer.togglePanel(); return null;                                                // T - toggle tags panel
73                                 case 90:        if (!event.shiftKey) { MainUndoStack.getGlobalStack().undo(); return null;}// Z - undo
74                                             else { MainUndoStack.getGlobalStack().redo(); return null;  }           // Shift-Z - redo                                           
75                                 case Keyboard.ESCAPE:   revertSelection(); break;                                                                       // ESC - revert to server version
76                                 case Keyboard.NUMPAD_ADD:                                                                                                                       // + - add tag
77                                 case 187:       controller.tagViewer.selectAdvancedPanel();                                                             //   |
78                                                         controller.tagViewer.addNewTag(); return null;                                                  //   |
79                         }
80                         return null;
81                 }
82
83                 /** Default behaviour for the current state that should be called if state-specific action has been taken care of or ruled out. */
84                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
85                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
86             var focus:Entity = getTopLevelFocusEntity(entity);
87
88                         if ( paint && paint.isBackground ) {
89                                 if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
90                                         // alt-click to pull data out of vector background layer
91                                         var newEntity:Entity=paint.findSource().pullThrough(entity,controller.connection);
92                                         if (entity is Way) { return new SelectedWay(newEntity as Way); }
93                                         else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
94                 } else if (event.type == MouseEvent.MOUSE_DOWN && entity is Marker) {
95                     return new SelectedMarker(entity as Marker, paint.findSource());
96                                 } else if ( event.type == MouseEvent.MOUSE_UP ) {
97                                         return (this is NoSelection) ? null : new NoSelection();
98                                 } else { return null; }
99                         }
100
101                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
102                                 if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
103                                         // select node within this way
104                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
105                                 } else if ( entity is Node && focus is Way ) {
106                                         // select way node
107                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
108                                 } else if ( controller.keyDown(Keyboard.SPACE) ) {
109                                         // drag the background imagery to compensate for poor alignment
110                                         return new DragBackground(event);
111                                 } else if (entity && selection.indexOf(entity)>-1) {
112                                         return new DragSelection(selection, event);
113                                 } else if (entity) {
114                                         return new DragSelection([entity], event);
115                                 }
116             } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING && this is SelectedMarker) {
117                 // this is identical to the below, but needed for unselecting markers on vector background layers.
118                 // Deselecting a POI or way on the main layer emits both CLICK and MOUSE_UP, but markers only CLICK
119                 // I'll leave it to someone who understands to decide whether they are the same thing and should be
120                 // combined with a (CLICK || MOUSE_UP)
121                 
122                 // "&& this is SelectedMarker" added by Steve Bennett. The CLICK event being processed for SelectedWay state
123                 // causes way to get unselected...so restrict the double processing as much as possible.  
124                 
125                 return (this is NoSelection) ? null : new NoSelection();
126                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
127                                 return (this is NoSelection) ? null : new NoSelection();
128                         } else if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
129                                 map.mouseUpHandler();   // in case the end-drag is over an EntityUI
130                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
131                                 controller.map.setHighlight(focus, { hover: true });
132                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
133                                 controller.map.setHighlight(focus, { hover: false });
134             } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
135                 if (event.delta > 0) {
136                   map.zoomIn();
137                 } else if (event.delta < 0) {
138                   map.zoomOut();
139                 }
140             }
141                         return null;
142                 }
143
144                 /** Gets the way that the selected node is part of, if that makes sense. If not, return the node, or the way, or nothing. */
145                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
146                         if ( entity is Node ) {
147                                 for each (var parent:Entity in entity.parentWays) {
148                                         return parent;
149                                 }
150                                 return entity;
151                         } else if ( entity is Way ) {
152                                 return entity;
153                         } else {
154                                 return null;
155                         }
156                 }
157
158                 /** Find the MapPaint object that this DisplayObject belongs to. */
159                 protected function getMapPaint(d:DisplayObject):MapPaint {
160                         while (d) {
161                                 if (d is MapPaint) { return MapPaint(d); }
162                                 d=d.parent;
163                         }
164                         return null;
165                 }
166
167                 protected function getNodeIndex(way:Way,node:Node):uint {
168                         for (var i:uint=0; i<way.length; i++) {
169                                 if (way.getNode(i)==node) { return i; }
170                         }
171                         return null;
172                 }
173
174                 /** Create a "repeat tags" action on the current entity, if possible. */
175                 protected function repeatTags(object:Entity):void {
176                         if (!controller.clipboards[object.getType()]) { return; }
177                         object.suspend();
178
179                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
180                         for (var k:String in controller.clipboards[object.getType()]) {
181                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
182                         }
183                         MainUndoStack.getGlobalStack().addAction(undo);
184                         controller.updateSelectionUI();
185                         object.resume();
186                 }
187
188         /** Show the history dialog, if only one object is selected. */
189         protected function showHistory():void {
190             if (selectCount == 1) {
191                 new HistoryDialog().init(firstSelected);
192             } else if (selectCount == 0) {
193                 map.connection.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Can't show history, nothing selected"));
194             } else {
195                 map.connection.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Can't show history, multiple objects selected"));
196             }
197         }
198
199                 /** Create an action to add "source=*" tag to current entity based on background imagery. This is a convenient shorthand for users. */
200                 protected function setSourceTag():void {
201                         if (selectCount!=1) { return; }
202                         if (Imagery.instance().selected && Imagery.instance().selected.sourcetag) {
203                                 if ("sourcekey" in Imagery.instance().selected)
204                                     firstSelected.setTag(Imagery.instance().selected.sourcekey,Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
205                                 else
206                                     firstSelected.setTag('source',Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
207                         }
208                         controller.updateSelectionUI();
209                 }
210
211                 /** Revert all selected items to previously saved state, via a dialog box. */
212                 protected function revertSelection():void {
213                         if (selectCount==0) return;
214                         Alert.show("Revert selected items to the last saved version, discarding your changes?","Are you sure?",Alert.YES | Alert.CANCEL,null,revertHandler);
215                 }
216                 protected function revertHandler(event:CloseEvent):void {
217                         if (event.detail==Alert.CANCEL) return;
218                         for each (var item:Entity in _selection) {
219                                 controller.connection.loadEntity(item);
220                         }
221                 }
222
223                 // Selection getters
224
225                 public function get selectCount():uint {
226                         return _selection.length;
227                 }
228
229                 public function get selection():Array {
230                         return _selection;
231                 }
232
233                 public function get firstSelected():Entity {
234                         if (_selection.length==0) { return null; }
235                         return _selection[0];
236                 }
237
238                 public function get selectedWay():Way {
239                         if (firstSelected is Way) { return firstSelected as Way; }
240                         return null;
241                 }
242
243                 public function get selectedWays():Array {
244                         var selectedWays:Array=[];
245                         for each (var item:Entity in _selection) {
246                                 if (item is Way) { selectedWays.push(item); }
247                         }
248                         return selectedWays;
249                 }
250
251         public function get selectedNodes():Array {
252             var selectedNodes:Array=[];
253             for each (var item:Entity in _selection) {
254                 if (item is Node) { selectedNodes.push(item); }
255             }
256             return selectedNodes;
257         }
258
259                 public function hasSelectedWays():Boolean {
260                         for each (var item:Entity in _selection) {
261                                 if (item is Way) { return true; }
262                         }
263                         return false;
264                 }
265
266                 public function hasSelectedAreas():Boolean {
267                         for each (var item:Entity in _selection) {
268                                 if (item is Way && Way(item).isArea()) { return true; }
269                         }
270                         return false;
271                 }
272
273                 public function hasSelectedUnclosedWays():Boolean {
274                         for each (var item:Entity in _selection) {
275                                 if (item is Way && !Way(item).isArea()) { return true; }
276                         }
277                         return false;
278                 }
279
280         /** Determine whether or not any nodes are selected, and if so whether any of them belong to areas. */
281         public function hasSelectedWayNodesInAreas():Boolean {
282             for each (var item:Entity in _selection) {
283                 if (item is Node) {
284                     var parentWays:Array = Node(item).parentWays;
285                     for each (var way:Entity in parentWays) {
286                         if (Way(way).isArea()) { return true; }
287                     }
288                 }
289             }
290             return false;
291         }
292
293                 public function hasAdjoiningWays():Boolean {
294                         if (_selection.length<2) { return false; }
295                         var endNodes:Object={};
296                         for each (var item:Entity in _selection) {
297                                 if (item is Way && !Way(item).isArea()) {
298                                         if (endNodes[Way(item).getNode(0).id]) return true;
299                                         if (endNodes[Way(item).getLastNode().id]) return true;
300                                         endNodes[Way(item).getNode(0).id]=true;
301                                         endNodes[Way(item).getLastNode().id]=true;
302                                 }
303                         }
304                         return false;
305                 }
306
307                 // Selection setters
308
309                 public function set selection(items:Array):void {
310                         _selection=items;
311                 }
312
313                 public function addToSelection(items:Array):void {
314                         for each (var item:Entity in items) {
315                                 if (_selection.indexOf(item)==-1) { _selection.push(item); }
316                         }
317                 }
318
319                 public function removeFromSelection(items:Array):void {
320                         for each (var item:Entity in items) {
321                                 if (_selection.indexOf(item)>-1) {
322                                         _selection.splice(_selection.indexOf(item),1);
323                                 }
324                         }
325                 }
326
327                 public function toggleSelection(item:Entity):Boolean {
328                         if (_selection.indexOf(item)==-1) {
329                                 _selection.push(item); return true;
330                         }
331                         _selection.splice(_selection.indexOf(item),1); return false;
332                 }
333     }
334 }