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