|
@@ -230,28 +230,25 @@ void ScrollContainer::_update_scrollbar_position() {
|
|
|
v_scroll->raise();
|
|
|
}
|
|
|
|
|
|
-void ScrollContainer::_ensure_focused_visible(Control *p_control) {
|
|
|
- if (!follow_focus) {
|
|
|
- return;
|
|
|
+void ScrollContainer::_gui_focus_changed(Control *p_control) {
|
|
|
+ if (follow_focus && is_a_parent_of(p_control)) {
|
|
|
+ ensure_control_visible(p_control);
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- if (is_a_parent_of(p_control)) {
|
|
|
- Rect2 global_rect = get_global_rect();
|
|
|
- Rect2 other_rect = p_control->get_global_rect();
|
|
|
- float right_margin = 0;
|
|
|
- if (v_scroll->is_visible()) {
|
|
|
- right_margin += v_scroll->get_size().x;
|
|
|
- }
|
|
|
- float bottom_margin = 0;
|
|
|
- if (h_scroll->is_visible()) {
|
|
|
- bottom_margin += h_scroll->get_size().y;
|
|
|
- }
|
|
|
+void ScrollContainer::ensure_control_visible(Control *p_control) {
|
|
|
+ ERR_FAIL_COND_MSG(!is_a_parent_of(p_control), "Must be a parent of the control.");
|
|
|
|
|
|
- float diff = MAX(MIN(other_rect.position.y, global_rect.position.y), other_rect.position.y + other_rect.size.y - global_rect.size.y + bottom_margin);
|
|
|
- set_v_scroll(get_v_scroll() + (diff - global_rect.position.y));
|
|
|
- diff = MAX(MIN(other_rect.position.x, global_rect.position.x), other_rect.position.x + other_rect.size.x - global_rect.size.x + right_margin);
|
|
|
- set_h_scroll(get_h_scroll() + (diff - global_rect.position.x));
|
|
|
- }
|
|
|
+ Rect2 global_rect = get_global_rect();
|
|
|
+ Rect2 other_rect = p_control->get_global_rect();
|
|
|
+ float right_margin = v_scroll->is_visible() ? v_scroll->get_size().x : 0.0f;
|
|
|
+ float bottom_margin = h_scroll->is_visible() ? h_scroll->get_size().y : 0.0f;
|
|
|
+
|
|
|
+ Vector2 diff = Vector2(MAX(MIN(other_rect.position.x, global_rect.position.x), other_rect.position.x + other_rect.size.x - global_rect.size.x + right_margin),
|
|
|
+ MAX(MIN(other_rect.position.y, global_rect.position.y), other_rect.position.y + other_rect.size.y - global_rect.size.y + bottom_margin));
|
|
|
+
|
|
|
+ set_h_scroll(get_h_scroll() + (diff.x - global_rect.position.x));
|
|
|
+ set_v_scroll(get_v_scroll() + (diff.y - global_rect.position.y));
|
|
|
}
|
|
|
|
|
|
void ScrollContainer::_notification(int p_what) {
|
|
@@ -260,7 +257,7 @@ void ScrollContainer::_notification(int p_what) {
|
|
|
};
|
|
|
|
|
|
if (p_what == NOTIFICATION_READY) {
|
|
|
- get_viewport()->connect("gui_focus_changed", this, "_ensure_focused_visible");
|
|
|
+ get_viewport()->connect("gui_focus_changed", this, "_gui_focus_changed");
|
|
|
}
|
|
|
|
|
|
if (p_what == NOTIFICATION_SORT_CHILDREN) {
|
|
@@ -560,12 +557,12 @@ VScrollBar *ScrollContainer::get_v_scrollbar() {
|
|
|
void ScrollContainer::_bind_methods() {
|
|
|
ClassDB::bind_method(D_METHOD("_scroll_moved"), &ScrollContainer::_scroll_moved);
|
|
|
ClassDB::bind_method(D_METHOD("_gui_input"), &ScrollContainer::_gui_input);
|
|
|
+ ClassDB::bind_method(D_METHOD("_gui_focus_changed"), &ScrollContainer::_gui_focus_changed);
|
|
|
ClassDB::bind_method(D_METHOD("set_enable_h_scroll", "enable"), &ScrollContainer::set_enable_h_scroll);
|
|
|
ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &ScrollContainer::is_h_scroll_enabled);
|
|
|
ClassDB::bind_method(D_METHOD("set_enable_v_scroll", "enable"), &ScrollContainer::set_enable_v_scroll);
|
|
|
ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &ScrollContainer::is_v_scroll_enabled);
|
|
|
ClassDB::bind_method(D_METHOD("_update_scrollbar_position"), &ScrollContainer::_update_scrollbar_position);
|
|
|
- ClassDB::bind_method(D_METHOD("_ensure_focused_visible"), &ScrollContainer::_ensure_focused_visible);
|
|
|
ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
|
|
|
ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
|
|
|
ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
|
|
@@ -577,6 +574,7 @@ void ScrollContainer::_bind_methods() {
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_h_scrollbar"), &ScrollContainer::get_h_scrollbar);
|
|
|
ClassDB::bind_method(D_METHOD("get_v_scrollbar"), &ScrollContainer::get_v_scrollbar);
|
|
|
+ ClassDB::bind_method(D_METHOD("ensure_control_visible", "control"), &ScrollContainer::ensure_control_visible);
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("scroll_started"));
|
|
|
ADD_SIGNAL(MethodInfo("scroll_ended"));
|