Skip to content

Commit

Permalink
Don't return Tree items outside visible rect
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Feb 1, 2025
1 parent 1586c56 commit 873142a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 77 deletions.
167 changes: 91 additions & 76 deletions scene/gui/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5494,22 +5494,30 @@ void Tree::_do_incr_search(const String &p_add) {
ensure_cursor_is_visible();
}

TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const {
TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &r_height, int &r_section) const {
r_column = -1;
r_height = 0;
r_section = -1;

if (!root) {
return nullptr;
}

Point2 pos = p_pos;

if ((root != p_item || !hide_root) && p_item->is_visible_in_tree()) {
h = compute_item_height(p_item) + theme_cache.v_separation;
r_height = compute_item_height(p_item) + theme_cache.v_separation;
if (pos.y < h) {

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🌐 Web / Template w/o threads (target=template_release, threads=no)

use of undeclared identifier 'h'

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Template arm32 (target=template_release, arch=arm32)

use of undeclared identifier 'h'

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🌐 Web / Template w/ threads (target=template_release, threads=yes)

use of undeclared identifier 'h'

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Template arm64 (target=template_release, arch=arm64)

use of undeclared identifier 'h'

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍏 iOS / Template (target=template_release)

use of undeclared identifier 'h'

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍎 macOS / Template (target=template_release, tests=yes)

use of undeclared identifier 'h'

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍎 macOS / Editor (target=editor, tests=yes)

use of undeclared identifier 'h'

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Editor (target=editor)

use of undeclared identifier 'h'

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'h'

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'h'

Check failure on line 5510 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor w/ clang-cl (target=editor, tests=yes, use_llvm=yes)

use of undeclared identifier 'h'
if (drop_mode_flags == DROP_MODE_ON_ITEM) {
section = 0;
r_section = 0;
} else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
section = pos.y < h / 2 ? -1 : 1;
r_section = pos.y < h / 2 ? -1 : 1;

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🌐 Web / Template w/o threads (target=template_release, threads=no)

use of undeclared identifier 'h'

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Template arm32 (target=template_release, arch=arm32)

use of undeclared identifier 'h'

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🌐 Web / Template w/ threads (target=template_release, threads=yes)

use of undeclared identifier 'h'

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Template arm64 (target=template_release, arch=arm64)

use of undeclared identifier 'h'

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍏 iOS / Template (target=template_release)

use of undeclared identifier 'h'

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍎 macOS / Template (target=template_release, tests=yes)

use of undeclared identifier 'h'

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍎 macOS / Editor (target=editor, tests=yes)

use of undeclared identifier 'h'

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Editor (target=editor)

use of undeclared identifier 'h'

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'h'

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'h'

Check failure on line 5514 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor w/ clang-cl (target=editor, tests=yes, use_llvm=yes)

use of undeclared identifier 'h'
} else if (pos.y < h / 4) {

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🌐 Web / Template w/o threads (target=template_release, threads=no)

use of undeclared identifier 'h'

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Template arm32 (target=template_release, arch=arm32)

use of undeclared identifier 'h'

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🌐 Web / Template w/ threads (target=template_release, threads=yes)

use of undeclared identifier 'h'

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Template arm64 (target=template_release, arch=arm64)

use of undeclared identifier 'h'

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍏 iOS / Template (target=template_release)

use of undeclared identifier 'h'

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍎 macOS / Template (target=template_release, tests=yes)

use of undeclared identifier 'h'

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍎 macOS / Editor (target=editor, tests=yes)

use of undeclared identifier 'h'

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Editor (target=editor)

use of undeclared identifier 'h'

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'h'

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'h'

Check failure on line 5515 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor w/ clang-cl (target=editor, tests=yes, use_llvm=yes)

use of undeclared identifier 'h'
section = -1;
r_section = -1;
} else if (pos.y >= (h * 3 / 4)) {

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🌐 Web / Template w/o threads (target=template_release, threads=no)

use of undeclared identifier 'h'

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Template arm32 (target=template_release, arch=arm32)

use of undeclared identifier 'h'

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🌐 Web / Template w/ threads (target=template_release, threads=yes)

use of undeclared identifier 'h'

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Template arm64 (target=template_release, arch=arm64)

use of undeclared identifier 'h'

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍏 iOS / Template (target=template_release)

use of undeclared identifier 'h'

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍎 macOS / Template (target=template_release, tests=yes)

use of undeclared identifier 'h'

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍎 macOS / Editor (target=editor, tests=yes)

use of undeclared identifier 'h'

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Editor (target=editor)

use of undeclared identifier 'h'

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'h'

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'h'

Check failure on line 5517 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor w/ clang-cl (target=editor, tests=yes, use_llvm=yes)

use of undeclared identifier 'h'
section = 1;
r_section = 1;
} else {
section = 0;
r_section = 0;
}

for (int i = 0; i < columns.size(); i++) {
Expand All @@ -5527,7 +5535,7 @@ TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_
pos.y -= h;

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🌐 Web / Template w/o threads (target=template_release, threads=no)

use of undeclared identifier 'h'

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Template arm32 (target=template_release, arch=arm32)

use of undeclared identifier 'h'

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🌐 Web / Template w/ threads (target=template_release, threads=yes)

use of undeclared identifier 'h'

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Template arm64 (target=template_release, arch=arm64)

use of undeclared identifier 'h'

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍏 iOS / Template (target=template_release)

use of undeclared identifier 'h'

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍎 macOS / Template (target=template_release, tests=yes)

use of undeclared identifier 'h'

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🍎 macOS / Editor (target=editor, tests=yes)

use of undeclared identifier 'h'

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🤖 Android / Editor (target=editor)

use of undeclared identifier 'h'

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'h'

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

use of undeclared identifier 'h'

Check failure on line 5535 in scene/gui/tree.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor w/ clang-cl (target=editor, tests=yes, use_llvm=yes)

use of undeclared identifier 'h'
}
} else {
h = 0;
r_height = 0;
}

if (p_item->is_collapsed() || !p_item->is_visible_in_tree()) {
Expand All @@ -5537,9 +5545,9 @@ TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_
TreeItem *n = p_item->get_first_child();
while (n) {
int ch;
TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, section);
TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, r_section);
pos.y -= ch;
h += ch;
r_height += ch;
if (r) {
return r;
}
Expand Down Expand Up @@ -5630,62 +5638,64 @@ void Tree::_find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_co
}

