create new ways w00t! and extend old ones
[potlatch2.git] / net / systemeD / potlatch2 / controller / CreateWay.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 CreateWay extends ControllerState {
9         private var start:Point;
10         private var mouse:Point;
11         private var elastic:Elastic;
12         
13         public function CreateWay(event:MouseEvent) {
14             start = new Point(event.localX, event.localY);
15             mouse = new Point(event.localX, event.localY);
16         }
17         
18         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
19             var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
20             if ( event.type == MouseEvent.CLICK ) {
21                 if ( focus == null ) {
22                     var lat:Number = controller.map.coord2lat(event.localY);
23                     var lon:Number = controller.map.coord2lon(event.localX);
24                     var endNode:Node = controller.connection.createNode({}, lat, lon);
25                     
26                     lat = Node.latp2lat(start.y);
27                     lon = start.x;
28                     var startNode:Node = controller.connection.createNode({}, lat, lon);
29                     
30                     var way:Way = controller.connection.createWay({}, [startNode, endNode]);
31                     return new DrawWay(way, true);
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         override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
44             if ( event.keyCode == 27 )
45                 return new NoSelection();
46             return this;
47         }
48
49         override public function enterState():void {
50             // transform points
51             start.x = controller.map.coord2lon(start.x);
52             start.y = controller.map.coord2latp(start.y);
53             mouse.x = controller.map.coord2lon(mouse.x);
54             mouse.y = controller.map.coord2latp(mouse.y);
55             
56             elastic = new Elastic(controller.map, start, mouse);
57         }
58         
59         override public function exitState():void {
60             elastic.removeSprites();
61             elastic = null;
62         }
63     }
64 }