diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-02-26 20:24:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-26 20:24:56 +0100 |
commit | 8e0dbd1b8e15dc4fb3722f514d2e54bdf2b672d8 (patch) | |
tree | ba3a2ee77f187c4c28fb1c31eaea4ec539a75eaa /scene/2d | |
parent | 30c5286936298055951ce5c428640e96fa1ddd51 (diff) | |
parent | 136e1e18bac67f0df8e698e5500dc3379966da6c (diff) |
Merge pull request #7858 from bojidar-bg/tilemap-get-size
Add Rect2 TileMap::get_used_rect()
Diffstat (limited to 'scene/2d')
-rw-r--r-- | scene/2d/tile_map.cpp | 26 | ||||
-rw-r--r-- | scene/2d/tile_map.h | 7 |
2 files changed, 31 insertions, 2 deletions
diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index 5de0b93ed9..15fbec4441 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<int>& 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<PosKey,Cell>::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 ba6de62f8e..c581aa8056 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; |