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