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 Dec 31, 2024
1 parent 2582793 commit 5a74652
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 22 additions & 8 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
}

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 && c->is_visible_in_tree() && p_clamp.intersects(c->get_global_rect())) {
Point2 points[4];

Transform2D xform = c->get_global_transform();
Expand Down Expand Up @@ -2371,16 +2374,16 @@ void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, cons
closest_center = closest_xform.xform(0.5 * closest->get_size());
}

real_t min = 1e7;
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)) {
for (int i = 0; i < 4; i++) {
Vector2 la = p_points[i];
Vector2 lb = p_points[(i + 1) % 4];
Expand All @@ -2406,13 +2409,24 @@ void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, cons
}
}

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
}
_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, p_at->get_child(i), 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 5a74652

Please sign in to comment.