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: selectedWay.reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;
66 case 89: Simplify.simplify(selectedWay, controller.map, true); return this;
67 case Keyboard.BACKSPACE: if (event.shiftKey) { return deleteWay(); } break;
68 case Keyboard.DELETE: if (event.shiftKey) { return deleteWay(); } break;
70 var cs:ControllerState = sharedKeyboardEvents(event);
71 return cs ? cs : this;
74 protected function addNode(event:MouseEvent):int {
75 // find which other ways are under the mouse
76 var ways:Array=[]; var w:Way;
77 for each (var wayui:WayUI in controller.map.paint.wayuis) {
78 w=wayui.hitTest(event.stageX, event.stageY);
79 if (w && w!=selectedWay) { ways.push(w); }
82 var lat:Number = controller.map.coord2lat(event.localY);
83 var lon:Number = controller.map.coord2lon(event.localX);
84 var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
85 var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
86 var index:int = selectedWay.insertNodeAtClosestPosition(node, true, undo.push);
87 for each (w in ways) { w.insertNodeAtClosestPosition(node, true, undo.push); }
88 MainUndoStack.getGlobalStack().addAction(undo);
92 protected function mergeWith(otherWay:Way):ControllerState {
95 if ( selectedWay.id < otherWay.id && selectedWay.id >= 0 ) {
103 var undo:Function = MainUndoStack.getGlobalStack().addAction;
106 if (way1 == way2) { return this; }
107 if (way1.getNode(0) ==way2.getNode(0) ) { way1.mergeWith(way2,0,0,undo); }
108 else if (way1.getNode(0) ==way2.getLastNode()) { way1.mergeWith(way2,0,way2.length-1,undo); }
109 else if (way1.getLastNode()==way2.getNode(0) ) { way1.mergeWith(way2,way1.length-1,0,undo); }
110 else if (way1.getLastNode()==way2.getLastNode()) { way1.mergeWith(way2,way1.length-1,way2.length-1,undo); }
111 return new SelectedWay(way1);
114 public function deleteWay():ControllerState {
115 selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
116 return new NoSelection();
119 override public function enterState():void {
121 Globals.vars.root.addDebug("**** -> "+this+" "+selectedWay.id);
123 override public function exitState():void {
125 Globals.vars.root.addDebug("**** <- "+this);
128 override public function toString():String {
129 return "SelectedWay";