Show the shortcut key (J) for Merge Ways text in the toolbox.
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedParallelWay.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.display.Stage;
4         import flash.ui.Keyboard;
5     import net.systemeD.halcyon.connection.*;
6     import net.systemeD.halcyon.Map;
7         import net.systemeD.potlatch2.tools.Parallelise;
8         import net.systemeD.halcyon.connection.actions.*;
9
10     /** The state midway during the use of the "parallelise tool", where a parallel way has been created but is stuck to the 
11     * mouse cursor, allowing the user to choose how far from the original way it should go. This transforms it in the process. */
12     public class SelectedParallelWay extends SelectedWay {
13                 private var startlon:Number;
14                 private var startlatp:Number;
15                 private var parallelise:Parallelise;
16                 private var originalWay:Way;
17
18         /** Initialises by parallelising the originalWay. */
19         public function SelectedParallelWay(originalWay:Way) {
20                         this.originalWay = originalWay;
21                         parallelise = new Parallelise(originalWay);
22                         super (parallelise.parallelWay);
23         }
24
25         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
26                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.MOUSE_UP) {
27                                 var lon:Number =controller.map.coord2lon(controller.map.mouseX);
28                                 var latp:Number=controller.map.coord2latp(controller.map.mouseY);
29                                 parallelise.draw(distanceFromWay(lon,latp));
30                         }
31                         if (event.type==MouseEvent.MOUSE_UP) {
32                                 return new SelectedWay(firstSelected as Way);
33                         }
34                         return this;
35         }
36
37                 /** Cancel parallel way creation if ESC pressed. */
38                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
39                         if (event.keyCode==Keyboard.ESCAPE) {
40                                 Way(firstSelected).remove(MainUndoStack.getGlobalStack().addAction);
41                                 // Parallel way wasn't created, so remove it from undo history.
42                                 MainUndoStack.getGlobalStack().removeLastIfAction(DeleteWayAction);
43                 MainUndoStack.getGlobalStack().removeLastIfAction(CreateEntityAction);
44                                 
45                                 return new NoSelection();
46                         }
47                         var cs:ControllerState = sharedKeyboardEvents(event);
48                         return cs ? cs : this;
49                 }
50
51                 private function sgn(a:Number):Number {
52                         if (a==0) return 0;
53                         if (a<0) return -1;
54                         return 1;
55                 }
56                 
57                 private function distanceFromWay(lon:Number,latp:Number):Number {
58                         var i:uint, ax:Number, ay:Number, bx:Number, by:Number, l:Number;
59                         var ad:Number, bd:Number;
60                         var r:Number, px:Number, py:Number;
61                         var furthdist:Number=-1; var furthsgn:int=1;
62                         for (i=0; i<originalWay.length-1; i++) {
63                                 ax=originalWay.getNode(i).lon;
64                                 ay=originalWay.getNode(i).latp;
65                                 bx=originalWay.getNode(i+1).lon;
66                                 by=originalWay.getNode(i+1).latp;
67
68                                 ad=Math.sqrt(Math.pow(lon-ax,2)+Math.pow(latp-ay,2));   // distance to ax,ay
69                                 bd=Math.sqrt(Math.pow(bx-lon,2)+Math.pow(by-latp,2));   // distance to bx,by
70                                 l =Math.sqrt(Math.pow(bx-ax ,2)+Math.pow(by-ay  ,2));   // length of segment
71                                 r =ad/(ad+bd);                                                                                  // proportion along segment
72                                 px=ax+r*(bx-ax); py=ay+r*(by-ay);                                               // nearest point on line
73                                 r=Math.sqrt(Math.pow(px-lon,2)+Math.pow(py-latp,2));    // distance from px,py to lon,latp
74
75                                 if (furthdist<0 || furthdist>r) {
76                                         furthdist=r; 
77                                         furthsgn=sgn((bx-ax)*(latp-ay)-(by-ay)*(lon-ax));
78                                 }
79                         }
80                         return furthdist*furthsgn;
81                 }
82
83                 /** Creates the WayUI for the parallel way. */
84                 override public function enterState():void {
85                         selection=[parallelise.parallelWay];
86                         layer.createWayUI(firstSelected as Way);
87                         startlon =controller.map.coord2lon(controller.map.mouseX);
88                         startlatp=controller.map.coord2latp(controller.map.mouseY);
89         }
90                 /** Unselects. */
91                 override public function exitState(newState:ControllerState):void {
92             clearSelection(newState);
93         }
94
95         override public function toString():String {
96             return "SelectedParallelWay";
97         }
98     }
99 }