From 665dc7805bb3547dfd44adc11a20910aea87eb5d Mon Sep 17 00:00:00 2001 From: kobewi Date: Thu, 5 Aug 2021 18:17:36 +0200 Subject: [PATCH] Fix get_used_rect() calculation --- scene/2d/tile_map.cpp | 24 +++++++++++++++--------- scene/2d/tile_map.h | 4 ++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index 6afa0c07794..e2a415e5aa8 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -1576,7 +1576,7 @@ void TileMap::set_cell(int p_layer, const Vector2i &p_coords, int p_source_id, c _make_quadrant_dirty(Q); } - used_size_cache_dirty = true; + used_rect_cache_dirty = true; } else { if (!E) { // Insert a new cell in the tile map. @@ -1604,7 +1604,7 @@ void TileMap::set_cell(int p_layer, const Vector2i &p_coords, int p_source_id, c c.alternative_tile = alternative_tile; _make_quadrant_dirty(Q); - used_size_cache_dirty = true; + used_rect_cache_dirty = true; } } @@ -1800,7 +1800,7 @@ void TileMap::clear_layer(int p_layer) { _clear_layer_internals(p_layer); layers[p_layer].tile_map.clear(); - used_size_cache_dirty = true; + used_rect_cache_dirty = true; } void TileMap::clear() { @@ -1809,7 +1809,7 @@ void TileMap::clear() { for (unsigned int i = 0; i < layers.size(); i++) { layers[i].tile_map.clear(); } - used_size_cache_dirty = true; + used_rect_cache_dirty = true; } void TileMap::_set_tile_data(int p_layer, const Vector &p_data) { @@ -2666,25 +2666,31 @@ TypedArray TileMap::get_used_cells(int p_layer) const { Rect2 TileMap::get_used_rect() { // Not const because of cache // Return the rect of the currently used area - if (used_size_cache_dirty) { + if (used_rect_cache_dirty) { bool first = true; + used_rect_cache = Rect2i(); + for (unsigned int i = 0; i < layers.size(); i++) { const Map &tile_map = layers[i].tile_map; if (tile_map.size() > 0) { if (first) { - used_size_cache = Rect2(tile_map.front()->key().x, tile_map.front()->key().y, 1, 1); + used_rect_cache = Rect2i(tile_map.front()->key().x, tile_map.front()->key().y, 0, 0); first = false; } for (Map::Element *E = tile_map.front(); E; E = E->next()) { - used_size_cache.expand_to(Vector2(E->key().x + 1, E->key().y + 1)); + used_rect_cache.expand_to(Vector2i(E->key().x, E->key().y)); } } } - used_size_cache_dirty = false; + + if (!first) { // first is true if every layer is empty. + used_rect_cache.size += Vector2i(1, 1); // The cache expands to top-left coordinate, so we add one full tile. + } + used_rect_cache_dirty = false; } - return used_size_cache; + return used_rect_cache; } // --- Override some methods of the CanvasItem class to pass the changes to the quadrants CanvasItems --- diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h index 4800780f947..dce18f7682f 100644 --- a/scene/2d/tile_map.h +++ b/scene/2d/tile_map.h @@ -213,8 +213,8 @@ private: // Rect. Rect2 rect_cache; bool rect_cache_dirty = true; - Rect2 used_size_cache; - bool used_size_cache_dirty = true; + Rect2i used_rect_cache; + bool used_rect_cache_dirty = true; // TileMap layers. struct TileMapLayer {