Skip to content

Commit 45a564f

Browse files
committed
[RTL] Add option to scroll follow visible characters.
1 parent d7cc121 commit 45a564f

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

doc/classes/RichTextLabel.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,9 @@
729729
<member name="scroll_following" type="bool" setter="set_scroll_follow" getter="is_scroll_following" default="false">
730730
If [code]true[/code], the window scrolls down to display new content automatically.
731731
</member>
732+
<member name="scroll_following_visible_characters" type="bool" setter="set_scroll_follow_visible_characters" getter="is_scroll_following_visible_characters" default="false">
733+
If [code]true[/code], the window scrolls to display the last visible line when [member visible_characters] or [member visible_ratio] is changed.
734+
</member>
732735
<member name="selection_enabled" type="bool" setter="set_selection_enabled" getter="is_selection_enabled" default="false">
733736
If [code]true[/code], the label allows text selection.
734737
</member>

scene/gui/rich_text_label.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2516,6 +2516,9 @@ void RichTextLabel::_notification(int p_what) {
25162516
ofs.y += main->lines[from_line].text_buf->get_size().y + (main->lines[from_line].text_buf->get_line_count() - 1) * (theme_cache.line_separation + vsep) + (theme_cache.paragraph_separation + vsep);
25172517
from_line++;
25182518
}
2519+
if (scroll_follow_visible_characters && scroll_active) {
2520+
vscroll->set_visible(follow_vc_pos > 0);
2521+
}
25192522
if (has_focus() && get_tree()->is_accessibility_enabled()) {
25202523
RID ae;
25212524
if (keyboard_focus_frame && keyboard_focus_item) {
@@ -5019,6 +5022,31 @@ bool RichTextLabel::is_scroll_following() const {
50195022
return scroll_follow;
50205023
}
50215024

5025+
void RichTextLabel::_update_follow_vc() {
5026+
if (!scroll_follow_visible_characters) {
5027+
return;
5028+
}
5029+
int vc = (visible_characters < 0 ? get_total_character_count() : MIN(visible_characters, get_total_character_count())) - 1;
5030+
int voff = get_character_line(vc) + 1;
5031+
if (voff <= get_line_count() - 1) {
5032+
follow_vc_pos = get_line_offset(voff) - _get_text_rect().size.y;
5033+
} else {
5034+
follow_vc_pos = vscroll->get_max();
5035+
}
5036+
vscroll->scroll_to(follow_vc_pos);
5037+
}
5038+
5039+
void RichTextLabel::set_scroll_follow_visible_characters(bool p_follow) {
5040+
if (scroll_follow_visible_characters != p_follow) {
5041+
scroll_follow_visible_characters = p_follow;
5042+
_update_follow_vc();
5043+
}
5044+
}
5045+
5046+
bool RichTextLabel::is_scroll_following_visible_characters() const {
5047+
return scroll_follow_visible_characters;
5048+
}
5049+
50225050
void RichTextLabel::parse_bbcode(const String &p_bbcode) {
50235051
clear();
50245052
append_text(p_bbcode);
@@ -7207,6 +7235,7 @@ void RichTextLabel::set_visible_ratio(float p_ratio) {
72077235
total_height = _calculate_line_vertical_offset(main->lines[i]);
72087236
}
72097237
}
7238+
_update_follow_vc();
72107239
queue_redraw();
72117240
}
72127241
}
@@ -7397,6 +7426,9 @@ void RichTextLabel::_bind_methods() {
73977426
ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active);
73987427
ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active);
73997428

7429+
ClassDB::bind_method(D_METHOD("set_scroll_follow_visible_characters", "follow"), &RichTextLabel::set_scroll_follow_visible_characters);
7430+
ClassDB::bind_method(D_METHOD("is_scroll_following_visible_characters"), &RichTextLabel::is_scroll_following_visible_characters);
7431+
74007432
ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow);
74017433
ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following);
74027434

@@ -7501,6 +7533,7 @@ void RichTextLabel::_bind_methods() {
75017533
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content"), "set_fit_content", "is_fit_content_enabled");
75027534
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
75037535
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
7536+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following_visible_characters"), "set_scroll_follow_visible_characters", "is_scroll_following_visible_characters");
75047537
ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
75057538
ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_trim_flags", PROPERTY_HINT_FLAGS, vformat("Trim Spaces After Break:%d,Trim Spaces Before Break:%d", TextServer::BREAK_TRIM_START_EDGE_SPACES, TextServer::BREAK_TRIM_END_EDGE_SPACES)), "set_autowrap_trim_flags", "get_autowrap_trim_flags");
75067539
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_size", PROPERTY_HINT_RANGE, "0,24,1"), "set_tab_size", "get_tab_size");
@@ -7682,6 +7715,7 @@ void RichTextLabel::set_visible_characters(int p_visible) {
76827715
total_height = _calculate_line_vertical_offset(main->lines[i]);
76837716
}
76847717
}
7718+
_update_follow_vc();
76857719
queue_redraw();
76867720
}
76877721
}

scene/gui/rich_text_label.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ class RichTextLabel : public Control {
527527

528528
bool scroll_visible = false;
529529
bool scroll_follow = false;
530+
bool scroll_follow_visible_characters = false;
531+
int follow_vc_pos = 0;
530532
bool scroll_following = false;
531533
bool scroll_active = true;
532534
int scroll_w = 0;
@@ -554,6 +556,7 @@ class RichTextLabel : public Control {
554556

555557
HashMap<RID, Rect2> ac_element_bounds_cache;
556558

559+
void _update_follow_vc();
557560
void _invalidate_accessibility();
558561
void _invalidate_current_line(ItemFrame *p_frame);
559562

@@ -839,6 +842,9 @@ class RichTextLabel : public Control {
839842
void set_scroll_follow(bool p_follow);
840843
bool is_scroll_following() const;
841844

845+
void set_scroll_follow_visible_characters(bool p_follow);
846+
bool is_scroll_following_visible_characters() const;
847+
842848
void set_tab_size(int p_spaces);
843849
int get_tab_size() const;
844850

0 commit comments

Comments
 (0)