fix way splitting and node/UI renumbering
[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 net.systemeD.potlatch2.EditController;
5     import net.systemeD.halcyon.connection.*;
6         import net.systemeD.halcyon.Globals;
7
8     public class SelectedWayNode extends SelectedWay {
9         protected var selectedNode:Node;
10         protected var initNode:Node;
11         
12         public function SelectedWayNode(way:Way,node:Node) {
13                         super (way);
14             initNode = node;
15         }
16  
17         protected function selectNode(way:Way,node:Node):void {
18             if ( way == selectedWay && node == selectedNode )
19                 return;
20
21             clearSelection();
22             controller.setSelectedEntity(node);
23             controller.map.setHighlight(way, { showNodes: true, nodeSelected: node.id });
24             selectedWay = way;   initWay  = way;
25             selectedNode = node; initNode = node;
26         }
27                 
28         override protected function clearSelection():void {
29             if ( selectedNode != null ) {
30                 controller.map.setHighlight(selectedWay, { selected: false, showNodes: false, nodeSelected: null });
31                 controller.setSelectedEntity(null);
32                 selectedNode = null;
33                                 selectedWay = null;
34             }
35         }
36         
37         override public function processMouseEvent(event:MouseEvent, entity:Entity):ControllerState {
38                         if (event.type==MouseEvent.MOUSE_MOVE || event.type==MouseEvent.ROLL_OVER || event.type==MouseEvent.MOUSE_OUT) { return this; }
39             var focus:Entity = NoSelection.getTopLevelFocusEntity(entity);
40
41             if ( event.type == MouseEvent.MOUSE_UP ) {
42                                 if ( entity is Node && event.shiftKey ) {
43                                         // start new way
44                     var way:Way = controller.connection.createWay({}, [entity]);
45                     return new DrawWay(way, true, false);
46                                 } else if ( entity is Node && focus == selectedWay ) {
47                                         // select node within way
48                                         return selectOrEdit(selectedWay, Node(entity));
49                 } else if ( entity is Way ) {
50                                         // select way
51                                         return new SelectedWay(Way(entity));
52                 } else if ( focus == null && map.dragstate!=map.DRAGGING ) {
53                     return new NoSelection();
54                                 }
55             } else if ( event.type == MouseEvent.MOUSE_DOWN ) {
56                                 if ( entity is Way && focus==selectedWay && event.shiftKey) {
57                                         // insert node within way (shift-click)
58                         var d:DragWayNode=new DragWayNode(selectedWay, addNode(event), event, true);
59                                         d.forceDragStart();
60                                         return d;
61                                 } else if ( entity is Node && entity.hasParent(selectedWay) ) {
62                     return new DragWayNode(selectedWay, Node(entity), event, false);
63                                 } else if ( focus is Node ) {
64                                         return new DragPOINode(entity as Node,event,false);
65                                 }
66             }
67
68             return this;
69         }
70
71                 override public function processKeyboardEvent(event:KeyboardEvent):ControllerState {
72                         switch (event.keyCode) {
73                                 case 88:                                        return splitWay();
74                                 case Keyboard.BACKSPACE:        return deleteNode();
75                                 case Keyboard.DELETE:           return deleteNode();
76                         }
77                         return this;
78                 }
79                 
80                 override public function enterState():void {
81             selectNode(initWay,initNode);
82                         Globals.vars.root.addDebug("**** -> "+this);
83         }
84                 override public function exitState():void {
85             clearSelection();
86                         Globals.vars.root.addDebug("**** <- "+this);
87         }
88
89         override public function toString():String {
90             return "SelectedWayNode";
91         }
92
93         public static function selectOrEdit(selectedWay:Way, entity:Node):ControllerState {
94                 var isFirst:Boolean = false;
95                         var isLast:Boolean = false;
96                         isFirst = selectedWay.getNode(0) == entity;
97                         isLast = selectedWay.getNode(selectedWay.length - 1) == entity;
98                         if ( isFirst == isLast )    // both == looped, none == central node 
99                             return new SelectedWayNode(selectedWay, entity);
100                         else
101                             return new DrawWay(selectedWay, isLast, true);
102         }
103
104                 public function splitWay():ControllerState {
105                         // abort if start or end
106                         if (selectedWay.getNode(0                   ) == selectedNode) { return this; }
107                         if (selectedWay.getNode(selectedWay.length-1) == selectedNode) { return this; }
108
109                         // create new way
110                         var newWay:Way = controller.connection.createWay(
111                                 selectedWay.getTagsCopy(), 
112                                 selectedWay.sliceNodes(selectedWay.indexOfNode(selectedNode),selectedWay.length));
113                         newWay.suspend();
114                         selectedWay.suspend();
115                         selectedWay.deleteNodesFrom(selectedWay.indexOfNode(selectedNode)+1);
116                         
117                         // copy relations
118                         for each (var r:Relation in selectedWay.parentRelations) {
119                                 // ** needs to copy roles as well
120                                 r.appendMember(new RelationMember(newWay, ''));
121                         }
122                         newWay.resume();
123                         selectedWay.resume();
124
125                         return new SelectedWay(selectedWay);
126                 }
127                 
128                 public function deleteNode():ControllerState {
129                         selectedNode.remove();
130                         return new SelectedWay(selectedWay);
131                 }
132
133     }
134 }