Merge branch 'master' of github.com:systemed/potlatch2
[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(originalWay.distanceFromWay(lon,latp).distance);
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                 /** Creates the WayUI for the parallel way. */
52                 override public function enterState():void {
53                         selection=[parallelise.parallelWay];
54                         layer.createWayUI(firstSelected as Way);
55                         startlon =controller.map.coord2lon(controller.map.mouseX);
56                         startlatp=controller.map.coord2latp(controller.map.mouseY);
57         }
58                 /** Unselects. */
59                 override public function exitState(newState:ControllerState):void {
60             clearSelection(newState);
61         }
62
63         override public function toString():String {
64             return "SelectedParallelWay";
65         }
66     }
67 }