package{ import flash.display.Sprite; import flash.display.Shape; import flash.geom.Point; import flash.events.MouseEvent; import flash.utils.setInterval; import flash.utils.clearInterval; public class PanZoomTest extends Sprite { var pz:PanZoomMap; var mapImg:Sprite = new Sprite(); var zoomPlus:Sprite = new Sprite(); var zoomMinus:Sprite = new Sprite(); var viewerWidth = 200; var viewerHeight = 200; // for zoom box example var box = new Shape(); var clickPt:Point; var boxInterval; public function PanZoomTest():void { drawMap(); // generate some Sprite as the map createButtons(); // zoom in and out buttons pz = new PanZoomMap(mapImg,viewerWidth,viewerHeight); // create the PanZoomMap using the map and specified size pz.x = 5; pz.y = 20; addChild(pz); // create red zoom box box.graphics.beginFill(0xff0000,.5); box.graphics.drawRect(0,0,10,10); /* Uncomment these lines to test zoom box */ //pz.allowPanning = false; //mapImg.addEventListener(MouseEvent.MOUSE_DOWN,startBox); } function zoomIn(e:MouseEvent) { pz.zoomTo(pz.zoomAmount*2); } function zoomOut(e:MouseEvent) { if (pz.zoomAmount/2 >= pz.minZoom) pz.zoomTo(pz.zoomAmount/2); else pz.zoomTo(pz.minZoom); } // start drawing a zoom box from the click location function startBox(e){ clickPt = new Point(stage.mouseX,stage.mouseY); box.x = clickPt.x; box.y = clickPt.y; box.width = box.height = 1; addChild(box); boxInterval = setInterval(sizeBox,50); stage.addEventListener(MouseEvent.MOUSE_UP,stopBox); } // zoom box follows the mouse function sizeBox(){ box.width = Math.abs(stage.mouseX - clickPt.x); if (stage.mouseX < clickPt.x) box.x = stage.mouseX; else box.x = clickPt.x; box.height = Math.abs(stage.mouseY - clickPt.y); if (stage.mouseY < clickPt.y) box.y = stage.mouseY; else box.y = clickPt.y; } // Here's the basic functionality of the zoom box. function stopBox(e){ clearInterval(boxInterval); stage.removeEventListener(MouseEvent.MOUSE_UP,stopBox); // center of box in map coordinates var where = mapImg.globalToLocal(new Point(box.x + box.width/2,box.y + box.height/2)); var scale; // viewer size divided by box size // e.g., if the box is half the width of the viewer, the scale has to be doubled for the area under the box to match the area of the viewer if (box.width > box.height){ scale = viewerWidth*pz.zoomAmount/box.width; } else { scale = viewerHeight*pz.zoomAmount/box.height; } pz.zoomTo(scale); // zoom to the calculated scale pz.panTo(where); // pan to the box center removeChild(box); } // just draw some random shapes as our "map" function drawMap():void { mapImg.graphics.beginFill(0xcccccc); mapImg.graphics.drawRect(0,0,300,300); mapImg.graphics.endFill(); for (var i=0; i<100; i++){ var radius = Math.random()*20; var x = Math.random()*(300-2*radius)+radius; var y = Math.random()*(300-2*radius)+radius; var color = Math.random()*0xffffff; mapImg.graphics.beginFill(color); mapImg.graphics.drawCircle(x,y,radius); mapImg.graphics.endFill(); } } function createButtons():void { zoomPlus.graphics.beginFill(0x666666); zoomPlus.graphics.drawRect(0,0,10,10); zoomPlus.graphics.endFill(); zoomPlus.graphics.lineStyle(2,0xcccccc); zoomPlus.graphics.moveTo(5,2); zoomPlus.graphics.lineTo(5,8); zoomPlus.graphics.moveTo(2,5); zoomPlus.graphics.lineTo(8,5); zoomPlus.x = zoomPlus.y = 5; addChild(zoomPlus); zoomMinus.graphics.beginFill(0x666666); zoomMinus.graphics.drawRect(0,0,10,10); zoomMinus.graphics.endFill(); zoomMinus.graphics.lineStyle(2,0xcccccc); zoomMinus.graphics.moveTo(2,5); zoomMinus.graphics.lineTo(8,5); zoomMinus.x = 20; zoomMinus.y = 5; addChild(zoomMinus); zoomPlus.addEventListener(MouseEvent.CLICK,zoomIn); zoomMinus.addEventListener(MouseEvent.CLICK,zoomOut); } } }