fix creating ways duplicate node bug, and connecting to existing way-node bug
[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
22                         if ( event.type == MouseEvent.MOUSE_UP ) {
23                                 if ( entity == null ) {
24                                         node = createAndAddNode(event);
25                                         resetElastic(node);
26                                 } else if ( entity is Node ) {
27                                         appendNode(entity as Node);
28                                         resetElastic(entity as Node);
29                                 } else if ( entity is Way ) {
30                                         node = createAndAddNode(event);
31                                         Way(entity).insertNodeAtClosestPosition(node, true);
32                                         resetElastic(node);
33                                 }
34                         } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
35                                 mouse = new Point(
36                                                   controller.map.coord2lon(event.localX),
37                                                   controller.map.coord2latp(event.localY));
38                                 elastic.end = mouse;
39                         }
40
41                         return this;
42                 }
43                 
44                 protected function resetElastic(node:Node):void {
45                         var mouse:Point = new Point(node.lon, node.latp);
46                         elastic.start = mouse;
47                         elastic.end = mouse;
48                 }
49
50                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
51                         if ( event.keyCode == 13 || event.keyCode == 27 )
52                                 return new SelectedWay(selectedWay);
53                         return this;
54                 }
55
56                 public function createAndAddNode(event:MouseEvent):Node {
57                         var lat:Number = controller.map.coord2lat(event.localY);
58                         var lon:Number = controller.map.coord2lon(event.localX);
59                         var node:Node = controller.connection.createNode({}, lat, lon);
60                         appendNode(node);
61                         return node;
62                 }
63                 
64                 protected function appendNode(node:Node):void {
65                         if ( editEnd )
66                                 selectedWay.appendNode(node);
67                         else
68                                 selectedWay.insertNode(0, node);
69                 }
70                 
71                 override public function enterState():void {
72                         super.enterState();
73                         
74                         var node:Node = selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0);
75                         var start:Point = new Point(node.lon, node.latp);
76                         elastic = new Elastic(controller.map, start, start);
77                         Globals.vars.root.addDebug("**** -> "+this);
78                 }
79                 override public function exitState():void {
80                         super.exitState();
81                         elastic.removeSprites();
82                         elastic = null;
83                         Globals.vars.root.addDebug("**** <- "+this);
84                 }
85                 override public function toString():String {
86                         return "DrawWay";
87                 }
88         }
89 }