0e6ff89719ceee9f0d513e2d60e43f6534a2d7c6
[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                 
14                 public function DrawWay(way:Way, editEnd:Boolean, leaveNodeSelected:Boolean) {
15                         super(way);
16                         this.editEnd = editEnd;
17                         this.leaveNodeSelected = leaveNodeSelected;
18                 }
19                 
20                 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
21                         var mouse:Point;
22                         var node:Node;
23                         var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
24
25                         if ( event.type == MouseEvent.MOUSE_UP ) {
26                                 if ( entity == null ) {
27                                         node = createAndAddNode(event);
28                                         resetElastic(node);
29                                 } else if ( entity is Node ) {
30                                         appendNode(entity as Node);
31                                         controller.map.setHighlight(focus, { showNodesHover: false });
32                                         controller.map.setHighlight(selectedWay, { showNodes: true });
33                                         resetElastic(entity as Node);
34                                 } else if ( entity is Way ) {
35                                         node = createAndAddNode(event);
36                                         Way(entity).insertNodeAtClosestPosition(node, true);
37                                         resetElastic(node);
38                                 }
39                         } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
40                                 mouse = new Point(
41                                                   controller.map.coord2lon(event.localX),
42                                                   controller.map.coord2latp(event.localY));
43                                 elastic.end = mouse;
44                         } else if ( event.type == MouseEvent.MOUSE_OVER && focus!=selectedWay) {
45                                 controller.map.setHighlight(focus, { showNodesHover: true });
46                         } else if ( event.type == MouseEvent.MOUSE_OUT  && focus!=selectedWay) {
47                                 controller.map.setHighlight(focus, { showNodesHover: false });
48                                 controller.map.setHighlight(selectedWay, { showNodes: true });
49                                 // ** this call to setHighlight(selectedWay) is necessary in case the hovered way (blue nodes)
50                                 // shares any nodes with the selected way (red nodes): if they do, they'll be wiped out by the
51                                 // first call.
52                                 // Ultimately we should fix this by referring to 'way :selected nodes' instead of 'nodes :selectedway'.
53                                 // But this will do for now.
54                                 // We could do with an optional way of calling WayUI.redraw to only do the nodes, which would be a
55                                 // useful optimisation.
56                         }
57
58                         return this;
59                 }
60                 
61                 protected function resetElastic(node:Node):void {
62                         var mouse:Point = new Point(node.lon, node.latp);
63                         elastic.start = mouse;
64                         elastic.end = mouse;
65                 }
66
67                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
68                         if ( event.keyCode == 13 || event.keyCode == 27 ) {
69                                 if ( leaveNodeSelected ) 
70                                     return new SelectedWayNode(selectedWay, selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0));
71                                 else
72                                     return new SelectedWay(selectedWay);
73                         }
74                         return this;
75                 }
76
77                 public function createAndAddNode(event:MouseEvent):Node {
78                         var lat:Number = controller.map.coord2lat(event.localY);
79                         var lon:Number = controller.map.coord2lon(event.localX);
80                         var node:Node = controller.connection.createNode({}, lat, lon);
81                         appendNode(node);
82                         return node;
83                 }
84                 
85                 protected function appendNode(node:Node):void {
86                         if ( editEnd )
87                                 selectedWay.appendNode(node);
88                         else
89                                 selectedWay.insertNode(0, node);
90                 }
91                 
92                 override public function enterState():void {
93                         super.enterState();
94                         
95                         var node:Node = selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0);
96                         var start:Point = new Point(node.lon, node.latp);
97                         elastic = new Elastic(controller.map, start, start);
98                         Globals.vars.root.addDebug("**** -> "+this);
99                 }
100                 override public function exitState():void {
101                         super.exitState();
102                         elastic.removeSprites();
103                         elastic = null;
104                         Globals.vars.root.addDebug("**** <- "+this);
105                 }
106                 override public function toString():String {
107                         return "DrawWay";
108                 }
109         }
110 }