05bae453abbcaad36429a9147876b5dc326c0e01
[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 */           repeatTags(firstSelected); return this;
75                                 case 191: /* / */           return cycleWays();
76                                 case Keyboard.BACKSPACE:        
77                                 case Keyboard.DELETE:           if (event.shiftKey) { return deleteWay(); } break;
78                         }
79                         if (!layer.isBackground) {
80                                 switch (event.keyCode) {
81                                         case 80:  /* P */       return new SelectedParallelWay(firstSelected as Way); 
82                                         case 81:  /* Q */       Quadrilateralise.quadrilateralise(firstSelected as Way, MainUndoStack.getGlobalStack().addAction); return this;
83                                         case 86:  /* V */       Way(firstSelected).reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;
84                                         case 89:  /* Y */       Simplify.simplify(firstSelected as Way, controller.map, true); return this;
85                                         case 188: /* , */       return new SelectedWayNode(initWay, initIndex); // allows navigating from one way to another by keyboard
86                                         case 190: /* . */       return new SelectedWayNode(initWay, initIndex); //  using <, > and /           
87                                 }
88                         }
89                         var cs:ControllerState = sharedKeyboardEvents(event);
90                         return cs ? cs : this;
91                 }
92         
93                 private function cycleWays():ControllerState {
94                         if (!clicked || (wayList && wayList.length<2)) { return this; }
95
96                         if (!wayList) {
97                                 wayList=[initWay].concat(layer.findWaysAtPoint(clicked.x,clicked.y,initWay));
98                         }
99                         wayList=wayList.slice(1).concat(wayList[0]);
100                         // Find the new way's index of the currently "selected" node, to facilitate keyboard navigation
101                         var newindex:int = Way(wayList[0]).indexOfNode(initWay.getNode(initIndex));
102                         return new SelectedWay(wayList[0], layer, clicked, wayList, newindex);
103                 }
104
105                 /** Perform deletion of currently selected way. */
106                 public function deleteWay():ControllerState {
107                         layer.setHighlightOnNodes(firstSelected as Way, {selectedway: false});
108                         selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
109                         return new NoSelection();
110                 }
111
112         /** Officially enter this state by marking the previously nominated way as selected. */
113         override public function enterState():void {
114             if (firstSelected!=initWay) {
115                     clearSelection(this);
116                     layer.setHighlight(initWay, { selected: true, hover: false });
117                     layer.setHighlightOnNodes(initWay, { selectedway: true });
118                     selection = [initWay];
119                     controller.updateSelectionUI();
120                     initWay.addEventListener(Connection.WAY_REORDERED, updateSelectionUI, false, 0, true);
121                         }
122                         layer.setPurgable(selection,false);
123         }
124         /** Officially leave the state, remembering the current way's tags for future repeats. */
125         // 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) 
126         override public function exitState(newState:ControllerState):void {
127                         if (firstSelected.hasTags()) {
128               controller.clipboards['way']=firstSelected.getTagsCopy();
129             }
130                         layer.setPurgable(selection,true);
131             firstSelected.removeEventListener(Connection.WAY_REORDERED, updateSelectionUI);
132             clearSelection(newState);
133         }
134
135         /** @return "SelectedWay" */
136         override public function toString():String {
137             return "SelectedWay";
138         }
139
140     }
141 }