first stage of undo/redo functionality -- there are still a number of actions that...
[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                                                 if (selectedWay.getNode(0)==selectedWay.getNode(selectedWay.length-1)) {
56                                                         return new SelectedWay(selectedWay);
57                                                 }
58                                         }
59                                 } else if ( entity is Way ) {
60                                         node = createAndAddNode(event);
61                                         Way(entity).insertNodeAtClosestPosition(node, true);
62                                         resetElastic(node);
63                                         lastClick=node;
64                                 }
65                                 lastClickTime=new Date();
66                         } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
67                                 mouse = new Point(
68                                                   controller.map.coord2lon(event.localX),
69                                                   controller.map.coord2latp(event.localY));
70                                 elastic.end = mouse;
71                         } else if ( event.type == MouseEvent.ROLL_OVER && focus!=selectedWay) {
72                                 controller.map.setHighlight(focus, { showNodesHover: true });
73                         } else if ( event.type == MouseEvent.MOUSE_OUT  && focus!=selectedWay) {
74                                 controller.map.setHighlight(focus, { showNodesHover: false });
75                                 controller.map.setHighlight(selectedWay, { showNodes: true });
76                                 // ** this call to setHighlight(selectedWay) is necessary in case the hovered way (blue nodes)
77                                 // shares any nodes with the selected way (red nodes): if they do, they'll be wiped out by the
78                                 // first call.
79                                 // Ultimately we should fix this by referring to 'way :selected nodes' instead of 'nodes :selectedway'.
80                                 // But this will do for now.
81                                 // We could do with an optional way of calling WayUI.redraw to only do the nodes, which would be a
82                                 // useful optimisation.
83                         }
84
85                         return this;
86                 }
87                 
88                 protected function resetElastic(node:Node):void {
89                         var mouse:Point = new Point(node.lon, node.latp);
90                         elastic.start = mouse;
91                         elastic.end = mouse;
92                 }
93
94                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
95                         if ( event.keyCode == 13 || event.keyCode == 27 ) { return stopDrawing(); }
96                         return this;
97                 }
98                 
99                 protected function stopDrawing():ControllerState {
100                         if ( selectedWay.length<2) {
101                                 controller.map.setHighlight(selectedWay, { showNodes: false });
102                                 selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
103                                 // delete controller.map.ways[selectedWay.id];
104                                 return new NoSelection();
105                         } else if ( leaveNodeSelected ) {
106                             return new SelectedWayNode(selectedWay, selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0));
107                         } else {
108                             return new SelectedWay(selectedWay);
109                         }
110                         return this;
111                 }
112
113                 public function createAndAddNode(event:MouseEvent):Node {
114                         var lat:Number = controller.map.coord2lat(event.localY);
115                         var lon:Number = controller.map.coord2lon(event.localX);
116                         var node:Node = controller.connection.createNode({}, lat, lon);
117                         appendNode(node);
118                         return node;
119                 }
120                 
121                 protected function appendNode(node:Node):void {
122                         if ( editEnd )
123                                 selectedWay.appendNode(node);
124                         else
125                                 selectedWay.insertNode(0, node);
126                 }
127                 
128                 override public function enterState():void {
129                         super.enterState();
130                         
131                         var node:Node = selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0);
132                         var start:Point = new Point(node.lon, node.latp);
133                         elastic = new Elastic(controller.map, start, start);
134                         Globals.vars.root.addDebug("**** -> "+this);
135                 }
136                 override public function exitState():void {
137                         super.exitState();
138                         elastic.removeSprites();
139                         elastic = null;
140                         Globals.vars.root.addDebug("**** <- "+this);
141                 }
142                 override public function toString():String {
143                         return "DrawWay";
144                 }
145         }
146 }