Shift-R: paste relations from previously selected object, as per #3596.
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWay.as
1 package net.systemeD.potlatch2.controller {
2         import flash.display.*;
3         import flash.events.*;
4         import flash.geom.Point;
5         import flash.ui.Keyboard;
6         
7         import net.systemeD.halcyon.MapPaint;
8         import net.systemeD.halcyon.connection.*;
9         import net.systemeD.potlatch2.tools.Quadrilateralise;
10         import net.systemeD.potlatch2.tools.Simplify;
11
12     /** Behaviour that takes place while a way is selected includes: adding a node to the way, straightening/reshaping the way, dragging it. */
13     public class SelectedWay extends ControllerState {
14         /** The selected way itself. */
15         protected var initWay:Way;
16         private var clicked:Point;              // did the user enter this state by clicking at a particular point?
17                 private var wayList:Array;              // list of ways to cycle through with '/' keypress
18                 private var initIndex: int;     // index of last selected node if entered from SelectedWayNode
19         
20         /** 
21         * @param way The way that is now selected.
22         * @param point The location that was clicked.
23         * @param ways An ordered list of ways sharing a node, to make "way cycling" work. */
24         public function SelectedWay(way:Way, layer:MapPaint=null, point:Point=null, ways:Array=null, index:int=0) {
25                         if (layer) this.layer=layer;
26             initWay = way;
27                         clicked = point;
28                         wayList = ways;
29                         initIndex=index;
30         }
31
32         private function updateSelectionUI(e:Event):void {
33             controller.updateSelectionUIWithoutTagChange();
34         }
35
36         /** Tidy up UI as we transition to a new state without the current selection. */
37         protected function clearSelection(newState:ControllerState):void {
38             if ( selectCount ) {
39                 layer.setHighlight(firstSelected, { selected: false, hover: false });
40                 layer.setHighlightOnNodes(firstSelected as Way, { selectedway: false });
41                 selection = [];
42                 if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
43             }
44         }
45         
46         /** Behaviour includes: start drawing a new way, insert a node within this way, select an additional way */
47         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
48                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
49                         var paint:MapPaint = getMapPaint(DisplayObject(event.target));
50             var focus:Entity = getTopLevelFocusEntity(entity);
51
52             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey && !layer.isBackground ) {
53                                 // start new way
54                                 var way:Way = entity.connection.createWay({}, [entity], MainUndoStack.getGlobalStack().addAction);
55                                 return new DrawWay(way, true, false);
56                         } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==firstSelected && event.shiftKey && !layer.isBackground ) {
57                                 // insert node within way (shift-click)
58                 var d:DragWayNode=new DragWayNode(firstSelected as Way, -1, event, true);
59                                 d.forceDragStart();
60                                 return d;
61                         } else if ( event.type == MouseEvent.MOUSE_DOWN && event.ctrlKey && !event.altKey && entity && entity!=firstSelected) {
62                                 // multiple selection
63                                 return new SelectedMultiple([firstSelected,entity],layer);
64                         } else if ( event.type == MouseEvent.MOUSE_UP && focus==firstSelected ) {
65                                 return this;
66                         }
67                         var cs:ControllerState = sharedMouseEvents(event, entity);
68                         return cs ? cs : this;
69         }
70         
71                 /** Behaviour includes: parallel way, repeat tags, reverse direction, simplify, cycle way selection, delete */
72                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
73                         switch (event.keyCode) {
74                                 case 82:  /* R */           { if (! event.shiftKey) repeatTags(firstSelected); 
75                                                               else                  repeatRelations(firstSelected);
76                                                               return this; }
77                                 case 191: /* / */           return cycleWays();
78                                 case Keyboard.BACKSPACE:        
79                                 case Keyboard.DELETE:           if (event.shiftKey) { return deleteWay(); } break;
80                         }
81                         if (!layer.isBackground) {
82                                 switch (event.keyCode) {
83                                         case 80:  /* P */       return new SelectedParallelWay(firstSelected as Way); 
84                                         case 81:  /* Q */       Quadrilateralise.quadrilateralise(firstSelected as Way, MainUndoStack.getGlobalStack().addAction); return this;
85                                         case 86:  /* V */       Way(firstSelected).reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;
86                                         case 89:  /* Y */       Simplify.simplify(firstSelected as Way, controller.map, true); return this;
87                                         case 188: /* , */       return new SelectedWayNode(initWay, initIndex); // allows navigating from one way to another by keyboard
88                                         case 190: /* . */       return new SelectedWayNode(initWay, initIndex); //  using <, > and /           
89                                 }
90                         }
91                         var cs:ControllerState = sharedKeyboardEvents(event);
92                         return cs ? cs : this;
93                 }
94         
95                 private function cycleWays():ControllerState {
96                         if (!clicked || (wayList && wayList.length<2)) { return this; }
97
98                         if (!wayList) {
99                                 wayList=[initWay].concat(layer.findWaysAtPoint(clicked.x,clicked.y,initWay));
100                         }
101                         wayList=wayList.slice(1).concat(wayList[0]);
102                         // Find the new way's index of the currently "selected" node, to facilitate keyboard navigation
103                         var newindex:int = Way(wayList[0]).indexOfNode(initWay.getNode(initIndex));
104                         return new SelectedWay(wayList[0], layer, clicked, wayList, newindex);
105                 }
106
107                 /** Perform deletion of currently selected way. */
108                 public function deleteWay():ControllerState {
109                         layer.setHighlightOnNodes(firstSelected as Way, {selectedway: false});
110                         selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
111                         return new NoSelection();
112                 }
113
114         /** Officially enter this state by marking the previously nominated way as selected. */
115         override public function enterState():void {
116             if (firstSelected!=initWay) {
117                     clearSelection(this);
118                     layer.setHighlight(initWay, { selected: true, hover: false });
119                     layer.setHighlightOnNodes(initWay, { selectedway: true });
120                     selection = [initWay];
121                     controller.updateSelectionUI();
122                     initWay.addEventListener(Connection.WAY_REORDERED, updateSelectionUI, false, 0, true);
123                         }
124                         layer.setPurgable(selection,false);
125         }
126         /** Officially leave the state, remembering the current way's tags and relations for future repeats. */
127         // TODO: tweak this so that repeat tags aren't remembered if you only select a way in order to branch off it. (a la PL1) 
128         override public function exitState(newState:ControllerState):void {
129                         if (firstSelected.hasTags()) {
130               controller.clipboards['way']=firstSelected.getTagsCopy();
131             }
132             copyRelations(firstSelected);
133                         layer.setPurgable(selection,true);
134             firstSelected.removeEventListener(Connection.WAY_REORDERED, updateSelectionUI);
135             clearSelection(newState);
136         }
137
138         /** @return "SelectedWay" */
139         override public function toString():String {
140             return "SelectedWay";
141         }
142
143     }
144 }