diff options
Diffstat (limited to 'scene/gui')
-rw-r--r-- | scene/gui/line_edit.cpp | 95 | ||||
-rw-r--r-- | scene/gui/line_edit.h | 18 | ||||
-rw-r--r-- | scene/gui/popup_menu.cpp | 7 | ||||
-rw-r--r-- | scene/gui/text_edit.cpp | 20 |
4 files changed, 121 insertions, 19 deletions
diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 4d73ee2d56..549daecdae 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -66,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); @@ -102,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(); } @@ -119,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) { @@ -550,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) { @@ -642,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(); @@ -657,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(); @@ -1096,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) @@ -1385,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; @@ -1481,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"))); @@ -1508,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"); @@ -1532,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); diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index e9314ba8dd..5294d99da0 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -87,6 +87,10 @@ private: int cached_width; int cached_placeholder_width; + bool clear_button_enabled; + + Ref<Texture> right_icon; + struct Selection { int begin; @@ -105,6 +109,13 @@ private: List<TextOperation> undo_stack; List<TextOperation>::Element *undo_stack_pos; + struct ClearButtonStatus { + bool press_attempt; + bool pressing_inside; + } clear_button_status; + + bool _is_over_clear_button(const Point2 &p_pos) const; + void _clear_undo_stack(); void _clear_redo(); void _create_undo_state(); @@ -150,6 +161,8 @@ public: virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const; virtual void drop_data(const Point2 &p_point, const Variant &p_data); + virtual CursorShape get_cursor_shape(const Point2 &p_pos) const; + void menu_option(int p_option); void set_context_menu_enabled(bool p_enable); bool is_context_menu_enabled(); @@ -201,6 +214,11 @@ public: void set_expand_to_text_length(bool p_enabled); bool get_expand_to_text_length() const; + void set_clear_button_enabled(bool p_enabled); + bool is_clear_button_enabled() const; + + void set_right_icon(const Ref<Texture> &p_icon); + virtual bool is_text_field() const; LineEdit(); ~LineEdit(); diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp index ab762e19ee..cdc6b868ec 100644 --- a/scene/gui/popup_menu.cpp +++ b/scene/gui/popup_menu.cpp @@ -1049,10 +1049,8 @@ void PopupMenu::activate_item(int p_item) { ERR_FAIL_INDEX(p_item, items.size()); ERR_FAIL_COND(items[p_item].separator); int id = items[p_item].ID >= 0 ? items[p_item].ID : p_item; - emit_signal("id_pressed", id); - emit_signal("index_pressed", p_item); - //hide all parent PopupMenue's + //hide all parent PopupMenus Node *next = get_parent(); PopupMenu *pop = Object::cast_to<PopupMenu>(next); while (pop) { @@ -1086,6 +1084,9 @@ void PopupMenu::activate_item(int p_item) { return; hide(); + + emit_signal("id_pressed", id); + emit_signal("index_pressed", p_item); } void PopupMenu::remove_item(int p_idx) { diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index c9dcf058aa..9a8dc62e4e 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -1177,7 +1177,12 @@ void TextEdit::_notification(int p_what) { int yofs = ofs_y + (get_row_height() - cache.font->get_height()) / 2; int w = drawer.draw_char(ci, Point2i(char_ofs + char_margin + ofs_x, yofs + ascent), str[j], str[j + 1], in_selection && override_selected_font_color ? cache.font_selected_color : color); if (underlined) { - draw_rect(Rect2(char_ofs + char_margin + ofs_x, yofs + ascent + 2, w, 1), in_selection && override_selected_font_color ? cache.font_selected_color : color); + float line_width = 1.0; +#ifdef TOOLS_ENABLED + line_width *= EDSCALE; +#endif + + draw_rect(Rect2(char_ofs + char_margin + ofs_x, yofs + ascent + 2, w, line_width), in_selection && override_selected_font_color ? cache.font_selected_color : color); } } else if (draw_tabs && str[j] == '\t') { int yofs = (get_row_height() - cache.tab_icon->get_height()) / 2; @@ -2195,9 +2200,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { k->set_shift(false); } - if (!k->get_command()) { - _reset_caret_blink_timer(); - } + _reset_caret_blink_timer(); // save here for insert mode, just in case it is cleared in the following section bool had_selection = selection.active; @@ -5765,8 +5768,11 @@ void TextEdit::_update_completion_candidates() { } // Calculate the similarity to keep completions in good order float similarity; - if (completion_strings[i].to_lower().begins_with(s.to_lower())) { - // Substrings are the best candidates + if (completion_strings[i].begins_with(s)) { + // Substrings (same case) are the best candidates + similarity = 1.2; + } else if (completion_strings[i].to_lower().begins_with(s.to_lower())) { + // then any substrings similarity = 1.1; } else { // Otherwise compute the similarity @@ -6410,7 +6416,7 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int } // check for dot or underscore or 'x' for hex notation in floating point number - if ((str[j] == '.' || str[j] == 'x' || str[j] == '_') && !in_word && prev_is_number && !is_number) { + if ((str[j] == '.' || str[j] == 'x' || str[j] == '_' || str[j] == 'f') && !in_word && prev_is_number && !is_number) { is_number = true; is_symbol = false; is_char = false; |