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.*;
13 import flash.ui.Keyboard;
14 import flash.text.TextField;
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 {
21 public var tagViewer:TagViewer;
22 private var toolbox:Toolbox;
24 public var state:ControllerState;
25 private var _connection:Connection;
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;
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;
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) {
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);
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);
60 if (this.moveFunction) {
61 map.addEventListener(MapEvent.MOVE, moveHandler);
65 public function setActive():void {
66 map.setController(this);
67 _connection = map.connection;
70 /** Accesses map object. */
71 public function get map():Map {
75 /** Accesss connection object. */
76 public function get connection():Connection {
81 * Updates the various user interfaces that change when the selection changes.
82 * Currently this is the TagViewer and the Toolbox
84 * @param layer Optionally pass the layer of the currently selected entity, eg for BugLayers
86 public function updateSelectionUI(layer:VectorLayer = null):void {
87 tagViewer.setEntity(state.selection, layer);
88 toolbox.updateSelectionUI();
91 public function updateSelectionUIWithoutTagChange():void {
92 toolbox.updateSelectionUI();
95 private function keyDownHandler(event:KeyboardEvent):void {
96 keys[event.keyCode]=true;
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; }
104 if (event.keyCode == 77) { toggleSize(); } // 'M'
105 var newState:ControllerState = state.processKeyboardEvent(event);
109 /** Is the given key currently pressed? */
110 public function keyDown(key:Number):Boolean {
111 return Boolean(keys[key]);
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; }
118 var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
119 event.localX = mapLoc.x;
120 event.localY = mapLoc.y;
122 var newState:ControllerState = state.processMouseEvent(event, null);
126 public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
127 if (isInteractionEvent(event)) map.stage.focus = map.parent;
128 event.stopPropagation();
130 var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
131 event.localX = mapLoc.x;
132 event.localY = mapLoc.y;
134 var newState:ControllerState = state.processMouseEvent(event, entity);
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;
149 /** Exit the current state and switch to a new one. */
150 public function setState(newState:ControllerState):void {
151 if ( newState == state )
155 state.exitState(newState);
156 newState.setController(this);
157 newState.setPreviousState(state);
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));
171 return new SelectedPOINode(sel[0] as Node);
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); }
181 private function toggleSize():void {
183 if (minimiseFunction) {
184 ExternalInterface.call(minimiseFunction);
189 if (maximiseFunction) {
190 ExternalInterface.call(maximiseFunction);
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);