diff options
Diffstat (limited to 'scene/gui/line_edit.cpp')
-rw-r--r-- | scene/gui/line_edit.cpp | 111 |
1 files changed, 82 insertions, 29 deletions
diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index d5347edb87..2445456a1f 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -59,6 +59,7 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) { menu->set_scale(get_global_transform().get_scale()); menu->popup(); grab_focus(); + accept_event(); return; } @@ -86,7 +87,7 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) { } else { - if (b->is_doubleclick()) { + if (b->is_doubleclick() && selecting_enabled) { selection.enabled = true; selection.begin = 0; @@ -194,7 +195,7 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) { unsigned int code = k->get_scancode(); - if (k->get_command()) { + if (k->get_command() && is_shortcut_keys_enabled()) { bool handled = true; @@ -209,7 +210,6 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) { } break; case (KEY_C): { // COPY - copy_text(); } break; @@ -274,6 +274,7 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) { } break; case (KEY_A): { //Select All select(); + } break; #ifdef APPLE_STYLE_KEYS case (KEY_LEFT): { // Go to start of text - like HOME key @@ -871,7 +872,9 @@ void LineEdit::_notification(int p_what) { } break; case NOTIFICATION_FOCUS_ENTER: { - if (!caret_blink_enabled) { + if (caret_blink_enabled) { + caret_blink_timer->start(); + } else { draw_caret = true; } @@ -885,6 +888,10 @@ void LineEdit::_notification(int p_what) { } break; case NOTIFICATION_FOCUS_EXIT: { + if (caret_blink_enabled) { + caret_blink_timer->stop(); + } + OS::get_singleton()->set_ime_position(Point2()); OS::get_singleton()->set_ime_active(false); ime_text = ""; @@ -1052,11 +1059,15 @@ bool LineEdit::cursor_get_blink_enabled() const { void LineEdit::cursor_set_blink_enabled(const bool p_enabled) { caret_blink_enabled = p_enabled; - if (p_enabled) { - caret_blink_timer->start(); - } else { - caret_blink_timer->stop(); + + if (has_focus()) { + if (p_enabled) { + caret_blink_timer->start(); + } else { + caret_blink_timer->stop(); + } } + draw_caret = true; } @@ -1071,10 +1082,12 @@ void LineEdit::cursor_set_blink_speed(const float p_speed) { void LineEdit::_reset_caret_blink_timer() { if (caret_blink_enabled) { - caret_blink_timer->stop(); - caret_blink_timer->start(); draw_caret = true; - update(); + if (has_focus()) { + caret_blink_timer->stop(); + caret_blink_timer->start(); + update(); + } } } @@ -1334,6 +1347,8 @@ int LineEdit::get_max_length() const { } void LineEdit::selection_fill_at_cursor() { + if (!selecting_enabled) + return; selection.begin = cursor_pos; selection.end = selection.cursor_start; @@ -1348,6 +1363,8 @@ void LineEdit::selection_fill_at_cursor() { } void LineEdit::select_all() { + if (!selecting_enabled) + return; if (!text.length()) return; @@ -1364,22 +1381,7 @@ void LineEdit::set_editable(bool p_editable) { return; editable = p_editable; - - // Reorganize context menu. - menu->clear(); - if (editable) - menu->add_item(RTR("Cut"), MENU_CUT, KEY_MASK_CMD | KEY_X); - menu->add_item(RTR("Copy"), MENU_COPY, KEY_MASK_CMD | KEY_C); - if (editable) - menu->add_item(RTR("Paste"), MENU_PASTE, KEY_MASK_CMD | KEY_V); - menu->add_separator(); - menu->add_item(RTR("Select All"), MENU_SELECT_ALL, KEY_MASK_CMD | KEY_A); - if (editable) { - menu->add_item(RTR("Clear"), MENU_CLEAR); - menu->add_separator(); - menu->add_item(RTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z); - menu->add_item(RTR("Redo"), MENU_REDO, KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z); - } + _generate_context_menu(); update(); } @@ -1404,8 +1406,7 @@ 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); + 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(); @@ -1416,6 +1417,8 @@ String LineEdit::get_secret_character() const { } void LineEdit::select(int p_from, int p_to) { + if (!selecting_enabled) + return; if (p_from == 0 && p_to == 0) { deselect(); @@ -1523,6 +1526,29 @@ bool LineEdit::is_clear_button_enabled() const { return clear_button_enabled; } +void LineEdit::set_shortcut_keys_enabled(bool p_enabled) { + shortcut_keys_enabled = p_enabled; + + _generate_context_menu(); +} + +bool LineEdit::is_shortcut_keys_enabled() const { + return shortcut_keys_enabled; +} + +void LineEdit::set_selecting_enabled(bool p_enabled) { + selecting_enabled = p_enabled; + + if (!selecting_enabled) + deselect(); + + _generate_context_menu(); +} + +bool LineEdit::is_selecting_enabled() const { + return selecting_enabled; +} + void LineEdit::set_right_icon(const Ref<Texture> &p_icon) { if (right_icon == p_icon) { return; @@ -1586,6 +1612,25 @@ void LineEdit::_create_undo_state() { undo_stack.push_back(op); } +void LineEdit::_generate_context_menu() { + // Reorganize context menu. + menu->clear(); + if (editable) + menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_X : 0); + menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_C : 0); + if (editable) + menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_V : 0); + menu->add_separator(); + if (is_selecting_enabled()) + menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_A : 0); + if (editable) { + menu->add_item(RTR("Clear"), MENU_CLEAR); + menu->add_separator(); + menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_Z : 0); + menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z : 0); + } +} + void LineEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("_text_changed"), &LineEdit::_text_changed); @@ -1630,6 +1675,10 @@ void LineEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &LineEdit::is_context_menu_enabled); ClassDB::bind_method(D_METHOD("set_clear_button_enabled", "enable"), &LineEdit::set_clear_button_enabled); ClassDB::bind_method(D_METHOD("is_clear_button_enabled"), &LineEdit::is_clear_button_enabled); + ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &LineEdit::set_shortcut_keys_enabled); + ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &LineEdit::is_shortcut_keys_enabled); + ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &LineEdit::set_selecting_enabled); + ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &LineEdit::is_selecting_enabled); ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "new_text"))); ADD_SIGNAL(MethodInfo("text_entered", PropertyInfo(Variant::STRING, "new_text"))); @@ -1658,6 +1707,8 @@ void LineEdit::_bind_methods() { 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"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clear_button_enabled"), "set_clear_button_enabled", "is_clear_button_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled"); ADD_GROUP("Placeholder", "placeholder_"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "placeholder_alpha", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_placeholder_alpha", "get_placeholder_alpha"); @@ -1685,6 +1736,8 @@ LineEdit::LineEdit() { clear_button_enabled = false; clear_button_status.press_attempt = false; clear_button_status.pressing_inside = false; + shortcut_keys_enabled = true; + selecting_enabled = true; deselect(); set_focus_mode(FOCUS_ALL); |