Fix the "can't deselect a marker by clicking on the background" bug. Note the comment...
[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                         }
63                         return null;
64                 }
65                 
66                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
67                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
68             var focus:Entity = getTopLevelFocusEntity(entity);
69
70                         if ( paint && paint.isBackground ) {
71                                 if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
72                                         // alt-click to pull data out of vector background layer
73                                         var newEntity:Entity=paint.findSource().pullThrough(entity,controller.connection);
74                                         if (entity is Way) { return new SelectedWay(newEntity as Way); }
75                                         else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
76                 } else if (event.type == MouseEvent.MOUSE_DOWN && entity is Marker) {
77                     return new SelectedMarker(entity as Marker, paint.findSource());
78                                 } else if ( event.type == MouseEvent.MOUSE_UP ) { 
79                                         return (this is NoSelection) ? null : new NoSelection();
80                                 } else { return null; }
81                         }
82
83                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
84                                 if ( entity is Way ) {
85                                         // click way
86                                         return new DragWay(focus as Way, event);
87                                 } else if ( focus is Node ) {
88                                         // select POI node
89                                         return new DragPOINode(entity as Node,event,false);
90                                 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
91                                         // select node within this way
92                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
93                                 } else if ( entity is Node && focus is Way ) {
94                                         // select way node
95                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
96                                 } else if ( controller.keyDown(32) ) {
97                                         // drag map
98                                         return new DragBackground(event);
99                                 }
100             } else if ( event.type == MouseEvent.CLICK && focus == null && map.dragstate!=map.DRAGGING) {
101                 // this is identical to the below, but needed for unselecting markers on vector background layers.
102                 // Deselecting a POI or way on the main layer emits both CLICK and MOUSE_UP, but markers only CLICK
103                 // I'll leave it to someone who understands to decide whether they are the same thing and should be
104                 // combined with a (CLICK || MOUSE_UP)
105                 return (this is NoSelection) ? null : new NoSelection();
106                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
107                                 return (this is NoSelection) ? null : new NoSelection();
108                         } else if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
109                                 map.mouseUpHandler();   // in case the end-drag is over an EntityUI
110                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
111                                 controller.map.setHighlight(focus, { hover: true });
112                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
113                                 controller.map.setHighlight(focus, { hover: false });
114             } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
115                 if (event.delta > 0) {
116                   map.zoomIn();
117                 } else if (event.delta < 0) {
118                   map.zoomOut();
119                 }
120             }
121                         return null;
122                 }
123
124                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
125                         if ( entity is Node ) {
126                                 for each (var parent:Entity in entity.parentWays) {
127                                         return parent;
128                                 }
129                                 return entity;
130                         } else if ( entity is Way ) {
131                                 return entity;
132                         } else {
133                                 return null;
134                         }
135                 }
136
137                 protected function getMapPaint(d:DisplayObject):MapPaint {
138                         while (d) {
139                                 if (d is MapPaint) { return MapPaint(d); }
140                                 d=d.parent;
141                         }
142                         return null;
143                 }
144                 
145                 protected function getNodeIndex(way:Way,node:Node):uint {
146                         for (var i:uint=0; i<way.length; i++) {
147                                 if (way.getNode(i)==node) { return i; }
148                         }
149                         return null;
150                 }
151                 
152                 protected function repeatTags(object:Entity):void {
153                         if (!controller.clipboards[object.getType()]) { return; }
154                         object.suspend();
155
156                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
157                         for (var k:String in controller.clipboards[object.getType()]) {
158                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
159                         }
160                         MainUndoStack.getGlobalStack().addAction(undo);
161
162                         object.resume();
163                 }
164                 
165                 protected function setSourceTag():void {
166                         if (selectCount!=1) { return; }
167                         if (Imagery.instance().selected && Imagery.instance().selected.sourcetag) {
168                                 firstSelected.setTag('source',Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
169                         }
170                 }
171                 
172                 // Selection getters
173                 
174                 public function get selectCount():uint {
175                         return _selection.length;
176                 }
177                 
178                 public function get selection():Array {
179                         return _selection;
180                 }
181                 
182                 public function get firstSelected():Entity {
183                         if (_selection.length==0) { return null; }
184                         return _selection[0];
185                 }
186
187                 public function get selectedWay():Way {
188                         if (firstSelected is Way) { return firstSelected as Way; }
189                         return null;
190                 }
191                 
192                 public function get selectedWays():Array {
193                         var selectedWays:Array=[];
194                         for each (var item:Entity in _selection) {
195                                 if (item is Way) { selectedWays.push(item); }
196                         }
197                         return selectedWays;
198                 }
199                 
200                 public function hasSelectedWays():Boolean {
201                         for each (var item:Entity in _selection) {
202                                 if (item is Way) { return true; }
203                         }
204                         return false;
205                 }
206                 
207                 public function hasSelectedAreas():Boolean {
208                         for each (var item:Entity in _selection) {
209                                 if (item is Way && Way(item).isArea()) { return true; }
210                         }
211                         return false;
212                 }
213                 
214                 public function hasSelectedUnclosedWays():Boolean {
215                         for each (var item:Entity in _selection) {
216                                 if (item is Way && !Way(item).isArea()) { return true; }
217                         }
218                         return false;
219                 }
220                 
221                 public function hasAdjoiningWays():Boolean {
222                         if (_selection.length<2) { return false; }
223                         var endNodes:Object={};
224                         for each (var item:Entity in _selection) {
225                                 if (item is Way && !Way(item).isArea()) {
226                                         if (endNodes[Way(item).getNode(0).id]) return true;
227                                         if (endNodes[Way(item).getLastNode().id]) return true;
228                                         endNodes[Way(item).getNode(0).id]=true;
229                                         endNodes[Way(item).getLastNode().id]=true;
230                                 }
231                         }
232                         return false;
233                 }
234                         
235                 // Selection setters
236                 
237                 public function set selection(items:Array):void {
238                         _selection=items;
239                 }
240                 
241                 public function addToSelection(items:Array):void {
242                         for each (var item:Entity in items) {
243                                 if (_selection.indexOf(item)==-1) { _selection.push(item); }
244                         }
245                 }
246                 
247                 public function removeFromSelection(items:Array):void {
248                         for each (var item:Entity in items) {
249                                 if (_selection.indexOf(item)>-1) {
250                                         _selection.splice(_selection.indexOf(item),1);
251                                 }
252                         }
253                 }
254                 
255                 public function toggleSelection(item:Entity):Boolean {
256                         if (_selection.indexOf(item)==-1) {
257                                 _selection.push(item); return true;
258                         }
259                         _selection.splice(_selection.indexOf(item),1); return false;
260                 }
261     }
262 }