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