Maps: Pad grid size to prevent missing tiles

Previously, we would divide the widget width and height by the tile size
and round up, which did not result in enough tiles to cover the entire
widget. Although this calculation is correct if you starting drawing
tiles in the top left corner, we have an additional offset to account
for.

Now, we take the number of tiles that fit in the widget completely and
pad it with 2 tiles to account for the partial left/right and top/bottom
sides. An additional tile is added to account for the iterator
translating by width / 2, which rounds down again.

The resulting tile rects are always intersected with the widget
dimensions, so even if we're generating more tile coordinates than
strictly necessary, we're not performing the actual download or draw
operations.
This commit is contained in:
Jelle Raaijmakers 2023-10-02 22:29:34 +02:00
parent eb4dd7f896
commit 3c02e3ba09

View file

@ -438,10 +438,11 @@ void MapWidget::paint_map(GUI::Painter& painter)
double offset_x = (longitude_to_tile_x(m_center.longitude, m_zoom) - center_tile_x) * TILE_SIZE;
double offset_y = (latitude_to_tile_y(m_center.latitude, m_zoom) - center_tile_y) * TILE_SIZE;
// Draw grid around center tile
int grid_width = (width() + TILE_SIZE - 1) / TILE_SIZE;
int grid_height = (height() + TILE_SIZE - 1) / TILE_SIZE;
for (auto const delta : CenterOutwardsIterable { grid_width + 1, grid_height + 1 }) {
// Draw grid around center tile; always pad the dimensions with 2 tiles for left/right and top/bottom edges
// plus one additional tile to account for the width() / 2 in CenterOutwardsIterable.
int grid_width = width() / TILE_SIZE + 3;
int grid_height = height() / TILE_SIZE + 3;
for (auto const delta : CenterOutwardsIterable { grid_width, grid_height }) {
int tile_x = center_tile_x + delta.x();
int tile_y = center_tile_y + delta.y();