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