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