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