Skip to content

Commit a08ebf9

Browse files
committed
Merge pull request #109329 from Giganzo/line_edit_center
Fix LineEdit center alignment
2 parents 5f402da + e3232b2 commit a08ebf9

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

scene/gui/line_edit.cpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,9 @@ void LineEdit::_notification(int p_what) {
12331233
if (!Math::is_zero_approx(scroll_offset)) {
12341234
x_ofs = style->get_offset().x;
12351235
} else {
1236-
x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - (text_width)) / 2);
1236+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
1237+
int centered = int((size.width - total_margin - text_width)) / 2;
1238+
x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
12371239
}
12381240
} break;
12391241
case HORIZONTAL_ALIGNMENT_RIGHT: {
@@ -1249,7 +1251,9 @@ void LineEdit::_notification(int p_what) {
12491251
Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
12501252
if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
12511253
if (Math::is_zero_approx(scroll_offset)) {
1252-
x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
1254+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
1255+
int center = int(size.width - total_margin - text_width - r_icon->get_width()) / 2;
1256+
x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
12531257
}
12541258
} else {
12551259
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) {
13551359
if (!Math::is_zero_approx(scroll_offset)) {
13561360
x_ofs = style->get_offset().x;
13571361
} else {
1358-
x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - (text_width)) / 2);
1362+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
1363+
int centered = int((size.width - total_margin - text_width)) / 2;
1364+
x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
13591365
}
13601366
} break;
13611367
case HORIZONTAL_ALIGNMENT_RIGHT: {
@@ -1403,7 +1409,9 @@ void LineEdit::_notification(int p_what) {
14031409

14041410
if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
14051411
if (Math::is_zero_approx(scroll_offset)) {
1406-
x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
1412+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
1413+
int center = int(size.width - total_margin - text_width - r_icon->get_width()) / 2;
1414+
x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
14071415
}
14081416
} else {
14091417
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) {
14961504
}
14971505
} break;
14981506
case HORIZONTAL_ALIGNMENT_CENTER: {
1499-
caret.l_caret = Rect2(Vector2(size.x / 2, y), Size2(caret_width, h));
1507+
int icon_width = 0;
1508+
if (right_icon.is_valid()) {
1509+
icon_width = right_icon->get_width();
1510+
}
1511+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
1512+
int center = int(size.width - total_margin - icon_width) / 2;
1513+
1514+
caret.l_caret = Rect2(Vector2(style->get_margin(SIDE_LEFT) + MAX(0, center), y), Size2(caret_width, h));
15001515
} break;
15011516
case HORIZONTAL_ALIGNMENT_RIGHT: {
15021517
if (rtl) {
@@ -1780,7 +1795,9 @@ void LineEdit::set_caret_at_pixel_pos(int p_x) {
17801795
if (!Math::is_zero_approx(scroll_offset)) {
17811796
x_ofs = style->get_offset().x;
17821797
} else {
1783-
x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
1798+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
1799+
int centered = int((get_size().width - total_margin - text_width)) / 2;
1800+
x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
17841801
}
17851802
} break;
17861803
case HORIZONTAL_ALIGNMENT_RIGHT: {
@@ -1798,7 +1815,9 @@ void LineEdit::set_caret_at_pixel_pos(int p_x) {
17981815
Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
17991816
if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
18001817
if (Math::is_zero_approx(scroll_offset)) {
1801-
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);
1818+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
1819+
int center = int(get_size().width - total_margin - text_width - r_icon->get_width()) / 2;
1820+
x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
18021821
}
18031822
} else {
18041823
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() {
18311850
if (!Math::is_zero_approx(scroll_offset)) {
18321851
x_ofs = style->get_offset().x;
18331852
} else {
1834-
x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
1853+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
1854+
int centered = int((get_size().width - total_margin - text_width)) / 2;
1855+
x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
18351856
}
18361857
} break;
18371858
case HORIZONTAL_ALIGNMENT_RIGHT: {
@@ -1849,7 +1870,9 @@ Vector2 LineEdit::get_caret_pixel_pos() {
18491870
Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
18501871
if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
18511872
if (Math::is_zero_approx(scroll_offset)) {
1852-
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);
1873+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
1874+
int center = int(get_size().width - total_margin - text_width - r_icon->get_width()) / 2;
1875+
x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
18531876
}
18541877
} else {
18551878
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) {
21822205
if (!Math::is_zero_approx(scroll_offset)) {
21832206
x_ofs = style->get_offset().x;
21842207
} else {
2185-
x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
2208+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
2209+
int centered = int((get_size().width - total_margin - text_width)) / 2;
2210+
x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
21862211
}
21872212
} break;
21882213
case HORIZONTAL_ALIGNMENT_RIGHT: {
@@ -2201,7 +2226,9 @@ void LineEdit::set_caret_column(int p_column) {
22012226
Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
22022227
if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
22032228
if (Math::is_zero_approx(scroll_offset)) {
2204-
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);
2229+
int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
2230+
int center = int(get_size().width - total_margin - text_width - r_icon->get_width()) / 2;
2231+
x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
22052232
}
22062233
} else {
22072234
x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));

0 commit comments

Comments
 (0)