put the cursor in the right place
[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         private 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
23                 [Embed(source="../../../embedded/pen.png")]             public var pen:Class;
24                 [Embed(source="../../../embedded/pen_x.png")]           public var pen_x:Class;
25                 [Embed(source="../../../embedded/pen_o.png")]           public var pen_o:Class;
26                 [Embed(source="../../../embedded/pen_so.png")]          public var pen_so:Class;
27                 [Embed(source="../../../embedded/pen_plus.png")]        public var pen_plus:Class;
28                 
29         public function EditController(map:Map, tagViewer:TagViewer, toolbox:Toolbox) {
30             this._map = map;
31             this.tagViewer = tagViewer;
32                         this.toolbox = toolbox;
33                         this.toolbox.init(this);
34             setState(new NoSelection());
35             
36             map.parent.addEventListener(MouseEvent.MOUSE_MOVE, mapMouseEvent);
37             map.parent.addEventListener(MouseEvent.MOUSE_UP, mapMouseEvent);
38             map.parent.addEventListener(MouseEvent.MOUSE_DOWN, mapMouseEvent);
39             map.parent.addEventListener(MouseEvent.CLICK, mapMouseEvent);
40             map.parent.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
41             map.parent.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
42         }
43
44         public function setActive():void {
45             map.setController(this);
46             _connection = map.connection;
47         }
48
49         public function get map():Map {
50             return _map;
51         }
52         
53         public function get connection():Connection {
54             return _connection;
55         }
56         
57         public function setSelectedEntity(entity:Entity):void {
58             tagViewer.setEntity(entity);
59                         toolbox.setEntity(entity);
60         }
61         
62         private function keyDownHandler(event:KeyboardEvent):void {
63                         keys[event.keyCode]=true;
64                 }
65
66         private function keyUpHandler(event:KeyboardEvent):void {
67             trace("key code "+event.keyCode);
68                         if (keys[event.keyCode]) { delete keys[event.keyCode]; }
69             var newState:ControllerState = state.processKeyboardEvent(event);
70             setState(newState);            
71                 }
72
73                 public function keyDown(key:Number):Boolean {
74                         return Boolean(keys[key]);
75                 }
76
77         private function mapMouseEvent(event:MouseEvent):void {
78             map.stage.focus = map.parent;
79             
80             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
81             event.localX = mapLoc.x;
82             event.localY = mapLoc.y;
83
84             var newState:ControllerState = state.processMouseEvent(event, null);
85             setState(newState);
86         }
87         
88         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
89             map.stage.focus = map.parent;
90             //if ( event.type == MouseEvent.MOUSE_DOWN )
91             event.stopPropagation();
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, entity);
98             setState(newState);
99         }
100         
101         public function setState(newState:ControllerState):void {
102             if ( newState == state )
103                 return;
104                 
105             if ( state != null )
106                 state.exitState();
107             newState.setController(this);
108             newState.setPreviousState(state);
109             state = newState;
110             state.enterState();
111         }
112
113                 public function setCursor(cursor:Class):void {
114                         CursorManager.removeAllCursors();
115                         if (cursor) { CursorManager.setCursor(cursor,2,-4,0); }
116                 }
117
118     }
119
120     
121 }
122