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