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