From 4ca83c635ffa8f868721affb301a8c8e91f7c5d0 Mon Sep 17 00:00:00 2001 From: Pawel Kowal Date: Sat, 17 Sep 2016 21:29:55 +0200 Subject: [PATCH] Add scrolling to Tree control in Drag and Drop mode (cherry picked from commit 9e5aaa27bc48bcba7392febeb583b4959a826c9e) --- scene/gui/tree.cpp | 31 +++++++++++++++++++ scene/gui/tree.h | 3 ++ .../resources/default_theme/default_theme.cpp | 2 ++ 3 files changed, 36 insertions(+) diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index 76c133b0b9..b8cc3786a7 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -32,6 +32,7 @@ #include "os/keyboard.h" #include "globals.h" #include "os/input.h" +#include "scene/main/viewport.h" @@ -814,6 +815,8 @@ void Tree::update_cache() { cache.guide_width=get_constant("guide_width"); cache.draw_relationship_lines=get_constant("draw_relationship_lines"); cache.relationship_line_color=get_color("relationship_line_color"); + cache.scroll_border=get_constant("scroll_border"); + cache.scroll_speed=get_constant("scroll_speed"); cache.title_button = get_stylebox("title_button_normal"); cache.title_button_pressed = get_stylebox("title_button_pressed"); @@ -2642,11 +2645,17 @@ void Tree::_notification(int p_what) { if (p_what==NOTIFICATION_DRAG_END) { drop_mode_flags=0; + scrolling = false; + set_fixed_process(false); update(); } if (p_what==NOTIFICATION_DRAG_BEGIN) { single_select_defer=NULL; + if (cache.scroll_speed > 0 && get_rect().has_point(get_viewport()->get_mouse_pos() - get_global_pos())) { + scrolling = true; + set_fixed_process(true); + } } if (p_what==NOTIFICATION_FIXED_PROCESS) { @@ -2692,6 +2701,28 @@ void Tree::_notification(int p_what) { } } + + if (scrolling) { + Point2 point = get_viewport()->get_mouse_pos() - get_global_pos(); + if (point.x < cache.scroll_border) { + point.x -= cache.scroll_border; + } else if (point.x > get_size().width - cache.scroll_border) { + point.x -= get_size().width - cache.scroll_border; + } else { + point.x = 0; + } + if (point.y < cache.scroll_border) { + point.y -= cache.scroll_border; + } else if (point.y > get_size().height - cache.scroll_border) { + point.y -= get_size().height - cache.scroll_border; + } else { + point.y = 0; + } + point *= cache.scroll_speed * get_fixed_process_delta_time(); + point += get_scroll(); + h_scroll->set_val(point.x); + v_scroll->set_val(point.y); + } } if (p_what==NOTIFICATION_DRAW) { diff --git a/scene/gui/tree.h b/scene/gui/tree.h index f5100ab5b6..06a07d7479 100644 --- a/scene/gui/tree.h +++ b/scene/gui/tree.h @@ -386,6 +386,8 @@ friend class TreeItem; int button_margin; Point2 offset; int draw_relationship_lines; + int scroll_border; + int scroll_speed; enum ClickType { CLICK_NONE, @@ -441,6 +443,7 @@ friend class TreeItem; bool drag_touching_deaccel; bool click_handled; bool allow_rmb_select; + bool scrolling; bool force_select_on_already_selected; diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 499cf0a169..6318963781 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -687,6 +687,8 @@ void fill_default_theme(Ref& t,const Ref & default_font,const Refset_constant("item_margin","Tree",12 *scale); t->set_constant("button_margin","Tree",4 *scale); t->set_constant("draw_relationship_lines", "Tree", 0); + t->set_constant("scroll_border", "Tree", 4); + t->set_constant("scroll_speed", "Tree", 12); // ItemList