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