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