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