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