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