From 7852bdd81e6e01faf6f9466f4e720ac7514a166d Mon Sep 17 00:00:00 2001 From: Richard Fairhurst Date: Sun, 20 Sep 2009 00:16:45 +0000 Subject: [PATCH] working (though incomplete) 900913; do the RIGHT SCALE dammit (I hate projection code. Not looking forward to making this projection-independent...); make Map display list a bit less fragile --- halcyon_viewer.as | 10 +-- net/systemeD/halcyon/Elastic.as | 2 +- .../{ImageLoader.as => ImageURLLoader.as} | 3 +- net/systemeD/halcyon/Map.as | 25 ++++++-- net/systemeD/halcyon/POI.as | 9 ++- net/systemeD/halcyon/TileSet.as | 63 +++++++------------ net/systemeD/halcyon/WayUI.as | 2 +- net/systemeD/halcyon/styleparser/RuleSet.as | 5 +- 8 files changed, 62 insertions(+), 57 deletions(-) rename net/systemeD/halcyon/{ImageLoader.as => ImageURLLoader.as} (78%) diff --git a/halcyon_viewer.as b/halcyon_viewer.as index da1818bf..d0fb05ff 100755 --- a/halcyon_viewer.as +++ b/halcyon_viewer.as @@ -22,6 +22,11 @@ package { private function init(e:Event):void { + theMap = new Map(this.loaderInfo.parameters); + theMap.updateSize(stage.stageWidth, stage.stageHeight); + addChild(theMap); + Globals.vars.root=theMap; + // add debug field var t:TextField=new TextField(); t.width=400; t.height=100; t.x=400; t.border=true; @@ -30,11 +35,6 @@ package { Globals.vars.debug=t; t.visible = true; - theMap = new Map(this.loaderInfo.parameters); - theMap.updateSize(stage.stageWidth, stage.stageHeight); - addChild(theMap); - Globals.vars.root=theMap; - stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler); stage.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler); stage.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler); diff --git a/net/systemeD/halcyon/Elastic.as b/net/systemeD/halcyon/Elastic.as index b7b3343d..134ea6dd 100755 --- a/net/systemeD/halcyon/Elastic.as +++ b/net/systemeD/halcyon/Elastic.as @@ -139,7 +139,7 @@ package net.systemeD.halcyon { // Add object (stroke/fill/roadname) to layer sprite private function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void { - var l:DisplayObject=Map(map).getChildAt(5); + var l:DisplayObject=Map(map).getChildAt(map.WAYSPRITE+5); var o:DisplayObject=Sprite(l).getChildAt(t); if (sublayer!=-1) { o=Sprite(o).getChildAt(sublayer); } Sprite(o).addChild(s); diff --git a/net/systemeD/halcyon/ImageLoader.as b/net/systemeD/halcyon/ImageURLLoader.as similarity index 78% rename from net/systemeD/halcyon/ImageLoader.as rename to net/systemeD/halcyon/ImageURLLoader.as index d729201e..2379e485 100755 --- a/net/systemeD/halcyon/ImageLoader.as +++ b/net/systemeD/halcyon/ImageURLLoader.as @@ -10,8 +10,9 @@ package net.systemeD.halcyon { import flash.events.*; import flash.net.*; + import flash.display.*; - public class ImageLoader extends URLLoader { + public class ImageURLLoader extends URLLoader { public var filename:*; } diff --git a/net/systemeD/halcyon/Map.as b/net/systemeD/halcyon/Map.as index 39d13b3f..4e3004f3 100755 --- a/net/systemeD/halcyon/Map.as +++ b/net/systemeD/halcyon/Map.as @@ -74,6 +74,11 @@ package net.systemeD.halcyon { public var connection:Connection; // server connection + public const TILESPRITE:uint=0; + public const WAYSPRITE:uint=1; + public const POISPRITE:uint=12; + public const NAMESPRITE:uint=13; + // ------------------------------------------------------------------------------------------ // Map constructor function @@ -89,7 +94,7 @@ package net.systemeD.halcyon { // [layer][1] - casing // [layer][0] - fill - for (var l:int=0; l<13; l++) { // 11 layers (10 is +5, 0 is -5) + for (var l:int=0; l<13; l++) { // 11 layers (11 is +5, 1 is -5) var s:Sprite = getHitSprite(); // | s.addChild(getPaintSprite()); // | 0 fill s.addChild(getPaintSprite()); // | 1 casing @@ -102,14 +107,16 @@ package net.systemeD.halcyon { s.addChild(getHitSprite()); // | 4 entity hit tests addChild(s); // | } - addChild(getPaintSprite()); // 11 - POIs - addChild(getPaintSprite()); // 12 - shields and POI names + addChild(getPaintSprite()); // 12 - POIs + addChild(getPaintSprite()); // 13 - shields and POI names this.initparams=initparams; connection = Connection.getConnection(initparams); connection.addEventListener(Connection.NEW_WAY, newWayCreated); connection.addEventListener(Connection.NEW_POI, newPOICreated); gotEnvironment(null); + + addEventListener(Event.ENTER_FRAME, everyFrame); } private function getPaintSprite():Sprite { @@ -156,7 +163,7 @@ package net.systemeD.halcyon { //updateSize(); scale=startscale; - scalefactor=MASTERSCALE/Math.pow(2,14-scale); + scalefactor=MASTERSCALE/Math.pow(2,13-scale); baselon =startlon -(mapwidth /2)/scalefactor; basey =lat2latp(startlat)+(mapheight/2)/scalefactor; addDebug("Baselon "+baselon+", basey "+basey); @@ -171,13 +178,14 @@ package net.systemeD.halcyon { public function updateCoords(tx:Number,ty:Number):void { x=tx; y=ty; - // ** calculate tile_l etc. edge_t=coord2lat(-y ); edge_b=coord2lat(-y+mapheight); edge_l=coord2lon(-x ); edge_r=coord2lon(-x+mapwidth ); addDebug("Lon "+edge_l+"-"+edge_r); addDebug("Lat "+edge_b+"-"+edge_t); + + tileset.update(); } public function updateCoordsFromLatLon(lat:Number,lon:Number):void { @@ -348,6 +356,13 @@ package net.systemeD.halcyon { lastxmouse=mouseX; lastymouse=mouseY; } + // ------------------------------------------------------------------------------------------ + // Do every frame + + private function everyFrame(event:Event):void { + tileset.serviceQueue(); + } + // ------------------------------------------------------------------------------------------ // Miscellaneous events diff --git a/net/systemeD/halcyon/POI.as b/net/systemeD/halcyon/POI.as index 92f23031..73e10b26 100644 --- a/net/systemeD/halcyon/POI.as +++ b/net/systemeD/halcyon/POI.as @@ -36,6 +36,7 @@ package net.systemeD.halcyon { // ** apply :hover etc. var sl:StyleList=map.ruleset.getStyles(this.node,tags); var r:Boolean=false; // ** rendered + var l:DisplayObject; for (var sublayer:uint=0; sublayer<10; sublayer++) { if (sl.pointStyles[sublayer]) { @@ -45,6 +46,8 @@ package net.systemeD.halcyon { if (s.icon_image!=iconname) { // 'load' icon (actually just from library) if (map.ruleset.images[s.icon_image]) { +// l=map.getChildAt(map.POISPRITE); +// Sprite(l).addChild(map.ruleset.images[s.icon_image]); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon); loader.loadBytes(map.ruleset.images[s.icon_image]); @@ -63,7 +66,7 @@ package net.systemeD.halcyon { // create name sprite if (!name) { name=new Sprite(); - var c:DisplayObject=map.getChildAt(12); + var c:DisplayObject=map.getChildAt(map.NAMESPRITE); Sprite(c).addChild(name); } t.writeNameLabel(name,tags[t.text],map.lon2coord(node.lon),map.latp2coord(node.latp)); @@ -71,7 +74,7 @@ package net.systemeD.halcyon { } if (!r && iconname!='') { // not rendered any more, so remove - var l:DisplayObject=map.getChildAt(11); + l=map.getChildAt(map.POISPRITE); Sprite(l).removeChild(icon); iconname=''; } @@ -79,7 +82,7 @@ package net.systemeD.halcyon { private function loadedIcon(event:Event):void { icon = Bitmap(event.target.content); - var l:DisplayObject=map.getChildAt(11); + var l:DisplayObject=map.getChildAt(map.POISPRITE); Sprite(l).addChild(icon); updatePosition(); } diff --git a/net/systemeD/halcyon/TileSet.as b/net/systemeD/halcyon/TileSet.as index 2c9a75db..d71013d5 100755 --- a/net/systemeD/halcyon/TileSet.as +++ b/net/systemeD/halcyon/TileSet.as @@ -5,15 +5,15 @@ package net.systemeD.halcyon { // - double or halve xoffset/yoffset accordingly // - blank the tile queue - import flash.display.DisplayObjectContainer; - import flash.display.Bitmap; + import flash.display.*; import flash.events.*; import flash.net.*; - import net.systemeD.halcyon.ImageLoader; + import net.systemeD.halcyon.ImageURLLoader; import net.systemeD.halcyon.Globals; - - public class TileSet extends DisplayObjectContainer { + import flash.system.LoaderContext; + + public class TileSet extends Sprite { public var baseurl:String; @@ -22,8 +22,8 @@ package net.systemeD.halcyon { public var tile_b:int; public var tile_t:int; - public var xoffset:Number; - public var yoffset:Number; + public var xoffset:Number=0; + public var yoffset:Number=0; private var requests:Array=[]; private var tiles:Object={}; // key is "z,x,y"; value "true" (needed) or reference to sprite @@ -42,11 +42,10 @@ package net.systemeD.halcyon { // Update bounds - called on every move public function update():void { - tile_l=lon2tile(map.coord2lon(-xoffset-map.x)); - tile_r=lon2tile(map.coord2lon(-xoffset-map.x+map.mapwidth)); - tile_t=lat2tile(map.coord2lat(-yoffset-map.y)); - tile_b=lat2tile(map.coord2lat(-yoffset-map.y+map.mapheight)); - + tile_l=lon2tile(map.edge_l+xoffset); + tile_r=lon2tile(map.edge_r+xoffset); + tile_t=lat2tile(map.edge_t+yoffset); + tile_b=lat2tile(map.edge_b+yoffset); for (var tx:int=tile_l; tx<=tile_r; tx++) { for (var ty:int=tile_t; ty<=tile_b; ty++) { if (!tiles[map.scale+','+tx+','+ty]) { addRequest(tx,ty); } @@ -66,50 +65,36 @@ package net.systemeD.halcyon { public function serviceQueue():void { if (waiting==4 || requests.length==0) { return; } var r:Array, tx:int, ty:int, tz:int; - var loader:ImageLoader, urlreq:URLRequest; for (var i:uint=0; i=tile_l && tx<=tile_r && ty>=tile_t && ty<=tile_b) { // Tile is on-screen, so load - urlreq=new URLRequest(tileURL(tx,ty)); - loader=new ImageLoader(); - loader.dataFormat=URLLoaderDataFormat.BINARY; - loader.filename=[tz,tx,ty]; - loader.addEventListener(Event.COMPLETE, loadedTile, false, 0, true); - loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true); - loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true); - loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true); - loader.load(urlreq); + var loader:Loader = new Loader(); + loader.contentLoaderInfo.addEventListener(Event.INIT, doImgInit); + loader.load(new URLRequest(tileURL(tx,ty)), + new LoaderContext(true)); + this.addChild(loader); + loader.x=map.lon2coord(tile2lon(tx)); + loader.y=map.lat2coord(tile2lat(ty)); + loader.alpha=0.5; } } } - // Tile has loaded, so place on display list - - private function loadedTile(event:Event):void { - var r:Array=event.target.filename as Array; - var tz:int=r[0]; var tx:int=r[1]; var ty:int=r[2]; - - var image:Bitmap = event.target.content as Bitmap; - addChild(image); - image.x=map.lon2coord(tile2lon(tx)); - image.y=map.lat2coord(tile2lat(ty)); - + protected function doImgInit(evt:Event):void { waiting--; + return; } // Assemble tile URL private function tileURL(tx:int,ty:int):String { - // ***** to do - return ''; + return "http://npe.openstreetmap.org/"+map.scale+"/"+tx+"/"+ty+".png"; +// return "http://andy.sandbox.cloudmade.com/tiles/cycle/"+map.scale+"/"+tx+"/"+ty+".png"; } - private function httpStatusHandler( event:HTTPStatusEvent ):void { } - private function securityErrorHandler( event:SecurityErrorEvent ):void { Globals.vars.root.addDebug("securityerrorevent"); } - private function ioErrorHandler( event:IOErrorEvent ):void { Globals.vars.root.addDebug("ioerrorevent"); } // ------------------------------------------------------------------ @@ -119,7 +104,7 @@ package net.systemeD.halcyon { return (Math.floor((lon+180)/360*Math.pow(2,map.scale))); } private function lat2tile(lat:Number):int { - return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,map.scale))); + return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,map.scale))); } private function tile2lon(t:int):Number { return (t/Math.pow(2,map.scale)*360-180); diff --git a/net/systemeD/halcyon/WayUI.as b/net/systemeD/halcyon/WayUI.as index 51c9f064..abad93af 100755 --- a/net/systemeD/halcyon/WayUI.as +++ b/net/systemeD/halcyon/WayUI.as @@ -397,7 +397,7 @@ package net.systemeD.halcyon { // Add object (stroke/fill/roadname) to layer sprite private function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void { - var l:DisplayObject=Map(map).getChildAt(layer); + var l:DisplayObject=Map(map).getChildAt(map.WAYSPRITE+layer); var o:DisplayObject=Sprite(l).getChildAt(t); if (sublayer!=-1) { o=Sprite(o).getChildAt(sublayer); } Sprite(o).addChild(s); diff --git a/net/systemeD/halcyon/styleparser/RuleSet.as b/net/systemeD/halcyon/styleparser/RuleSet.as index 4afcfcfc..517856f3 100644 --- a/net/systemeD/halcyon/styleparser/RuleSet.as +++ b/net/systemeD/halcyon/styleparser/RuleSet.as @@ -4,7 +4,7 @@ package net.systemeD.halcyon.styleparser { import flash.net.*; import net.systemeD.halcyon.Globals; import net.systemeD.halcyon.Map; - import net.systemeD.halcyon.ImageLoader; + import net.systemeD.halcyon.ImageURLLoader; import net.systemeD.halcyon.connection.Entity; // import bustin.dev.Inspector; @@ -73,7 +73,7 @@ package net.systemeD.halcyon.styleparser { iconsToLoad++; var request:URLRequest=new URLRequest(filename); - var loader:ImageLoader=new ImageLoader(); + var loader:ImageURLLoader=new ImageURLLoader(); loader.dataFormat=URLLoaderDataFormat.BINARY; loader.filename=filename; loader.addEventListener(Event.COMPLETE, loadedImage, false, 0, true); @@ -88,6 +88,7 @@ package net.systemeD.halcyon.styleparser { // data handler private function loadedImage(event:Event):void { +Globals.vars.root.addDebug("adding image at "+event.target.filename); images[event.target.filename]=event.target.data; iconsToLoad--; if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); } -- 2.30.0