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.Parallelise;
7 import net.systemeD.potlatch2.tools.Quadrilateralise;
8 import net.systemeD.potlatch2.tools.Simplify;
9 import net.systemeD.halcyon.connection.*;
10 import net.systemeD.halcyon.MapPaint;
11 import net.systemeD.halcyon.Globals;
13 public class SelectedWay extends ControllerState {
14 protected var initWay:Way;
16 public function SelectedWay(way:Way) {
20 protected function selectWay(way:Way):void {
21 if ( way == selectedWay )
25 controller.setSelectedEntity(way);
26 controller.map.setHighlight(way, { selected: true, showNodes: true, hover: false });
31 protected function clearSelection():void {
32 if ( selectedWay != null ) {
33 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, hover: false });
34 controller.setSelectedEntity(null);
39 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
40 if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
41 var focus:Entity = getTopLevelFocusEntity(entity);
43 if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
45 var way:Way = controller.connection.createWay({}, [entity], MainUndoStack.getGlobalStack().addAction);
46 return new DrawWay(way, true, false);
47 } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && event.ctrlKey ) {
49 return mergeWith(entity as Way);
50 } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==selectedWay && event.shiftKey) {
51 // insert node within way (shift-click)
52 var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
56 var cs:ControllerState = sharedMouseEvents(event, entity);
61 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
62 switch (event.keyCode) {
63 case 80: return new SelectedParallelWay(selectedWay);
64 // var p:Parallelise=new Parallelise(selectedWay); p.draw(0.001); return this;
65 case 81: Quadrilateralise.quadrilateralise(selectedWay); return this;
66 case 82: selectedWay.reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;
67 case 89: Simplify.simplify(selectedWay, controller.map, true); return this;
68 case Keyboard.BACKSPACE: if (event.shiftKey) { return deleteWay(); } break;
69 case Keyboard.DELETE: if (event.shiftKey) { return deleteWay(); } break;
74 protected function addNode(event:MouseEvent):int {
76 var lat:Number = controller.map.coord2lat(event.localY);
77 var lon:Number = controller.map.coord2lon(event.localX);
78 var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
79 var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
80 var index:int = selectedWay.insertNodeAtClosestPosition(node, true, undo.push);
81 MainUndoStack.getGlobalStack().addAction(undo);
85 protected function mergeWith(otherWay:Way):ControllerState {
88 if ( selectedWay.id < otherWay.id && selectedWay.id >= 0 ) {
96 var undo:Function = MainUndoStack.getGlobalStack().addAction;
99 if (way1 == way2) { return this; }
100 if (way1.getNode(0) ==way2.getNode(0) ) { way1.mergeWith(way2,0,0,undo); }
101 else if (way1.getNode(0) ==way2.getLastNode()) { way1.mergeWith(way2,0,way2.length-1,undo); }
102 else if (way1.getLastNode()==way2.getNode(0) ) { way1.mergeWith(way2,way1.length-1,0,undo); }
103 else if (way1.getLastNode()==way2.getLastNode()) { way1.mergeWith(way2,way1.length-1,way2.length-1,undo); }
104 return new SelectedWay(way1);
107 public function deleteWay():ControllerState {
108 selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
109 return new NoSelection();
112 override public function enterState():void {
114 Globals.vars.root.addDebug("**** -> "+this+" "+selectedWay.id);
116 override public function exitState():void {
118 Globals.vars.root.addDebug("**** <- "+this);
121 override public function toString():String {
122 return "SelectedWay";