this'll keep the Germans happy
[potlatch2.git] / net / systemeD / halcyon / connection / actions / SplitWayAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4         import net.systemeD.halcyon.Globals;
5     
6     public class SplitWayAction extends CompositeUndoableAction {
7     
8         private var selectedWay:Way;
9         private var selectedNode:Node;
10         private var newWay:Way;
11     
12         public function SplitWayAction(selectedWay:Way, selectedNode:Node) {
13             super("Split way "+selectedWay.id);
14             this.selectedWay = selectedWay;
15             this.selectedNode = selectedNode;
16         }
17     
18         public override function doAction():uint {
19             if (newWay==null) {
20                                 newWay = Connection.getConnection().createWay(
21                                         selectedWay.getTagsCopy(), 
22                                         selectedWay.sliceNodes(selectedWay.indexOfNode(selectedNode),selectedWay.length),
23                                         push);
24
25                                 // we reverse the list, which is already sorted by position. This way positions aren't affected
26                                 // for previous inserts when all the inserts are eventually executed
27                                 for each (var o:Object in selectedWay.memberships.reverse()) {
28                                         // don't add a turn restriction to the relation if it's no longer relevant
29                                         if (o.relation.tagIs('type','restriction')) {
30                                                 var vias:Array=o.relation.findMembersByRole('via');
31                                                 if (vias.length && vias[0] is Node) {
32                                                         if (newWay.indexOfNode(Node(vias[0]))==-1) { 
33                                                                 continue;
34                                                         }
35                                                 }
36                                         }
37
38                   // newWay should be added immediately after the selectedWay, unless the setup
39                   // is arse-backwards. By that I mean either:
40                   // a) The first node (0) of selectedWay is in the subsequentWay, or
41                   // b) The last node (N) of selectedWay is in the preceedingWay
42                   // Note that the code above means newWay is the tail of selectedWay S-->.-->N
43                   // i.e. preceedingWay x--x--x--x                             P-1   ↓
44                   //      selectedWay            N<--.<--S<--.<--0             P     ↓ relation members list
45                   //      subsequentWay                           x--x--x--x   P+1   ↓
46                   // There are some edge cases:
47                   // 1) If the immediately adjacent member isn't a way - handled fine
48                   // 2) If the selectedWay shares first/last node with non-adjacent ways - phooey
49                   
50                   var backwards:Boolean = false;
51                   // note that backwards is actually a ternary of 'true', 'false', and 'itdoesntmatter' (== 'false')
52                   
53                   var offset:int = 1; //work from o.position outwards along the length of the relationmembers
54                   while ((o.position - offset) >= 0 || (o.position + offset < o.relation.length)) {
55                     if ((o.position - offset >= 0) && o.relation.getMember(o.position - offset).entity is Way)  {
56                       var preceedingWay:Way = o.relation.getMember(o.position - offset).entity as Way;
57                       if(preceedingWay.indexOfNode(selectedWay.getLastNode()) >= 0) {
58                         backwards = true;
59                       }
60                     }
61                     if ((o.position + offset < o.relation.length) && o.relation.getMember(o.position + offset).entity is Way) {
62                       var subsequentWay:Way = o.relation.getMember(o.position + offset).entity as Way;
63                       if(subsequentWay.indexOfNode(selectedWay.getNode(0)) >= 0) {
64                         backwards = true;
65                       }
66                     }
67                     offset++;
68                   }
69                   if (backwards) {
70                     o.relation.insertMember(o.position, new RelationMember(newWay, o.role), push); //insert newWay before selectedWay
71                   } else {
72                     o.relation.insertMember(o.position + 1, new RelationMember(newWay, o.role), push); // insert after
73                   }
74                 }
75                 
76                 // now that we're done with the selectedWay, remove the nodes
77                 selectedWay.deleteNodesFrom(selectedWay.indexOfNode(selectedNode)+1, push);
78
79                                 // and remove from any turn restrictions that aren't relevant
80                                 for each (var r:Relation in selectedWay.findParentRelationsOfType('restriction')) {
81                                         vias=r.findMembersByRole('via');
82                                         if (vias.length && vias[0] is Node) {
83                                                 if (selectedWay.indexOfNode(Node(vias[0]))==-1) { 
84                                                         r.removeMember(selectedWay,push);
85                                                 }
86                                         }
87                                 }
88
89             }
90             newWay.suspend();
91             selectedWay.suspend();
92             super.doAction();
93             newWay.resume();
94             selectedWay.resume();
95             return SUCCESS;
96         }
97         
98         public override function undoAction():uint {
99             selectedWay.suspend();
100             newWay.suspend();
101             
102             super.undoAction();
103             
104             newWay.resume();
105             selectedWay.resume();
106             return SUCCESS;
107         }
108     }
109
110 }