Browse Source

Merge pull request #62771 from bruvzg/line_edit_trim

Rémi Verschelde 3 years ago
parent
commit
fed1189099
3 changed files with 30 additions and 31 deletions
  1. 1 1
      doc/classes/LineEdit.xml
  2. 24 24
      scene/gui/line_edit.cpp
  3. 5 6
      scene/gui/line_edit.h

+ 1 - 1
doc/classes/LineEdit.xml

@@ -65,7 +65,7 @@
 			</description>
 			</description>
 		</method>
 		</method>
 		<method name="get_scroll_offset" qualifiers="const">
 		<method name="get_scroll_offset" qualifiers="const">
-			<return type="int" />
+			<return type="float" />
 			<description>
 			<description>
 				Returns the scroll offset due to [member caret_column], as a number of characters.
 				Returns the scroll offset due to [member caret_column], as a number of characters.
 			</description>
 			</description>

+ 24 - 24
scene/gui/line_edit.cpp

@@ -718,7 +718,7 @@ void LineEdit::_notification(int p_what) {
 
 
 		case NOTIFICATION_RESIZED: {
 		case NOTIFICATION_RESIZED: {
 			_fit_to_width();
 			_fit_to_width();
-			scroll_offset = 0;
+			scroll_offset = 0.0;
 			set_caret_column(get_caret_column());
 			set_caret_column(get_caret_column());
 		} break;
 		} break;
 
 
@@ -801,7 +801,7 @@ void LineEdit::_notification(int p_what) {
 					}
 					}
 				} break;
 				} break;
 				case HORIZONTAL_ALIGNMENT_CENTER: {
 				case HORIZONTAL_ALIGNMENT_CENTER: {
-					if (scroll_offset != 0) {
+					if (!Math::is_zero_approx(scroll_offset)) {
 						x_ofs = style->get_offset().x;
 						x_ofs = style->get_offset().x;
 					} else {
 					} else {
 						x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - (text_width)) / 2);
 						x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - (text_width)) / 2);
