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.WayUI;
12 import net.systemeD.halcyon.Globals;
14 public class SelectedWay extends ControllerState {
15 protected var initWay:Way;
17 public function SelectedWay(way:Way) {
21 protected function selectWay(way:Way):void {
22 if ( way == selectedWay )
26 controller.setSelectedEntity(way);
27 controller.map.setHighlight(way, { selected: true, showNodes: true, hover: false });
32 protected function clearSelection():void {
33 if ( selectedWay != null ) {
34 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, hover: false });
35 controller.setSelectedEntity(null);
40 override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
41 if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
42 var focus:Entity = getTopLevelFocusEntity(entity);
44 if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
46 var way:Way = controller.connection.createWay({}, [entity], MainUndoStack.getGlobalStack().addAction);
47 return new DrawWay(way, true, false);
48 } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && event.ctrlKey ) {
50 return mergeWith(entity as Way);
51 } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==selectedWay && event.shiftKey) {
52 // insert node within way (shift-click)
53 var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
57 var cs:ControllerState = sharedMouseEvents(event, entity);
58 return cs ? cs : this;
61 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
62 switch (event.keyCode) {
63 case 80: return new SelectedParallelWay(selectedWay);
64 case 81: Quadrilateralise.quadrilateralise(selectedWay); return this;
65 case 82: repeatTags(selectedWay); return this;
66 case 86: 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;
71 var cs:ControllerState = sharedKeyboardEvents(event);
72 return cs ? cs : this;
75 protected function addNode(event:MouseEvent):int {
76 // find which other ways are under the mouse
77 var ways:Array=[]; var w:Way;
78 for each (var wayui:WayUI in controller.map.paint.wayuis) {
79 w=wayui.hitTest(event.stageX, event.stageY);
80 if (w && w!=selectedWay) { ways.push(w); }
83 var lat:Number = controller.map.coord2lat(event.localY);
84 var lon:Number = controller.map.coord2lon(event.localX);
85 var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
86 var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
87 var index:int = selectedWay.insertNodeAtClosestPosition(node, true, undo.push);
88 for each (w in ways) { w.insertNodeAtClosestPosition(node, true, undo.push); }
89 MainUndoStack.getGlobalStack().addAction(undo);
93 protected function mergeWith(otherWay:Way):ControllerState {
96 if ( selectedWay.id < otherWay.id && selectedWay.id >= 0 ) {
104 var undo:Function = MainUndoStack.getGlobalStack().addAction;
107 if (way1 == way2) { return this; }
108 if (way1.getNode(0) ==way2.getNode(0) ) { way1.mergeWith(way2,0,0,undo); }
109 else if (way1.getNode(0) ==way2.getLastNode()) { way1.mergeWith(way2,0,way2.length-1,undo); }
110 else if (way1.getLastNode()==way2.getNode(0) ) { way1.mergeWith(way2,way1.length-1,0,undo); }
111 else if (way1.getLastNode()==way2.getLastNode()) { way1.mergeWith(way2,way1.length-1,way2.length-1,undo); }
112 return new SelectedWay(way1);
115 public function deleteWay():ControllerState {
116 selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
117 return new NoSelection();
120 override public function enterState():void {
122 Globals.vars.root.addDebug("**** -> "+this+" "+selectedWay.id);
124 override public function exitState():void {
125 controller.clipboards['way']=selectedWay.getTagsCopy();
127 Globals.vars.root.addDebug("**** <- "+this);
130 override public function toString():String {
131 return "SelectedWay";