int Tree::get_column_at_position(const Point2 &p_pos) const {
if (root) {
Point2 pos = p_pos;
if (is_layout_rtl()) {
pos.x = get_size().width - pos.x;
}
pos -= theme_cache.panel_style->get_offset();
pos.y -= _get_title_button_height();
if (pos.y < 0) {
return -1;
}

if (h_scroll->is_visible_in_tree()) {
pos.x += h_scroll->get_value();
}
if (v_scroll->is_visible_in_tree()) {
pos.y += v_scroll->get_value();
}
if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
return -1;
}

int col, h, section;
TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
Point2 pos = p_pos;
if (is_layout_rtl()) {
pos.x = get_size().width - pos.x;
}
pos -= theme_cache.panel_style->get_offset();
pos.y -= _get_title_button_height();
if (pos.y < 0) {
return -1;
}

if (it) {
return col;
}
if (h_scroll->is_visible_in_tree()) {
pos.x += h_scroll->get_value();
}
if (v_scroll->is_visible_in_tree()) {
pos.y += v_scroll->get_value();
}

int col, h, section;
TreeItem *it = _find_item_at_pos(root, pos, col, h, section);

if (it) {
return col;
}
return -1;
}

int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
if (root) {
Point2 pos = p_pos;
if (is_layout_rtl()) {
pos.x = get_size().width - pos.x;
}
pos -= theme_cache.panel_style->get_offset();
pos.y -= _get_title_button_height();
if (pos.y < 0) {
return -100;
}

if (h_scroll->is_visible_in_tree()) {
pos.x += h_scroll->get_value();
}
if (v_scroll->is_visible_in_tree()) {
pos.y += v_scroll->get_value();
}
if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
return -100;
}

int col, h, section;
TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
Point2 pos = p_pos;
if (is_layout_rtl()) {
pos.x = get_size().width - pos.x;
}
pos -= theme_cache.panel_style->get_offset();
pos.y -= _get_title_button_height();
if (pos.y < 0) {
return -100;
}

if (it) {
return section;
}
if (h_scroll->is_visible_in_tree()) {
pos.x += h_scroll->get_value();
}
if (v_scroll->is_visible_in_tree()) {
pos.y += v_scroll->get_value();
}

int col, h, section;
TreeItem *it = _find_item_at_pos(root, pos, col, h, section);

if (it) {
return section;
}
return -100;
}

Expand All @@ -5708,36 +5718,41 @@ Variant Tree::get_drag_data(const Point2 &p_point) {
}

TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
if (root) {
Point2 pos = p_pos;
if (is_layout_rtl()) {
pos.x = get_size().width - pos.x;
}
pos -= theme_cache.panel_style->get_offset();
pos.y -= _get_title_button_height();
if (pos.y < 0) {
return nullptr;
}

if (h_scroll->is_visible_in_tree()) {
pos.x += h_scroll->get_value();
}
if (v_scroll->is_visible_in_tree()) {
pos.y += v_scroll->get_value();
}
if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
return nullptr;
}

int col, h, section;
TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
Point2 pos = p_pos;
if (is_layout_rtl()) {
pos.x = get_size().width - pos.x;
}
pos -= theme_cache.panel_style->get_offset();
pos.y -= _get_title_button_height();
if (pos.y < 0) {
return nullptr;
}

if (it) {
return it;
}
if (h_scroll->is_visible_in_tree()) {
pos.x += h_scroll->get_value();
}
if (v_scroll->is_visible_in_tree()) {
pos.y += v_scroll->get_value();
}

int col, h, section;
TreeItem *it = _find_item_at_pos(root, pos, col, h, section);

if (it) {
return it;
}
return nullptr;
}

int Tree::get_button_id_at_position(const Point2 &p_pos) const {
if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
return -1;
}

TreeItem *it;
int col, index;
_find_button_at_pos(p_pos, it, col, index);
Expand Down
2 changes: 1 addition & 1 deletion scene/gui/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ class Tree : public Control {

TreeItem *_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards = false);

TreeItem *_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const;
TreeItem *_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &r_height, int &r_section) const;
int _get_item_h_offset(TreeItem *p_item) const;

void _find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_column, int &r_index) const;
Expand Down

0 comments on commit 873142a

Please sign in to comment.