From e17a57d88a5a9594fb1b1b4285daa851bdee0778 Mon Sep 17 00:00:00 2001 From: Bojidar Marinov Date: Mon, 8 Jul 2019 12:35:52 +0300 Subject: [PATCH] Fix some issue with TileMap's and other nodes' boundaries Fixes #30348 Addresses a small part of #30012 (cherry picked from commit ebf2a4d5531d9a2fe86f0628352f438e6db7dd01) --- scene/2d/path_2d.cpp | 2 +- scene/2d/polygon_2d.cpp | 2 +- scene/2d/tile_map.cpp | 14 +++++++++----- scene/2d/tile_map.h | 7 +++++++ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/scene/2d/path_2d.cpp b/scene/2d/path_2d.cpp index 4097006b33b..45927b28c25 100644 --- a/scene/2d/path_2d.cpp +++ b/scene/2d/path_2d.cpp @@ -58,7 +58,7 @@ Rect2 Path2D::_edit_get_rect() const { } bool Path2D::_edit_use_rect() const { - return true; + return curve.is_valid() && curve->get_point_count() != 0; } bool Path2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { diff --git a/scene/2d/polygon_2d.cpp b/scene/2d/polygon_2d.cpp index f6f1bad5817..32a0b732c09 100644 --- a/scene/2d/polygon_2d.cpp +++ b/scene/2d/polygon_2d.cpp @@ -76,7 +76,7 @@ Rect2 Polygon2D::_edit_get_rect() const { } bool Polygon2D::_edit_use_rect() const { - return true; + return polygon.size() > 0; } bool Polygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index 3562011bc29..788d93a6a6d 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -760,7 +760,7 @@ void TileMap::set_cell(int p_x, int p_y, int p_tile, bool p_flip_x, bool p_flip_ if (!E && p_tile == INVALID_CELL) return; //nothing to do - PosKey qk(p_x / _get_quadrant_size(), p_y / _get_quadrant_size()); + PosKey qk = pk.to_quadrant(_get_quadrant_size()); if (p_tile == INVALID_CELL) { //erase existing tile_map.erase(pk); @@ -919,7 +919,7 @@ void TileMap::update_cell_bitmask(int p_x, int p_y) { E->get().autotile_coord_x = (int)coord.x; E->get().autotile_coord_y = (int)coord.y; - PosKey qk(p_x / _get_quadrant_size(), p_y / _get_quadrant_size()); + PosKey qk = p.to_quadrant(_get_quadrant_size()); Map::Element *Q = quadrant_map.find(qk); _make_quadrant_dirty(Q); @@ -1007,7 +1007,7 @@ void TileMap::set_cell_autotile_coord(int p_x, int p_y, const Vector2 &p_coord) c.autotile_coord_y = p_coord.y; tile_map[pk] = c; - PosKey qk(p_x / _get_quadrant_size(), p_y / _get_quadrant_size()); + PosKey qk = pk.to_quadrant(_get_quadrant_size()); Map::Element *Q = quadrant_map.find(qk); if (!Q) @@ -1034,7 +1034,7 @@ void TileMap::_recreate_quadrants() { for (Map::Element *E = tile_map.front(); E; E = E->next()) { - PosKey qk(E->key().x / _get_quadrant_size(), E->key().y / _get_quadrant_size()); + PosKey qk = PosKey(E->key().x, E->key().y).to_quadrant(_get_quadrant_size()); Map::Element *Q = quadrant_map.find(qk); if (!Q) { @@ -1174,7 +1174,11 @@ PoolVector TileMap::_get_tile_data() const { } Rect2 TileMap::_edit_get_rect() const { - const_cast(this)->update_dirty_quadrants(); + if (pending_update) { + const_cast(this)->update_dirty_quadrants(); + } else { + const_cast(this)->_recompute_rect_cache(); + } return rect_cache; } diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h index 6a1467aa48f..e06eab25110 100644 --- a/scene/2d/tile_map.h +++ b/scene/2d/tile_map.h @@ -90,6 +90,13 @@ private: bool operator==(const PosKey &p_k) const { return (y == p_k.y && x == p_k.x); } + PosKey to_quadrant(const int &p_quadrant_size) const { + // rounding down, instead of simply rounding towards zero (truncating) + return PosKey( + x > 0 ? x / p_quadrant_size : (x - (p_quadrant_size - 1)) / p_quadrant_size, + y > 0 ? y / p_quadrant_size : (y - (p_quadrant_size - 1)) / p_quadrant_size); + } + PosKey(int16_t p_x, int16_t p_y) { x = p_x; y = p_y;