Don't refresh the CategorySelector unless we need to
[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 || event.type==MouseEvent.MOUSE_UP) {
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                         }
29                         if (event.type==MouseEvent.MOUSE_UP) {
30                                 return new SelectedWay(firstSelected as Way);
31                         }
32                         return this;
33         }
34
35                 /** Cancel parallel way creation if ESC pressed. */
36                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
37                         if (event.keyCode==27) {                        // Escape
38                                 Way(firstSelected).remove(MainUndoStack.getGlobalStack().addAction);
39                                 return new NoSelection();
40                         }
41                         var cs:ControllerState = sharedKeyboardEvents(event);
42                         return cs ? cs : this;
43                 }
44
45                 private function sgn(a:Number):Number {
46                         if (a==0) return 0;
47                         if (a<0) return -1;
48                         return 1;
49                 }
50                 
51                 private function distanceFromWay(lon:Number,latp:Number):Number {
52                         var i:uint, ax:Number, ay:Number, bx:Number, by:Number, l:Number;
53                         var ad:Number, bd:Number;
54                         var r:Number, px:Number, py:Number;
55                         var furthdist:Number=-1; var furthsgn:int=1;
56                         for (i=0; i<originalWay.length-1; i++) {
57                                 ax=originalWay.getNode(i).lon;
58                                 ay=originalWay.getNode(i).latp;
59                                 bx=originalWay.getNode(i+1).lon;
60                                 by=originalWay.getNode(i+1).latp;
61
62                                 ad=Math.sqrt(Math.pow(lon-ax,2)+Math.pow(latp-ay,2));   // distance to ax,ay
63                                 bd=Math.sqrt(Math.pow(bx-lon,2)+Math.pow(by-latp,2));   // distance to bx,by
64                                 l =Math.sqrt(Math.pow(bx-ax ,2)+Math.pow(by-ay  ,2));   // length of segment
65                                 r =ad/(ad+bd);                                                                                  // proportion along segment
66                                 px=ax+r*(bx-ax); py=ay+r*(by-ay);                                               // nearest point on line
67                                 r=Math.sqrt(Math.pow(px-lon,2)+Math.pow(py-latp,2));    // distance from px,py to lon,latp
68
69                                 if (furthdist<0 || furthdist>r) {
70                                         furthdist=r; 
71                                         furthsgn=sgn((bx-ax)*(latp-ay)-(by-ay)*(lon-ax));
72                                 }
73                         }
74                         return furthdist*furthsgn;
75                 }
76
77                 /** Creates the WayUI for the parallel way. */
78                 override public function enterState():void {
79                         selection=[parallelise.parallelWay];
80                         layer.createWayUI(firstSelected as Way);
81                         startlon =controller.map.coord2lon(controller.map.mouseX);
82                         startlatp=controller.map.coord2latp(controller.map.mouseY);
83         }
84                 /** Unselects. */
85                 override public function exitState(newState:ControllerState):void {
86             clearSelection(newState);
87         }
88
89         override public function toString():String {
90             return "SelectedParallelWay";
91         }
92     }
93 }