fix coord transport for the bitmap sprites
[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 selectedWay:Way;
14         private var selectedNode:Node;
15         
16         private var draggingNode:Node = null;
17         
18         private var state:ControllerState;
19         private var _connection:Connection;
20         
21
22         public function EditController(map:Map, tagViewer:TagViewer) {
23             this._map = map;
24             this.tagViewer = tagViewer;
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_UP, keyUpHandler);
32         }
33
34         public function setActive():void {
35             map.setController(this);
36             _connection = map.connection;
37         }
38
39         public function get map():Map {
40             return _map;
41         }
42         
43         public function get connection():Connection {
44             return _connection;
45         }
46         
47         public function setTagViewer(entity:Entity):void {
48             tagViewer.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         private 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         private function processNodeEvent(event:MouseEvent, entity:Entity):void {
94             if ( draggingNode != null ) {
95                 if ( event.type == MouseEvent.MOUSE_UP ) {
96                     draggingNode = null;
97                 } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
98                     draggingNode.lat = map.coord2lat(event.localY);
99                     draggingNode.lon = map.coord2lon(event.localX);
100                 }
101             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
102                 draggingNode = entity as Node;
103             }
104         }
105         
106     }
107
108     
109 }
110