Update cached_width of the line_edit element when setting it to be secret

This commit is contained in:
sumit0190 2020-01-29 14:46:49 -05:00
parent 6fcb58f40d
commit 2e08578985
2 changed files with 16 additions and 18 deletions

View File

@ -240,15 +240,7 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) {
deselect();
text = text.substr(cursor_pos, text.length() - cursor_pos);
Ref<Font> font = get_font("font");
cached_width = 0;
if (font != NULL) {
for (int i = 0; i < text.length(); i++)
cached_width += font->get_char_size(text[i]).width;
}
update_cached_width();
set_cursor_position(0);
_text_changed();
}
@ -1358,18 +1350,10 @@ void LineEdit::set_window_pos(int p_pos) {
void LineEdit::append_at_cursor(String p_text) {
if ((max_length <= 0) || (text.length() + p_text.length() <= max_length)) {
Ref<Font> font = get_font("font");
if (font != NULL) {
for (int i = 0; i < p_text.length(); i++)
cached_width += font->get_char_size(p_text[i]).width;
} else {
cached_width = 0;
}
String pre = text.substr(0, cursor_pos);
String post = text.substr(cursor_pos, text.length() - cursor_pos);
text = pre + p_text + post;
update_cached_width();
set_cursor_position(cursor_pos + p_text.length());
} else {
emit_signal("text_change_rejected");
@ -1499,6 +1483,7 @@ bool LineEdit::is_editable() const {
void LineEdit::set_secret(bool p_secret) {
pass = p_secret;
update_cached_width();
update();
}
@ -1514,6 +1499,7 @@ void LineEdit::set_secret_character(const String &p_string) {
ERR_FAIL_COND_MSG(p_string.length() != 1, "Secret character must be exactly one character long (" + itos(p_string.length()) + " characters given).");
secret_character = p_string;
update_cached_width();
update();
}
@ -1685,6 +1671,17 @@ void LineEdit::_emit_text_change() {
text_changed_dirty = false;
}
void LineEdit::update_cached_width() {
Ref<Font> font = get_font("font");
cached_width = 0;
if (font != NULL) {
String text = get_text();
for (int i = 0; i < text.length(); i++) {
cached_width += font->get_char_size(pass ? secret_character[0] : text[i]).width;
}
}
}
void LineEdit::update_placeholder_width() {
if ((max_length <= 0) || (placeholder_translated.length() <= max_length)) {
Ref<Font> font = get_font("font");

View File

@ -132,6 +132,7 @@ private:
void _emit_text_change();
bool expand_to_text_length;
void update_cached_width();
void update_placeholder_width();
bool caret_blink_enabled;