Allow two javascript functions to be specified which will maximise and
[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.connection.*;
5     import net.systemeD.halcyon.VectorLayer;
6     import net.systemeD.potlatch2.controller.*;
7     import net.systemeD.potlatch2.FunctionKeyManager;
8         import mx.managers.CursorManager;
9     import flash.external.ExternalInterface;
10     import flash.events.*;
11         import flash.geom.*;
12
13     public class EditController implements MapController {
14
15         private var _map:Map;
16         public var tagViewer:TagViewer;
17                 private var toolbox:Toolbox;
18         
19         public var state:ControllerState;
20         private var _connection:Connection;
21         
22                 private var keys:Object={};
23                 public var clipboards:Object={};
24                 public var cursorsEnabled:Boolean=true;
25         private var maximised:Boolean=false;
26         private var maximiseFunction:String;
27         private var minimiseFunction:String;
28
29                 [Embed(source="../../../embedded/pen.png")]             public var pen:Class;
30                 [Embed(source="../../../embedded/pen_x.png")]           public var pen_x:Class;
31                 [Embed(source="../../../embedded/pen_o.png")]           public var pen_o:Class;
32                 [Embed(source="../../../embedded/pen_so.png")]          public var pen_so:Class;
33                 [Embed(source="../../../embedded/pen_plus.png")]        public var pen_plus:Class;
34                 
35         public function EditController(map:Map, tagViewer:TagViewer, toolbox:Toolbox) {
36             this._map = map;
37             setState(new NoSelection());
38             this.tagViewer = tagViewer;
39                         this.toolbox = toolbox;
40                         this.toolbox.init(this);
41             this.maximiseFunction = Connection.getParam("maximise_function", null);
42             this.minimiseFunction = Connection.getParam("minimise_function", null);
43             
44             map.parent.addEventListener(MouseEvent.MOUSE_MOVE, mapMouseEvent);
45             map.parent.addEventListener(MouseEvent.MOUSE_UP, mapMouseEvent);
46             map.parent.addEventListener(MouseEvent.MOUSE_DOWN, mapMouseEvent);
47             map.parent.addEventListener(MouseEvent.MOUSE_WHEEL, mapMouseEvent);
48             map.parent.addEventListener(MouseEvent.CLICK, mapMouseEvent);
49             map.parent.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
50             map.parent.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
51         }
52
53         public function setActive():void {
54             map.setController(this);
55             _connection = map.connection;
56         }
57
58         public function get map():Map {
59             return _map;
60         }
61         
62         public function get connection():Connection {
63             return _connection;
64         }
65
66         /**
67         * Updates the various user interfaces that change when the selection changes.
68         * Currently this is the TagViewer and the Toolbox
69         *
70         * @param layer Optionally pass the layer of the currently selected entity, eg for BugLayers
71         */
72                 public function updateSelectionUI(layer:VectorLayer = null):void {
73                         tagViewer.setEntity(state.selection, layer);
74                         toolbox.updateSelectionUI();
75                 }
76         
77         private function keyDownHandler(event:KeyboardEvent):void {
78                         keys[event.keyCode]=true;
79                 }
80
81         private function keyUpHandler(event:KeyboardEvent):void {
82             trace("key code "+event.keyCode);
83                         if (keys[event.keyCode]) { delete keys[event.keyCode]; }
84                         if (FunctionKeyManager.instance().handleKeypress(event.keyCode)) { return; }
85             if (event.keyCode == 77) { toggleSize(); }
86             var newState:ControllerState = state.processKeyboardEvent(event);
87             setState(newState);            
88                 }
89
90                 public function keyDown(key:Number):Boolean {
91                         return Boolean(keys[key]);
92                 }
93
94         private function mapMouseEvent(event:MouseEvent):void {
95             if (event.type!=MouseEvent.ROLL_OVER) map.stage.focus = map.parent;
96             if (event.type==MouseEvent.MOUSE_UP && map.dragstate==map.DRAGGING) { return; }
97             
98             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
99             event.localX = mapLoc.x;
100             event.localY = mapLoc.y;
101
102             var newState:ControllerState = state.processMouseEvent(event, null);
103             setState(newState);
104         }
105         
106         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
107             if (event.type!=MouseEvent.ROLL_OVER) map.stage.focus = map.parent;
108             //if ( event.type == MouseEvent.MOUSE_DOWN )
109             event.stopPropagation();
110                 
111             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
112             event.localX = mapLoc.x;
113             event.localY = mapLoc.y;
114
115             var newState:ControllerState = state.processMouseEvent(event, entity);
116             setState(newState);
117         }
118         
119         public function setState(newState:ControllerState):void {
120             if ( newState == state )
121                 return;
122                 
123             if ( state != null )
124                 state.exitState(newState);
125             newState.setController(this);
126             newState.setPreviousState(state);
127             state = newState;
128             state.enterState();
129         }
130
131                 public function findStateForSelection(sel:Array):ControllerState {
132                         if (sel.length==0) { return new NoSelection(); }
133                         else if (sel.length>1) { return new SelectedMultiple(sel); }
134                         else if (sel[0] is Way) { return new SelectedWay(sel[0]); }
135                         else if (sel[0] is Node && Node(sel[0]).hasParentWays) {
136                                 var way:Way=sel[0].parentWays[0] as Way;
137                                 return new SelectedWayNode(way, way.indexOfNode(sel[0] as Node));
138                         } else {
139                                 return new SelectedPOINode(sel[0] as Node);
140                         }
141                 }
142
143                 public function setCursor(cursor:Class):void {
144                         CursorManager.removeAllCursors();
145                         if (cursor && cursorsEnabled) { CursorManager.setCursor(cursor,2,-4,0); }
146                 }
147
148         private function toggleSize():void {
149             if (maximised) {
150                 if (minimiseFunction) {
151                     ExternalInterface.call(minimiseFunction);
152                 }
153
154                 maximised = false;
155             } else {
156                 if (maximiseFunction) {
157                     ExternalInterface.call(maximiseFunction);
158                 }
159
160                 maximised = true;
161             }
162         }
163
164     }
165     
166 }