1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Merge pull request #102842 from Hilderin/fix-tree-mouse-hover

Fix Tree Mouse hover position
This commit is contained in:
Rémi Verschelde
2025-02-17 09:47:50 +01:00
2 changed files with 171 additions and 283 deletions

View File

@@ -2872,10 +2872,7 @@ void Tree::_range_click_timeout() {
Ref<InputEventMouseButton> mb; Ref<InputEventMouseButton> mb;
mb.instantiate(); mb.instantiate();
int x_limit = get_size().width - theme_cache.panel_style->get_minimum_size().width; int x_limit = _get_content_rect().size.x;
if (v_scroll->is_visible()) {
x_limit -= v_scroll->get_minimum_size().width;
}
cache.rtl = is_layout_rtl(); cache.rtl = is_layout_rtl();
@@ -2909,6 +2906,10 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int
// Skip any processing of invisible items. // Skip any processing of invisible items.
return 0; return 0;
} }
if (p_pos.x > x_limit) {
// Inside the scroll area.
return -1;
}
int item_h = compute_item_height(p_item) + theme_cache.v_separation; int item_h = compute_item_height(p_item) + theme_cache.v_separation;
@@ -2930,96 +2931,42 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int
return -1; return -1;
} }
int x = p_pos.x; FindColumnButtonResult find_result = _find_column_and_button_at_pos(p_pos.x, p_item, x_ofs, x_limit);
/* find clicked column */
int col = -1;
int col_ofs = 0;
int col_width = 0;
int limit_w = x_limit; if (find_result.column_index == -1) {
for (int i = 0; i < columns.size(); i++) {
col_width = get_column_width(i);
if (p_item->cells[i].expand_right) {
int plus = 1;
while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].text.is_empty() && p_item->cells[i + plus].icon.is_null()) {
col_width += theme_cache.h_separation;
col_width += get_column_width(i + plus);
plus++;
}
}
if (x > col_width) {
col_ofs += col_width;
x -= col_width;
limit_w -= col_width;
continue;
}
col = i;
break;
}
if (col == -1) {
return -1; return -1;
} else if (col == 0) {
int margin = x_ofs + theme_cache.item_margin; //-theme_cache.h_separation;
//int lm = theme_cache.panel_style->get_margin(SIDE_LEFT);
col_width -= margin;
limit_w -= margin;
col_ofs += margin;
x -= margin;
} else {
col_width -= theme_cache.h_separation;
limit_w -= theme_cache.h_separation;
x -= theme_cache.h_separation;
} }
int col = find_result.column_index;
int col_width = find_result.column_width;
int col_ofs = find_result.column_offset;
int x = find_result.pos_x;
const TreeItem::Cell &c = p_item->cells[col]; const TreeItem::Cell &c = p_item->cells[col];
if (!cache.rtl && !p_item->cells[col].buttons.is_empty()) { if (find_result.button_index >= 0) {
int button_w = 0; if (c.buttons[find_result.button_index].disabled) {
for (int j = p_item->cells[col].buttons.size() - 1; j >= 0; j--) { pressed_button = -1;
Ref<Texture2D> b = p_item->cells[col].buttons[j].texture; cache.click_type = Cache::CLICK_NONE;
button_w += b->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
}
col_width = MAX(button_w, MIN(limit_w, col_width));
}
// Cell button detection code.
for (int j = c.buttons.size() - 1; j >= 0; j--) {
Ref<Texture2D> b = c.buttons[j].texture;
int w = b->get_size().width + theme_cache.button_pressed->get_minimum_size().width;
if (x > col_width - w) {
if (c.buttons[j].disabled) {
pressed_button = -1;
cache.click_type = Cache::CLICK_NONE;
return -1;
}
// Make sure the click is correct.
const Point2 click_pos = get_local_mouse_position();
if (!get_item_at_position(click_pos)) {
pressed_button = -1;
cache.click_type = Cache::CLICK_NONE;
return -1;
}
pressed_button = j;
cache.click_type = Cache::CLICK_BUTTON;
cache.click_index = j;
cache.click_id = c.buttons[j].id;
cache.click_item = p_item;
cache.click_column = col;
cache.click_pos = click_pos;
queue_redraw();
return -1; return -1;
} }
col_width -= w + theme_cache.button_margin; // Make sure the click is correct.
const Point2 click_pos = get_local_mouse_position();
if (!get_item_at_position(click_pos)) {
pressed_button = -1;
cache.click_type = Cache::CLICK_NONE;
return -1;
}
pressed_button = find_result.button_index;
cache.click_type = Cache::CLICK_BUTTON;
cache.click_index = find_result.button_index;
cache.click_id = c.buttons[find_result.button_index].id;
cache.click_item = p_item;
cache.click_column = col;
cache.click_pos = click_pos;
queue_redraw();
return -1;
} }
if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_first_child()) { if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_first_child()) {
@@ -3790,7 +3737,7 @@ void Tree::gui_input(const Ref<InputEvent> &p_event) {
mb->get_button_index() == MouseButton::RIGHT) { mb->get_button_index() == MouseButton::RIGHT) {
Point2 pos = mb->get_position(); Point2 pos = mb->get_position();
if (rtl) { if (rtl) {
pos.x = get_size().width - pos.x; pos.x = get_size().width - pos.x - 1;
} }
pos -= theme_cache.panel_style->get_offset(); pos -= theme_cache.panel_style->get_offset();
if (show_column_titles) { if (show_column_titles) {
@@ -3890,7 +3837,7 @@ void Tree::gui_input(const Ref<InputEvent> &p_event) {
Point2 pos = mb->get_position(); Point2 pos = mb->get_position();
if (rtl) { if (rtl) {
pos.x = get_size().width - pos.x; pos.x = get_size().width - pos.x - 1;
} }
pos -= bg->get_offset(); pos -= bg->get_offset();
cache.click_type = Cache::CLICK_NONE; cache.click_type = Cache::CLICK_NONE;
@@ -3922,10 +3869,7 @@ void Tree::gui_input(const Ref<InputEvent> &p_event) {
pressing_for_editor = false; pressing_for_editor = false;
propagate_mouse_activated = false; propagate_mouse_activated = false;
int x_limit = get_size().width - theme_cache.panel_style->get_minimum_size().width; int x_limit = _get_content_rect().size.x;
if (v_scroll->is_visible()) {
x_limit -= v_scroll->get_minimum_size().width;
}
cache.rtl = is_layout_rtl(); cache.rtl = is_layout_rtl();
blocked++; blocked++;
@@ -4028,7 +3972,7 @@ void Tree::_determine_hovered_item() {
Point2 pos = hovered_pos; Point2 pos = hovered_pos;
if (rtl) { if (rtl) {
pos.x = get_size().width - pos.x; pos.x = get_size().width - pos.x - 1;
} }
pos -= theme_cache.panel_style->get_offset(); pos -= theme_cache.panel_style->get_offset();
@@ -4059,116 +4003,37 @@ void Tree::_determine_hovered_item() {
} }
// Determine hover on rows and items. // Determine hover on rows and items.
if (root && is_mouse_hovering) { if (root && is_mouse_hovering && hovered_pos.y >= 0) {
Point2 mpos = hovered_pos; TreeItem *it;
if (rtl) { int col, section, col_button_index;
mpos.x = get_size().width - mpos.x; _find_button_at_pos(hovered_pos, it, col, col_button_index, section);
if (drop_mode_flags) {
if (it != drop_mode_over) {
drop_mode_over = it;
queue_redraw();
}
if (it && section != drop_mode_section) {
drop_mode_section = section;
queue_redraw();
}
} }
mpos -= theme_cache.panel_style->get_offset();
mpos.y -= _get_title_button_height();
if (mpos.y >= 0) {
if (h_scroll->is_visible_in_tree()) {
mpos.x += h_scroll->get_value();
}
if (v_scroll->is_visible_in_tree()) {
mpos.y += v_scroll->get_value();
}
int col, h, section; cache.hover_item = it;
TreeItem *it = _find_item_at_pos(root, mpos, col, h, section); cache.hover_column = col;
cache.hover_button_index_in_column = col_button_index;
// Find possible hovered button in cell. if (it != old_item || col != old_column) {
int col_button_index = -1; if (old_item && old_column >= old_item->cells.size()) {
// Columns may have changed since last redraw().
Point2 cpos = mpos; queue_redraw();
} else {
if (it) { // Only need to update if mouse enters/exits a button.
const TreeItem::Cell &c = it->cells[col]; bool was_over_button = old_item && old_item->cells[old_column].custom_button;
int col_width = get_column_width(col); bool is_over_button = it && it->cells[col].custom_button;
if (was_over_button || is_over_button) {
// In the first column, tree nesting indent impacts the leftmost possible buttons position
// and the clickable area of the folding arrow.
int col_indent = 0;
if (col == 0) {
col_indent = _get_item_h_offset(it);
}
// Compute total width of buttons block including spacings.
int buttons_width = 0;
for (int j = c.buttons.size() - 1; j >= 0; j--) {
Ref<Texture2D> b = c.buttons[j].texture;
Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
buttons_width += size.width + theme_cache.button_margin;
}
// Adjust when buttons are shifted left into view so that they remain visible even
// if part of the cell is beyond the right border due to horizontal scrolling and
// a long string in one of the items. This matches the drawing & click handling algorithms
// that are based on recursion.
int clamped_column_offset = 0;
int col_left = 0;
for (int i = 0; i < col; i++) {
int i_col_w = get_column_width(i);
cpos.x -= i_col_w;
col_left += i_col_w;
}
col_left -= theme_cache.offset.x;
// Compute buttons offset that makes them visible, in comparison to what would be their
// natural position that would cut them off.
if (!rtl) {
const Rect2 content_rect = _get_content_rect();
int cw = content_rect.size.width;
int col_right = col_left + col_width;
if (col_right > cw) {
clamped_column_offset = col_right - cw - theme_cache.scrollbar_h_separation;
int max_clamp_offset = col_width - col_indent - buttons_width;
if (clamped_column_offset > max_clamp_offset) {
clamped_column_offset = max_clamp_offset;
}
}
}
col_width -= clamped_column_offset;
// Find the actual button under coordinates.
for (int j = c.buttons.size() - 1; j >= 0; j--) {
Ref<Texture2D> b = c.buttons[j].texture;
Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
if (cpos.x > col_width - size.width && col_button_index == -1) {
col_button_index = j;
}
col_width -= size.width + theme_cache.button_margin;
}
}
if (drop_mode_flags) {
if (it != drop_mode_over) {
drop_mode_over = it;
queue_redraw(); queue_redraw();
} }
if (it && section != drop_mode_section) {
drop_mode_section = section;
queue_redraw();
}
}
cache.hover_item = it;
cache.hover_column = col;
cache.hover_button_index_in_column = col_button_index;
if (it != old_item || col != old_column) {
if (old_item && old_column >= old_item->cells.size()) {
// Columns may have changed since last redraw().
queue_redraw();
} else {
// Only need to update if mouse enters/exits a button.
bool was_over_button = old_item && old_item->cells[old_column].custom_button;
bool is_over_button = it && it->cells[col].custom_button;
if (was_over_button || is_over_button) {
queue_redraw();
}
}
} }
} }
} }
@@ -5107,46 +4972,6 @@ int Tree::get_item_offset(TreeItem *p_item) const {
return -1; // Not found. return -1; // Not found.
} }
int Tree::_get_item_h_offset(TreeItem *p_item) const {
TreeItem *it = root;
int nesting_level = 0;
if (!it) {
return 0;
}
while (true) {
if (it == p_item) {
if (!hide_root) {
nesting_level += 1;
}
if (hide_folding) {
nesting_level -= 1;
}
return nesting_level * theme_cache.item_margin;
}
if (it->first_child && !it->collapsed) {
it = it->first_child;
nesting_level += 1;
} else if (it->next) {
it = it->next;
} else {
while (!it->next) {
it = it->parent;
nesting_level -= 1;
if (it == nullptr) {
return 0;
}
}
it = it->next;
}
}
return -1; // Not found.
}
void Tree::ensure_cursor_is_visible() { void Tree::ensure_cursor_is_visible() {
if (!is_inside_tree()) { if (!is_inside_tree()) {
return; return;
@@ -5591,26 +5416,34 @@ TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_
// When on a button, r_index is valid. // When on a button, r_index is valid.
// When on an item, both r_item and r_column are valid. // When on an item, both r_item and r_column are valid.
// Otherwise, all output arguments are invalid. // Otherwise, all output arguments are invalid.
void Tree::_find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_column, int &r_index) const { void Tree::_find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_column, int &r_index, int &r_section) const {
r_item = nullptr; r_item = nullptr;
r_column = -1; r_column = -1;
r_index = -1; r_index = -1;
r_section = -1;
if (!root) { if (!root) {
return; return;
} }
Point2 pos = p_pos - theme_cache.panel_style->get_offset(); Point2 pos = p_pos;
if (cache.rtl) {
pos.x = get_size().width - pos.x - 1;
}
pos -= theme_cache.panel_style->get_offset();
pos.y -= _get_title_button_height(); pos.y -= _get_title_button_height();
if (pos.y < 0) { if (pos.y < 0) {
return; return;
} }
if (cache.rtl) {
pos.x = get_size().width - pos.x;
}
pos += theme_cache.offset; // Scrolling. pos += theme_cache.offset; // Scrolling.
int x_limit = _get_content_rect().size.x + theme_cache.offset.width;
if (pos.x > x_limit) {
// Inside the scroll area.
return;
}
int col, h, section; int col, h, section;
TreeItem *it = _find_item_at_pos(root, pos, col, h, section); TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
if (!it) { if (!it) {
@@ -5619,53 +5452,99 @@ void Tree::_find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_co
r_item = it; r_item = it;
r_column = col; r_column = col;
r_section = section;
const TreeItem::Cell &c = it->cells[col]; const TreeItem::Cell &c = it->cells[col];
if (c.buttons.is_empty()) { if (c.buttons.is_empty()) {
return; return;
} }
int x_limit = get_size().width - theme_cache.panel_style->get_minimum_size().width + theme_cache.offset.x; // Search the button index.
if (v_scroll->is_visible_in_tree()) { FindColumnButtonResult result = _find_column_and_button_at_pos(pos.x, it, 0, x_limit);
x_limit -= v_scroll->get_minimum_size().width; r_index = result.button_index;
} }
for (int i = 0; i < col; i++) { Tree::FindColumnButtonResult Tree::_find_column_and_button_at_pos(int p_x, const TreeItem *p_item, int p_x_ofs, int p_x_limit) const {
const int col_w = get_column_width(i); int x = p_x;
pos.x -= col_w;
x_limit -= col_w;
}
int x_check; int col = -1;
if (cache.rtl) { int col_ofs = 0;
x_check = get_column_width(col); int col_width = 0;
} else {
// Right edge of the buttons area, relative to the start of the column. int limit_w = p_x_limit;
int buttons_area_min = 0;
if (col == 0) { FindColumnButtonResult result;
// Content of column 0 should take indentation into account.
for (TreeItem *current = it; current && (current != root || !hide_root); current = current->parent) { for (int i = 0; i < columns.size(); i++) {
buttons_area_min += theme_cache.item_margin; col_width = get_column_width(i);
if (p_item->cells[i].expand_right) {
int plus = 1;
while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].text.is_empty() && p_item->cells[i + plus].icon.is_null()) {
col_width += theme_cache.h_separation;
col_width += get_column_width(i + plus);
plus++;
} }
} }
for (int i = c.buttons.size() - 1; i >= 0; i--) {
Ref<Texture2D> b = c.buttons[i].texture; if (x > col_width) {
buttons_area_min += b->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin; col_ofs += col_width;
x -= col_width;
limit_w -= col_width;
continue;
} }
x_check = MAX(buttons_area_min, MIN(get_column_width(col), x_limit)); col = i;
break;
} }
for (int i = c.buttons.size() - 1; i >= 0; i--) { if (col >= 0) {
Ref<Texture2D> b = c.buttons[i].texture; if (col == 0) {
Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size(); int margin = p_x_ofs + theme_cache.item_margin;
if (pos.x > x_check - size.width) { col_width -= margin;
x_limit -= theme_cache.item_margin; limit_w -= margin;
r_index = i; col_ofs += margin;
return; x -= margin;
} else {
col_width -= theme_cache.h_separation;
limit_w -= theme_cache.h_separation;
x -= theme_cache.h_separation;
}
if (!cache.rtl && !p_item->cells[col].buttons.is_empty()) {
int button_w = 0;
for (int j = p_item->cells[col].buttons.size() - 1; j >= 0; j--) {
const Ref<Texture2D> &b = p_item->cells[col].buttons[j].texture;
button_w += b->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
}
col_width = MAX(button_w, MIN(limit_w, col_width));
}
// Cell button detection code.
// The first half of the button margin will be applied to the left button,
// and the other half to the right button.
const int offset_button_width = theme_cache.button_margin / 2;
const TreeItem::Cell &c = p_item->cells[col];
for (int j = c.buttons.size() - 1; j >= 0; j--) {
const Ref<Texture2D> &b = c.buttons[j].texture;
int w = b->get_size().width + theme_cache.button_pressed->get_minimum_size().width;
if (x >= col_width - w - offset_button_width - 1) {
result.button_index = j;
break;
}
col_width -= w + theme_cache.button_margin;
} }
x_check -= size.width + theme_cache.button_margin;
} }
result.column_index = col;
result.column_offset = col_ofs;
result.column_width = col_width;
result.pos_x = x;
return result;
} }
int Tree::get_column_at_position(const Point2 &p_pos) const { int Tree::get_column_at_position(const Point2 &p_pos) const {
@@ -5675,7 +5554,7 @@ int Tree::get_column_at_position(const Point2 &p_pos) const {
Point2 pos = p_pos; Point2 pos = p_pos;
if (is_layout_rtl()) { if (is_layout_rtl()) {
pos.x = get_size().width - pos.x; pos.x = get_size().width - pos.x - 1;
} }
pos -= theme_cache.panel_style->get_offset(); pos -= theme_cache.panel_style->get_offset();
pos.y -= _get_title_button_height(); pos.y -= _get_title_button_height();
@@ -5706,7 +5585,7 @@ int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
Point2 pos = p_pos; Point2 pos = p_pos;
if (is_layout_rtl()) { if (is_layout_rtl()) {
pos.x = get_size().width - pos.x; pos.x = get_size().width - pos.x - 1;
} }
pos -= theme_cache.panel_style->get_offset(); pos -= theme_cache.panel_style->get_offset();
pos.y -= _get_title_button_height(); pos.y -= _get_title_button_height();
@@ -5755,7 +5634,7 @@ TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
Point2 pos = p_pos; Point2 pos = p_pos;
if (is_layout_rtl()) { if (is_layout_rtl()) {
pos.x = get_size().width - pos.x; pos.x = get_size().width - pos.x - 1;
} }
pos -= theme_cache.panel_style->get_offset(); pos -= theme_cache.panel_style->get_offset();
pos.y -= _get_title_button_height(); pos.y -= _get_title_button_height();
@@ -5785,8 +5664,8 @@ int Tree::get_button_id_at_position(const Point2 &p_pos) const {
} }
TreeItem *it; TreeItem *it;
int col, index; int col, index, section;
_find_button_at_pos(p_pos, it, col, index); _find_button_at_pos(p_pos, it, col, index, section);
if (index == -1) { if (index == -1) {
return -1; return -1;
@@ -5802,8 +5681,8 @@ String Tree::get_tooltip(const Point2 &p_pos) const {
} }
TreeItem *it; TreeItem *it;
int col, index; int col, index, section;
_find_button_at_pos(p_pos, it, col, index); _find_button_at_pos(p_pos, it, col, index, section);
if (index != -1) { if (index != -1) {
return it->cells[col].buttons[index].tooltip; return it->cells[col].buttons[index].tooltip;

View File

@@ -670,9 +670,18 @@ private:
TreeItem *_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards = false); 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 &r_height, int &r_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; void _find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_column, int &r_index, int &r_section) const;
struct FindColumnButtonResult {
int column_index = -1;
int button_index = -1;
int column_width = -1;
int column_offset = -1;
int pos_x = -1;
};
FindColumnButtonResult _find_column_and_button_at_pos(int p_x, const TreeItem *p_item, int p_x_ofs, int p_x_limit) const;
/* float drag_speed; /* float drag_speed;
float drag_accum; float drag_accum;