summaryrefslogtreecommitdiff
path: root/scene/gui
diff options
context:
space:
mode:
authorTomasz Chabora <kobewi4e@gmail.com>2019-12-11 14:29:36 +0100
committerTomasz Chabora <kobewi4e@gmail.com>2019-12-11 14:29:36 +0100
commit77a865763333ec690746e48b1dd5b8288b87cbbe (patch)
tree2005d45db824ade7902a34c42c27e8129ecee8e3 /scene/gui
parentcd9d5132854e239726a08098d9029bdd00eee752 (diff)
Scroll ScrollContainer to focused children
Diffstat (limited to 'scene/gui')
-rw-r--r--scene/gui/scroll_container.cpp24
-rw-r--r--scene/gui/scroll_container.h1
2 files changed, 25 insertions, 0 deletions
diff --git a/scene/gui/scroll_container.cpp b/scene/gui/scroll_container.cpp
index fa23bf91dd..a882835aed 100644
--- a/scene/gui/scroll_container.cpp
+++ b/scene/gui/scroll_container.cpp
@@ -30,6 +30,7 @@
#include "scroll_container.h"
#include "core/os/os.h"
+#include "scene/main/viewport.h"
bool ScrollContainer::clips_input() const {
@@ -232,6 +233,23 @@ void ScrollContainer::_update_scrollbar_position() {
v_scroll->raise();
}
+void ScrollContainer::_ensure_focused_visible(Control *p_control) {
+
+ if (is_a_parent_of(p_control)) {
+ 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;
+ }
+
+ set_v_scroll(MAX(MIN(p_control->get_begin().y, get_v_scroll()), p_control->get_end().y - get_size().y + bottom_margin));
+ set_h_scroll(MAX(MIN(p_control->get_begin().x, get_h_scroll()), p_control->get_end().x - get_size().x + right_margin));
+ }
+}
+
void ScrollContainer::_notification(int p_what) {
if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
@@ -239,6 +257,11 @@ void ScrollContainer::_notification(int p_what) {
call_deferred("_update_scrollbar_position");
};
+ if (p_what == NOTIFICATION_READY) {
+
+ get_viewport()->connect("gui_focus_changed", this, "_ensure_focused_visible");
+ }
+
if (p_what == NOTIFICATION_SORT_CHILDREN) {
child_max_size = Size2(0, 0);
@@ -521,6 +544,7 @@ void ScrollContainer::_bind_methods() {
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);
diff --git a/scene/gui/scroll_container.h b/scene/gui/scroll_container.h
index 2ab169f4d0..1d247f14c6 100644
--- a/scene/gui/scroll_container.h
+++ b/scene/gui/scroll_container.h
@@ -75,6 +75,7 @@ protected:
static void _bind_methods();
void _update_scrollbar_position();
+ void _ensure_focused_visible(Control *p_node);
public:
int get_v_scroll() const;