Add some documenation notes, and remove some traces
[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.styleparser.RuleSet;
8
9     /** The VectorLayer class is used for the concept of Vector Background Layers.
10     * It is similar in concept to the various Connection layers used for the core
11     * OpenStreetMap data, and as such it stores its own list of nodes, ways and relations.
12     * The most interesting function is pullThrough which allows moving entities out
13     * of a VectorLayer and into the main map layer
14     */
15         public class VectorLayer extends Object {
16
17                 public var map:Map;
18                 public var paint:MapPaint;                                              // sprites
19                 public var name:String;
20                 public var url:String;
21                 public var style:String='';
22
23                 public var ways:Object=new Object();                    // geodata
24                 public var nodes:Object=new Object();                   //  |
25                 public var relations:Object=new Object();               //  |
26                 private var pois:Array=[];                                              //  |
27
28         private var markers:Object=new Object();        // markers
29         private var negativeID:Number = -1;
30
31         /** Create a new VectorLayer
32         * @param n The name of the VectorLayer (eg 'GPS tracks')
33         * @param m The map. You probably have a global reference to this
34         * @param s The style you wish to use (eg 'gpx.css')
35         */
36                 public function VectorLayer(n:String,m:Map,s:String) {
37                         name=n;
38                         map=m;
39                         style=s;
40                         paint=new MapPaint(m,0,0);
41                         redrawFromCSS(style);
42                 }
43
44                 public function redrawFromCSS(style:String):void {
45                         paint.ruleset=new RuleSet(map.MINSCALE,map.MAXSCALE,paint.redraw,paint.redrawPOIs);
46                         paint.ruleset.loadFromCSS(style);
47                 }
48
49         /** Create a new node on the vector layer. Note that the node won't show up until on the map
50         * until the the relevant nodeUI is created, so you will need to instruct the paint to create one
51         *
52         * e.g. <code>layer.paint.updateEntityUIs(layer.getObjectsByBbox(...)...);</code>
53         */
54                 public function createNode(tags:Object,lat:Number,lon:Number):Node {
55                         var node:Node = new Node(negativeID, 0, tags, true, lat, lon);
56                         nodes[negativeID]=node; negativeID--;
57                         return node;
58                 }
59
60         /**
61         * @param tags The tags for the new Way
62         * @param nodes An array of Node objects
63         */
64                 public function createWay(tags:Object,nodes:Array):Way {
65                         var way:Way = new Way(negativeID, 0, tags, true, nodes.concat());
66                         ways[negativeID]=way; negativeID--;
67                         return way;
68                 }
69
70         /**
71         * @param tags The tags for the new relation
72         * @param members An array of RelationMember objects
73         */
74                 public function createRelation(tags:Object,members:Array):Relation {
75             var relation:Relation = new Relation(negativeID, 0, tags, true, members.concat());
76                         relations[negativeID]=relation; negativeID--;
77             return relation;
78                 }
79
80         /**
81         * Create a new Marker on the VectorLayer. If you pass in an id it'll check first whether that
82         * marker has been created already, and won't duplicate it.
83         *
84         * @param tags The tags for the new Marker
85         * @param lat The latitude
86         * @param lon The longitude
87         * @param id Use this id for the marker, useful for when layer might be reloaded during panning
88         */
89         public function createMarker(tags:Object,lat:Number,lon:Number,id:Number=NaN):Marker {
90             if (!id) {
91               id = negativeID;
92               negativeID--;
93             }
94             var marker:Marker = markers[id];
95             if (marker == null) {
96               marker = new Marker(id, 0, tags, true, lat, lon);
97               markers[id]=marker;
98             }
99             return marker;
100         }
101
102         public function registerPOI(node:Node):void {
103             if (pois.indexOf(node)<0) { pois.push(node); }
104         }
105         public function unregisterPOI(node:Node):void {
106                         var index:uint = pois.indexOf(node);
107                         if ( index >= 0 ) { pois.splice(index,1); }
108         }
109
110                 public function getObjectsByBbox(left:Number, right:Number, top:Number, bottom:Number):Object {
111                         // ** FIXME: this is just copied-and-pasted from Connection.as, which really isn't very
112                         // good practice. Is there a more elegant way of doing it?
113                         var o:Object = { poisInside: [], poisOutside: [], waysInside: [], waysOutside: [],
114                               markersInside: [], markersOutside: [] };
115                               
116                         for each (var way:Way in ways) {
117                                 if (way.within(left,right,top,bottom)) { o.waysInside.push(way); }
118                                                                   else { o.waysOutside.push(way); }
119                         }
120                         for each (var poi:Node in pois) {
121                                 if (poi.within(left,right,top,bottom)) { o.poisInside.push(poi); }
122                                                                   else { o.poisOutside.push(poi); }
123                         }
124             for each (var marker:Marker in markers) {
125                 if (marker.within(left,right,top,bottom)) { o.markersInside.push(marker); }
126                                                      else { o.markersOutside.push(marker); }
127             }
128                         return o;
129                 }
130
131         /**
132         * Transfers an entity from the VectorLayer into the main layer
133         * @param entity The entity from the VectorLayer that you want to transfer.
134         * @param connection The Connection instance to transfer to (eg Connection.getConnection() )
135         *
136         * @return either the newly created entity, or null
137         */
138                 public function pullThrough(entity:Entity,connection:Connection):Entity {
139                         var i:uint=0;
140                         var oldNode:Node, newNode:Node;
141                         if (entity is Way) {
142                                 // copy way through to main layer
143                                 // ** shouldn't do this if the nodes are already in the main layer
144                                 //    (or maybe we should just match on lat/long to avoid ways in background having nodes in foreground)
145                                 var oldWay:Way=Way(entity);
146                                 var newWay:Way=connection.createWay(oldWay.getTagsCopy(), [], MainUndoStack.getGlobalStack().addAction);
147                                 var nodemap:Object={};
148                                 for (i=0; i<oldWay.length; i++) {
149                                         oldNode = oldWay.getNode(i);
150                                         newNode = nodemap[oldNode.id] ? nodemap[oldNode.id] : connection.createNode(
151                                                 oldNode.getTagsCopy(), oldNode.lat, oldNode.lon, 
152                                                 MainUndoStack.getGlobalStack().addAction);
153                                         newWay.appendNode(newNode, MainUndoStack.getGlobalStack().addAction);
154                                         nodemap[oldNode.id]=newNode;
155                                 }
156                                 // delete this way
157                                 while (oldWay.length) { 
158                                         var id:Number=oldWay.getNode(0).id;
159                                         oldWay.removeNodeByIndex(0,MainUndoStack.getGlobalStack().addAction,false);
160                                         delete nodes[id];
161                                 }
162                                 paint.wayuis[oldWay.id].redraw();
163                                 delete ways[oldWay.id];
164                                 map.paint.createWayUI(newWay);
165                                 return newWay;
166
167                         } else if (entity is Node && !entity.hasParentWays) {
168                                 // copy node through to main layer
169                                 // ** should be properly undoable
170                                 oldNode=Node(entity);
171                                 unregisterPOI(oldNode);
172                                 var newPoiAction:CreatePOIAction = new CreatePOIAction(
173                                         oldNode.getTagsCopy(), oldNode.lat, oldNode.lon);
174                                 MainUndoStack.getGlobalStack().addAction(newPoiAction);
175                                 paint.deleteNodeUI(oldNode);
176                                 delete nodes[oldNode.id];
177                                 return newPoiAction.getNode();
178                         }
179                         return null;
180                 }
181
182         /**
183         * Remove all the nodes, ways, and relations from the VectorLayer.
184         * Also removes the associated NodeUIs, WayUIs and POIs
185         */
186                 public function blank():void {
187                         for each (var node:Node in nodes) { paint.deleteNodeUI(node); }
188                         for each (var way:Way in ways) { paint.deleteWayUI(way); }
189                         relations={}; nodes={}; ways={}; pois=[];
190                 }
191
192         /**
193         * Load more data, eg during panning of the map. This can be overridden by
194         * vector layers if it makes sense to support it.
195         */
196         public function loadBbox(left:Number, right:Number,
197                                 top:Number, bottom:Number):void {
198         }
199
200         }
201 }