Make LineEdit secret character easier to change and enter
1. Allow values longer than 1 character in the property, but trim characters after the first one. 2. Allow empty strings, this acts like if a space was used as a secret character, so that an error isn't printed when you erase all characters in the property.
This commit is contained in:
parent
dce1aab174
commit
6950aab078
@ -269,7 +269,7 @@
|
|||||||
If [code]true[/code], every character is replaced with the secret character (see [member secret_character]).
|
If [code]true[/code], every character is replaced with the secret character (see [member secret_character]).
|
||||||
</member>
|
</member>
|
||||||
<member name="secret_character" type="String" setter="set_secret_character" getter="get_secret_character" default=""•"">
|
<member name="secret_character" type="String" setter="set_secret_character" getter="get_secret_character" default=""•"">
|
||||||
The character to use to mask secret input (defaults to "•"). Only a single character can be used as the secret character.
|
The character to use to mask secret input. Only a single character can be used as the secret character. If it is longer than one character, only the first one will be used. If it is empty, a space will be used instead.
|
||||||
</member>
|
</member>
|
||||||
<member name="select_all_on_focus" type="bool" setter="set_select_all_on_focus" getter="is_select_all_on_focus" default="false">
|
<member name="select_all_on_focus" type="bool" setter="set_select_all_on_focus" getter="is_select_all_on_focus" default="false">
|
||||||
If [code]true[/code], the [LineEdit] will select the whole text when it gains focus.
|
If [code]true[/code], the [LineEdit] will select the whole text when it gains focus.
|
||||||
|
@ -1914,15 +1914,12 @@ bool LineEdit::is_secret() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LineEdit::set_secret_character(const String &p_string) {
|
void LineEdit::set_secret_character(const String &p_string) {
|
||||||
// An empty string as the secret character would crash the engine.
|
|
||||||
// It also wouldn't make sense to use multiple characters as the secret character.
|
|
||||||
ERR_FAIL_COND_MSG(p_string.length() != 1, "Secret character must be exactly one character long (" + itos(p_string.length()) + " characters given).");
|
|
||||||
|
|
||||||
if (secret_character == p_string) {
|
if (secret_character == p_string) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
secret_character = p_string;
|
secret_character = p_string;
|
||||||
|
update_configuration_warnings();
|
||||||
_shape();
|
_shape();
|
||||||
queue_redraw();
|
queue_redraw();
|
||||||
}
|
}
|
||||||
@ -2266,6 +2263,13 @@ void LineEdit::_emit_text_change() {
|
|||||||
emit_signal(SNAME("text_changed"), text);
|
emit_signal(SNAME("text_changed"), text);
|
||||||
text_changed_dirty = false;
|
text_changed_dirty = false;
|
||||||
}
|
}
|
||||||
|
PackedStringArray LineEdit::get_configuration_warnings() const {
|
||||||
|
PackedStringArray warnings = Control::get_configuration_warnings();
|
||||||
|
if (secret_character.length() > 1) {
|
||||||
|
warnings.push_back("Secret Character property supports only one character. Extra characters will be ignored.");
|
||||||
|
}
|
||||||
|
return warnings;
|
||||||
|
}
|
||||||
|
|
||||||
void LineEdit::_shape() {
|
void LineEdit::_shape() {
|
||||||
const Ref<Font> &font = theme_cache.font;
|
const Ref<Font> &font = theme_cache.font;
|
||||||
@ -2281,7 +2285,14 @@ void LineEdit::_shape() {
|
|||||||
if (text.length() == 0 && ime_text.length() == 0) {
|
if (text.length() == 0 && ime_text.length() == 0) {
|
||||||
t = placeholder_translated;
|
t = placeholder_translated;
|
||||||
} else if (pass) {
|
} else if (pass) {
|
||||||
t = secret_character.repeat(text.length() + ime_text.length());
|
// TODO: Integrate with text server to add support for non-latin scripts.
|
||||||
|
// Allow secret_character as empty strings, act like if a space was used as a secret character.
|
||||||
|
String secret = " ";
|
||||||
|
// Allow values longer than 1 character in the property, but trim characters after the first one.
|
||||||
|
if (!secret_character.is_empty()) {
|
||||||
|
secret = secret_character.left(1);
|
||||||
|
}
|
||||||
|
t = secret.repeat(text.length() + ime_text.length());
|
||||||
} else {
|
} else {
|
||||||
if (ime_text.length() > 0) {
|
if (ime_text.length() > 0) {
|
||||||
t = text.substr(0, caret_column) + ime_text + text.substr(caret_column, text.length());
|
t = text.substr(0, caret_column) + ime_text + text.substr(caret_column, text.length());
|
||||||
|
@ -385,6 +385,8 @@ public:
|
|||||||
|
|
||||||
virtual bool is_text_field() const override;
|
virtual bool is_text_field() const override;
|
||||||
|
|
||||||
|
PackedStringArray get_configuration_warnings() const override;
|
||||||
|
|
||||||
void show_virtual_keyboard();
|
void show_virtual_keyboard();
|
||||||
|
|
||||||
LineEdit(const String &p_placeholder = String());
|
LineEdit(const String &p_placeholder = String());
|
||||||
|
Loading…
Reference in New Issue
Block a user