slash and burn trace statements, and fix areas on vector background layers
[potlatch2.git] / net / systemeD / halcyon / VectorLayer.as
1 package net.systemeD.halcyon {
2
3         import net.systemeD.halcyon.Map;
4         import net.systemeD.halcyon.MapPaint;
5         import net.systemeD.halcyon.connection.*;
6         import net.systemeD.halcyon.Globals;
7         import net.systemeD.halcyon.styleparser.RuleSet;
8
9         public class VectorLayer extends Object {
10
11                 public var map:Map;
12                 public var paint:MapPaint;                                              // sprites
13                 public var name:String;
14                 public var url:String;
15                 public var style:String='';
16
17                 public var ways:Object=new Object();                    // geodata
18                 public var nodes:Object=new Object();                   //  |
19                 public var relations:Object=new Object();               //  |
20         private var negativeID:Number = -1;
21
22                 public function VectorLayer(n:String,m:Map,s:String) {
23                         name=n;
24                         map=m;
25                         style=s;
26                         paint=new MapPaint(m,0,0);
27                         redrawFromCSS(style);
28                 }
29
30                 public function redrawFromCSS(style:String):void {
31                         paint.ruleset=new RuleSet(map.MINSCALE,map.MAXSCALE,paint.redraw);
32                         paint.ruleset.loadFromCSS(style);
33                 }
34                 
35                 public function createNode(tags:Object,lat:Number,lon:Number):Node {
36                         var node:Node = new Node(negativeID, 0, tags, true, lat, lon);
37                         nodes[negativeID]=node; negativeID--;
38                         return node;
39                 }
40                 public function createWay(tags:Object,nodes:Array):Way {
41                         var way:Way = new Way(negativeID, 0, tags, true, nodes.concat());
42                         ways[negativeID]=way; negativeID--;
43                         return way;
44                 }
45                 public function createRelation(tags:Object,members:Array):Relation {
46             var relation:Relation = new Relation(negativeID, 0, tags, true, members.concat());
47                         relations[negativeID]=relation; negativeID--;
48             return relation;
49                 }
50
51                 public function getObjectsByBbox(left:Number, right:Number, top:Number, bottom:Number):Object {
52                         // ** FIXME: this is just copied-and-pasted from Connection.as, which really isn't very
53                         // good practice. Is there a more elegant way of doing it?
54                         var o:Object = { poisInside: [], poisOutside: [], waysInside: [], waysOutside: [] };
55                         for each (var way:Way in ways) {
56                                 if (way.within(left,right,top,bottom)) { o.waysInside.push(way); }
57                                                                   else { o.waysOutside.push(way); }
58                         }
59                         for each (var node:Node in nodes) {
60                                 if (!node.hasParentWays) {
61                                         if (node.within(left,right,top,bottom)) { o.poisInside.push(node); }
62                                                                            else { o.poisOutside.push(node); }
63                                 }
64                         }
65                         return o;
66                 }
67                 
68                 public function pullThrough(entity:Entity,connection:Connection):Way {
69                         var i:uint=0;
70                         if (entity is Way) {
71                                 // copy way through to main layer
72                                 // ** shouldn't do this if the nodes are already in the main layer
73                                 //    (or maybe we should just match on lat/long to avoid ways in background having nodes in foreground)
74                                 var oldWay:Way=Way(entity);
75                                 var newWay:Way=connection.createWay(oldWay.getTagsCopy(), [], MainUndoStack.getGlobalStack().addAction);
76                                 var nodemap:Object={};
77                                 var oldNode:Node, newNode:Node;
78                                 for (i=0; i<oldWay.length; i++) {
79                                         oldNode = oldWay.getNode(i);
80                                         newNode = nodemap[oldNode.id] ? nodemap[oldNode.id] : connection.createNode(
81                                                 oldNode.getTagsCopy(), oldNode.lat, oldNode.lon, 
82                                                 MainUndoStack.getGlobalStack().addAction);
83                                         newWay.appendNode(newNode, MainUndoStack.getGlobalStack().addAction);
84                                         nodemap[oldNode.id]=newNode;
85                                 }
86                                 // delete this way
87                                 while (oldWay.length) { 
88                                         var id:Number=oldWay.getNode(0).id;
89                                         oldWay.removeNodeByIndex(0,MainUndoStack.getGlobalStack().addAction,false);
90                                         delete nodes[id];
91                                 }
92                                 paint.wayuis[oldWay.id].redraw();
93                                 delete ways[oldWay.id];
94                                 map.paint.createWayUI(newWay);
95                         }
96                         return newWay;
97                 }
98                 
99                 public function blank():void {
100                         for each (var node:Node in nodes) { paint.deleteNodeUI(node); }
101                         for each (var way:Way in ways) { paint.deleteWayUI(way); }
102                         relations={}; nodes={}; ways={};
103                 }
104
105         }
106 }