various click-handling improvements/fixes
[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                         this.editEnd = editEnd;
15                 }
16                 
17                 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
18                         var mouse:Point;
19                         var node:Node;
20                         var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
21                         if ( event.type == MouseEvent.CLICK ) {
22                                 if ( focus == null ) {
23                                         node = createAndAddNode(event);
24                                         resetElastic(node);
25                                 } else if ( focus is Node ) {
26                                         appendNode(focus as Node);
27                                 } else if ( focus is Way ) {
28                                         node = createAndAddNode(event);
29                                         Way(focus).insertNodeAtClosestPosition(node, true);
30                                         resetElastic(node);
31                                 }
32                         } else if ( event.type == MouseEvent.MOUSE_MOVE ) {
33                                 mouse = new Point(
34                                                   controller.map.coord2lon(event.localX),
35                                                   controller.map.coord2latp(event.localY));
36                                 elastic.end = mouse;
37                         }
38
39                         return this;
40                 }
41                 
42                 protected function resetElastic(node:Node):void {
43                         var mouse:Point = new Point(node.lon, node.latp);
44                         elastic.start = mouse;
45                         elastic.end = mouse;
46                 }
47
48                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
49                         if ( event.keyCode == 13 || event.keyCode == 27 )
50                                 return new SelectedWay(selectedWay);
51                         return this;
52                 }
53
54                 public function createAndAddNode(event:MouseEvent):Node {
55                         var lat:Number = controller.map.coord2lat(event.localY);
56                         var lon:Number = controller.map.coord2lon(event.localX);
57                         var node:Node = controller.connection.createNode({}, lat, lon);
58                         appendNode(node);
59                         return node;
60                 }
61                 
62                 protected function appendNode(node:Node):void {
63                         if ( editEnd )
64                                 selectedWay.appendNode(node);
65                         else
66                                 selectedWay.insertNode(0, node);
67                 }
68                 
69                 override public function enterState():void {
70                         super.enterState();
71                         
72                         var node:Node = selectedWay.getNode(editEnd ? selectedWay.length - 1 : 0);
73                         var start:Point = new Point(node.lon, node.latp);
74                         elastic = new Elastic(controller.map, start, start);
75                 }
76                 override public function exitState():void {
77                         super.exitState();
78                         elastic.removeSprites();
79                         elastic = null;
80                 }
81                 override public function toString():String {
82                         return "DrawWay";
83                 }
84         }
85 }