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