1 package net.systemeD.potlatch2.controller {
3 import flash.display.DisplayObject;
4 import flash.ui.Keyboard;
5 import net.systemeD.potlatch2.EditController;
6 import net.systemeD.potlatch2.tools.Quadrilateralise;
7 import net.systemeD.halcyon.connection.*;
8 import net.systemeD.halcyon.MapPaint;
9 import net.systemeD.halcyon.Globals;
11 public class SelectedWay extends ControllerState {
12 public var selectedWay:Way;
13 protected var initWay:Way;
15 public function SelectedWay(way:Way) {
19 protected function selectWay(way:Way):void {
20 if ( way == selectedWay )
24 controller.setSelectedEntity(way);
25 controller.map.setHighlight(way, { selected: true, showNodes: true, hover: false });
30 protected function clearSelection():void {
31 if ( selectedWay != null ) {
32 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, hover: false });
33 controller.setSelectedEntity(null);
38 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
39 if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
40 var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
42 if ( event.type == MouseEvent.MOUSE_UP ) {
43 if ( entity is Node && event.shiftKey ) {
45 var way:Way = controller.connection.createWay({}, [entity],
46 MainUndoStack.getGlobalStack().addAction);
47 return new DrawWay(way, true, false);
48 } else if ( entity is Way && event.ctrlKey ) {
50 mergeWith(entity as Way);
51 } else if ( entity is Way ) {
53 selectWay(entity as Way);
54 } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
55 return new NoSelection();
58 } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
59 var paint:MapPaint = getMapPaint(DisplayObject(event.target));
60 if ( entity is Way && event.altKey && paint.isBackground ) {
61 // pull way out of vector background layer
62 return new SelectedWay(paint.findSource().pullThrough(entity,controller.connection));
63 } else if ( entity is Way && focus==selectedWay && event.shiftKey) {
64 // insert node within way (shift-click)
65 var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
68 } else if ( entity is Node && entity.hasParent(selectedWay) ) {
69 // select node within this way
70 return new DragWayNode(selectedWay, Node(entity), event, false);
71 } else if ( focus is Node ) {
73 return new DragPOINode(entity as Node,event,false);
74 } else if ( entity is Node && focus is Way ) {
76 return new DragWayNode(focus as Way,entity as Node,event,false);
83 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
84 switch (event.keyCode) {
85 case 81: Quadrilateralise.quadrilateralise(selectedWay); return this;
86 case Keyboard.BACKSPACE: if (event.shiftKey) { return deleteWay(); } break;
87 case Keyboard.DELETE: if (event.shiftKey) { return deleteWay(); } break;
92 protected function addNode(event:MouseEvent):Node {
94 var lat:Number = controller.map.coord2lat(event.localY);
95 var lon:Number = controller.map.coord2lon(event.localX);
96 var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
97 var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
98 selectedWay.insertNodeAtClosestPosition(node, true, undo.push);
99 MainUndoStack.getGlobalStack().addAction(undo);
103 protected function mergeWith(otherWay:Way):Boolean {
106 if ( selectedWay.id < otherWay.id && selectedWay.id >= 0 ) {
114 var undo:Function = MainUndoStack.getGlobalStack().addAction;
117 if (way1 == way2) { return false; }
118 if (way1.getNode(0) ==way2.getNode(0) ) { way1.mergeWith(way2,0,0,undo); }
119 else if (way1.getNode(0) ==way2.getLastNode()) { way1.mergeWith(way2,0,way2.length-1,undo); }
120 else if (way1.getLastNode()==way2.getNode(0) ) { way1.mergeWith(way2,way1.length-1,0,undo); }
121 else if (way1.getLastNode()==way2.getLastNode()) { way1.mergeWith(way2,way1.length-1,way2.length-1,undo); }
125 public function deleteWay():ControllerState {
126 selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
127 return new NoSelection();
130 override public function enterState():void {
132 Globals.vars.root.addDebug("**** -> "+this+" "+selectedWay.id);
134 override public function exitState():void {
136 Globals.vars.root.addDebug("**** <- "+this);
139 override public function toString():String {
140 return "SelectedWay";