Merge pull request #104715 from Andrewyuan34/add-auto-scroll

Add auto-scroll behavior when selecting text outside the visible area in RichTextLabel
This commit is contained in:
Thaddeus Crews 2025-10-03 12:01:05 -05:00
commit 71a485a1af
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
2 changed files with 132 additions and 80 deletions

View file

@ -2670,6 +2670,11 @@ void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
deselect();
}
}
if (!selection.drag_attempt) {
is_selecting_text = true;
click_select_held->start();
}
}
}
} else if (b->is_pressed() && b->is_double_click() && selection.enabled) {
@ -2751,6 +2756,9 @@ void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
}
}
}
is_selecting_text = false;
click_select_held->stop();
}
}
@ -2939,13 +2947,37 @@ void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseMotion> m = p_event;
if (m.is_valid()) {
local_mouse_pos = get_local_mouse_position();
last_clamped_mouse_pos = local_mouse_pos.clamp(Vector2(), get_size());
}
}
void RichTextLabel::_update_selection() {
ItemFrame *c_frame = nullptr;
int c_line = 0;
Item *c_item = nullptr;
int c_index = 0;
bool outside;
_find_click(main, m->get_position(), &c_frame, &c_line, &c_item, &c_index, &outside, false);
// Handle auto scrolling.
const Size2 size = get_size();
if (!(local_mouse_pos.x >= 0.0 && local_mouse_pos.y >= 0.0 &&
local_mouse_pos.x < size.x && local_mouse_pos.y < size.y)) {
real_t scroll_delta = 0.0;
if (local_mouse_pos.y < 0) {
scroll_delta = -auto_scroll_speed * (1 - (local_mouse_pos.y / 15.0));
} else if (local_mouse_pos.y > size.y) {
scroll_delta = auto_scroll_speed * (1 + (local_mouse_pos.y - size.y) / 15.0);
}
if (scroll_delta != 0.0) {
vscroll->scroll(scroll_delta);
queue_redraw();
}
}
// Update selection area.
_find_click(main, last_clamped_mouse_pos, &c_frame, &c_line, &c_item, &c_index, &outside, false);
if (selection.click_item && c_item) {
selection.from_frame = selection.click_frame;
selection.from_line = selection.click_line;
@ -3004,7 +3036,8 @@ void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
queue_redraw();
}
_find_click(main, m->get_position(), nullptr, nullptr, &c_item, nullptr, &outside, true);
// Update meta hovering.
_find_click(main, local_mouse_pos, nullptr, nullptr, &c_item, nullptr, &outside, true);
Variant meta;
ItemMeta *item_meta;
ItemMeta *prev_meta = meta_hovering;
@ -3028,7 +3061,6 @@ void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
queue_redraw();
}
}
}
}
String RichTextLabel::get_tooltip(const Point2 &p_pos) const {
@ -8076,6 +8108,11 @@ RichTextLabel::RichTextLabel(const String &p_text) {
parsing_bbcode.store(false);
set_clip_contents(true);
click_select_held = memnew(Timer);
add_child(click_select_held, false, INTERNAL_MODE_FRONT);
click_select_held->set_wait_time(0.05);
click_select_held->connect("timeout", callable_mp(this, &RichTextLabel::_update_selection));
}
RichTextLabel::~RichTextLabel() {

View file

@ -37,6 +37,10 @@
#include "scene/resources/image_texture.h"
#include "scene/resources/text_paragraph.h"
#ifdef TOOLS_ENABLED
#include "editor/themes/editor_scale.h"
#endif
class CharFXTransform;
class RichTextEffect;
@ -680,6 +684,17 @@ private:
void _scroll_changed(double);
int _find_first_line(int p_from, int p_to, int p_vofs) const;
#ifdef TOOLS_ENABLED
const real_t auto_scroll_speed = EDSCALE * 2.0f;
#else
const real_t auto_scroll_speed = 2.0f;
#endif
Vector2 last_clamped_mouse_pos;
Timer *click_select_held = nullptr;
Vector2 local_mouse_pos;
bool is_selecting_text = false;
void _update_selection();
_FORCE_INLINE_ float _calculate_line_vertical_offset(const Line &line) const;
virtual void gui_input(const Ref<InputEvent> &p_event) override;