Skip to content

Commit

Permalink
[API] + OSM.SlippyMapUtils.pas, add ToTileHeightGreater, ToTileHeight…
Browse files Browse the repository at this point in the history
…Lesser, ToTileWidthGreater, ToTileWidthLesser, ToTileBoundary functions (first 4 extrated from private section of OSM.MapControl.pas)
  • Loading branch information
Fr0sT-Brutal committed Oct 4, 2021
1 parent 0c86ade commit 5a5513f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
42 changes: 7 additions & 35 deletions Source/OSM.MapControl.pas
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,15 @@ TMapControl = class(TScrollBox)
property OnSelectionBox: TOnSelectionBox read FOnSelectionBox write FOnSelectionBox;
end;

// Like Client<=>Screen

// Convert absolute map coords to a point inside a viewport having given top-left point
function ToInnerCoords(const StartPt, Pt: TPoint): TPoint; overload; inline;
// Convert a point inside a viewport having given top-left point to absolute map coords
function ToOuterCoords(const StartPt, Pt: TPoint): TPoint; overload; inline;
// Convert absolute map rect to a rect inside a viewport having given top-left point
function ToInnerCoords(const StartPt: TPoint; const Rect: TRect): TRect; overload; inline;
// Convert a rect inside a viewport having given top-left point to absolute map rect
function ToOuterCoords(const StartPt: TPoint; const Rect: TRect): TRect; overload; inline;

const
Expand Down Expand Up @@ -394,34 +400,6 @@ function ToOuterCoords(const StartPt: TPoint; const Rect: TRect): TRect;
);
end;

// Floor value to tile size

function ToTileWidthLesser(Width: Cardinal): Cardinal; inline;
begin
Result := (Width div TILE_IMAGE_WIDTH)*TILE_IMAGE_WIDTH;
end;

function ToTileHeightLesser(Height: Cardinal): Cardinal; inline;
begin
Result := (Height div TILE_IMAGE_HEIGHT)*TILE_IMAGE_HEIGHT;
end;

// Ceil value to tile size

function ToTileWidthGreater(Width: Cardinal): Cardinal; inline;
begin
Result := ToTileWidthLesser(Width);
if Width mod TILE_IMAGE_WIDTH > 0 then
Inc(Result, TILE_IMAGE_WIDTH);
end;

function ToTileHeightGreater(Height: Cardinal): Cardinal; inline;
begin
Result := ToTileHeightLesser(Height);
if Height mod TILE_IMAGE_HEIGHT > 0 then
Inc(Result, TILE_IMAGE_HEIGHT);
end;

// Draw triangle on canvas
procedure Triangle(Canvas: TCanvas; const Rect: TRect);
begin
Expand Down Expand Up @@ -1025,13 +1003,7 @@ procedure TMapControl.MoveCache;
ViewRect: TRect;
MarginH, MarginV: Cardinal;
begin
ViewRect := ViewAreaRect;
// move view rect to the border of tiles (to lesser value)
ViewRect.Left := ToTileWidthLesser(ViewRect.Left);
ViewRect.Top := ToTileHeightLesser(ViewRect.Top);
// resize view rect to the border of tiles (to greater value)
ViewRect.Right := ToTileWidthGreater(ViewRect.Right);
ViewRect.Bottom := ToTileHeightGreater(ViewRect.Bottom);
ViewRect := ToTileBoundary(ViewAreaRect);

// reposition new cache rect to cover tile-aligned view area
// calc margins
Expand Down
51 changes: 51 additions & 0 deletions Source/OSM.SlippyMapUtils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ function TileToStr(const Tile: TTile): string;
// Compares tiles
function TilesEqual(const Tile1, Tile2: TTile): Boolean; inline;

// Floor horizontal map coord to tile size
function ToTileWidthLesser(Width: Cardinal): Cardinal; inline;
// Floor vertical map coord to tile size
function ToTileHeightLesser(Height: Cardinal): Cardinal; inline;
// Ceil horizontal map coord to tile size
function ToTileWidthGreater(Width: Cardinal): Cardinal; inline;
// Ceil vertical map coord to tile size
function ToTileHeightGreater(Height: Cardinal): Cardinal; inline;
// Align absolute map rect to tile boundaries
function ToTileBoundary(const Rect: TRect): TRect;

// Returns width of map at zoom level `Zoom` in pixels
function MapWidth(Zoom: TMapZoomLevel): Cardinal; inline;
// Returns height of map at zoom level `Zoom` in pixels
Expand Down Expand Up @@ -182,6 +193,46 @@ function TilesEqual(const Tile1, Tile2: TTile): Boolean;
(Tile1.ParameterY = Tile2.ParameterY);
end;

// Floor value to tile size

function ToTileWidthLesser(Width: Cardinal): Cardinal; inline;
begin
Result := (Width div TILE_IMAGE_WIDTH)*TILE_IMAGE_WIDTH;
end;

function ToTileHeightLesser(Height: Cardinal): Cardinal; inline;
begin
Result := (Height div TILE_IMAGE_HEIGHT)*TILE_IMAGE_HEIGHT;
end;

// Ceil value to tile size

function ToTileWidthGreater(Width: Cardinal): Cardinal; inline;
begin
Result := ToTileWidthLesser(Width);
if Width mod TILE_IMAGE_WIDTH > 0 then
Inc(Result, TILE_IMAGE_WIDTH);
end;

function ToTileHeightGreater(Height: Cardinal): Cardinal; inline;
begin
Result := ToTileHeightLesser(Height);
if Height mod TILE_IMAGE_HEIGHT > 0 then
Inc(Result, TILE_IMAGE_HEIGHT);
end;

function ToTileBoundary(const Rect: TRect): TRect;
begin
Result := TRect.Create(
// move view rect to the border of tiles (to lesser value)
ToTileWidthLesser(Rect.Left),
ToTileHeightLesser(Rect.Top),
// resize view rect to the border of tiles (to greater value)
ToTileWidthGreater(Rect.Right),
ToTileHeightGreater(Rect.Bottom)
);
end;

function MapWidth(Zoom: TMapZoomLevel): Cardinal;
begin
Result := TileCount(Zoom)*TILE_IMAGE_WIDTH;
Expand Down

0 comments on commit 5a5513f

Please sign in to comment.