refactor EditController to make it stateful, and add addWayNodes functionality with...
[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         }
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 setTagViewer(entity:Entity):void {
47             tagViewer.setEntity(entity);
48         }
49         
50         private function mapMouseEvent(event:MouseEvent):void {
51             var mapLoc:Point = map.globalToLocal(new Point(event.stageX, event.stageY));
52             event.localX = mapLoc.x;
53             event.localY = mapLoc.y;
54
55             var newState:ControllerState = state.processMouseEvent(event, null);
56             setState(newState);
57             if ( draggingNode != null ) {
58             }
59         }
60         
61         public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
62             //if ( event.type == MouseEvent.MOUSE_DOWN )
63                 event.stopPropagation();
64                 
65             var newState:ControllerState = state.processMouseEvent(event, entity);
66             setState(newState);
67
68             /*
69             if ( entity is Node || draggingNode != null ) {
70                 processNodeEvent(event, entity);
71             } else if ( enity is Way ) {
72                 processWayEvent(event, entity);
73             }
74             
75             if ( event.type == MouseEvent.CLICK ) {
76                 if ( selectedWay != null ) {
77                     map.setHighlight(selectedWay, "selected", false);
78                     map.setHighlight(selectedWay, "showNodes", false);
79                 }
80                 tagViewer.setEntity(entity);
81                 map.setHighlight(entity, "selected", true);
82                 map.setHighlight(entity, "showNodes", true);
83                 selectedWay = entity;
84             } else if ( event.type == MouseEvent.MOUSE_OVER )
85                 map.setHighlight(entity, "hover", true);
86             else if ( event.type == MouseEvent.MOUSE_OUT )
87                 map.setHighlight(entity, "hover", false);
88             */
89         }
90         
91         private function setState(newState:ControllerState):void {
92             if ( newState == state )
93                 return;
94                 
95             if ( state != null )
96                 state.exitState();
97             newState.setController(this);
98             newState.setPreviousState(state);
99             state = newState;
100             state.enterState();
101         }
102
103         private function processNodeEvent(event:MouseEvent, entity:Entity):void {
104             if ( draggingNode != null ) {
105                 if ( event.type == MouseEvent.MOUSE_UP ) {
106                     draggingNode = null;
107                 } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
108                     draggingNode.lat = map.coord2lat(event.localY);
109                     draggingNode.lon = map.coord2lon(event.localX);
110                 }
111             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
112                 draggingNode = entity as Node;
113             }
114         }
115         
116     }
117
118     
119 }
120