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