create new ways w00t! and extend old ones
[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 newState:ControllerState = state.processMouseEvent(event, entity);
74             setState(newState);
75         }
76         
77         private 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         private function processNodeEvent(event:MouseEvent, entity:Entity):void {
90             if ( draggingNode != null ) {
91                 if ( event.type == MouseEvent.MOUSE_UP ) {
92                     draggingNode = null;
93                 } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
94                     draggingNode.lat = map.coord2lat(event.localY);
95                     draggingNode.lon = map.coord2lon(event.localX);
96                 }
97             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
98                 draggingNode = entity as Node;
99             }
100         }
101         
102     }
103
104     
105 }
106