2
0
Эх сурвалжийг харах

Merge pull request #108399 from bruvzg/rtl_vflscr

[RTL] Add option to scroll follow visible characters.
Thaddeus Crews 2 долоо хоног өмнө
parent
commit
d36f394714

+ 3 - 0
doc/classes/RichTextLabel.xml

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

+ 34 - 0
scene/gui/rich_text_label.cpp

@@ -2539,6 +2539,9 @@ void RichTextLabel::_notification(int p_what) {
 				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);
 				from_line++;
 			}
+			if (scroll_follow_visible_characters && scroll_active) {
+				vscroll->set_visible(follow_vc_pos > 0);
+			}
 			if (has_focus() && get_tree()->is_accessibility_enabled()) {
 				RID ae;
 				if (keyboard_focus_frame && keyboard_focus_item) {
@@ -5042,6 +5045,31 @@ bool RichTextLabel::is_scroll_following() const {
 	return scroll_follow;
 }
 
+void RichTextLabel::_update_follow_vc() {
+	if (!scroll_follow_visible_characters) {
+		return;
+	}
+	int vc = (visible_characters < 0 ? get_total_character_count() : MIN(visible_characters, get_total_character_count())) - 1;
+	int voff = get_character_line(vc) + 1;
+	if (voff <= get_line_count() - 1) {
+		follow_vc_pos = get_line_offset(voff) - _get_text_rect().size.y;
+	} else {
+		follow_vc_pos = vscroll->get_max();
+	}
+	vscroll->scroll_to(follow_vc_pos);
+}
+
+void RichTextLabel::set_scroll_follow_visible_characters(bool p_follow) {
+	if (scroll_follow_visible_characters != p_follow) {
+		scroll_follow_visible_characters = p_follow;
+		_update_follow_vc();
+	}
+}
+
+bool RichTextLabel::is_scroll_following_visible_characters() const {
+	return scroll_follow_visible_characters;
+}
+
 void RichTextLabel::parse_bbcode(const String &p_bbcode) {
 	clear();
 	append_text(p_bbcode);
@@ -7230,6 +7258,7 @@ void RichTextLabel::set_visible_ratio(float p_ratio) {
 				total_height = _calculate_line_vertical_offset(main->lines[i]);
 			}
 		}
+		_update_follow_vc();
 		queue_redraw();
 	}
 }
@@ -7424,6 +7453,9 @@ void RichTextLabel::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active);
 	ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active);
 
+	ClassDB::bind_method(D_METHOD("set_scroll_follow_visible_characters", "follow"), &RichTextLabel::set_scroll_follow_visible_characters);
+	ClassDB::bind_method(D_METHOD("is_scroll_following_visible_characters"), &RichTextLabel::is_scroll_following_visible_characters);
+
 	ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow);
 	ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following);
 
@@ -7530,6 +7562,7 @@ void RichTextLabel::_bind_methods() {
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content"), "set_fit_content", "is_fit_content_enabled");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
+	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following_visible_characters"), "set_scroll_follow_visible_characters", "is_scroll_following_visible_characters");
 	ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
 	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");
 	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) {
 				total_height = _calculate_line_vertical_offset(main->lines[i]);
 			}
 		}
+		_update_follow_vc();
 		queue_redraw();
 	}
 }

+ 6 - 0
scene/gui/rich_text_label.h

@@ -527,6 +527,8 @@ private:
 
 	bool scroll_visible = false;
 	bool scroll_follow = false;
+	bool scroll_follow_visible_characters = false;
+	int follow_vc_pos = 0;
 	bool scroll_following = false;
 	bool scroll_active = true;
 	int scroll_w = 0;
@@ -555,6 +557,7 @@ private:
 
 	HashMap<RID, Rect2> ac_element_bounds_cache;
 
+	void _update_follow_vc();
 	void _invalidate_accessibility();
 	void _invalidate_current_line(ItemFrame *p_frame);
 
@@ -840,6 +843,9 @@ public:
 	void set_scroll_follow(bool p_follow);
 	bool is_scroll_following() const;
 
+	void set_scroll_follow_visible_characters(bool p_follow);
+	bool is_scroll_following_visible_characters() const;
+
 	void set_tab_size(int p_spaces);
 	int get_tab_size() const;