make + keypress work even if the advanced panel isn't currently selected
[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 Way ) {
96                                         // click way
97                                         return new DragWay(focus as Way, event);
98                                 } else if ( focus is Node ) {
99                                         // select POI node
100                                         return new DragPOINode(entity as Node,event,false);
101                                 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
102                                         // select node within this way
103                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
104                                 } else if ( entity is Node && focus is Way ) {
105                                         // select way node
106                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
107                                 } else if ( controller.keyDown(Keyboard.SPACE) ) {
108                                         // drag the background imagery to compensate for poor alignment
109                                         return new DragBackground(event);
110                                 }
111             } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING && this is SelectedMarker) {
112                 // this is identical to the below, but needed for unselecting markers on vector background layers.
113                 // Deselecting a POI or way on the main layer emits both CLICK and MOUSE_UP, but markers only CLICK
114                 // I'll leave it to someone who understands to decide whether they are the same thing and should be
115                 // combined with a (CLICK || MOUSE_UP)
116                 
117                 // "&& this is SelectedMarker" added by Steve Bennett. The CLICK event being processed for SelectedWay state
118                 // causes way to get unselected...so restrict the double processing as much as possible.  
119                 
120                 return (this is NoSelection) ? null : new NoSelection();
121                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
122                                 return (this is NoSelection) ? null : new NoSelection();
123                         } else if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
124                                 map.mouseUpHandler();   // in case the end-drag is over an EntityUI
125                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
126                                 controller.map.setHighlight(focus, { hover: true });
127                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
128                                 controller.map.setHighlight(focus, { hover: false });
129             } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
130                 if (event.delta > 0) {
131                   map.zoomIn();
132                 } else if (event.delta < 0) {
133                   map.zoomOut();
134                 }
135             }
136                         return null;
137                 }
138
139                 /** Gets the way that the selected node is part of, if that makes sense. If not, return the node, or the way, or nothing. */
140                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
141                         if ( entity is Node ) {
142                                 for each (var parent:Entity in entity.parentWays) {
143                                         return parent;
144                                 }
145                                 return entity;
146                         } else if ( entity is Way ) {
147                                 return entity;
148                         } else {
149                                 return null;
150                         }
151                 }
152
153                 /** Find the MapPaint object that this DisplayObject belongs to. */
154                 protected function getMapPaint(d:DisplayObject):MapPaint {
155                         while (d) {
156                                 if (d is MapPaint) { return MapPaint(d); }
157                                 d=d.parent;
158                         }
159                         return null;
160                 }
161
162                 protected function getNodeIndex(way:Way,node:Node):uint {
163                         for (var i:uint=0; i<way.length; i++) {
164                                 if (way.getNode(i)==node) { return i; }
165                         }
166                         return null;
167                 }
168
169                 /** Create a "repeat tags" action on the current entity, if possible. */
170                 protected function repeatTags(object:Entity):void {
171                         if (!controller.clipboards[object.getType()]) { return; }
172                         object.suspend();
173
174                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
175                         for (var k:String in controller.clipboards[object.getType()]) {
176                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
177                         }
178                         MainUndoStack.getGlobalStack().addAction(undo);
179                         controller.updateSelectionUI();
180                         object.resume();
181
182
183                 }
184
185                 /** Create an action to add "source=*" tag to current entity based on background imagery. This is a convenient shorthand for users. */
186                 protected function setSourceTag():void {
187                         if (selectCount!=1) { return; }
188                         if (Imagery.instance().selected && Imagery.instance().selected.sourcetag) {
189                                 firstSelected.setTag('source',Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
190                         }
191                         controller.updateSelectionUI();
192                 }
193
194                 // Selection getters
195
196                 public function get selectCount():uint {
197                         return _selection.length;
198                 }
199
200                 public function get selection():Array {
201                         return _selection;
202                 }
203
204                 public function get firstSelected():Entity {
205                         if (_selection.length==0) { return null; }
206                         return _selection[0];
207                 }
208
209                 public function get selectedWay():Way {
210                         if (firstSelected is Way) { return firstSelected as Way; }
211                         return null;
212                 }
213
214                 public function get selectedWays():Array {
215                         var selectedWays:Array=[];
216                         for each (var item:Entity in _selection) {
217                                 if (item is Way) { selectedWays.push(item); }
218                         }
219                         return selectedWays;
220                 }
221
222                 public function hasSelectedWays():Boolean {
223                         for each (var item:Entity in _selection) {
224                                 if (item is Way) { return true; }
225                         }
226                         return false;
227                 }
228
229                 public function hasSelectedAreas():Boolean {
230                         for each (var item:Entity in _selection) {
231                                 if (item is Way && Way(item).isArea()) { return true; }
232                         }
233                         return false;
234                 }
235
236                 public function hasSelectedUnclosedWays():Boolean {
237                         for each (var item:Entity in _selection) {
238                                 if (item is Way && !Way(item).isArea()) { return true; }
239                         }
240                         return false;
241                 }
242
243                 public function hasAdjoiningWays():Boolean {
244                         if (_selection.length<2) { return false; }
245                         var endNodes:Object={};
246                         for each (var item:Entity in _selection) {
247                                 if (item is Way && !Way(item).isArea()) {
248                                         if (endNodes[Way(item).getNode(0).id]) return true;
249                                         if (endNodes[Way(item).getLastNode().id]) return true;
250                                         endNodes[Way(item).getNode(0).id]=true;
251                                         endNodes[Way(item).getLastNode().id]=true;
252                                 }
253                         }
254                         return false;
255                 }
256
257                 // Selection setters
258
259                 public function set selection(items:Array):void {
260                         _selection=items;
261                 }
262
263                 public function addToSelection(items:Array):void {
264                         for each (var item:Entity in items) {
265                                 if (_selection.indexOf(item)==-1) { _selection.push(item); }
266                         }
267                 }
268
269                 public function removeFromSelection(items:Array):void {
270                         for each (var item:Entity in items) {
271                                 if (_selection.indexOf(item)>-1) {
272                                         _selection.splice(_selection.indexOf(item),1);
273                                 }
274                         }
275                 }
276
277                 public function toggleSelection(item:Entity):Boolean {
278                         if (_selection.indexOf(item)==-1) {
279                                 _selection.push(item); return true;
280                         }
281                         _selection.splice(_selection.indexOf(item),1); return false;
282                 }
283     }
284 }