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