diff options
Diffstat (limited to 'scene/gui/line_edit.cpp')
| -rw-r--r-- | scene/gui/line_edit.cpp | 715 |
1 files changed, 403 insertions, 312 deletions
diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 20725194cf..c1784fb7ba 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -6,6 +6,7 @@ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -41,445 +42,442 @@ static bool _is_text_char(CharType c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'; } -void LineEdit::_gui_input(InputEvent p_event) { +void LineEdit::_gui_input(Ref<InputEvent> p_event) { - switch (p_event.type) { + Ref<InputEventMouseButton> b = p_event; - case InputEvent::MOUSE_BUTTON: { + if (b.is_valid()) { - const InputEventMouseButton &b = p_event.mouse_button; - - if (b.pressed && b.button_index == BUTTON_RIGHT) { - menu->set_pos(get_global_transform().xform(get_local_mouse_pos())); - menu->set_size(Vector2(1, 1)); - menu->popup(); - grab_focus(); - return; - } + if (b->is_pressed() && b->get_button_index() == BUTTON_RIGHT) { + menu->set_position(get_global_transform().xform(get_local_mouse_pos())); + menu->set_size(Vector2(1, 1)); + menu->popup(); + grab_focus(); + return; + } - if (b.button_index != BUTTON_LEFT) - break; + if (b->get_button_index() != BUTTON_LEFT) + return; - _reset_caret_blink_timer(); - if (b.pressed) { + _reset_caret_blink_timer(); + if (b->is_pressed()) { - shift_selection_check_pre(b.mod.shift); + shift_selection_check_pre(b->get_shift()); - set_cursor_at_pixel_pos(b.x); + set_cursor_at_pixel_pos(b->get_position().x); - if (b.mod.shift) { + if (b->get_shift()) { - selection_fill_at_cursor(); - selection.creating = true; + selection_fill_at_cursor(); + selection.creating = true; - } else { + } else { - if (b.doubleclick) { + if (b->is_doubleclick()) { - selection.enabled = true; - selection.begin = 0; - selection.end = text.length(); - selection.doubleclick = true; - } + selection.enabled = true; + selection.begin = 0; + selection.end = text.length(); + selection.doubleclick = true; + } - selection.drag_attempt = false; + selection.drag_attempt = false; - if ((cursor_pos < selection.begin) || (cursor_pos > selection.end) || !selection.enabled) { + if ((cursor_pos < selection.begin) || (cursor_pos > selection.end) || !selection.enabled) { - selection_clear(); - selection.cursor_start = cursor_pos; - selection.creating = true; - } else if (selection.enabled) { + selection_clear(); + selection.cursor_start = cursor_pos; + selection.creating = true; + } else if (selection.enabled) { - selection.drag_attempt = true; - } + selection.drag_attempt = true; } + } - update(); - - } else { + update(); - if ((!selection.creating) && (!selection.doubleclick)) { - selection_clear(); - } - selection.creating = false; - selection.doubleclick = false; + } else { - if (OS::get_singleton()->has_virtual_keyboard()) - OS::get_singleton()->show_virtual_keyboard(text, get_global_rect()); + if ((!selection.creating) && (!selection.doubleclick)) { + selection_clear(); } + selection.creating = false; + selection.doubleclick = false; - update(); - } break; - case InputEvent::MOUSE_MOTION: { + if (OS::get_singleton()->has_virtual_keyboard()) + OS::get_singleton()->show_virtual_keyboard(text, get_global_rect()); + } - const InputEventMouseMotion &m = p_event.mouse_motion; + update(); + } - if (m.button_mask & BUTTON_LEFT) { + Ref<InputEventMouseMotion> m = p_event; - if (selection.creating) { - set_cursor_at_pixel_pos(m.x); - selection_fill_at_cursor(); - } - } + if (m.is_valid()) { - } break; - case InputEvent::KEY: { + if (m->get_button_mask() & BUTTON_LEFT) { - const InputEventKey &k = p_event.key; + if (selection.creating) { + set_cursor_at_pixel_pos(m->get_position().x); + selection_fill_at_cursor(); + } + } + } - if (!k.pressed) - return; - unsigned int code = k.scancode; + Ref<InputEventKey> k = p_event; - if (k.mod.command) { + if (k.is_valid()) { - bool handled = true; + if (!k->is_pressed()) + return; + unsigned int code = k->get_scancode(); - switch (code) { + if (k->get_command()) { - case (KEY_X): { // CUT + bool handled = true; - if (editable) { - cut_text(); - } + switch (code) { - } break; + case (KEY_X): { // CUT - case (KEY_C): { // COPY + if (editable) { + cut_text(); + } - copy_text(); + } break; - } break; + case (KEY_C): { // COPY - case (KEY_V): { // PASTE + copy_text(); - if (editable) { + } break; - paste_text(); - } + case (KEY_V): { // PASTE - } break; + if (editable) { - case (KEY_Z): { // Simple One level undo + paste_text(); + } - if (editable) { + } break; - undo(); - } + case (KEY_Z): { // Simple One level undo - } break; + if (editable) { - case (KEY_U): { // Delete from start to cursor + undo(); + } - if (editable) { + } break; - selection_clear(); - undo_text = text; - text = text.substr(cursor_pos, text.length() - cursor_pos); + case (KEY_U): { // Delete from start to cursor - Ref<Font> font = get_font("font"); + if (editable) { - cached_width = 0; - if (font != NULL) { - for (int i = 0; i < text.length(); i++) - cached_width += font->get_char_size(text[i]).width; - } + selection_clear(); + undo_text = text; + text = text.substr(cursor_pos, text.length() - cursor_pos); + + Ref<Font> font = get_font("font"); - set_cursor_pos(0); - _text_changed(); + cached_width = 0; + if (font != NULL) { + for (int i = 0; i < text.length(); i++) + cached_width += font->get_char_size(text[i]).width; } - } break; + set_cursor_pos(0); + _text_changed(); + } - case (KEY_Y): { // PASTE (Yank for unix users) + } break; - if (editable) { + case (KEY_Y): { // PASTE (Yank for unix users) - paste_text(); - } + if (editable) { - } break; - case (KEY_K): { // Delete from cursor_pos to end + paste_text(); + } - if (editable) { + } break; + case (KEY_K): { // Delete from cursor_pos to end - selection_clear(); - undo_text = text; - text = text.substr(0, cursor_pos); - _text_changed(); - } + if (editable) { - } break; - case (KEY_A): { //Select All - select(); - } break; - default: { handled = false; } - } + selection_clear(); + undo_text = text; + text = text.substr(0, cursor_pos); + _text_changed(); + } - if (handled) { - accept_event(); - return; - } + } break; + case (KEY_A): { //Select All + select(); + } break; + default: { handled = false; } } - _reset_caret_blink_timer(); - if (!k.mod.meta) { + if (handled) { + accept_event(); + return; + } + } - bool handled = true; - switch (code) { + _reset_caret_blink_timer(); + if (!k->get_metakey()) { - case KEY_ENTER: - case KEY_RETURN: { + bool handled = true; + switch (code) { - emit_signal("text_entered", text); - if (OS::get_singleton()->has_virtual_keyboard()) - OS::get_singleton()->hide_virtual_keyboard(); + case KEY_KP_ENTER: + case KEY_ENTER: { - return; - } break; + emit_signal("text_entered", text); + if (OS::get_singleton()->has_virtual_keyboard()) + OS::get_singleton()->hide_virtual_keyboard(); - case KEY_BACKSPACE: { + return; + } break; - if (!editable) - break; + case KEY_BACKSPACE: { - if (selection.enabled) { - undo_text = text; - selection_delete(); - break; - } + if (!editable) + break; + + if (selection.enabled) { + undo_text = text; + selection_delete(); + break; + } #ifdef APPLE_STYLE_KEYS - if (k.mod.alt) { + if (k->get_alt()) { #else - if (k.mod.alt) { - handled = false; - break; - } else if (k.mod.command) { + if (k->get_alt()) { + handled = false; + break; + } else if (k->get_command()) { #endif - int cc = cursor_pos; - bool prev_char = false; + int cc = cursor_pos; + bool prev_char = false; - while (cc > 0) { - bool ischar = _is_text_char(text[cc - 1]); + while (cc > 0) { + bool ischar = _is_text_char(text[cc - 1]); - if (prev_char && !ischar) - break; + if (prev_char && !ischar) + break; - prev_char = ischar; - cc--; - } + prev_char = ischar; + cc--; + } - delete_text(cc, cursor_pos); + delete_text(cc, cursor_pos); - set_cursor_pos(cc); + set_cursor_pos(cc); - } else { - undo_text = text; - delete_char(); - } + } else { + undo_text = text; + delete_char(); + } - } break; - case KEY_KP_4: { - if (k.unicode != 0) { - handled = false; - break; - } - // numlock disabled. fallthrough to key_left + } break; + case KEY_KP_4: { + if (k->get_unicode() != 0) { + handled = false; + break; } - case KEY_LEFT: { + // numlock disabled. fallthrough to key_left + } + case KEY_LEFT: { #ifndef APPLE_STYLE_KEYS - if (!k.mod.alt) + if (!k->get_alt()) #endif - shift_selection_check_pre(k.mod.shift); + shift_selection_check_pre(k->get_shift()); #ifdef APPLE_STYLE_KEYS - if (k.mod.command) { - set_cursor_pos(0); - } else if (k.mod.alt) { + if (k->get_command()) { + set_cursor_pos(0); + } else if (k->get_alt()) { #else - if (k.mod.alt) { - handled = false; - break; - } else if (k.mod.command) { + if (k->get_alt()) { + handled = false; + break; + } else if (k->get_command()) { #endif - bool prev_char = false; - int cc = cursor_pos; + bool prev_char = false; + int cc = cursor_pos; - while (cc > 0) { - bool ischar = _is_text_char(text[cc - 1]); + while (cc > 0) { + bool ischar = _is_text_char(text[cc - 1]); - if (prev_char && !ischar) - break; + if (prev_char && !ischar) + break; - prev_char = ischar; - cc--; - } + prev_char = ischar; + cc--; + } - set_cursor_pos(cc); + set_cursor_pos(cc); - } else { - set_cursor_pos(get_cursor_pos() - 1); - } + } else { + set_cursor_pos(get_cursor_pos() - 1); + } - shift_selection_check_post(k.mod.shift); + shift_selection_check_post(k->get_shift()); - } break; - case KEY_KP_6: { - if (k.unicode != 0) { - handled = false; - break; - } - // numlock disabled. fallthrough to key_right + } break; + case KEY_KP_6: { + if (k->get_unicode() != 0) { + handled = false; + break; } - case KEY_RIGHT: { + // numlock disabled. fallthrough to key_right + } + case KEY_RIGHT: { - shift_selection_check_pre(k.mod.shift); + shift_selection_check_pre(k->get_shift()); #ifdef APPLE_STYLE_KEYS - if (k.mod.command) { - set_cursor_pos(text.length()); - } else if (k.mod.alt) { + if (k->get_command()) { + set_cursor_pos(text.length()); + } else if (k->get_alt()) { #else - if (k.mod.alt) { - handled = false; - break; - } else if (k.mod.command) { + if (k->get_alt()) { + handled = false; + break; + } else if (k->get_command()) { #endif - bool prev_char = false; - int cc = cursor_pos; + bool prev_char = false; + int cc = cursor_pos; - while (cc < text.length()) { - bool ischar = _is_text_char(text[cc]); + while (cc < text.length()) { + bool ischar = _is_text_char(text[cc]); - if (prev_char && !ischar) - break; + if (prev_char && !ischar) + break; - prev_char = ischar; - cc++; - } + prev_char = ischar; + cc++; + } - set_cursor_pos(cc); + set_cursor_pos(cc); - } else { - set_cursor_pos(get_cursor_pos() + 1); - } + } else { + set_cursor_pos(get_cursor_pos() + 1); + } - shift_selection_check_post(k.mod.shift); + shift_selection_check_post(k->get_shift()); - } break; - case KEY_DELETE: { + } break; + case KEY_DELETE: { - if (!editable) - break; + if (!editable) + break; - if (k.mod.shift && !k.mod.command && !k.mod.alt) { - cut_text(); - break; - } + if (k->get_shift() && !k->get_command() && !k->get_alt()) { + cut_text(); + break; + } - if (selection.enabled) { - undo_text = text; - selection_delete(); - break; - } + if (selection.enabled) { + undo_text = text; + selection_delete(); + break; + } - int text_len = text.length(); + int text_len = text.length(); - if (cursor_pos == text_len) - break; // nothing to do + if (cursor_pos == text_len) + break; // nothing to do #ifdef APPLE_STYLE_KEYS - if (k.mod.alt) { + if (k->get_alt()) { #else - if (k.mod.alt) { - handled = false; - break; - } else if (k.mod.command) { + if (k->get_alt()) { + handled = false; + break; + } else if (k->get_command()) { #endif - int cc = cursor_pos; + int cc = cursor_pos; - bool prev_char = false; + bool prev_char = false; - while (cc < text.length()) { + while (cc < text.length()) { - bool ischar = _is_text_char(text[cc]); + bool ischar = _is_text_char(text[cc]); - if (prev_char && !ischar) - break; - prev_char = ischar; - cc++; - } - - delete_text(cursor_pos, cc); - - } else { - undo_text = text; - set_cursor_pos(cursor_pos + 1); - delete_char(); + if (prev_char && !ischar) + break; + prev_char = ischar; + cc++; } - } break; - case KEY_KP_7: { - if (k.unicode != 0) { - handled = false; - break; - } - // numlock disabled. fallthrough to key_home - } - case KEY_HOME: { + delete_text(cursor_pos, cc); - shift_selection_check_pre(k.mod.shift); - set_cursor_pos(0); - shift_selection_check_post(k.mod.shift); - } break; - case KEY_KP_1: { - if (k.unicode != 0) { - handled = false; - break; - } - // numlock disabled. fallthrough to key_end + } else { + undo_text = text; + set_cursor_pos(cursor_pos + 1); + delete_char(); } - case KEY_END: { - - shift_selection_check_pre(k.mod.shift); - set_cursor_pos(text.length()); - shift_selection_check_post(k.mod.shift); - } break; - default: { + } break; + case KEY_KP_7: { + if (k->get_unicode() != 0) { + handled = false; + break; + } + // numlock disabled. fallthrough to key_home + } + case KEY_HOME: { + shift_selection_check_pre(k->get_shift()); + set_cursor_pos(0); + shift_selection_check_post(k->get_shift()); + } break; + case KEY_KP_1: { + if (k->get_unicode() != 0) { handled = false; - } break; + break; + } + // numlock disabled. fallthrough to key_end } + case KEY_END: { - if (handled) { - accept_event(); - } else if (!k.mod.alt && !k.mod.command) { - if (k.unicode >= 32 && k.scancode != KEY_DELETE) { - - if (editable) { - selection_delete(); - CharType ucodestr[2] = { (CharType)k.unicode, 0 }; - append_at_cursor(ucodestr); - _text_changed(); - accept_event(); - } + shift_selection_check_pre(k->get_shift()); + set_cursor_pos(text.length()); + shift_selection_check_post(k->get_shift()); + } break; - } else { - return; + default: { + + handled = false; + } break; + } + + if (handled) { + accept_event(); + } else if (!k->get_alt() && !k->get_command()) { + if (k->get_unicode() >= 32 && k->get_scancode() != KEY_DELETE) { + + if (editable) { + selection_delete(); + CharType ucodestr[2] = { (CharType)k->get_unicode(), 0 }; + append_at_cursor(ucodestr); + _text_changed(); + accept_event(); } - } - update(); + } else { + return; + } } - return; + update(); + } - } break; + return; } } @@ -576,8 +574,12 @@ void LineEdit::_notification(int p_what) { RID ci = get_canvas_item(); Ref<StyleBox> style = get_stylebox("normal"); - if (!is_editable()) + float disabled_alpha = 1.0; // used to set the disabled input text color + if (!is_editable()) { style = get_stylebox("read_only"); + disabled_alpha = .5; + draw_caret = false; + } Ref<Font> font = get_font("font"); @@ -624,6 +626,13 @@ void LineEdit::_notification(int p_what) { // draw placeholder color if (text.empty()) font_color.a *= placeholder_alpha; + font_color.a *= disabled_alpha; + + if (has_icon("right_icon")) { + Ref<Texture> r_icon = Control::get_icon("right_icon"); + ofs_max -= r_icon->get_width(); + r_icon->draw(ci, Point2(width - r_icon->get_width() - x_ofs, y_ofs), Color(1, 1, 1, disabled_alpha * .9)); + } int caret_height = font->get_height() > y_area ? y_area : font->get_height(); while (true) { @@ -632,8 +641,37 @@ void LineEdit::_notification(int p_what) { if (char_ofs >= t.length()) break; - CharType cchar = pass ? '*' : t[char_ofs]; - CharType next = pass ? '*' : t[char_ofs + 1]; + if (char_ofs == cursor_pos) { + if (ime_text.length() > 0) { + int ofs = 0; + while (true) { + if (ofs >= ime_text.length()) + break; + + CharType cchar = (pass && !text.empty()) ? '*' : ime_text[ofs]; + CharType next = (pass && !text.empty()) ? '*' : ime_text[ofs + 1]; + int im_char_width = font->get_char_size(cchar, next).width; + + if ((x_ofs + im_char_width) > ofs_max) + break; + + bool selected = ofs >= ime_selection.x && ofs < ime_selection.x + ime_selection.y; + if (selected) { + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 3)), font_color); + } else { + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 1)), font_color); + } + + font->draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, font_color); + + x_ofs += im_char_width; + ofs++; + } + } + } + + CharType cchar = (pass && !text.empty()) ? '*' : t[char_ofs]; + CharType next = (pass && !text.empty()) ? '*' : t[char_ofs + 1]; int char_width = font->get_char_size(cchar, next).width; // end of widget, break! @@ -648,19 +686,54 @@ void LineEdit::_notification(int p_what) { font->draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, selected ? font_color_selected : font_color); if (char_ofs == cursor_pos && draw_caret) { - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2( - Point2(x_ofs, y_ofs), Size2(1, caret_height)), - cursor_color); + if (ime_text.length() == 0) { + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(1, caret_height)), cursor_color); + } } x_ofs += char_width; char_ofs++; } + if (char_ofs == cursor_pos) { + if (ime_text.length() > 0) { + int ofs = 0; + while (true) { + if (ofs >= ime_text.length()) + break; + + CharType cchar = (pass && !text.empty()) ? '*' : ime_text[ofs]; + CharType next = (pass && !text.empty()) ? '*' : ime_text[ofs + 1]; + int im_char_width = font->get_char_size(cchar, next).width; + + if ((x_ofs + im_char_width) > ofs_max) + break; + + bool selected = ofs >= ime_selection.x && ofs < ime_selection.x + ime_selection.y; + if (selected) { + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 3)), font_color); + } else { + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 1)), font_color); + } + + font->draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, font_color); + + x_ofs += im_char_width; + ofs++; + } + } + } + if (char_ofs == cursor_pos && draw_caret) { //may be at the end - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2( - Point2(x_ofs, y_ofs), Size2(1, caret_height)), - cursor_color); + if (ime_text.length() == 0) { + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(1, caret_height)), cursor_color); + } + } + + if (has_focus()) { + + OS::get_singleton()->set_ime_position(get_global_position() + Point2(x_ofs, y_ofs + caret_height)); + OS::get_singleton()->set_ime_intermediate_text_callback(_ime_text_callback, this); } } break; case NOTIFICATION_FOCUS_ENTER: { @@ -669,12 +742,21 @@ void LineEdit::_notification(int p_what) { draw_caret = true; } + Point2 cursor_pos = Point2(get_cursor_pos(), 1) * get_minimum_size().height; + OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos); + OS::get_singleton()->set_ime_intermediate_text_callback(_ime_text_callback, this); + if (OS::get_singleton()->has_virtual_keyboard()) OS::get_singleton()->show_virtual_keyboard(text, get_global_rect()); } break; case NOTIFICATION_FOCUS_EXIT: { + OS::get_singleton()->set_ime_position(Point2()); + OS::get_singleton()->set_ime_intermediate_text_callback(NULL, NULL); + ime_text = ""; + ime_selection = Point2(); + if (OS::get_singleton()->has_virtual_keyboard()) OS::get_singleton()->hide_virtual_keyboard(); @@ -1183,7 +1265,9 @@ void LineEdit::menu_option(int p_option) { select_all(); } break; case MENU_UNDO: { - undo(); + if (editable) { + undo(); + } } break; } } @@ -1210,6 +1294,13 @@ bool LineEdit::get_expand_to_text_length() const { return expand_to_text_length; } +void LineEdit::_ime_text_callback(void *p_self, String p_text, Point2 p_selection) { + LineEdit *self = (LineEdit *)p_self; + self->ime_text = p_text; + self->ime_selection = p_selection; + self->update(); +} + void LineEdit::_text_changed() { if (expand_to_text_length) @@ -1256,7 +1347,7 @@ void LineEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret); ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1)); ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option); - ClassDB::bind_method(D_METHOD("get_menu:PopupMenu"), &LineEdit::get_menu); + ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu); ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "text"))); ADD_SIGNAL(MethodInfo("text_entered", PropertyInfo(Variant::STRING, "text"))); |
