Unify selection ControllerStates so they can work on either background or editable...
[potlatch2.git] / net / systemeD / potlatch2 / EditController.as
1 package net.systemeD.potlatch2 {
2     import net.systemeD.halcyon.Map;
3     import net.systemeD.halcyon.MapPaint;
4     import net.systemeD.halcyon.MapController;
5     import net.systemeD.halcyon.MapEvent;
6     import net.systemeD.halcyon.connection.*;
7     import net.systemeD.halcyon.Globals;
8     import net.systemeD.potlatch2.controller.*;
9     import net.systemeD.potlatch2.FunctionKeyManager;
10         import mx.managers.CursorManager;
11     import flash.external.ExternalInterface;
12     import flash.events.*;
13         import flash.geom.*;
14         import flash.ui.Keyboard;
15         import flash.text.TextField;
16     import mx.controls.TextArea;
17
18     /** Controller for the main map editing window itself. The logic that responds to mouse and keyboard events is all 
19     * buried in various ControllerState classes. */
20     public class EditController extends EventDispatcher implements MapController {
21
22         private var _map:Map;
23         public var tagViewer:TagViewer;
24                 private var toolbox:Toolbox;
25
26         /** The current ControllerState */
27         public var state:ControllerState;
28         
29                 private var keys:Object={};
30                 public var clipboards:Object={};
31                 public var cursorsEnabled:Boolean=true;
32         private var maximised:Boolean=false;
33         private var maximiseFunction:String;
34         private var minimiseFunction:String;
35         private var moveFunction:String;
36
37                 [Embed(source="../../../embedded/pen.png")]             public var pen:Class;
38                 [Embed(source="../../../embedded/pen_x.png")]           public var pen_x:Class;
39                 [Embed(source="../../../embedded/pen_o.png")]           public var pen_o:Class;
40                 [Embed(source="../../../embedded/pen_so.png")]          public var pen_so:Class;
41                 [Embed(source="../../../embedded/pen_plus.png")]        public var pen_plus:Class;
42                 
43         /** Constructor function: needs the map information, a panel to edit tags with, and the toolbox to manipulate ways with. */
44         public function EditController(map:Map, tagViewer:TagViewer, toolbox:Toolbox) {
45             this._map = map;
46             setState(new NoSelection());
47             this.tagViewer = tagViewer;
48                         this.toolbox = toolbox;
49                         this.toolbox.init(this);
50             this.maximiseFunction = Globals.vars.flashvars["maximise_function"];
51             this.minimiseFunction = Globals.vars.flashvars["minimise_function"];
52             this.moveFunction     = Globals.vars.flashvars["move_function"];
53
54             map.parent.addEventListener(MouseEvent.MOUSE_MOVE, mapMouseEvent);
55             map.parent.addEventListener(MouseEvent.MOUSE_UP, mapMouseEvent);
56             map.parent.addEventListener(MouseEvent.MOUSE_DOWN, mapMouseEvent);
57             map.parent.addEventListener(MouseEvent.MOUSE_WHEEL, mapMouseEvent);
58             map.parent.addEventListener(MouseEvent.CLICK, mapMouseEvent);
59             map.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
60             map.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
61
62             if (this.moveFunction) {
63                 map.addEventListener(MapEvent.MOVE, moveHandler);
64             }
65         }
66
67         public function setActive():void {
68             map.setController(this);
69         }
70
71         /** Accesses map object. */
72         public function get map():Map {
73             return _map;
74         }
75         
76         /**
77         * Updates the various user interfaces that change when the selection changes.
78         * Currently this is the TagViewer and the Toolbox
79         *
80         * @param layer Optionally pass the layer of the currently selected entity, eg for BugLayers
81         */
82                 public function updateSelectionUI(layer:MapPaint = null):void {
83                         tagViewer.setEntity(state.selection, layer);
84                         toolbox.updateSelectionUI();
85                 }
86
87                 public function updateSelectionUIWithoutTagChange():void {
88                         toolbox.updateSelectionUI();
89                 }
90         
91         private function keyDownHandler(event:KeyboardEvent):void {
92                         if ((event.target is TextField) || (event.target is TextArea)) return;
93                         keys[event.keyCode]=true;
94                 }
95
96         private function keyUpHandler(event:KeyboardEvent):void {
97             if (!keys[event.keyCode]) return;
98             delete keys[event.keyCode];
99             if ((event.target is TextField) || (event.target is TextArea)) return;                              // not meant for us
100
101                         if (FunctionKeyManager.instance().handleKeypress(event.keyCode)) { return; }
102             
103             if (event.keyCode == 77) { toggleSize(); } // 'M'
104             var newState:ControllerState = state.processKeyboardEvent(event);
105             setState(newState);            
106                 }
107
108                 /** Is the given key currently pressed? */
109                 public function keyDown(key:Number):Boolean {
110                         return Boolean(keys[key]);
111                 }
112
113         private function mapMouseEvent(event:MouseEvent):void {
114             if (isInteractionEvent(event)) map.stage.focus = map.parent;
115             if (event.type==MouseEvent.MOUSE_UP && map.dragstate==map.DRAGGING) { return; }
116             
117             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
118             event.localX = mapLoc.x;
119             event.localY = mapLoc.y;
120
121             var newState:ControllerState = state.processMouseEvent(event, null);
122             setState(newState);
123         }
124         
125         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
126             if (isInteractionEvent(event)) map.stage.focus = map.parent;
127             event.stopPropagation();
128                 
129             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
130             event.localX = mapLoc.x;
131             event.localY = mapLoc.y;
132
133             var newState:ControllerState = state.processMouseEvent(event, entity);
134             setState(newState);
135         }
136
137                 private function isInteractionEvent(event:MouseEvent):Boolean {
138                         switch (event.type) {
139                                 case MouseEvent.ROLL_OUT:       return false;
140                                 case MouseEvent.ROLL_OVER:      return false;
141                                 case MouseEvent.MOUSE_OUT:      return false;
142                                 case MouseEvent.MOUSE_OVER:     return false;
143                                 case MouseEvent.MOUSE_MOVE:     return false;
144                 }
145                         return true;
146                 }
147
148         /** Exit the current state and switch to a new one.
149         *
150         *   @param newState The ControllerState to switch to. */
151         public function setState(newState:ControllerState):void {
152             if ( newState == state )
153                 return;
154                 
155             if ( state != null )
156                 state.exitState(newState);
157             newState.setController(this);
158             newState.setPreviousState(state);
159             state = newState;
160             state.enterState();
161         }
162
163                 /** Given what is currently selected (or not), find the matching ControllerState. */
164                 public function findStateForSelection(sel:Array):ControllerState {
165                         if (sel.length==0) { return new NoSelection(); }
166                         var layer:MapPaint=_map.getLayerForEntity(sel[0]);
167                         
168                         if (sel.length>1) { return new SelectedMultiple(sel, layer); }
169                         else if (sel[0] is Way) { return new SelectedWay(sel[0], layer); }
170                         else if (sel[0] is Node && Node(sel[0]).hasParentWays) {
171                                 var way:Way=sel[0].parentWays[0] as Way;
172                                 return new SelectedWayNode(way, way.indexOfNode(sel[0] as Node));
173                         } else {
174                                 return new SelectedPOINode(sel[0] as Node, layer);
175                         }
176                 }
177
178                 /** Set a mouse pointer. */
179                 public function setCursor(cursor:Class):void {
180                         CursorManager.removeAllCursors();
181                         if (cursor && cursorsEnabled) { CursorManager.setCursor(cursor,2,-4,0); }
182                 }
183
184         private function toggleSize():void {
185             if (maximised) {
186                 if (minimiseFunction) {
187                     ExternalInterface.call(minimiseFunction);
188                 }
189
190                 maximised = false;
191             } else {
192                 if (maximiseFunction) {
193                     ExternalInterface.call(maximiseFunction);
194                 }
195
196                 maximised = true;
197             }
198         }
199
200                 private function moveHandler(event:MapEvent):void {
201                         ExternalInterface.call(this.moveFunction,
202                                    event.params.lon, event.params.lat, event.params.scale,
203                                    event.params.minlon, event.params.minlat,
204                                    event.params.maxlon, event.params.maxlat);
205                 }
206
207     }
208     
209 }