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