support GPX 1.1 too
[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.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
101                                 return (this is NoSelection) ? null : new NoSelection();
102                         } else if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
103                                 map.mouseUpHandler();   // in case the end-drag is over an EntityUI
104                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
105                                 controller.map.setHighlight(focus, { hover: true });
106                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
107                                 controller.map.setHighlight(focus, { hover: false });
108             } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
109                 if (event.delta > 0) {
110                   map.zoomIn();
111                 } else if (event.delta < 0) {
112                   map.zoomOut();
113                 }
114             }
115                         return null;
116                 }
117
118                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
119                         if ( entity is Node ) {
120                                 for each (var parent:Entity in entity.parentWays) {
121                                         return parent;
122                                 }
123                                 return entity;
124                         } else if ( entity is Way ) {
125                                 return entity;
126                         } else {
127                                 return null;
128                         }
129                 }
130
131                 protected function getMapPaint(d:DisplayObject):MapPaint {
132                         while (d) {
133                                 if (d is MapPaint) { return MapPaint(d); }
134                                 d=d.parent;
135                         }
136                         return null;
137                 }
138                 
139                 protected function getNodeIndex(way:Way,node:Node):uint {
140                         for (var i:uint=0; i<way.length; i++) {
141                                 if (way.getNode(i)==node) { return i; }
142                         }
143                         return null;
144                 }
145                 
146                 protected function repeatTags(object:Entity):void {
147                         if (!controller.clipboards[object.getType()]) { return; }
148                         object.suspend();
149
150                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
151                         for (var k:String in controller.clipboards[object.getType()]) {
152                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
153                         }
154                         MainUndoStack.getGlobalStack().addAction(undo);
155
156                         object.resume();
157                 }
158                 
159                 protected function setSourceTag():void {
160                         if (selectCount!=1) { return; }
161                         if (Imagery.instance().selected && Imagery.instance().selected.sourcetag) {
162                                 firstSelected.setTag('source',Imagery.instance().selected.sourcetag, MainUndoStack.getGlobalStack().addAction);
163                         }
164                 }
165                 
166                 // Selection getters
167                 
168                 public function get selectCount():uint {
169                         return _selection.length;
170                 }
171                 
172                 public function get selection():Array {
173                         return _selection;
174                 }
175                 
176                 public function get firstSelected():Entity {
177                         if (_selection.length==0) { return null; }
178                         return _selection[0];
179                 }
180
181                 public function get selectedWay():Way {
182                         if (firstSelected is Way) { return firstSelected as Way; }
183                         return null;
184                 }
185                 
186                 public function get selectedWays():Array {
187                         var selectedWays:Array=[];
188                         for each (var item:Entity in _selection) {
189                                 if (item is Way) { selectedWays.push(item); }
190                         }
191                         return selectedWays;
192                 }
193                 
194                 public function hasSelectedWays():Boolean {
195                         for each (var item:Entity in _selection) {
196                                 if (item is Way) { return true; }
197                         }
198                         return false;
199                 }
200                 
201                 public function hasSelectedAreas():Boolean {
202                         for each (var item:Entity in _selection) {
203                                 if (item is Way && Way(item).isArea()) { return true; }
204                         }
205                         return false;
206                 }
207                 
208                 public function hasSelectedUnclosedWays():Boolean {
209                         for each (var item:Entity in _selection) {
210                                 if (item is Way && !Way(item).isArea()) { return true; }
211                         }
212                         return false;
213                 }
214                 
215                 public function hasAdjoiningWays():Boolean {
216                         if (_selection.length<2) { return false; }
217                         var endNodes:Object={};
218                         for each (var item:Entity in _selection) {
219                                 if (item is Way && !Way(item).isArea()) {
220                                         if (endNodes[Way(item).getNode(0).id]) return true;
221                                         if (endNodes[Way(item).getLastNode().id]) return true;
222                                         endNodes[Way(item).getNode(0).id]=true;
223                                         endNodes[Way(item).getLastNode().id]=true;
224                                 }
225                         }
226                         return false;
227                 }
228                         
229                 // Selection setters
230                 
231                 public function set selection(items:Array):void {
232                         _selection=items;
233                 }
234                 
235                 public function addToSelection(items:Array):void {
236                         for each (var item:Entity in items) {
237                                 if (_selection.indexOf(item)==-1) { _selection.push(item); }
238                         }
239                 }
240                 
241                 public function removeFromSelection(items:Array):void {
242                         for each (var item:Entity in items) {
243                                 if (_selection.indexOf(item)>-1) {
244                                         _selection.splice(_selection.indexOf(item),1);
245                                 }
246                         }
247                 }
248                 
249                 public function toggleSelection(item:Entity):Boolean {
250                         if (_selection.indexOf(item)==-1) {
251                                 _selection.push(item); return true;
252                         }
253                         _selection.splice(_selection.indexOf(item),1); return false;
254                 }
255     }
256 }