space-drag for backgrounds, plus D to dim (au revoir caps lock)
[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 flash.events.*;
7         import flash.geom.*;
8
9     public class EditController implements MapController {
10
11         private var _map:Map;
12         private var tagViewer:TagViewer;
13                 private var toolbox:Toolbox;
14         
15         public var state:ControllerState;
16         private var _connection:Connection;
17         
18                 private var keys:Object={};
19
20         public function EditController(map:Map, tagViewer:TagViewer, toolbox:Toolbox) {
21             this._map = map;
22             this.tagViewer = tagViewer;
23                         this.toolbox = toolbox;
24                         this.toolbox.init(this);
25             setState(new NoSelection());
26             
27             map.parent.addEventListener(MouseEvent.MOUSE_MOVE, mapMouseEvent);
28             map.parent.addEventListener(MouseEvent.MOUSE_UP, mapMouseEvent);
29             map.parent.addEventListener(MouseEvent.MOUSE_DOWN, mapMouseEvent);
30             map.parent.addEventListener(MouseEvent.CLICK, mapMouseEvent);
31             map.parent.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
32             map.parent.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
33         }
34
35         public function setActive():void {
36             map.setController(this);
37             _connection = map.connection;
38         }
39
40         public function get map():Map {
41             return _map;
42         }
43         
44         public function get connection():Connection {
45             return _connection;
46         }
47         
48         public function setSelectedEntity(entity:Entity):void {
49             tagViewer.setEntity(entity);
50                         toolbox.setEntity(entity);
51         }
52         
53         private function keyDownHandler(event:KeyboardEvent):void {
54                         keys[event.keyCode]=true;
55                 }
56
57         private function keyUpHandler(event:KeyboardEvent):void {
58             trace("key code "+event.keyCode);
59                         if (keys[event.keyCode]) { delete keys[event.keyCode]; }
60             var newState:ControllerState = state.processKeyboardEvent(event);
61             setState(newState);            
62                 }
63
64                 public function keyDown(key:Number):Boolean {
65                         return Boolean(keys[key]);
66                 }
67
68         private function mapMouseEvent(event:MouseEvent):void {
69             map.stage.focus = map.parent;
70             
71             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
72             event.localX = mapLoc.x;
73             event.localY = mapLoc.y;
74
75             var newState:ControllerState = state.processMouseEvent(event, null);
76             setState(newState);
77         }
78         
79         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
80             map.stage.focus = map.parent;
81             //if ( event.type == MouseEvent.MOUSE_DOWN )
82             event.stopPropagation();
83                 
84             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
85             event.localX = mapLoc.x;
86             event.localY = mapLoc.y;
87
88             var newState:ControllerState = state.processMouseEvent(event, entity);
89             setState(newState);
90         }
91         
92         public function setState(newState:ControllerState):void {
93             if ( newState == state )
94                 return;
95                 
96             if ( state != null )
97                 state.exitState();
98             newState.setController(this);
99             newState.setPreviousState(state);
100             state = newState;
101             state.enterState();
102         }
103
104     }
105
106     
107 }
108