create new ways w00t! and extend old ones
[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
8     public class DrawWay extends SelectedWay {
9         private var elastic:Elastic;
10         private var editEnd:Boolean;
11         
12         public function DrawWay(way:Way, editEnd:Boolean) {
13             super(way);
14             
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             if ( event.type == MouseEvent.CLICK ) {
23                 if ( focus == null ) {
24                     node = createAndAddNode(event);
25                     resetElastic(node);
26                 } else if ( focus is Node ) {
27                     appendNode(focus as Node);
28                 } else if ( focus is Way ) {
29                     node = createAndAddNode(event);
30                     Way(focus).insertNodeAtClosestPosition(node, true);
31                     resetElastic(node);
32                 }
33             } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
34                 mouse = new Point(
35                           controller.map.coord2lon(event.localX),
36                           controller.map.coord2latp(event.localY));
37                 elastic.end = mouse;
38             }
39
40             return this;
41         }
42         
43         protected function resetElastic(node:Node):void {
44             var mouse:Point = new Point(node.lon, node.latp);
45             elastic.start = mouse;
46             elastic.end = mouse;
47         }
48
49         override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
50             if ( event.keyCode == 13 || event.keyCode == 27 )
51                 return new SelectedWay(selectedWay);
52             return this;
53         }
54
55         public function createAndAddNode(event:MouseEvent):Node {
56             var lat:Number = controller.map.coord2lat(event.localY);
57             var lon:Number = controller.map.coord2lon(event.localX);
58             var node:Node = controller.connection.createNode({}, lat, lon);
59             appendNode(node);
60             return node;
61         }
62         
63         protected function appendNode(node:Node):void {
64             if ( editEnd )
65                 selectedWay.appendNode(node);
66             else
67                 selectedWay.insertNode(0, node);
68         }
69         
70         override public function enterState():void {
71             super.enterState();
72             
73             var node:Node = selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0);
74             var start:Point = new Point(node.lon, node.latp);
75             elastic = new Elastic(controller.map, start, start);
76         }
77         override public function exitState():void {
78             super.exitState();
79             elastic.removeSprites();
80             elastic = null;
81         }
82     }
83 }