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