Make the LineEdit "secret" character customizable

This commit is contained in:
Hugo Locurcio
2018-04-28 20:24:48 +02:00
parent 1226720c01
commit c118a0ee5e
3 changed files with 35 additions and 7 deletions

View File

@ -663,8 +663,8 @@ void LineEdit::_notification(int p_what) {
if (ofs >= ime_text.length())
break;
CharType cchar = (pass && !text.empty()) ? '*' : ime_text[ofs];
CharType next = (pass && !text.empty()) ? '*' : ime_text[ofs + 1];
CharType cchar = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs];
CharType next = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs + 1];
int im_char_width = font->get_char_size(cchar, next).width;
if ((x_ofs + im_char_width) > ofs_max)
@ -685,8 +685,8 @@ void LineEdit::_notification(int p_what) {
}
}
CharType cchar = (pass && !text.empty()) ? '*' : t[char_ofs];
CharType next = (pass && !text.empty()) ? '*' : t[char_ofs + 1];
CharType cchar = (pass && !text.empty()) ? secret_character[0] : t[char_ofs];
CharType next = (pass && !text.empty()) ? secret_character[0] : t[char_ofs + 1];
int char_width = font->get_char_size(cchar, next).width;
// end of widget, break!
@ -717,8 +717,8 @@ void LineEdit::_notification(int p_what) {
if (ofs >= ime_text.length())
break;
CharType cchar = (pass && !text.empty()) ? '*' : ime_text[ofs];
CharType next = (pass && !text.empty()) ? '*' : ime_text[ofs + 1];
CharType cchar = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs];
CharType next = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs + 1];
int im_char_width = font->get_char_size(cchar, next).width;
if ((x_ofs + im_char_width) > ofs_max)
@ -1225,6 +1225,7 @@ void LineEdit::select_all() {
selection.enabled = true;
update();
}
void LineEdit::set_editable(bool p_editable) {
editable = p_editable;
@ -1241,11 +1242,27 @@ void LineEdit::set_secret(bool p_secret) {
pass = p_secret;
update();
}
bool LineEdit::is_secret() const {
return pass;
}
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_EXPLAIN("Secret character must be exactly one character long (" + itos(p_string.length()) + " characters given)");
ERR_FAIL_COND(p_string.length() != 1);
secret_character = p_string;
update();
}
String LineEdit::get_secret_character() const {
return secret_character;
}
void LineEdit::select(int p_from, int p_to) {
if (p_from == 0 && p_to == 0) {
@ -1434,6 +1451,8 @@ void LineEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable);
ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret);
ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret);
ClassDB::bind_method(D_METHOD("set_secret_character", "character"), &LineEdit::set_secret_character);
ClassDB::bind_method(D_METHOD("get_secret_character"), &LineEdit::get_secret_character);
ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option);
ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu);
ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &LineEdit::set_context_menu_enabled);
@ -1461,6 +1480,7 @@ void LineEdit::_bind_methods() {
ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "max_length"), "set_max_length", "get_max_length");
ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "secret"), "set_secret", "is_secret");
ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "secret_character"), "set_secret_character", "get_secret_character");
ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "expand_to_text_length"), "set_expand_to_text_length", "get_expand_to_text_length");
ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_focus_mode", "get_focus_mode");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
@ -1485,6 +1505,7 @@ LineEdit::LineEdit() {
window_has_focus = true;
max_length = 0;
pass = false;
secret_character = "*";
text_changed_dirty = false;
placeholder_alpha = 0.6;