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