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