Don't copy blank tags to clipboard for nodes either. Don't copy them at all for markers.
[potlatch2.git] / net / systemeD / potlatch2 / controller / SelectedWayNode.as
1 package net.systemeD.potlatch2.controller {
2         import flash.events.*;
3         import flash.ui.Keyboard;
4         import flash.geom.Point;
5     import net.systemeD.potlatch2.EditController;
6     import net.systemeD.halcyon.connection.*;
7     import net.systemeD.halcyon.connection.actions.*;
8         import net.systemeD.halcyon.Globals;
9
10     public class SelectedWayNode extends ControllerState {
11                 private var parentWay:Way;
12                 private var initIndex:int;
13                 private var selectedIndex:int;
14         
15         public function SelectedWayNode(way:Way,index:int) {
16             parentWay = way;
17                         initIndex = index;
18         }
19  
20         protected function selectNode(way:Way,index:int):void {
21                         var node:Node=way.getNode(index);
22             if ( way == parentWay && node == firstSelected )
23                 return;
24
25             clearSelection(this);
26             controller.map.setHighlight(way, { hover: false });
27             controller.map.setHighlight(node, { selected: true });
28             controller.map.setHighlightOnNodes(way, { selectedway: true });
29             selection = [node]; parentWay = way;
30             controller.updateSelectionUI();
31                         selectedIndex = index; initIndex = index;
32         }
33                 
34         protected function clearSelection(newState:ControllerState):void {
35             if ( selectCount ) {
36                 controller.map.setHighlight(parentWay, { selected: false });
37                                 controller.map.setHighlight(firstSelected, { selected: false });
38                                 controller.map.setHighlightOnNodes(parentWay, { selectedway: false });
39                                 selection = []; parentWay = null;
40                 if (!newState.isSelectionState()) { controller.updateSelectionUI(); }
41             }
42         }
43         
44         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
45                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
46             var focus:Entity = getTopLevelFocusEntity(entity);
47
48             if ( event.type == MouseEvent.MOUSE_UP && entity is Node && event.shiftKey ) {
49                                 // start new way
50                 var way:Way = controller.connection.createWay({}, [entity],
51                     MainUndoStack.getGlobalStack().addAction);
52                 return new DrawWay(way, true, false);
53                         } else if ( event.type == MouseEvent.MOUSE_UP && entity is Node && focus == parentWay ) {
54                                 // select node within way
55                                 return selectOrEdit(parentWay, getNodeIndex(parentWay,Node(entity)));
56             } else if ( event.type == MouseEvent.MOUSE_DOWN && entity is Way && focus==parentWay && event.shiftKey) {
57                                 // insert node within way (shift-click)
58                         var d:DragWayNode=new DragWayNode(parentWay, -1, event, true);
59                                 d.forceDragStart();
60                                 return d;
61                         }
62                         var cs:ControllerState = sharedMouseEvents(event, entity);
63                         return cs ? cs : this;
64         }
65
66                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
67                         switch (event.keyCode) {
68                                 case 189:                                       return removeNode();                                    // '-'
69                                 case 88:                                        return splitWay();                                              // 'X'
70                                 case 82:                                        repeatTags(firstSelected); return this; // 'R'
71                                 case 87:                                        return new SelectedWay(parentWay);              // 'W'
72                                 case 191:                                       return cycleWays();                                             // '/'
73                 case 74:                    if (event.shiftKey) { return unjoin() }; return this;// 'J'
74                                 case Keyboard.BACKSPACE:        return deleteNode();
75                                 case Keyboard.DELETE:           return deleteNode();
76                         }
77                         var cs:ControllerState = sharedKeyboardEvents(event);
78                         return cs ? cs : this;
79                 }
80
81                 override public function get selectedWay():Way {
82                         return parentWay;
83                 }
84
85                 private function cycleWays():ControllerState {
86                         var wayList:Array=firstSelected.parentWays;
87                         if (wayList.length==1) { return this; }
88                         wayList.splice(wayList.indexOf(parentWay),1);
89                         return new SelectedWay(wayList[0],
90                                                new Point(controller.map.lon2coord(Node(firstSelected).lon),
91                                                          controller.map.latp2coord(Node(firstSelected).latp)),
92                                                wayList.concat(parentWay));
93                 }
94
95                 override public function enterState():void {
96             selectNode(parentWay,initIndex);
97                         controller.map.setPurgable(selection,false);
98                         Globals.vars.root.addDebug("**** -> "+this);
99         }
100                 override public function exitState(newState:ControllerState):void {
101             if (firstSelected.hasTags()) {
102               controller.clipboards['node']=firstSelected.getTagsCopy();
103             }
104                         controller.map.setPurgable(selection,true);
105             clearSelection(newState);
106                         Globals.vars.root.addDebug("**** <- "+this);
107         }
108
109         override public function toString():String {
110             return "SelectedWayNode";
111         }
112
113                 public static function selectOrEdit(selectedWay:Way, index:int):ControllerState {
114                         var isFirst:Boolean = false;
115                         var isLast:Boolean = false;
116                         var node:Node = selectedWay.getNode(index);
117                         isFirst = selectedWay.getNode(0) == node;
118                         isLast = selectedWay.getLastNode() == node;
119                         if ( isFirst == isLast )    // both == looped, none == central node 
120                             return new SelectedWayNode(selectedWay, index);
121                         else
122                             return new DrawWay(selectedWay, isLast, true);
123         }
124
125                 public function splitWay():ControllerState {
126                         // abort if start or end
127                         if (parentWay.getNode(0)    == firstSelected) { return this; }
128                         if (parentWay.getLastNode() == firstSelected) { return this; }
129
130                         controller.map.setHighlightOnNodes(parentWay, { selectedway: false } );
131                         controller.map.setPurgable([parentWay],true);
132             MainUndoStack.getGlobalStack().addAction(new SplitWayAction(parentWay, firstSelected as Node));
133
134                         return new SelectedWay(parentWay);
135                 }
136                 
137                 public function removeNode():ControllerState {
138                         if (firstSelected.numParentWays==1 && parentWay.hasOnceOnly(firstSelected as Node)) {
139                                 return deleteNode();
140                         }
141                         parentWay.removeNodeByIndex(selectedIndex, MainUndoStack.getGlobalStack().addAction);
142                         return new SelectedWay(parentWay);
143                 }
144                 
145                 public function deleteNode():ControllerState {
146                         controller.map.setPurgable(selection,true);
147                         firstSelected.remove(MainUndoStack.getGlobalStack().addAction);
148                         return new SelectedWay(parentWay);
149                 }
150
151         public function unjoin():ControllerState {
152             Node(firstSelected).unjoin(parentWay, MainUndoStack.getGlobalStack().addAction);
153             return this;
154         }
155
156     }
157 }