Changeset 580
- Timestamp:
- 06/26/08 16:37:47 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/tom-tweenlite/lib/com/modestmaps/core/TileGrid.as
r578 r580 13 13 import flash.events.IOErrorEvent; 14 14 import flash.events.MouseEvent; 15 import flash.geom.ColorTransform;16 15 import flash.geom.Matrix; 17 16 import flash.geom.Point; … … 41 40 42 41 // pan, zoom and rotate 43 protected var tx:Number = -TILE_WIDTH/2, ty:Number = -TILE_HEIGHT/2; 44 protected var sc:Number = 1; 42 private var _panX:Number = -TILE_WIDTH/2; 43 private var _panY:Number = -TILE_HEIGHT/2; 44 private var _scale:Number = 1; 45 45 46 46 protected var worldMatrix:Matrix; … … 127 127 // 0 or 1, really: 2 will load *lots* of extra tiles 128 128 public static var TILE_BUFFER:int = 0; 129 130 // set this to true to enable enforcing of map bounds from the map provider's limits 131 public static var ENFORCE_BOUNDS:Boolean = false; 129 132 130 133 public var mapWidth:Number; … … 174 177 175 178 worldMatrix = new Matrix(); 176 worldMatrix.translate( tx,tx);177 worldMatrix.scale( sc,sc);179 worldMatrix.translate(_panX,_panX); 180 worldMatrix.scale(_scale,_scale); 178 181 worldMatrix.translate(mapWidth/2, mapHeight/2); 179 182 … … 220 223 } 221 224 222 debugField.text = "tx: " + tx.toFixed(3) + " ty: " + ty.toFixed(3) + " sc: " + sc.toFixed(4)225 debugField.text = "tx: " + _panX.toFixed(3) + " ty: " + _panY.toFixed(3) + " sc: " + _scale.toFixed(4) 223 226 + "\nfps: " + fps.toFixed(0) 224 227 + "\ncurrent child count: " + well.numChildren … … 693 696 { 694 697 var mousePoint:Point = globalToLocal(new Point(event.stageX, event.stageY)); 695 tx += (mousePoint.x - pmouse.x) / sc;696 ty += (mousePoint.y - pmouse.y) / sc;698 _panX += (mousePoint.x - pmouse.x) / _scale; 699 _panY += (mousePoint.y - pmouse.y) / _scale; 697 700 pmouse = mousePoint; 698 701 calculateMatrix(); 699 var origin:Point = startPan || new Point( tx,ty);700 dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x- tx)*sc, -(origin.y-ty)*sc)));702 var origin:Point = startPan || new Point(_panX,_panY); 703 dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-_panX)*_scale, -(origin.y-_panY)*_scale))); 701 704 event.updateAfterEvent(); 702 705 } … … 706 709 // first, simply do what was asked: 707 710 worldMatrix.identity(); 708 worldMatrix.translate( tx,ty);709 worldMatrix.scale( sc,sc);711 worldMatrix.translate(_panX,_panY); 712 worldMatrix.scale(_scale,_scale); 710 713 worldMatrix.translate(mapWidth/2, mapHeight/2); 711 714 712 // TODO: later, is this a good place to enforce map bounds? 713 714 // last, nullify things that will be recalculated as needed 715 // nullify things that will be recalculated as needed 715 716 _invertedMatrix = null; 716 717 _topLeftCoordinate = null; 717 718 _bottomRightCoordinate = null; 719 720 // enforce map bounds? 721 if (enforceBounds()) { 722 // try again! 723 worldMatrix.identity(); 724 worldMatrix.translate(_panX,_panY); 725 worldMatrix.scale(_scale,_scale); 726 worldMatrix.translate(mapWidth/2, mapHeight/2); 727 728 // nullify things that will be recalculated as needed 729 _invertedMatrix = null; 730 _topLeftCoordinate = null; 731 _bottomRightCoordinate = null; 732 } 718 733 719 734 // and request a redraw: … … 741 756 if (!_topLeftCoordinate) { 742 757 var tl:Point = invertedMatrix.transformPoint(new Point()); 743 tl.x *= sc/TILE_WIDTH;744 tl.y *= sc/TILE_HEIGHT;758 tl.x *= _scale/TILE_WIDTH; 759 tl.y *= _scale/TILE_HEIGHT; 745 760 _topLeftCoordinate = new Coordinate(tl.y, tl.x, zoomLevel); 746 761 } … … 756 771 if (!_bottomRightCoordinate) { 757 772 var br:Point = invertedMatrix.transformPoint(new Point(mapWidth, mapHeight)); 758 br.x *= sc/TILE_WIDTH;759 br.y *= sc/TILE_HEIGHT;773 br.x *= _scale/TILE_WIDTH; 774 br.y *= _scale/TILE_HEIGHT; 760 775 _bottomRightCoordinate = new Coordinate(br.y, br.x, zoomLevel); 761 776 } … … 772 787 { 773 788 var c:Point = invertedMatrix.transformPoint(new Point(mapWidth/2, mapHeight/2)); 774 c.x *= sc/TILE_WIDTH;775 c.y *= sc/TILE_HEIGHT;789 c.x *= _scale/TILE_WIDTH; 790 c.y *= _scale/TILE_HEIGHT; 776 791 return new Coordinate(c.y, c.x, zoomLevel); 777 792 } … … 805 820 806 821 var p:Point = invertedMatrix.transformPoint(point); 807 return new Coordinate( sc*p.y/TILE_HEIGHT, sc*p.x/TILE_WIDTH, zoomLevel);822 return new Coordinate(_scale*p.y/TILE_HEIGHT, _scale*p.x/TILE_WIDTH, zoomLevel); 808 823 } 809 824 … … 818 833 } 819 834 } 820 startPan = new Point( tx,ty);835 startPan = new Point(_panX,_panY); 821 836 panning = true; 822 837 dispatchEvent(new MapEvent(MapEvent.START_PANNING)); … … 859 874 { 860 875 // set new scale according to zoom 861 sc= Math.pow(2, coord.zoom);876 _scale = Math.pow(2, coord.zoom); 862 877 863 878 // figure out where in the world we are 864 tx = -TILE_WIDTH*coord.column/sc;865 ty = -TILE_HEIGHT*coord.row/sc;879 _panX = -TILE_WIDTH*coord.column/_scale; 880 _panY = -TILE_HEIGHT*coord.row/_scale; 866 881 867 882 // plus the offset 868 tx += point.x/sc;869 ty += point.y/sc;883 _panX += point.x/_scale; 884 _panY += point.y/_scale; 870 885 871 886 // reset the worldMatrix … … 875 890 public function get panX():Number 876 891 { 877 return tx;892 return _panX; 878 893 } 879 894 public function set panX(n:Number):void 880 895 { 881 tx = n; 896 var origin:Point = startPan || new Point(_panX,_panY); 897 _panX = n; 882 898 calculateMatrix(); 883 var origin:Point = startPan || new Point(tx,ty); 884 dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-tx)*sc, -(origin.y-ty)*sc))); 899 dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-_panX)*_scale, -(origin.y-_panY)*_scale))); 885 900 } 886 901 public function get panY():Number 887 902 { 888 return ty;903 return _panY; 889 904 } 890 905 public function set panY(n:Number):void 891 906 { 892 ty = n; 907 var origin:Point = startPan || new Point(_panX,_panY); 908 _panY = n; 893 909 calculateMatrix(); 894 var origin:Point = startPan || new Point(tx,ty); 895 dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-tx)*sc, -(origin.y-ty)*sc))); 910 dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-_panX)*_scale, -(origin.y-_panY)*_scale))); 896 911 } 897 912 898 913 public function get zoomLevel():Number 899 914 { 900 return Math.log( sc) / Math.log(2);915 return Math.log(_scale) / Math.log(2); 901 916 } 902 917 903 918 public function set zoomLevel(n:Number):void 904 919 { 905 sc = Math.pow(2, n);920 _scale = Math.pow(2, n); 906 921 calculateMatrix(); 907 922 var zoomEvent:MapEvent = new MapEvent(MapEvent.ZOOMED_BY); … … 913 928 public function get scale():Number 914 929 { 915 return sc;930 return _scale; 916 931 } 917 932 public function set scale(n:Number):void 918 933 { 919 934 var old:Number = zoomLevel; 920 sc= n;935 _scale = n; 921 936 calculateMatrix(); 922 937 prepareForZooming(); … … 944 959 calculateBounds(); 945 960 946 // TODO: enforce bounds here 961 // TODO: enforce bounds here? 947 962 948 963 while (well.numChildren > 0) { … … 998 1013 maxTy = br.row * TILE_HEIGHT; 999 1014 1000 minTx -= TILE_WIDTH; 1001 maxTx -= TILE_WIDTH; 1002 minTy -= TILE_HEIGHT; 1003 maxTy -= TILE_HEIGHT; 1004 1005 //trace("bounds: ", minTx, maxTx, minTy, maxTy); 1015 trace("bounds of useful map area: ", minTx, maxTx, minTy, maxTy); 1016 } 1017 1018 /** called inside of calculateMatrix before events are fired 1019 * don't use setters inside of here to correct values otherwise we'll get stuck in a loop! */ 1020 protected function enforceBounds():Boolean 1021 { 1022 if (!ENFORCE_BOUNDS) { 1023 return false; 1024 } 1025 1026 var touched:Boolean = false; 1027 1028 var tl:Coordinate = topLeftCoordinate.zoomTo(0); 1029 var br:Coordinate = bottomRightCoordinate.zoomTo(0); 1030 1031 var leftX:Number = tl.column * TILE_WIDTH; 1032 var rightX:Number = br.column * TILE_WIDTH; 1033 1034 if (rightX-leftX > maxTx-minTx) { 1035 trace("CENTERING X"); 1036 _panX = -(minTx+maxTx)/2; 1037 touched = true; 1038 } 1039 else if (leftX < minTx) { 1040 trace("TOO LEFT"); 1041 _panX += leftX-minTx; 1042 touched = true; 1043 } 1044 else if (rightX > maxTx) { 1045 trace("TOO RIGHT"); 1046 _panX += rightX-maxTx; 1047 touched = true; 1048 } 1049 1050 var upY:Number = tl.row * TILE_HEIGHT; 1051 var downY:Number = br.row * TILE_HEIGHT; 1052 1053 if (downY-upY > maxTy-minTy) { 1054 trace("CENTERING Y"); 1055 _panY = -(minTy+maxTy)/2; 1056 touched = true; 1057 } 1058 else if (upY < minTy) { 1059 trace("TOO HIGH"); 1060 _panY += upY-minTy; 1061 touched = true; 1062 } 1063 else if (downY > maxTy) { 1064 trace("TOO LOW"); 1065 _panY += downY-maxTy; 1066 touched = true; 1067 } 1068 1069 return touched; 1006 1070 } 1007 1071 branches/tom-tweenlite/lib/com/modestmaps/Map.as
r577 r580 139 139 } 140 140 else { 141 // set a default extent 142 // whole world at 512 x 512px 143 // (don't use setExtent here because the whole world 144 // won't fit in the default size, and not all 145 // providers have a zoom level 0) 146 setCenterZoom(new Location(0,0), 1); 141 var extent:MapExtent = new MapExtent(85, -85, 180, -180); 142 setExtent(extent); 143 144 var l1:Location = mapProvider.coordinateLocation(mapProvider.outerLimits()[0]); 145 var l2:Location = mapProvider.coordinateLocation(mapProvider.outerLimits()[1]); 146 147 if (!isNaN(l1.lat) && Math.abs(l1.lat) != Infinity) { 148 extent.north = l1.lat; 149 } 150 if (!isNaN(l2.lat) && Math.abs(l2.lat) != Infinity) { 151 extent.south = l2.lat; 152 } 153 if (!isNaN(l1.lon) && Math.abs(l1.lon) != Infinity) { 154 extent.west = l1.lon; 155 } 156 if (!isNaN(l2.lon) && Math.abs(l2.lon) != Infinity) { 157 extent.east = l2.lon; 158 } 159 160 trace(extent); 161 162 setExtent(extent); 147 163 } 148 164
