make Toolbox act on multiple selections (and light up accordingly)
[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 67:        controller.connection.closeChangeset(); break;                                                  // C - close changeset
55                                 case 68:        controller.map.paint.alpha=1.3-controller.map.paint.alpha; return null; // D - dim
56                                 case 83:        SaveManager.saveChanges(); break;                                                                               // S - save
57                                 case 84:        controller.tagViewer.togglePanel(); return null;                                                // T - toggle tags panel
58                                 case 90:        MainUndoStack.getGlobalStack().undo(); return null;                                             // Z - undo
59                                 case 187:       controller.tagViewer.addNewTag(); return null;                                                  // + - add tag
60                         }
61                         return null;
62                 }
63                 
64                 protected function sharedMouseEvents(event:MouseEvent, entity:Entity):ControllerState {
65                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
66             var focus:Entity = getTopLevelFocusEntity(entity);
67
68                         if ( paint && paint.isBackground ) {
69                                 if ( event.type == MouseEvent.MOUSE_DOWN && ((event.shiftKey && event.ctrlKey) || event.altKey) ) {
70                                         // alt-click to pull data out of vector background layer
71                                         var newEntity:Entity=paint.findSource().pullThrough(entity,controller.connection);
72                                         if (entity is Way) { return new SelectedWay(newEntity as Way); }
73                                         else if (entity is Node) { return new SelectedPOINode(newEntity as Node); }
74                                 } else if ( event.type == MouseEvent.MOUSE_UP ) { 
75                                         return (this is NoSelection) ? null : new NoSelection();
76                                 } else { return null; }
77                         }
78
79                         if ( event.type == MouseEvent.MOUSE_DOWN ) {
80                                 if ( entity is Way ) {
81                                         // click way
82                                         return new DragWay(focus as Way, event);
83                                 } else if ( focus is Node ) {
84                                         // select POI node
85                                         return new DragPOINode(entity as Node,event,false);
86                                 } else if ( entity is Node && selectedWay && entity.hasParent(selectedWay) ) {
87                                         // select node within this way
88                         return new DragWayNode(selectedWay,  getNodeIndex(selectedWay,entity as Node),  event, false);
89                                 } else if ( entity is Node && focus is Way ) {
90                                         // select way node
91                                         return new DragWayNode(focus as Way, getNodeIndex(focus as Way,entity as Node), event, false);
92                                 } else if ( controller.keyDown(32) ) {
93                                         // drag map
94                                         return new DragBackground(event);
95                                 }
96                         } else if ( event.type == MouseEvent.MOUSE_UP && focus == null && map.dragstate!=map.DRAGGING) {
97                                 return (this is NoSelection) ? null : new NoSelection();
98                         } else if ( event.type == MouseEvent.MOUSE_UP && focus && map.dragstate!=map.NOT_DRAGGING) {
99                                 map.mouseUpHandler();   // in case the end-drag is over an EntityUI
100                         } else if ( event.type == MouseEvent.ROLL_OVER ) {
101                                 controller.map.setHighlight(focus, { hover: true });
102                         } else if ( event.type == MouseEvent.MOUSE_OUT ) {
103                                 controller.map.setHighlight(focus, { hover: false });
104             } else if ( event.type == MouseEvent.MOUSE_WHEEL ) {
105                 if (event.delta > 0) {
106                   map.zoomIn();
107                 } else if (event.delta < 0) {
108                   map.zoomOut();
109                 }
110             }
111                         return null;
112                 }
113
114                 public static function getTopLevelFocusEntity(entity:Entity):Entity {
115                         if ( entity is Node ) {
116                                 for each (var parent:Entity in entity.parentWays) {
117                                         return parent;
118                                 }
119                                 return entity;
120                         } else if ( entity is Way ) {
121                                 return entity;
122                         } else {
123                                 return null;
124                         }
125                 }
126
127                 protected function getMapPaint(d:DisplayObject):MapPaint {
128                         while (d) {
129                                 if (d is MapPaint) { return MapPaint(d); }
130                                 d=d.parent;
131                         }
132                         return null;
133                 }
134                 
135                 protected function getNodeIndex(way:Way,node:Node):uint {
136                         for (var i:uint=0; i<way.length; i++) {
137                                 if (way.getNode(i)==node) { return i; }
138                         }
139                         return null;
140                 }
141                 
142                 protected function repeatTags(object:Entity):void {
143                         if (!controller.clipboards[object.getType()]) { return; }
144                         object.suspend();
145
146                     var undo:CompositeUndoableAction = new CompositeUndoableAction("Repeat tags");
147                         for (var k:String in controller.clipboards[object.getType()]) {
148                                 object.setTag(k, controller.clipboards[object.getType()][k], undo.push)
149                         }
150                         MainUndoStack.getGlobalStack().addAction(undo);
151
152                         object.resume();
153                 }
154                 
155                 // Selection getters
156                 
157                 public function get selectCount():uint {
158                         return _selection.length;
159                 }
160                 
161                 public function get selection():Array {
162                         return _selection;
163                 }
164                 
165                 public function get firstSelected():Entity {
166                         if (_selection.length==0) { return null; }
167                         return _selection[0];
168                 }
169
170                 public function get selectedWay():Way {
171                         if (firstSelected is Way) { return firstSelected as Way; }
172                         return null;
173                 }
174                 
175                 public function get selectedWays():Array {
176                         var selectedWays:Array=[];
177                         for each (var item:Entity in _selection) {
178                                 if (item is Way) { selectedWays.push(item); }
179                         }
180                         return selectedWays;
181                 }
182                 
183                 public function hasSelectedWays():Boolean {
184                         for each (var item:Entity in _selection) {
185                                 if (item is Way) { return true; }
186                         }
187                         return false;
188                 }
189                 
190                 public function hasSelectedAreas():Boolean {
191                         for each (var item:Entity in _selection) {
192                                 if (item is Way && Way(item).isArea()) { return true; }
193                         }
194                         return false;
195                 }
196                 
197                 public function hasSelectedUnclosedWays():Boolean {
198                         for each (var item:Entity in _selection) {
199                                 if (item is Way && !Way(item).isArea()) { return true; }
200                         }
201                         return false;
202                 }
203                         
204                 // Selection setters
205                 
206                 public function set selection(items:Array):void {
207                         _selection=items;
208                 }
209                 
210                 public function addToSelection(items:Array):void {
211                         for each (var item:Entity in items) {
212                                 if (_selection.indexOf(item)==-1) { _selection.push(item); }
213                         }
214                 }
215                 
216                 public function removeFromSelection(items:Array):void {
217                         for each (var item:Entity in items) {
218                                 if (_selection.indexOf(item)>-1) {
219                                         _selection.splice(_selection.indexOf(item),1);
220                                 }
221                         }
222                 }
223                 
224                 public function toggleSelection(item:Entity):Boolean {
225                         if (_selection.indexOf(item)==-1) {
226                                 _selection.push(item); return true;
227                         }
228                         _selection.splice(_selection.indexOf(item),1); return false;
229                 }
230     }
231 }