Allow numpad + as well as "equals plus". (Like P1)
[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
12     public class ControllerState {
13
14         protected var controller:EditController;
15         protected var previousState:ControllerState;
16
17                 protected var _selection:Array=[];
18
19         public function ControllerState() {}
20
21         public function setController(controller:EditController):void {
22             this.controller = controller;
23         }
24
25         public function setPreviousState(previousState:ControllerState):void {
26             if ( this.previousState == null )
27                 this.previousState = previousState;
28         }
29
30                 public function isSelectionState():Boolean {
31                         return true;
32                 }
33
34         public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
35             return this;
36         }
37
38         public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
39             return this;
40         }
41
42                 public function get map():Map {
43                         return controller.map;
44                 }
45
46         public function enterState():void {}
47         public function exitState(newState:ControllerState):void {}
48
49                 public function toString():String {
50                         return "(No state)";
51                 }
52
53                 protected function sharedKeyboardEvents(event:KeyboardEvent):ControllerState {
54                         switch (event.keyCode) {
55                                 case 66:        setSourceTag(); break;                                                                                                  // B - set source tag for current object
56                                 case 67:        controller.connection.closeChangeset(); break;                                                  // C - close changeset
57                                 case 68:        controller.map.paint.alpha=1.3-controller.map.paint.alpha; return null; // D - dim
58                                 case 83:        SaveManager.saveChanges(); break;                                                                               // S - save
59                                 case 84:        controller.tagViewer.togglePanel(); return null;                                                // T - toggle tags panel
60                                 case 90:        MainUndoStack.getGlobalStack().undo(); return null;                                             // Z - undo
61                                 case 187:       controller.tagViewer.addNewTag(); return null;                                                  // + - add tag
62                                 case 107:       controller.tagViewer.addNewTag(); return null;                                                  // numpad + - add tag
63                         }
64                         return null;
65                 }
66
67                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
68                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
69             var focus:Entity = getTopLevelFocusEntity(entity);
70
71                         if ( paint && paint.isBackground ) {
72                                 if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
73                                         // alt-click to pull data out of vector background layer
74                                         var newEntity:Entity=paint.findSource().pullThrough(entity,controller.connection);
75                                         if (entity is Way) { return new SelectedWay(newEntity as Way); }
76                                         else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
77                 } else if (event.type == MouseEvent.MOUSE_DOWN && entity is Marker) {
78                     return new SelectedMarker(entity as Marker, paint.findSource());
79                                 } else if ( event.type == MouseEvent.MOUSE_UP ) {
80                                         return (this is NoSelection) ? null : new NoSelection();
81                                 } else { return null; }
82                         }
83
84                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
85                                 if ( entity is Way ) {
86                                         // click way
87                                         return new DragWay(focus as Way, event);
88                                 } else if ( focus is Node ) {
89                                         // select POI node
90                                         return new DragPOINode(entity as Node,event,false);
91                                 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
92                                         // select node within this way
93                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
94                                 } else if ( entity is Node && focus is Way ) {
95                                         // select way node
96                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
97                                 } else if ( controller.keyDown(32) ) {
98                                         // drag map
99                                         return new DragBackground(event);
100                                 }
101             } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING) {
102                 // this is identical to the below, but needed for unselecting markers on vector background layers.
103                 // Deselecting a POI or way on the main layer emits both CLICK and MOUSE_UP, but markers only CLICK
104                 // I'll leave it to someone who understands to decide whether they are the same thing and should be
105                 // combined with a (CLICK || MOUSE_UP)
106                 return (this is NoSelection) ? null : new NoSelection();
107                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
108                                 return (this is NoSelection) ? null : new NoSelection();
109                         } else if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
110                                 map.mouseUpHandler();   // in case the end-drag is over an EntityUI
111                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
112                                 controller.map.setHighlight(focus, { hover: true });
113                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
114                                 controller.map.setHighlight(focus, { hover: false });
115             } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
116                 if (event.delta > 0) {
117                   map.zoomIn();
118                 } else if (event.delta < 0) {
119                   map.zoomOut();
120                 }
121             }
122                         return null;
123                 }
124
125                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
126                         if ( entity is Node ) {
127                                 for each (var parent:Entity in entity.parentWays) {
128                                         return parent;
129                                 }
130                                 return entity;
131                         } else if ( entity is Way ) {
132                                 return entity;
133                         } else {
134                                 return null;
135                         }
136                 }
137
138                 protected function getMapPaint(d:DisplayObject):MapPaint {
139                         while (d) {
140                                 if (d is MapPaint) { return MapPaint(d); }
141                                 d=d.parent;
142                         }
143                         return null;
144                 }
145
146                 protected function getNodeIndex(way:Way,node:Node):uint {
147                         for (var i:uint=0; i<way.length; i++) {
148                                 if (way.getNode(i)==node) { return i; }
149                         }
150                         return null;
151                 }
152
153                 protected function repeatTags(object:Entity):void {
154                         if (!controller.clipboards[object.getType()]) { return; }
155                         object.suspend();
156
157                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
158                         for (var k:String in controller.clipboards[object.getType()]) {
159                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
160                         }
161                         MainUndoStack.getGlobalStack().addAction(undo);
162                         controller.updateSelectionUI();
163                         object.resume();
164
165
166                 }
167
168                 protected function setSourceTag():void {
169                         if (selectCount!=1) { return; }
170                         if (Imagery.instance().selected && Imagery.instance().selected.sourcetag) {
171                                 firstSelected.setTag('source',Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
172                         }
173                         controller.updateSelectionUI();
174                 }
175
176                 // Selection getters
177
178                 public function get selectCount():uint {
179                         return _selection.length;
180                 }
181
182                 public function get selection():Array {
183                         return _selection;
184                 }
185
186                 public function get firstSelected():Entity {
187                         if (_selection.length==0) { return null; }
188                         return _selection[0];
189                 }
190
191                 public function get selectedWay():Way {
192                         if (firstSelected is Way) { return firstSelected as Way; }
193                         return null;
194                 }
195
196                 public function get selectedWays():Array {
197                         var selectedWays:Array=[];
198                         for each (var item:Entity in _selection) {
199                                 if (item is Way) { selectedWays.push(item); }
200                         }
201                         return selectedWays;
202                 }
203
204                 public function hasSelectedWays():Boolean {
205                         for each (var item:Entity in _selection) {
206                                 if (item is Way) { return true; }
207                         }
208                         return false;
209                 }
210
211                 public function hasSelectedAreas():Boolean {
212                         for each (var item:Entity in _selection) {
213                                 if (item is Way && Way(item).isArea()) { return true; }
214                         }
215                         return false;
216                 }
217
218                 public function hasSelectedUnclosedWays():Boolean {
219                         for each (var item:Entity in _selection) {
220                                 if (item is Way && !Way(item).isArea()) { return true; }
221                         }
222                         return false;
223                 }
224
225                 public function hasAdjoiningWays():Boolean {
226                         if (_selection.length<2) { return false; }
227                         var endNodes:Object={};
228                         for each (var item:Entity in _selection) {
229                                 if (item is Way && !Way(item).isArea()) {
230                                         if (endNodes[Way(item).getNode(0).id]) return true;
231                                         if (endNodes[Way(item).getLastNode().id]) return true;
232                                         endNodes[Way(item).getNode(0).id]=true;
233                                         endNodes[Way(item).getLastNode().id]=true;
234                                 }
235                         }
236                         return false;
237                 }
238
239                 // Selection setters
240
241                 public function set selection(items:Array):void {
242                         _selection=items;
243                 }
244
245                 public function addToSelection(items:Array):void {
246                         for each (var item:Entity in items) {
247                                 if (_selection.indexOf(item)==-1) { _selection.push(item); }
248                         }
249                 }
250
251                 public function removeFromSelection(items:Array):void {
252                         for each (var item:Entity in items) {
253                                 if (_selection.indexOf(item)>-1) {
254                                         _selection.splice(_selection.indexOf(item),1);
255                                 }
256                         }
257                 }
258
259                 public function toggleSelection(item:Entity):Boolean {
260                         if (_selection.indexOf(item)==-1) {
261                                 _selection.push(item); return true;
262                         }
263                         _selection.splice(_selection.indexOf(item),1); return false;
264                 }
265     }
266 }