]> git.openstreetmap.org Git - potlatch2.git/blob - net/systemeD/potlatch2/controller/SelectedWay.as
backspace ways
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWay.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
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.potlatch2.tools.Simplify;
8     import net.systemeD.halcyon.connection.*;
9         import net.systemeD.halcyon.MapPaint;
10         import net.systemeD.halcyon.Globals;
11
12     public class SelectedWay extends ControllerState {
13         protected var initWay:Way;
14         
15         public function SelectedWay(way:Way) {
16             initWay = way;
17         }
18  
19         protected function selectWay(way:Way):void {
20             if ( way == selectedWay )
21                 return;
22
23             clearSelection();
24             controller.setSelectedEntity(way);
25             controller.map.setHighlight(way, { selected: true, showNodes: true, hover: false });
26             selectedWay = way;
27             initWay = way;
28         }
29
30         protected function clearSelection():void {
31             if ( selectedWay != null ) {
32                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, hover: false });
33                 controller.setSelectedEntity(null);
34                 selectedWay = null;
35             }
36         }
37         
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 = getTopLevelFocusEntity(entity);
41
42             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
43                                 // start new way
44                                 var way:Way = controller.connection.createWay({}, [entity], MainUndoStack.getGlobalStack().addAction);
45                                 return new DrawWay(way, true, false);
46                         } else if ( event.type == MouseEvent.MOUSE_UP && entity is Way && event.ctrlKey ) {
47                                 // merge way
48                                 mergeWith(entity as Way);
49                                 return this;
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);
53                                 d.forceDragStart();
54                                 return d;
55                         }
56                         var cs:ControllerState = sharedMouseEvents(event, entity);
57                         if (cs) return cs;
58                         return this;
59         }
60         
61                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
62                         switch (event.keyCode) {
63                                 case 81:                                        Quadrilateralise.quadrilateralise(selectedWay); return this;
64                 case 82:                    selectedWay.reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;         
65                 case 89:                    Simplify.simplify(selectedWay, controller.map, true); return this;         
66                                 case Keyboard.BACKSPACE:        if (event.shiftKey) { return deleteWay(); } break;
67                                 case Keyboard.DELETE:           if (event.shiftKey) { return deleteWay(); } break;
68                         }
69                         return this;
70                 }
71
72         protected function addNode(event:MouseEvent):Node {
73             trace("add node");
74             var lat:Number = controller.map.coord2lat(event.localY);
75             var lon:Number = controller.map.coord2lon(event.localX);
76             var undo:CompositeUndoableAction = new CompositeUndoableAction("Insert node");
77             var node:Node = controller.connection.createNode({}, lat, lon, undo.push);
78             selectedWay.insertNodeAtClosestPosition(node, true, undo.push);
79             MainUndoStack.getGlobalStack().addAction(undo);
80                         return node;
81         }
82
83                 protected function mergeWith(otherWay:Way):Boolean {
84                         var way1:Way;
85                         var way2:Way;
86                         if ( selectedWay.id < otherWay.id && selectedWay.id >= 0 ) {
87                             way1 = selectedWay;
88                             way2 = otherWay;
89                         } else {
90                             way1 = otherWay;
91                             way2 = selectedWay;
92                         }
93                         
94                         var undo:Function = MainUndoStack.getGlobalStack().addAction;
95                         
96                         // find common point
97                         if (way1 == way2) { return false; }
98                         if      (way1.getNode(0)   ==way2.getNode(0)   ) { way1.mergeWith(way2,0,0,undo); }
99                         else if (way1.getNode(0)   ==way2.getLastNode()) { way1.mergeWith(way2,0,way2.length-1,undo); }
100                         else if (way1.getLastNode()==way2.getNode(0)   ) { way1.mergeWith(way2,way1.length-1,0,undo); }
101                         else if (way1.getLastNode()==way2.getLastNode()) { way1.mergeWith(way2,way1.length-1,way2.length-1,undo); }
102                         return true;
103                 }
104         
105                 public function deleteWay():ControllerState {
106                         selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
107                         return new NoSelection();
108                 }
109
110         override public function enterState():void {
111             selectWay(initWay);
112                         Globals.vars.root.addDebug("**** -> "+this+" "+selectedWay.id);
113         }
114         override public function exitState():void {
115             clearSelection();
116                         Globals.vars.root.addDebug("**** <- "+this);
117         }
118
119         override public function toString():String {
120             return "SelectedWay";
121         }
122
123     }
124 }