diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index 5de0b93ed95..15fbec44415 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -730,6 +730,7 @@ void TileMap::set_cell(int p_x,int p_y,int p_tile,bool p_flip_x,bool p_flip_y,bo c.transpose=p_transpose; _make_quadrant_dirty(Q); + used_size_cache_dirty=true; } @@ -818,6 +819,7 @@ void TileMap::clear() { _clear_quadrants(); tile_map.clear(); + used_size_cache_dirty=true; } void TileMap::_set_tile_data(const PoolVector& p_data) { @@ -1159,6 +1161,28 @@ Array TileMap::get_used_cells() const { return a; } +Rect2 TileMap::get_used_rect() { // Not const because of cache + + if (used_size_cache_dirty) { + if(tile_map.size() > 0) { + used_size_cache = Rect2(tile_map.front()->key().x, tile_map.front()->key().y, 0, 0); + + for (Map::Element *E=tile_map.front();E;E=E->next()) { + used_size_cache.expand_to(Vector2(E->key().x, E->key().y)); + } + + used_size_cache.size += Vector2(1,1); + } else { + used_size_cache = Rect2(); + } + + used_size_cache_dirty = false; + } + + return used_size_cache; +} + + void TileMap::set_occluder_light_mask(int p_mask) { occluder_light_mask=p_mask; @@ -1251,6 +1275,7 @@ void TileMap::_bind_methods() { ClassDB::bind_method(D_METHOD("clear"),&TileMap::clear); ClassDB::bind_method(D_METHOD("get_used_cells"),&TileMap::get_used_cells); + ClassDB::bind_method(D_METHOD("get_used_rect"),&TileMap::get_used_rect); ClassDB::bind_method(D_METHOD("map_to_world","mappos","ignore_half_ofs"),&TileMap::map_to_world,DEFVAL(false)); ClassDB::bind_method(D_METHOD("world_to_map","worldpos"),&TileMap::world_to_map); @@ -1305,6 +1330,7 @@ TileMap::TileMap() { rect_cache_dirty=true; + used_size_cache_dirty=true; pending_update=false; quadrant_order_dirty=false; quadrant_size=16; diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h index ba6de62f8ed..c581aa8056c 100644 --- a/scene/2d/tile_map.h +++ b/scene/2d/tile_map.h @@ -141,6 +141,8 @@ private: Rect2 rect_cache; bool rect_cache_dirty; + Rect2 used_size_cache; + bool used_size_cache_dirty; bool quadrant_order_dirty; bool y_sort_mode; float fp_adjust; @@ -176,8 +178,6 @@ private: _FORCE_INLINE_ Vector2 _map_to_world(int p_x,int p_y,bool p_ignore_ofs=false) const; - Array get_used_cells() const; - protected: @@ -252,6 +252,9 @@ public: void set_y_sort_mode(bool p_enable); bool is_y_sort_mode_enabled() const; + Array get_used_cells() const; + Rect2 get_used_rect(); // Not const because of cache + void set_occluder_light_mask(int p_mask); int get_occluder_light_mask() const;