first stage of undo/redo functionality -- there are still a number of actions that...
[potlatch2.git] / net / systemeD / potlatch2 / tools / Straighten.as
1 package net.systemeD.potlatch2.tools {
2         import net.systemeD.halcyon.Map;
3         import net.systemeD.halcyon.connection.CompositeUndoableAction;
4         import net.systemeD.halcyon.connection.Way;
5         import net.systemeD.halcyon.connection.Node;
6         import net.systemeD.halcyon.connection.MainUndoStack;
7
8         public class Straighten {
9
10                 public static function straighten(way:Way,map:Map):void {
11                         if (way.length<3) { return; }
12
13                         var a:Node=way.getNode(0);
14                         var b:Node=way.getNode(way.length-1);
15                         if (a==b) { return; }
16                         
17                         // ** could potentially do the 'too bendy?' check here as per Potlatch 1
18                         
19                         var action:CompositeUndoableAction = new CompositeUndoableAction("Straighten");
20                         
21                         var todelete:Array=[];
22                         var n:Node;
23                 
24                         for (var i:uint=1; i<way.length-1; i++) {
25                                 n=way.getNode(i);
26                                 if (n.parentWays.length>1 || n.hasTags() ||
27                                     n.lon<map.edge_l || n.lon>map.edge_r ||
28                                     n.lat<map.edge_b || n.lat>map.edge_t) {
29
30                                         // junction node, tagged, or off-screen - so retain and move
31                                         var u:Number=((n.lon -a.lon )*(b.lon -a.lon )+
32                                                       (n.latp-a.latp)*(b.latp-a.latp))/
33                                                      (Math.pow(b.lon-a.lon,2)+Math.pow(b.latp-a.latp,2));
34                                         n.setLonLatp(a.lon +u*(b.lon -a.lon ),
35                                                      a.latp+u*(b.latp-a.latp), action.push);
36                                         
37                                 } else {
38                                         // safe to delete
39                                         if (todelete.indexOf(n)==-1) { todelete.push(n); }
40                                 }
41                         }
42                         for each (n in todelete) { n.remove(action.push); }
43                         
44                         MainUndoStack.getGlobalStack().addAction(action);
45                 }
46         }
47 }