first stage of undo/redo functionality -- there are still a number of actions that...
[potlatch2.git] / net / systemeD / halcyon / connection / Way.as
1 package net.systemeD.halcyon.connection {
2     import flash.geom.Point;
3         import net.systemeD.halcyon.Globals;
4         import net.systemeD.halcyon.connection.actions.*;
5
6     public class Way extends Entity {
7         private var nodes:Array;
8                 public static var entity_type:String = 'way';
9
10         public function Way(id:Number, version:uint, tags:Object, loaded:Boolean, nodes:Array) {
11             super(id, version, tags, loaded);
12             this.nodes = nodes;
13                         for each (var node:Node in nodes) { node.addParent(this); }
14         }
15
16                 public function update(version:uint, tags:Object, loaded:Boolean, nodes:Array):void {
17                         var node:Node;
18                         for each (node in this.nodes) { node.removeParent(this); }
19                         updateEntityProperties(version,tags,loaded); this.nodes=nodes;
20                         for each (node in nodes) { node.addParent(this); }
21                 }
22                 
23         public function get length():uint {
24             return nodes.length;
25         }
26         
27         public function getNode(index:uint):Node {
28             return nodes[index];
29         }
30
31                 public function getLastNode():Node {
32                         return nodes[nodes.length-1];
33                 }
34
35         public function insertNode(index:uint, node:Node):void {
36                         node.addParent(this);
37             nodes.splice(index, 0, node);
38             markDirty();
39             dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, this, index));
40         }
41
42         public function appendNode(node:Node):uint {
43                         node.addParent(this);
44             nodes.push(node);
45             markDirty();
46             dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, this, nodes.length - 1));
47             return nodes.length;
48         }
49         
50         public function prependNode(node:Node):uint {
51                         node.addParent(this);
52             nodes.unshift(node);
53             markDirty();
54             dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, this, 0));
55             return nodes.length;
56         }
57         
58         public function indexOfNode(node:Node):uint {
59             return nodes.indexOf(node);
60         }
61
62                 public function removeNode(node:Node, performAction:Function):void {
63                         performAction(new RemoveNodeFromWayAction(this, node, nodes));
64                 }
65
66         public function removeNodeByIndex(index:uint):void {
67             var removed:Array=nodes.splice(index, 1);
68                         if (nodes.indexOf(removed[0])==-1) { removed[0].removeParent(this); }
69                         markDirty();
70             dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, removed[0], this, index));
71         }
72
73                 public function sliceNodes(start:int,end:int):Array {
74                         return nodes.slice(start,end);
75                 }
76
77                 public function deleteNodesFrom(start:int):void {
78                         for (var i:int=start; i<nodes.length; i++) {
79                                 nodes[i].removeParent(this);
80                         }
81                         nodes.splice(start);
82                         markDirty();
83                 }
84
85                 public function mergeWith(way:Way,topos:int,frompos:int, performAction:Function):void {
86                         performAction(new MergeWaysAction(this, way, topos, frompos));
87                 }
88                 
89                 public function addToEnd(topos:int,node:Node):void {
90                         if (topos==0) {
91                                 if (nodes[0]==node) { return; }
92                                 prependNode(node);
93                         } else {
94                                 if (nodes[nodes.length-1]==node) { return; }
95                                 appendNode(node);
96                         }
97                 }
98
99
100
101         /**
102          * Finds the 1st way segment which intersects the projected
103          * coordinate and adds the node to that segment. If snap is
104          * specified then the node is moved to exactly bisect the
105          * segment.
106          */
107         public function insertNodeAtClosestPosition(newNode:Node, isSnap:Boolean):int {
108             var closestProportion:Number = 1;
109             var newIndex:uint = 0;
110             var nP:Point = new Point(newNode.lon, newNode.latp);
111             var snapped:Point = null;
112             
113             for ( var i:uint; i < length - 1; i++ ) {
114                 var node1:Node = getNode(i);
115                 var node2:Node = getNode(i+1);
116                 var p1:Point = new Point(node1.lon, node1.latp);
117                 var p2:Point = new Point(node2.lon, node2.latp);
118                 
119                 var directDist:Number = Point.distance(p1, p2);
120                 var viaNewDist:Number = Point.distance(p1, nP) + Point.distance(nP, p2);
121                         
122                 var proportion:Number = Math.abs(viaNewDist/directDist - 1);
123                 if ( proportion < closestProportion ) {
124                     newIndex = i+1;
125                     closestProportion = proportion;
126                     snapped = calculateSnappedPoint(p1, p2, nP);
127                 }
128             }
129             
130             // splice in new node
131             if ( isSnap ) {
132                 newNode.latp = snapped.y;
133                 newNode.lon = snapped.x;
134             }
135             insertNode(newIndex, newNode);
136             return newIndex;
137         }
138         
139         private function calculateSnappedPoint(p1:Point, p2:Point, nP:Point):Point {
140             var w:Number = p2.x - p1.x;
141             var h:Number = p2.y - p1.y;
142             var u:Number = ((nP.x-p1.x) * w + (nP.y-p1.y) * h) / (w*w + h*h);
143             return new Point(p1.x + u*w, p1.y+u*h);
144         }
145         
146         public override function toString():String {
147             return "Way("+id+"@"+version+"): "+getTagList()+
148                      " "+nodes.map(function(item:Node,index:int, arr:Array):String {return item.id.toString();}).join(",");
149         }
150
151                 public function isArea():Boolean {
152                         return (nodes[0].id==nodes[nodes.length-1].id && nodes.length>2);
153                 }
154                 
155                 public override function remove(performAction:Function):void {
156                         performAction(new DeleteWayAction(this, setDeletedState, nodes));
157                 }
158
159                 public function get clockwise():Boolean {
160                         var lowest:uint=0;
161                         var xmin:Number=-999999; var ymin:Number=-999999;
162                         for (var i:uint=0; i<nodes.length; i++) {
163                                 if      (nodes[i].latp> ymin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
164                                 else if (nodes[i].latp==ymin
165                                           && nodes[i].lon > xmin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
166                         }
167                         return (this.onLeft(lowest)>0);
168                 }
169                 
170                 private function onLeft(j:uint):Number {
171                         var left:Number=0;
172                         var i:int, k:int;
173                         if (nodes.length>=3) {
174                                 i=j-1; if (i==-1) { i=nodes.length-2; }
175                                 k=j+1; if (k==nodes.length) { k=1; }
176                                 left=((nodes[j].lon-nodes[i].lon) * (nodes[k].latp-nodes[i].latp) -
177                                           (nodes[k].lon-nodes[i].lon) * (nodes[j].latp-nodes[i].latp));
178                         }
179                         return left;
180                 }
181
182                 internal override function isEmpty():Boolean {
183                         return (deleted || (nodes.length==0));
184                 }
185
186                 public override function getType():String {
187                         return 'way';
188                 }
189     }
190
191 }