Skip to content

Commit

Permalink
Fix getting wrong focus neighbor when the control is in `ScrollContai…
Browse files Browse the repository at this point in the history
…ner`

Exclude controls inside a `ScrollContainer` that are outside the visible area of ​​
the `ScrollContainer` when looking for focus neighbors.
  • Loading branch information
Rindbee committed Jan 2, 2025
1 parent 2582793 commit 6c89ffb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 39 deletions.
98 changes: 60 additions & 38 deletions scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "core/math/geometry_2d.h"
#include "core/os/os.h"
#include "core/string/translation_server.h"
#include "scene/gui/scroll_container.h"
#include "scene/main/canvas_layer.h"
#include "scene/main/window.h"
#include "scene/theme/theme_db.h"
Expand Down Expand Up @@ -2327,7 +2328,9 @@ Control *Control::_get_focus_neighbor(Side p_side, int p_count) {
return nullptr;
}

_window_find_focus_neighbor(vdir, base, points, maxd, dist, &result);
Rect2 clamp = Rect2(Point2(-1e7, -1e7), Size2(2e7, 2e7));

_window_find_focus_neighbor(vdir, base, points, clamp, maxd, dist, &result);

return result;
}
Expand All @@ -2336,14 +2339,14 @@ Control *Control::find_valid_focus_neighbor(Side p_side) const {
return const_cast<Control *>(this)->_get_focus_neighbor(p_side);
}

void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, const Point2 *p_points, real_t p_min, real_t &r_closest_dist, Control **r_closest) {
void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, const Point2 *p_points, Rect2 &p_clamp, real_t p_min, real_t &r_closest_dist, Control **r_closest) {
if (Object::cast_to<Viewport>(p_at)) {
return; //bye
return; // Bye.
}

Control *c = Object::cast_to<Control>(p_at);

if (c && c != this && c->get_focus_mode() == FOCUS_ALL && c->is_visible_in_tree()) {
if (c && c != this && c->get_focus_mode() == FOCUS_ALL && p_clamp.intersects(c->get_global_rect())) {
Point2 points[4];

Transform2D xform = c->get_global_transform();
Expand All @@ -2353,34 +2356,19 @@ void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, cons
points[2] = xform.xform(c->get_size());
points[3] = xform.xform(Point2(0, c->get_size().y));

// Tie-breaking aims to address situations where a potential focus neighbor's bounding rect
// is right next to the currently focused control (e.g. in BoxContainer with
// separation overridden to 0). This needs specific handling so that the correct
// focus neighbor is selected.

// Calculate centers of the potential neighbor, currently focused, and closest controls.
Point2 center = xform.xform(0.5 * c->get_size());
// We only have the points, not an actual reference.
Point2 p_center = 0.25 * (p_points[0] + p_points[1] + p_points[2] + p_points[3]);
Point2 closest_center;
bool should_tiebreak = false;
if (*r_closest != nullptr) {
should_tiebreak = true;
Control *closest = *r_closest;
Transform2D closest_xform = closest->get_global_transform();
closest_center = closest_xform.xform(0.5 * closest->get_size());
}

real_t min = 1e7;
// Use max to allow navigation to overlapping controls.
real_t max = -1e7;

for (int i = 0; i < 4; i++) {
real_t d = p_dir.dot(points[i]);
if (d < min) {
min = d;
if (d > max) {
max = d;
}
}

if (min > (p_min - CMP_EPSILON)) {
if (max > (p_min + CMP_EPSILON)) {
real_t min_d = 1e7;

for (int i = 0; i < 4; i++) {
Vector2 la = p_points[i];
Vector2 lb = p_points[(i + 1) % 4];
Expand All @@ -2391,28 +2379,62 @@ void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, cons

Vector2 pa, pb;
real_t d = Geometry2D::get_closest_points_between_segments(la, lb, fa, fb, pa, pb);
if (d < r_closest_dist) {
r_closest_dist = d;
*r_closest = c;
} else if (should_tiebreak && d == r_closest_dist) {
// Tie-break in favor of the control most aligned with p_dir.
if (p_dir.dot((center - p_center).normalized()) > p_dir.dot((closest_center - p_center).normalized())) {
r_closest_dist = d;
*r_closest = c;
}

if (d < min_d) {
min_d = d;
}
}
}

if (min_d < r_closest_dist || *r_closest == nullptr) {
r_closest_dist = min_d;
*r_closest = c;
} else if (min_d == r_closest_dist) {
// Tie-breaking aims to address situations where a potential focus neighbor's bounding rect
// is right next to the currently focused control (e.g. in BoxContainer with
// separation overridden to 0). This needs specific handling so that the correct
// focus neighbor is selected.

// Calculate centers of the potential neighbor, currently focused, and closest controls.
Point2 center = xform.xform(0.5 * c->get_size());
// We only have the points, not an actual reference.
Point2 p_center = 0.25 * (p_points[0] + p_points[1] + p_points[2] + p_points[3]);
Point2 closest_center = Point2();
Control *closest = *r_closest;
Transform2D closest_xform = closest->get_global_transform();
closest_center = closest_xform.xform(0.5 * closest->get_size());

// Tie-break in favor of the control most aligned with p_dir.
if (ABS(p_dir.cross(center - p_center)) < ABS(p_dir.cross(closest_center - p_center))) {
*r_closest = c;
}
}
}
}

ScrollContainer *sc = Object::cast_to<ScrollContainer>(c);
Rect2 intersection = p_clamp;
if (sc) {
if (!sc->is_following_focus() || !sc->is_ancestor_of(this)) {
intersection = p_clamp.intersection(sc->get_global_rect());
if (!intersection.has_area()) {
return;
}
}
}

for (int i = 0; i < p_at->get_child_count(); i++) {
Node *child = p_at->get_child(i);
Control *childc = Object::cast_to<Control>(child);
if (childc && childc->data.RI) {
continue; //subwindow, ignore
if (childc) {
if (childc->data.RI) {
continue; // Subwindow, ignore.
}
if (!childc->is_visible_in_tree()) {
continue; // Skip invisible node trees.
}
}
_window_find_focus_neighbor(p_dir, p_at->get_child(i), p_points, p_min, r_closest_dist, r_closest);
_window_find_focus_neighbor(p_dir, child, p_points, intersection, p_min, r_closest_dist, r_closest);
}
}

Expand Down
2 changes: 1 addition & 1 deletion scene/gui/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class Control : public CanvasItem {

// Focus.

void _window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, const Point2 *p_points, real_t p_min, real_t &r_closest_dist, Control **r_closest);
void _window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, const Point2 *p_points, Rect2 &p_clamp, real_t p_min, real_t &r_closest_dist, Control **r_closest);
Control *_get_focus_neighbor(Side p_side, int p_count = 0);

// Theming.
Expand Down

0 comments on commit 6c89ffb

Please sign in to comment.