@@ -846,7 +846,7 @@ void LineEdit::_notification(int p_what) {
 				r_icon->draw(ci, Point2(width - r_icon->get_width() - style->get_margin(SIDE_RIGHT), height / 2 - r_icon->get_height() / 2), color_icon);
 				r_icon->draw(ci, Point2(width - r_icon->get_width() - style->get_margin(SIDE_RIGHT), height / 2 - r_icon->get_height() / 2), color_icon);
 
 
 				if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
 				if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
-					if (scroll_offset == 0) {
+					if (Math::is_zero_approx(scroll_offset)) {
 						x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
 						x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
 					}
 					}
 				} else {
 				} else {
@@ -1208,7 +1208,7 @@ void LineEdit::set_caret_at_pixel_pos(int p_x) {
 			}
 			}
 		} break;
 		} break;
 		case HORIZONTAL_ALIGNMENT_CENTER: {
 		case HORIZONTAL_ALIGNMENT_CENTER: {
-			if (scroll_offset != 0) {
+			if (!Math::is_zero_approx(scroll_offset)) {
 				x_ofs = style->get_offset().x;
 				x_ofs = style->get_offset().x;
 			} else {
 			} else {
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
@@ -1228,7 +1228,7 @@ void LineEdit::set_caret_at_pixel_pos(int p_x) {
 	if (right_icon.is_valid() || display_clear_icon) {
 	if (right_icon.is_valid() || display_clear_icon) {
 		Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon(SNAME("clear")) : right_icon;
 		Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon(SNAME("clear")) : right_icon;
 		if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
 		if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
-			if (scroll_offset == 0) {
+			if (Math::is_zero_approx(scroll_offset)) {
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
 			}
 			}
 		} else {
 		} else {
@@ -1236,11 +1236,11 @@ void LineEdit::set_caret_at_pixel_pos(int p_x) {
 		}
 		}
 	}
 	}
 
 
-	int ofs = TS->shaped_text_hit_test_position(text_rid, p_x - x_ofs - scroll_offset);
+	int ofs = ceil(TS->shaped_text_hit_test_position(text_rid, p_x - x_ofs - scroll_offset));
 	set_caret_column(ofs);
 	set_caret_column(ofs);
 }
 }
 
 
-Vector2i LineEdit::get_caret_pixel_pos() {
+Vector2 LineEdit::get_caret_pixel_pos() {
 	Ref<StyleBox> style = get_theme_stylebox(SNAME("normal"));
 	Ref<StyleBox> style = get_theme_stylebox(SNAME("normal"));
 	bool rtl = is_layout_rtl();
 	bool rtl = is_layout_rtl();
 
 
@@ -1256,7 +1256,7 @@ Vector2i LineEdit::get_caret_pixel_pos() {
 			}
 			}
 		} break;
 		} break;
 		case HORIZONTAL_ALIGNMENT_CENTER: {
 		case HORIZONTAL_ALIGNMENT_CENTER: {
-			if (scroll_offset != 0) {
+			if (!Math::is_zero_approx(scroll_offset)) {
 				x_ofs = style->get_offset().x;
 				x_ofs = style->get_offset().x;
 			} else {
 			} else {
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
@@ -1276,7 +1276,7 @@ Vector2i LineEdit::get_caret_pixel_pos() {
 	if (right_icon.is_valid() || display_clear_icon) {
 	if (right_icon.is_valid() || display_clear_icon) {
 		Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon(SNAME("clear")) : right_icon;
 		Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon(SNAME("clear")) : right_icon;
 		if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
 		if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
-			if (scroll_offset == 0) {
+			if (Math::is_zero_approx(scroll_offset)) {
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
 			}
 			}
 		} else {
 		} else {
@@ -1284,7 +1284,7 @@ Vector2i LineEdit::get_caret_pixel_pos() {
 		}
 		}
 	}
 	}
 
 
-	Vector2i ret;
+	Vector2 ret;
 	CaretInfo caret;
 	CaretInfo caret;
 	// Get position of the start of caret.
 	// Get position of the start of caret.
 	if (ime_text.length() != 0 && ime_selection.x != 0) {
 	if (ime_text.length() != 0 && ime_selection.x != 0) {
@@ -1427,7 +1427,7 @@ void LineEdit::set_text(String p_text) {
 
 
 	update();
 	update();
 	caret_column = 0;
 	caret_column = 0;
-	scroll_offset = 0;
+	scroll_offset = 0.0;
 }
 }
 
 
 void LineEdit::set_text_direction(Control::TextDirection p_text_direction) {
 void LineEdit::set_text_direction(Control::TextDirection p_text_direction) {
@@ -1555,7 +1555,7 @@ void LineEdit::set_caret_column(int p_column) {
 	// Fit to window.
 	// Fit to window.
 
 
 	if (!is_inside_tree()) {
 	if (!is_inside_tree()) {
-		scroll_offset = 0;
+		scroll_offset = 0.0;
 		return;
 		return;
 	}
 	}
 
 
@@ -1574,7 +1574,7 @@ void LineEdit::set_caret_column(int p_column) {
 			}
 			}
 		} break;
 		} break;
 		case HORIZONTAL_ALIGNMENT_CENTER: {
 		case HORIZONTAL_ALIGNMENT_CENTER: {
-			if (scroll_offset != 0) {
+			if (!Math::is_zero_approx(scroll_offset)) {
 				x_ofs = style->get_offset().x;
 				x_ofs = style->get_offset().x;
 			} else {
 			} else {
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
@@ -1595,7 +1595,7 @@ void LineEdit::set_caret_column(int p_column) {
 	if (right_icon.is_valid() || display_clear_icon) {
 	if (right_icon.is_valid() || display_clear_icon) {
 		Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon(SNAME("clear")) : right_icon;
 		Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon(SNAME("clear")) : right_icon;
 		if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
 		if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
-			if (scroll_offset == 0) {
+			if (Math::is_zero_approx(scroll_offset)) {
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
 				x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
 			}
 			}
 		} else {
 		} else {
@@ -1605,12 +1605,12 @@ void LineEdit::set_caret_column(int p_column) {
 	}
 	}
 
 
 	// Note: Use two coordinates to fit IME input range.
 	// Note: Use two coordinates to fit IME input range.
-	Vector2i primary_catret_offset = get_caret_pixel_pos();
+	Vector2 primary_caret_offset = get_caret_pixel_pos();
 
 
-	if (MIN(primary_catret_offset.x, primary_catret_offset.y) <= x_ofs) {
-		scroll_offset += (x_ofs - MIN(primary_catret_offset.x, primary_catret_offset.y));
-	} else if (MAX(primary_catret_offset.x, primary_catret_offset.y) >= ofs_max) {
-		scroll_offset += (ofs_max - MAX(primary_catret_offset.x, primary_catret_offset.y));
+	if (MIN(primary_caret_offset.x, primary_caret_offset.y) <= x_ofs) {
+		scroll_offset += x_ofs - MIN(primary_caret_offset.x, primary_caret_offset.y);
+	} else if (MAX(primary_caret_offset.x, primary_caret_offset.y) >= ofs_max) {
+		scroll_offset += ofs_max - MAX(primary_caret_offset.x, primary_caret_offset.y);
 	}
 	}
 	scroll_offset = MIN(0, scroll_offset);
 	scroll_offset = MIN(0, scroll_offset);
 
 
@@ -1621,14 +1621,14 @@ int LineEdit::get_caret_column() const {
 	return caret_column;
 	return caret_column;
 }
 }
 
 
-void LineEdit::set_scroll_offset(int p_pos) {
+void LineEdit::set_scroll_offset(float p_pos) {
 	scroll_offset = p_pos;
 	scroll_offset = p_pos;
-	if (scroll_offset < 0) {
-		scroll_offset = 0;
+	if (scroll_offset < 0.0) {
+		scroll_offset = 0.0;
 	}
 	}
 }
 }
 
 
-int LineEdit::get_scroll_offset() const {
+float LineEdit::get_scroll_offset() const {
 	return scroll_offset;
 	return scroll_offset;
 }
 }
 
 
@@ -1656,7 +1656,7 @@ void LineEdit::clear_internal() {
 	deselect();
 	deselect();
 	_clear_undo_stack();
 	_clear_undo_stack();
 	caret_column = 0;
 	caret_column = 0;
-	scroll_offset = 0;
+	scroll_offset = 0.0;
 	undo_text = "";
 	undo_text = "";
 	text = "";
 	text = "";
 	_shape();
 	_shape();

+ 5 - 6
scene/gui/line_edit.h

@@ -113,7 +113,7 @@ private:
 	bool caret_mid_grapheme_enabled = true;
 	bool caret_mid_grapheme_enabled = true;
 
 
 	int caret_column = 0;
 	int caret_column = 0;
-	int scroll_offset = 0;
+	float scroll_offset = 0.0;
 	int max_length = 0; // 0 for no maximum.
 	int max_length = 0; // 0 for no maximum.
 
 
 	String language;
 	String language;
@@ -153,8 +153,7 @@ private:
 
 
 	struct TextOperation {
 	struct TextOperation {
 		int caret_column = 0;
 		int caret_column = 0;
-		int scroll_offset = 0;
-		int cached_width = 0;
+		float scroll_offset = 0.0;
 		String text;
 		String text;
 	};
 	};
 	List<TextOperation> undo_stack;
 	List<TextOperation> undo_stack;
@@ -192,11 +191,11 @@ private:
 	void shift_selection_check_post(bool);
 	void shift_selection_check_post(bool);
 
 
 	void selection_fill_at_caret();
 	void selection_fill_at_caret();
-	void set_scroll_offset(int p_pos);
-	int get_scroll_offset() const;
+	void set_scroll_offset(float p_pos);
+	float get_scroll_offset() const;
 
 
 	void set_caret_at_pixel_pos(int p_x);
 	void set_caret_at_pixel_pos(int p_x);
-	Vector2i get_caret_pixel_pos();
+	Vector2 get_caret_pixel_pos();
 
 
 	void _reset_caret_blink_timer();
 	void _reset_caret_blink_timer();
 	void _toggle_draw_caret();
 	void _toggle_draw_caret();