Skip to content

Commit d36f394

Browse files
committed
Merge pull request #108399 from bruvzg/rtl_vflscr
[RTL] Add option to scroll follow visible characters.
2 parents c977b59 + 45a564f commit d36f394

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
@@ -766,6 +766,9 @@
766766
<member name="scroll_following" type="bool" setter="set_scroll_follow" getter="is_scroll_following" default="false">
767767
If [code]true[/code], the window scrolls down to display new content automatically.
768768
</member>
769+
<member name="scroll_following_visible_characters" type="bool" setter="set_scroll_follow_visible_characters" getter="is_scroll_following_visible_characters" default="false">
770+
If [code]true[/code], the window scrolls to display the last visible line when [member visible_characters] or [member visible_ratio] is changed.
771+
</member>
769772
<member name="selection_enabled" type="bool" setter="set_selection_enabled" getter="is_selection_enabled" default="false">
770773
If [code]true[/code], the label allows text selection.
771774
</member>

scene/gui/rich_text_label.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,6 +2539,9 @@ void RichTextLabel::_notification(int p_what) {
25392539
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);
25402540
from_line++;
25412541
}
2542+
if (scroll_follow_visible_characters && scroll_active) {
2543+
vscroll->set_visible(follow_vc_pos > 0);
2544+
}
25422545
if (has_focus() && get_tree()->is_accessibility_enabled()) {
25432546
RID ae;
25442547
if (keyboard_focus_frame && keyboard_focus_item) {
@@ -5042,6 +5045,31 @@ bool RichTextLabel::is_scroll_following() const {
50425045
return scroll_follow;
50435046
}
50445047

5048+
void RichTextLabel::_update_follow_vc() {
5049+
if (!scroll_follow_visible_characters) {
5050+
return;
5051+
}
5052+
int vc = (visible_characters < 0 ? get_total_character_count() : MIN(visible_characters, get_total_character_count())) - 1;
5053+
int voff = get_character_line(vc) + 1;
5054+
if (voff <= get_line_count() - 1) {
5055+
follow_vc_pos = get_line_offset(voff) - _get_text_rect().size.y;
5056+
} else {
5057+
follow_vc_pos = vscroll->get_max();
5058+
}
5059+
vscroll->scroll_to(follow_vc_pos);
5060+
}
5061+
5062+
void RichTextLabel::set_scroll_follow_visible_characters(bool p_follow) {
5063+
if (scroll_follow_visible_characters != p_follow) {
5064+
scroll_follow_visible_characters = p_follow;
5065+
_update_follow_vc();
5066+
}
5067+
}
5068+
5069+
bool RichTextLabel::is_scroll_following_visible_characters() const {
5070+
return scroll_follow_visible_characters;
5071+
}
5072+
50455073
void RichTextLabel::parse_bbcode(const String &p_bbcode) {
50465074
clear();
50475075
append_text(p_bbcode);
@@ -7230,6 +7258,7 @@ void RichTextLabel::set_visible_ratio(float p_ratio) {
72307258
total_height = _calculate_line_vertical_offset(main->lines[i]);
72317259
}
72327260
}
7261+
_update_follow_vc();
72337262
queue_redraw();
72347263
}
72357264
}
@@ -7424,6 +7453,9 @@ void RichTextLabel::_bind_methods() {
74247453
ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active);
74257454
ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active);
74267455

7456+
ClassDB::bind_method(D_METHOD("set_scroll_follow_visible_characters", "follow"), &RichTextLabel::set_scroll_follow_visible_characters);
7457+
ClassDB::bind_method(D_METHOD("is_scroll_following_visible_characters"), &RichTextLabel::is_scroll_following_visible_characters);
7458+
74277459
ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow);
74287460
ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following);
74297461

@@ -7530,6 +7562,7 @@ void RichTextLabel::_bind_methods() {
75307562
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content"), "set_fit_content", "is_fit_content_enabled");
75317563
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
75327564
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
7565+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following_visible_characters"), "set_scroll_follow_visible_characters", "is_scroll_following_visible_characters");
75337566
ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
75347567
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");
75357568
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_size", PROPERTY_HINT_RANGE, "0,24,1"), "set_tab_size", "get_tab_size");
@@ -7711,6 +7744,7 @@ void RichTextLabel::set_visible_characters(int p_visible) {
77117744
total_height = _calculate_line_vertical_offset(main->lines[i]);
77127745
}
77137746
}
7747+
_update_follow_vc();
77147748
queue_redraw();
77157749
}
77167750
}

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;
@@ -555,6 +557,7 @@ class RichTextLabel : public Control {
555557

556558
HashMap<RID, Rect2> ac_element_bounds_cache;
557559

560+
void _update_follow_vc();
558561
void _invalidate_accessibility();
559562
void _invalidate_current_line(ItemFrame *p_frame);
560563

@@ -840,6 +843,9 @@ class RichTextLabel : public Control {
840843
void set_scroll_follow(bool p_follow);
841844
bool is_scroll_following() const;
842845

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

0 commit comments

Comments
 (0)