summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsumit0190 <sumi29@gmail.com>2020-01-29 14:46:49 -0500
committersumit0190 <sumi29@gmail.com>2020-02-08 10:49:08 -0500
commit2e08578985689857271a2c554d524f3eee6cc08d (patch)
tree95c6a073784cb192ae0aa1da25b2fc346033ab75
parent6fcb58f40dae8228074591e3262d5db97db3e2d7 (diff)
Update cached_width of the line_edit element when setting it to be secret
-rw-r--r--scene/gui/line_edit.cpp33
-rw-r--r--scene/gui/line_edit.h1
2 files changed, 16 insertions, 18 deletions
diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp
index 7afc3b0d00..7cc47d351e 100644
--- a/scene/gui/line_edit.cpp
+++ b/scene/gui/line_edit.cpp
@@ -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");
diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h
index cf597d11b6..037238d682 100644
--- a/scene/gui/line_edit.h
+++ b/scene/gui/line_edit.h
@@ -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;