more selection fixes
[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         import net.systemeD.halcyon.Globals;
8
9     public class CreateWay extends ControllerState {
10         private var start:Point;
11         private var mouse:Point;
12         private var elastic:Elastic;
13         
14         public function CreateWay(event:MouseEvent) {
15             start = new Point(event.localX, event.localY);
16             mouse = new Point(event.localX, event.localY);
17         }
18         
19         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
20             var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
21             if ( event.type == MouseEvent.CLICK ) {
22                 if ( focus == null ) {
23                     var lat:Number = controller.map.coord2lat(event.localY);
24                     var lon:Number = controller.map.coord2lon(event.localX);
25                     var endNode:Node = controller.connection.createNode({}, lat, lon);
26                     
27                     lat = Node.latp2lat(start.y);
28                     lon = start.x;
29                     var startNode:Node = controller.connection.createNode({}, lat, lon);
30                     
31                     var way:Way = controller.connection.createWay({}, [startNode, endNode]);
32                     return new DrawWay(way, true);
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         override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
45             if ( event.keyCode == 27 )
46                 return new NoSelection();
47             return this;
48         }
49
50         override public function enterState():void {
51             // transform points
52             start.x = controller.map.coord2lon(start.x);
53             start.y = controller.map.coord2latp(start.y);
54             mouse.x = controller.map.coord2lon(mouse.x);
55             mouse.y = controller.map.coord2latp(mouse.y);
56             
57             elastic = new Elastic(controller.map, start, mouse);
58                         Globals.vars.root.addDebug("**** -> "+this);
59         }
60         
61         override public function exitState():void {
62             elastic.removeSprites();
63             elastic = null;
64                         Globals.vars.root.addDebug("**** <- "+this);
65         }
66
67         override public function toString():String {
68             return "CreateWay";
69         }
70     }
71 }