diff options
Diffstat (limited to 'scene/gui/line_edit.cpp')
-rw-r--r-- | scene/gui/line_edit.cpp | 105 |
1 files changed, 96 insertions, 9 deletions
diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index b71a4dd133..549daecdae 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -35,7 +35,9 @@ #include "os/os.h" #include "print_string.h" #include "translation.h" + #ifdef TOOLS_ENABLED +#include "editor/editor_scale.h" #include "editor/editor_settings.h" #endif @@ -64,6 +66,12 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) { _reset_caret_blink_timer(); if (b->is_pressed()) { + if (!text.empty() && is_editable() && _is_over_clear_button(b->get_position())) { + clear_button_status.press_attempt = true; + clear_button_status.pressing_inside = true; + return; + } + shift_selection_check_pre(b->get_shift()); set_cursor_at_pixel_pos(b->get_position().x); @@ -100,6 +108,15 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) { } else { + if (!text.empty() && is_editable() && clear_button_enabled) { + bool press_attempt = clear_button_status.press_attempt; + clear_button_status.press_attempt = false; + if (press_attempt && clear_button_status.pressing_inside && _is_over_clear_button(b->get_position())) { + clear(); + return; + } + } + if ((!selection.creating) && (!selection.doubleclick)) { deselect(); } @@ -117,6 +134,14 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) { if (m.is_valid()) { + if (!text.empty() && is_editable() && clear_button_enabled) { + bool last_press_inside = clear_button_status.pressing_inside; + clear_button_status.pressing_inside = clear_button_status.press_attempt && _is_over_clear_button(m->get_position()); + if (last_press_inside != clear_button_status.pressing_inside) { + update(); + } + } + if (m->get_button_mask() & BUTTON_LEFT) { if (selection.creating) { @@ -548,6 +573,25 @@ void LineEdit::drop_data(const Point2 &p_point, const Variant &p_data) { } } +Control::CursorShape LineEdit::get_cursor_shape(const Point2 &p_pos) const { + if (!text.empty() && is_editable() && _is_over_clear_button(p_pos)) { + return CURSOR_ARROW; + } + return Control::get_cursor_shape(p_pos); +} + +bool LineEdit::_is_over_clear_button(const Point2 &p_pos) const { + if (!clear_button_enabled || !has_point(p_pos)) { + return false; + } + Ref<Texture> icon = Control::get_icon("clear"); + int x_ofs = get_stylebox("normal")->get_offset().x; + if (p_pos.x > get_size().width - icon->get_width() - x_ofs) { + return true; + } + return false; +} + void LineEdit::_notification(int p_what) { switch (p_what) { @@ -640,7 +684,7 @@ void LineEdit::_notification(int p_what) { int char_ofs = window_pos; int y_area = height - style->get_minimum_size().height; - int y_ofs = style->get_offset().y; + int y_ofs = style->get_offset().y + (y_area - font->get_height()) / 2; int font_ascent = font->get_ascent(); @@ -655,10 +699,26 @@ void LineEdit::_notification(int p_what) { 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, height / 2 - r_icon->get_height() / 2), Color(1, 1, 1, disabled_alpha * .9)); + bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled; + if (right_icon.is_valid() || display_clear_icon) { + Ref<Texture> r_icon = display_clear_icon ? Control::get_icon("clear") : right_icon; + Color color_icon(1, 1, 1, disabled_alpha * .9); + if (display_clear_icon) { + if (clear_button_status.press_attempt && clear_button_status.pressing_inside) { + color_icon = get_color("clear_button_color_pressed"); + } else { + color_icon = get_color("clear_button_color"); + } + } + r_icon->draw(ci, Point2(width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT), height / 2 - r_icon->get_height() / 2), color_icon); + + if (align == ALIGN_CENTER) { + if (window_pos == 0) { + x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - cached_text_width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT) * 2) / 2); + } + } else { + x_ofs = MAX(style->get_margin(MARGIN_LEFT), x_ofs - r_icon->get_width() - style->get_margin(MARGIN_RIGHT)); + } } int caret_height = font->get_height() > y_area ? y_area : font->get_height(); @@ -716,7 +776,11 @@ void LineEdit::_notification(int p_what) { if (char_ofs == cursor_pos && draw_caret) { if (ime_text.length() == 0) { +#ifdef TOOLS_ENABLED + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(Math::round(EDSCALE), caret_height)), cursor_color); +#else VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(1, caret_height)), cursor_color); +#endif } } @@ -755,7 +819,11 @@ void LineEdit::_notification(int p_what) { if (char_ofs == cursor_pos && draw_caret) { //may be at the end if (ime_text.length() == 0) { +#ifdef TOOLS_ENABLED + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(Math::round(EDSCALE), caret_height)), cursor_color); +#else VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(1, caret_height)), cursor_color); +#endif } } @@ -1086,9 +1154,8 @@ void LineEdit::set_cursor_position(int p_pos) { } else if (cursor_pos > window_pos) { /* Adjust window if cursor goes too much to the right */ int window_width = get_size().width - style->get_minimum_size().width; - if (has_icon("right_icon")) { - Ref<Texture> r_icon = Control::get_icon("right_icon"); - window_width -= r_icon->get_width(); + if (right_icon.is_valid()) { + window_width -= right_icon->get_width(); } if (window_width < 0) @@ -1375,10 +1442,26 @@ void LineEdit::set_expand_to_text_length(bool p_enabled) { } bool LineEdit::get_expand_to_text_length() const { - return expand_to_text_length; } +void LineEdit::set_clear_button_enabled(bool p_enabled) { + clear_button_enabled = p_enabled; + update(); +} + +bool LineEdit::is_clear_button_enabled() const { + return clear_button_enabled; +} + +void LineEdit::set_right_icon(const Ref<Texture> &p_icon) { + if (right_icon == p_icon) { + return; + } + right_icon = p_icon; + update(); +} + void LineEdit::_ime_text_callback(void *p_self, String p_text, Point2 p_selection) { LineEdit *self = (LineEdit *)p_self; self->ime_text = p_text; @@ -1471,6 +1554,8 @@ void LineEdit::_bind_methods() { 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); 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); ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "new_text"))); ADD_SIGNAL(MethodInfo("text_entered", PropertyInfo(Variant::STRING, "new_text"))); @@ -1498,6 +1583,7 @@ void LineEdit::_bind_methods() { 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"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clear_button_enabled"), "set_clear_button_enabled", "is_clear_button_enabled"); ADD_GROUP("Placeholder", "placeholder_"); ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder"); ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "placeholder_alpha", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_placeholder_alpha", "get_placeholder_alpha"); @@ -1522,6 +1608,7 @@ LineEdit::LineEdit() { secret_character = "*"; text_changed_dirty = false; placeholder_alpha = 0.6; + clear_button_enabled = false; deselect(); set_focus_mode(FOCUS_ALL); |