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