ADD keyboard shortcuts , and . (< and >) to step node by node along a way. Maintain...
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWay.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.geom.Point;
4         import flash.ui.Keyboard;
5         
6         import net.systemeD.halcyon.Globals;
7         import net.systemeD.halcyon.WayUI;
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, point:Point=null, ways:Array=null, index:int=0) {
25             initWay = way;
26                         clicked = point;
27                         wayList = ways;
28                         initIndex=index;
29         }
30
31         private function updateSelectionUI(e:Event):void {
32             controller.updateSelectionUIWithoutTagChange();
33         }
34
35         /** Tidy up UI as we transition to a new state without the current selection. */
36         protected function clearSelection(newState:ControllerState):void {
37             if ( selectCount ) {
38                 controller.map.setHighlight(firstSelected, { selected: false, hover: false });
39                 controller.map.setHighlightOnNodes(firstSelected as Way, { selectedway: false });
40                 selection = [];
41                 if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
42             }
43         }
44         
45         /** Behaviour includes: start drawing a new way, insert a node within this way, select an additional way */
46         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
47                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
48             var focus:Entity = getTopLevelFocusEntity(entity);
49
50             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
51                                 // start new way
52                                 var way:Way = controller.connection.createWay({}, [entity], MainUndoStack.getGlobalStack().addAction);
53                                 return new DrawWay(way, true, false);
54                         } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==firstSelected && event.shiftKey) {
55                                 // insert node within way (shift-click)
56                 var d:DragWayNode=new DragWayNode(firstSelected as Way, -1, event, true);
57                                 d.forceDragStart();
58                                 return d;
59                         } else if ( event.type == MouseEvent.MOUSE_DOWN && event.ctrlKey && entity && entity!=firstSelected) {
60                                 // multiple selection
61                                 return new SelectedMultiple([firstSelected,entity]);
62                         }
63                         var cs:ControllerState = sharedMouseEvents(event, entity);
64                         return cs ? cs : this;
65         }
66         
67                 /** Behaviour includes: parallel way, repeat tags, reverse direction, simplify, cycle way selection, delete */
68                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
69                         switch (event.keyCode) {
70                                 case 80:  /* P */           return new SelectedParallelWay(firstSelected as Way); 
71                                 case 81:  /* Q */           Quadrilateralise.quadrilateralise(firstSelected as Way, MainUndoStack.getGlobalStack().addAction); return this;
72                                 case 82:  /* R */           repeatTags(firstSelected); return this;
73                 case 86:  /* V */           Way(firstSelected).reverseNodes(MainUndoStack.getGlobalStack().addAction); return this;
74                 case 89:  /* Y */           Simplify.simplify(firstSelected as Way, controller.map, true); return this;         
75                                 case 191: /* / */           return cycleWays();
76                                 case Keyboard.BACKSPACE:        
77                                 case Keyboard.DELETE:           if (event.shiftKey) { return deleteWay(); } break;
78                 case 188: /* , */           return new SelectedWayNode(initWay, initIndex); // allows navigating from one way to another by keyboard
79                 case 190: /* . */           return new SelectedWayNode(initWay, initIndex); //  using <, > and /           
80
81                         }
82                         var cs:ControllerState = sharedKeyboardEvents(event);
83                         return cs ? cs : this;
84                 }
85         
86                 private function cycleWays():ControllerState {
87                         if (!clicked || (wayList && wayList.length<2)) { return this; }
88
89                         if (!wayList) {
90                                 wayList=[initWay];
91                                 for each (var wayui:WayUI in controller.map.paint.wayuis) {
92                                         var w:Way=wayui.hitTest(clicked.x, clicked.y);
93                                         if (w && w!=initWay) { wayList.push(w); }
94                                 }
95                         }
96                         wayList=wayList.slice(1).concat(wayList[0]);
97                         // Find the new way's index of the currently "selected" node, to facilitate keyboard navigation
98                         var newindex:int = Way(wayList[0]).indexOfNode(initWay.getNode(initIndex));
99                         return new SelectedWay(wayList[0], clicked, wayList, newindex);
100                 }
101
102                 /** Perform deletion of currently selected way. */
103                 public function deleteWay():ControllerState {
104                         controller.map.setHighlightOnNodes(firstSelected as Way, {selectedway: false});
105                         selectedWay.remove(MainUndoStack.getGlobalStack().addAction);
106                         return new NoSelection();
107                 }
108
109         /** Officially enter this state by marking the previously nominated way as selected. */
110         override public function enterState():void {
111             if (firstSelected!=initWay) {
112                     clearSelection(this);
113                     controller.map.setHighlight(initWay, { selected: true, hover: false });
114                     controller.map.setHighlightOnNodes(initWay, { selectedway: true });
115                     selection = [initWay];
116                     controller.updateSelectionUI();
117                     initWay.addEventListener(Connection.WAY_REORDERED, updateSelectionUI, false, 0, true);
118                         }
119                         controller.map.setPurgable(selection,false);
120                         Globals.vars.root.addDebug("**** -> "+this+" "+firstSelected.id);
121         }
122         /** Officially leave the state, remembering the current way's tags for future repeats. */
123         // 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) 
124         override public function exitState(newState:ControllerState):void {
125                         if (firstSelected.hasTags()) {
126               controller.clipboards['way']=firstSelected.getTagsCopy();
127             }
128                         controller.map.setPurgable(selection,true);
129             firstSelected.removeEventListener(Connection.WAY_REORDERED, updateSelectionUI);
130             clearSelection(newState);
131                         Globals.vars.root.addDebug("**** <- "+this);
132         }
133
134         /** @return "SelectedWay" */
135         override public function toString():String {
136             return "SelectedWay";
137         }
138
139     }
140 }