beginning of vector background layers. And a whole load of other stuff too
[potlatch2.git] / net / systemeD / potlatch2 / controller / DrawWay.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.geom.*;
4         import net.systemeD.potlatch2.EditController;
5         import net.systemeD.halcyon.connection.*;
6         import net.systemeD.halcyon.Elastic;
7         import net.systemeD.halcyon.Globals;
8
9         public class DrawWay extends SelectedWay {
10                 private var elastic:Elastic;
11                 private var editEnd:Boolean;
12                 private var leaveNodeSelected:Boolean;
13                 private var lastClick:Entity=null;
14                 private var lastClickTime:Date;
15                 
16                 public function DrawWay(way:Way, editEnd:Boolean, leaveNodeSelected:Boolean) {
17                         super(way);
18                         this.editEnd = editEnd;
19                         this.leaveNodeSelected = leaveNodeSelected;
20                         if (way.length==1 && way.getNode(0).parentWays.length==1) {
21                                 // drawing new way, so keep track of click in case creating a POI
22                                 lastClick=way.getNode(0);
23                                 lastClickTime=new Date();
24                         }
25                 }
26                 
27                 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
28                         var mouse:Point;
29                         var node:Node;
30                         var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
31
32                         if ( event.type == MouseEvent.MOUSE_UP ) {
33                                 if ( entity == null ) {
34                                         node = createAndAddNode(event);
35                                         resetElastic(node);
36                                         lastClick=node;
37                                 } else if ( entity is Node ) {
38                                         if (entity==lastClick && (new Date().getTime()-lastClickTime.getTime())<1000) {
39                                                 if (selectedWay.length==1 && selectedWay.getNode(0).parentWays.length==1) {
40                                                         // double-click to create new POI
41                                                         node=selectedWay.getNode(0);
42                                                         stopDrawing();
43                                                         controller.connection.registerPOI(node);
44                                                         return new SelectedPOINode(node);
45                                                 } else {
46                                                         // double-click at end of way
47                                                         return stopDrawing();
48                                                 }
49                                         } else {
50                                                 appendNode(entity as Node);
51                                                 controller.map.setHighlight(focus, { showNodesHover: false });
52                                                 controller.map.setHighlight(selectedWay, { showNodes: true });
53                                                 resetElastic(entity as Node);
54                                                 lastClick=entity;
55                                         }
56                                 } else if ( entity is Way ) {
57                                         node = createAndAddNode(event);
58                                         Way(entity).insertNodeAtClosestPosition(node, true);
59                                         resetElastic(node);
60                                         lastClick=node;
61                                 }
62                                 lastClickTime=new Date();
63                         } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
64                                 mouse = new Point(
65                                                   controller.map.coord2lon(event.localX),
66                                                   controller.map.coord2latp(event.localY));
67                                 elastic.end = mouse;
68                         } else if ( event.type == MouseEvent.ROLL_OVER && focus!=selectedWay) {
69                                 controller.map.setHighlight(focus, { showNodesHover: true });
70                         } else if ( event.type == MouseEvent.MOUSE_OUT  && focus!=selectedWay) {
71                                 controller.map.setHighlight(focus, { showNodesHover: false });
72                                 controller.map.setHighlight(selectedWay, { showNodes: true });
73                                 // ** this call to setHighlight(selectedWay) is necessary in case the hovered way (blue nodes)
74                                 // shares any nodes with the selected way (red nodes): if they do, they'll be wiped out by the
75                                 // first call.
76                                 // Ultimately we should fix this by referring to 'way :selected nodes' instead of 'nodes :selectedway'.
77                                 // But this will do for now.
78                                 // We could do with an optional way of calling WayUI.redraw to only do the nodes, which would be a
79                                 // useful optimisation.
80                         }
81
82                         return this;
83                 }
84                 
85                 protected function resetElastic(node:Node):void {
86                         var mouse:Point = new Point(node.lon, node.latp);
87                         elastic.start = mouse;
88                         elastic.end = mouse;
89                 }
90
91                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
92                         if ( event.keyCode == 13 || event.keyCode == 27 ) { return stopDrawing(); }
93                         return this;
94                 }
95                 
96                 protected function stopDrawing():ControllerState {
97                         if ( selectedWay.length<2) {
98                                 controller.map.setHighlight(selectedWay, { showNodes: false });
99                                 selectedWay.remove();
100                                 // delete controller.map.ways[selectedWay.id];
101                                 return new NoSelection();
102                         } else if ( leaveNodeSelected ) {
103                             return new SelectedWayNode(selectedWay, selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0));
104                         } else {
105                             return new SelectedWay(selectedWay);
106                         }
107                         return this;
108                 }
109
110                 public function createAndAddNode(event:MouseEvent):Node {
111                         var lat:Number = controller.map.coord2lat(event.localY);
112                         var lon:Number = controller.map.coord2lon(event.localX);
113                         var node:Node = controller.connection.createNode({}, lat, lon);
114                         appendNode(node);
115                         return node;
116                 }
117                 
118                 protected function appendNode(node:Node):void {
119                         if ( editEnd )
120                                 selectedWay.appendNode(node);
121                         else
122                                 selectedWay.insertNode(0, node);
123                 }
124                 
125                 override public function enterState():void {
126                         super.enterState();
127                         
128                         var node:Node = selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0);
129                         var start:Point = new Point(node.lon, node.latp);
130                         elastic = new Elastic(controller.map, start, start);
131                         Globals.vars.root.addDebug("**** -> "+this);
132                 }
133                 override public function exitState():void {
134                         super.exitState();
135                         elastic.removeSprites();
136                         elastic = null;
137                         Globals.vars.root.addDebug("**** <- "+this);
138                 }
139                 override public function toString():String {
140                         return "DrawWay";
141                 }
142         }
143 }