support GPX 1.1 too
[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         import net.systemeD.halcyon.Globals;
8
9     public class SelectedParallelWay extends SelectedWay {
10                 private var startlon:Number;
11                 private var startlatp:Number;
12                 private var parallelise:Parallelise;
13                 private var originalWay:Way;
14
15         public function SelectedParallelWay(originalWay:Way) {
16                         this.originalWay = originalWay;
17                         parallelise = new Parallelise(originalWay);
18                         super (parallelise.parallelWay);
19         }
20
21         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
22                         if (event.type==MouseEvent.MOUSE_MOVE) {
23                                 var lon:Number =controller.map.coord2lon(controller.map.mouseX);
24                                 var latp:Number=controller.map.coord2latp(controller.map.mouseY);
25                                 parallelise.draw(distanceFromWay(lon,latp));
26                         } else if (event.type==MouseEvent.MOUSE_UP) {
27                                 return new SelectedWay(firstSelected as Way);
28                         }
29                         return this;
30         }
31
32                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
33                         if (event.keyCode==27) {                        // Escape
34                                 Way(firstSelected).remove(MainUndoStack.getGlobalStack().addAction);
35                                 return new NoSelection();
36                         }
37                         var cs:ControllerState = sharedKeyboardEvents(event);
38                         return cs ? cs : this;
39                 }
40
41                 private function sgn(a:Number):Number {
42                         if (a==0) return 0;
43                         if (a<0) return -1;
44                         return 1;
45                 }
46                 
47                 private function distanceFromWay(lon:Number,latp:Number):Number {
48                         var i:uint, ax:Number, ay:Number, bx:Number, by:Number, l:Number;
49                         var ad:Number, bd:Number;
50                         var r:Number, px:Number, py:Number;
51                         var furthdist:Number=-1; var furthsgn:int=1;
52                         for (i=0; i<originalWay.length-1; i++) {
53                                 ax=originalWay.getNode(i).lon;
54                                 ay=originalWay.getNode(i).latp;
55                                 bx=originalWay.getNode(i+1).lon;
56                                 by=originalWay.getNode(i+1).latp;
57
58                                 ad=Math.sqrt(Math.pow(lon-ax,2)+Math.pow(latp-ay,2));   // distance to ax,ay
59                                 bd=Math.sqrt(Math.pow(bx-lon,2)+Math.pow(by-latp,2));   // distance to bx,by
60                                 l =Math.sqrt(Math.pow(bx-ax ,2)+Math.pow(by-ay  ,2));   // length of segment
61                                 r =ad/(ad+bd);                                                                                  // proportion along segment
62                                 px=ax+r*(bx-ax); py=ay+r*(by-ay);                                               // nearest point on line
63                                 r=Math.sqrt(Math.pow(px-lon,2)+Math.pow(py-latp,2));    // distance from px,py to lon,latp
64
65                                 if (furthdist<0 || furthdist>r) {
66                                         furthdist=r; 
67                                         furthsgn=sgn((bx-ax)*(latp-ay)-(by-ay)*(lon-ax));
68                                 }
69                         }
70                         return furthdist*furthsgn;
71                 }
72
73                 override public function enterState():void {
74                         selection=[parallelise.parallelWay];
75                         controller.map.paint.createWayUI(firstSelected as Way);
76                         startlon =controller.map.coord2lon(controller.map.mouseX);
77                         startlatp=controller.map.coord2latp(controller.map.mouseY);
78                         Globals.vars.root.addDebug("**** -> "+this);
79         }
80                 override public function exitState(newState:ControllerState):void {
81             clearSelection(newState);
82                         Globals.vars.root.addDebug("**** <- "+this);
83         }
84
85         override public function toString():String {
86             return "SelectedParallelWay";
87         }
88     }
89 }