refactor node stateClasses and stylelist calculation
[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 = { nodesInside: [], nodesOutside: [], 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.within(left,right,top,bottom)) { o.nodesInside.push(node); }
61                                                                    else { o.nodesOutside.push(node); }
62                         }
63                         return o;
64                 }
65                 
66                 public function pullThrough(entity:Entity,connection:Connection):Way {
67                         var i:uint=0;
68                         if (entity is Way) {
69                                 // copy way through to main layer
70                                 // ** shouldn't do this if the nodes are already in the main layer
71                                 //    (or maybe we should just match on lat/long to avoid ways in background having nodes in foreground)
72                                 var oldWay:Way=Way(entity);
73                                 var newWay:Way=connection.createWay(oldWay.getTagsCopy(), [], MainUndoStack.getGlobalStack().addAction);
74                                 var nodemap:Object={};
75                                 var oldNode:Node, newNode:Node;
76                                 for (i=0; i<oldWay.length; i++) {
77                                         oldNode = oldWay.getNode(i);
78                                         newNode = nodemap[oldNode.id] ? nodemap[oldNode.id] : connection.createNode(
79                                                 oldNode.getTagsCopy(), oldNode.lat, oldNode.lon, 
80                                                 MainUndoStack.getGlobalStack().addAction);
81                                         newWay.appendNode(newNode, MainUndoStack.getGlobalStack().addAction);
82                                         nodemap[oldNode.id]=newNode;
83                                 }
84                                 // delete this way
85                                 while (oldWay.length) { 
86                                         var id:Number=oldWay.getNode(0).id;
87                                         oldWay.removeNodeByIndex(0,MainUndoStack.getGlobalStack().addAction,false);
88                                         delete nodes[id];
89                                 }
90                                 paint.wayuis[oldWay.id].redraw();
91                                 delete ways[oldWay.id];
92                                 map.paint.createWayUI(newWay);
93                         }
94                         return newWay;
95                 }
96                 
97                 public function blank():void {
98                         for each (var node:Node in nodes) { paint.deleteNodeUI(node); }
99                         for each (var way:Way in ways) { paint.deleteWayUI(way); }
100                         relations={}; nodes={}; ways={};
101                 }
102
103         }
104 }