Show the shortcut key (J) for Merge Ways text in the toolbox.
[potlatch2.git] / net / systemeD / potlatch2 / controller / DrawQuadrilateral.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
9     public class DrawQuadrilateral extends ControllerState {
10                 private var sourceNode:Node;
11                 private var way:Way;
12                 private var centrelon:Number;
13                 private var centrelatp:Number;
14                 private var radius:Number;
15                 private var startX:Number;
16                 private var startY:Number;
17
18         public function DrawQuadrilateral(node:Node) {
19                         sourceNode = node;
20         }
21
22         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
23                         if (event.type==MouseEvent.MOUSE_MOVE) {
24                                 // redraw the rectangle sprite
25                                 var undo:CompositeUndoableAction = new CompositeUndoableAction("Draw quadrilateral");
26
27                                 var dx:Number=controller.map.mouseX-startX;
28                                 var dy:Number=controller.map.mouseY-startY;
29                                 var angle:Number=(dy % 360) * Math.PI/180;
30
31                                 way.getNode(1).setLonLatp(centrelon +radius*Math.sin(angle),
32                                                               centrelatp+radius*Math.cos(angle),
33                                                               undo.push);
34                                 way.getNode(3).setLonLatp(centrelon -radius*Math.sin(angle),
35                                                           centrelatp-radius*Math.cos(angle),
36                                                           undo.push);
37                                 undo.doAction();
38                         } else if (event.type==MouseEvent.CLICK || event.type==MouseEvent.MOUSE_UP) {
39                                 // select the new rectangle
40                                 return new SelectedWay(way);
41                         } else {
42                                 trace(event.type);
43                         }
44                         return this;
45         }
46
47                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
48                         if (event.keyCode==Keyboard.ESCAPE) {
49                                 // make sure the rectangle sprite is cleared
50                                 return new NoSelection();
51                         }
52                         var cs:ControllerState = sharedKeyboardEvents(event);
53                         return cs ? cs : this;
54                 }
55
56                 override public function enterState():void {
57                         controller.map.draggable=false;
58                         var conn:Connection=sourceNode.connection;
59                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Draw quadrilateral");
60                         var cornerlon:Number =controller.map.coord2lon(controller.map.mouseX);
61                         var cornerlat:Number =controller.map.coord2lat(controller.map.mouseY);
62                         var cornerlatp:Number=controller.map.coord2latp(controller.map.mouseY);
63                         
64                         var xradius:Number=(cornerlon-sourceNode.lon)/2;
65                         var yradius:Number=(cornerlatp-sourceNode.latp)/2;
66                         centrelon =sourceNode.lon +xradius;
67                         centrelatp=sourceNode.latp+yradius;
68                         radius=Math.sqrt(xradius*xradius+yradius*yradius);
69
70                         startX=controller.map.mouseX;
71                         startY=controller.map.mouseY;
72                         var node1:Node=conn.createNode({}, cornerlat     , sourceNode.lon, undo.push);
73                         var node2:Node=conn.createNode({}, cornerlat     , cornerlon     , undo.push);
74                         var node3:Node=conn.createNode({}, sourceNode.lat, cornerlon     , undo.push);
75                         way = conn.createWay(sourceNode.getTagsCopy(), [sourceNode,node1,node2,node3,sourceNode], undo.push);
76                         for (var k:String in sourceNode.getTagsCopy()) sourceNode.setTag(k, null, undo.push);
77
78                         MainUndoStack.getGlobalStack().addAction(undo);
79         }
80                 override public function exitState(newState:ControllerState):void {
81                         controller.map.draggable=true;
82         }
83
84         override public function toString():String {
85             return "DrawQuadrilateral";
86         }
87     }
88 }