dd60e0a9108d94c043c45f2935495a09b3f13bdc
[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                   // newWay should be added immediately after the selectedWay, unless the setup
29                   // is arse-backwards. By that I mean either:
30                   // a) The first node (0) of selectedWay is in the subsequentWay, or
31                   // b) The last node (N) of selectedWay is in the preceedingWay
32                   // Note that the code above means newWay is the tail of selectedWay S-->.-->N
33                   // i.e. preceedingWay x--x--x--x                             P-1   ↓
34                   //      selectedWay            N<--.<--S<--.<--0             P     ↓ relation members list
35                   //      subsequentWay                           x--x--x--x   P+1   ↓
36                   // There are some edge cases:
37                   // 1) If the immediately adjacent member isn't a way - handled fine
38                   // 2) If the selectedWay shares first/last node with non-adjacent ways - phooey
39                   
40                   var backwards:Boolean = false;
41                   // note that backwards is actually a ternary of 'true', 'false', and 'itdoesntmatter' (== 'false')
42                   
43                   var offset:int = 1; //work from o.position outwards along the length of the relationmembers
44                   while ((o.position - offset) >= 0 || (o.position + offset < o.relation.length)) {
45                     if ((o.position - offset >= 0) && o.relation.getMember(o.position - offset).entity is Way)  {
46                       var preceedingWay:Way = o.relation.getMember(o.position - offset).entity as Way;
47                       if(preceedingWay.indexOfNode(selectedWay.getLastNode()) >= 0) {
48                         backwards = true;
49                       }
50                     }
51                     if ((o.position + offset < o.relation.length) && o.relation.getMember(o.position + offset).entity is Way) {
52                       var subsequentWay:Way = o.relation.getMember(o.position + offset).entity as Way;
53                       if(subsequentWay.indexOfNode(selectedWay.getNode(0)) >= 0) {
54                         backwards = true;
55                       }
56                     }
57                     offset++;
58                   }
59                   if (backwards) {
60                     o.relation.insertMember(o.position, new RelationMember(newWay, o.role), push); //insert newWay before selectedWay
61                   } else {
62                     o.relation.insertMember(o.position + 1, new RelationMember(newWay, o.role), push); // insert after
63                   }
64                 }
65                 
66                 // now that we're done with the selectedWay, remove the nodes
67                 selectedWay.deleteNodesFrom(selectedWay.indexOfNode(selectedNode)+1, push);
68             }
69             newWay.suspend();
70             selectedWay.suspend();
71             super.doAction();
72             newWay.resume();
73             selectedWay.resume();
74             return SUCCESS;
75         }
76         
77         public override function undoAction():uint {
78             selectedWay.suspend();
79             newWay.suspend();
80             
81             super.undoAction();
82             
83             newWay.resume();
84             selectedWay.resume();
85             return SUCCESS;
86         }
87     }
88
89 }