Explorar el Código

Add Rect2 TileMap::get_used_rect(), closes #4390

Bojidar Marinov hace 8 años
padre
commit
136e1e18ba
Se han modificado 2 ficheros con 31 adiciones y 2 borrados
  1. 26 0
      scene/2d/tile_map.cpp
  2. 5 2
      scene/2d/tile_map.h

+ 26 - 0
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;

+ 5 - 2
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;