diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 0f54d6a2b60..aec992ba5e1 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -1233,7 +1233,9 @@ void LineEdit::_notification(int p_what) { if (!Math::is_zero_approx(scroll_offset)) { x_ofs = style->get_offset().x; } else { - x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - (text_width)) / 2); + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int centered = int((size.width - total_margin - text_width)) / 2; + x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered); } } break; case HORIZONTAL_ALIGNMENT_RIGHT: { @@ -1249,7 +1251,9 @@ void LineEdit::_notification(int p_what) { Ref r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon; if (alignment == HORIZONTAL_ALIGNMENT_CENTER) { 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); + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int center = int(size.width - total_margin - text_width - r_icon->get_width()) / 2; + x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center); } } else { x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT)); @@ -1355,7 +1359,9 @@ void LineEdit::_notification(int p_what) { if (!Math::is_zero_approx(scroll_offset)) { x_ofs = style->get_offset().x; } else { - x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - (text_width)) / 2); + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int centered = int((size.width - total_margin - text_width)) / 2; + x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered); } } break; case HORIZONTAL_ALIGNMENT_RIGHT: { @@ -1403,7 +1409,9 @@ void LineEdit::_notification(int p_what) { if (alignment == HORIZONTAL_ALIGNMENT_CENTER) { 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); + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int center = int(size.width - total_margin - text_width - r_icon->get_width()) / 2; + x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center); } } else { x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT)); @@ -1496,7 +1504,14 @@ void LineEdit::_notification(int p_what) { } } break; case HORIZONTAL_ALIGNMENT_CENTER: { - caret.l_caret = Rect2(Vector2(size.x / 2, y), Size2(caret_width, h)); + int icon_width = 0; + if (right_icon.is_valid()) { + icon_width = right_icon->get_width(); + } + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int center = int(size.width - total_margin - icon_width) / 2; + + caret.l_caret = Rect2(Vector2(style->get_margin(SIDE_LEFT) + MAX(0, center), y), Size2(caret_width, h)); } break; case HORIZONTAL_ALIGNMENT_RIGHT: { if (rtl) { @@ -1780,7 +1795,9 @@ void LineEdit::set_caret_at_pixel_pos(int p_x) { if (!Math::is_zero_approx(scroll_offset)) { x_ofs = style->get_offset().x; } else { - x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2); + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int centered = int((get_size().width - total_margin - text_width)) / 2; + x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered); } } break; case HORIZONTAL_ALIGNMENT_RIGHT: { @@ -1798,7 +1815,9 @@ void LineEdit::set_caret_at_pixel_pos(int p_x) { Ref r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon; if (alignment == HORIZONTAL_ALIGNMENT_CENTER) { 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); + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int center = int(get_size().width - total_margin - text_width - r_icon->get_width()) / 2; + x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center); } } else { x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT)); @@ -1831,7 +1850,9 @@ Vector2 LineEdit::get_caret_pixel_pos() { if (!Math::is_zero_approx(scroll_offset)) { x_ofs = style->get_offset().x; } else { - x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2); + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int centered = int((get_size().width - total_margin - text_width)) / 2; + x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered); } } break; case HORIZONTAL_ALIGNMENT_RIGHT: { @@ -1849,7 +1870,9 @@ Vector2 LineEdit::get_caret_pixel_pos() { Ref r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon; if (alignment == HORIZONTAL_ALIGNMENT_CENTER) { 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); + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int center = int(get_size().width - total_margin - text_width - r_icon->get_width()) / 2; + x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center); } } else { x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT)); @@ -2182,7 +2205,9 @@ void LineEdit::set_caret_column(int p_column) { if (!Math::is_zero_approx(scroll_offset)) { x_ofs = style->get_offset().x; } else { - x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2); + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int centered = int((get_size().width - total_margin - text_width)) / 2; + x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered); } } break; case HORIZONTAL_ALIGNMENT_RIGHT: { @@ -2201,7 +2226,9 @@ void LineEdit::set_caret_column(int p_column) { Ref r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon; if (alignment == HORIZONTAL_ALIGNMENT_CENTER) { 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); + int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); + int center = int(get_size().width - total_margin - text_width - r_icon->get_width()) / 2; + x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center); } } else { x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));