diff options
Diffstat (limited to 'scene/gui/item_list.cpp')
-rw-r--r-- | scene/gui/item_list.cpp | 294 |
1 files changed, 101 insertions, 193 deletions
diff --git a/scene/gui/item_list.cpp b/scene/gui/item_list.cpp index 1406586361..54150d130d 100644 --- a/scene/gui/item_list.cpp +++ b/scene/gui/item_list.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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 */ @@ -32,8 +32,7 @@ #include "core/os/os.h" #include "core/project_settings.h" -void ItemList::add_item(const String &p_item, const Ref<Texture> &p_texture, bool p_selectable) { - +void ItemList::add_item(const String &p_item, const Ref<Texture2D> &p_texture, bool p_selectable) { Item item; item.icon = p_texture; item.icon_transposed = false; @@ -51,8 +50,7 @@ void ItemList::add_item(const String &p_item, const Ref<Texture> &p_texture, boo shape_changed = true; } -void ItemList::add_icon_item(const Ref<Texture> &p_item, bool p_selectable) { - +void ItemList::add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable) { Item item; item.icon = p_item; item.icon_transposed = false; @@ -71,7 +69,6 @@ void ItemList::add_icon_item(const Ref<Texture> &p_item, bool p_selectable) { } void ItemList::set_item_text(int p_idx, const String &p_text) { - ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].text = p_text; @@ -80,7 +77,6 @@ void ItemList::set_item_text(int p_idx, const String &p_text) { } String ItemList::get_item_text(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), String()); return items[p_idx].text; } @@ -96,7 +92,6 @@ bool ItemList::is_item_tooltip_enabled(int p_idx) const { } void ItemList::set_item_tooltip(int p_idx, const String &p_tooltip) { - ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].tooltip = p_tooltip; @@ -105,13 +100,11 @@ void ItemList::set_item_tooltip(int p_idx, const String &p_tooltip) { } String ItemList::get_item_tooltip(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), String()); return items[p_idx].tooltip; } -void ItemList::set_item_icon(int p_idx, const Ref<Texture> &p_icon) { - +void ItemList::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) { ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].icon = p_icon; @@ -119,15 +112,13 @@ void ItemList::set_item_icon(int p_idx, const Ref<Texture> &p_icon) { shape_changed = true; } -Ref<Texture> ItemList::get_item_icon(int p_idx) const { - - ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture>()); +Ref<Texture2D> ItemList::get_item_icon(int p_idx) const { + ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture2D>()); return items[p_idx].icon; } void ItemList::set_item_icon_transposed(int p_idx, const bool p_transposed) { - ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].icon_transposed = p_transposed; @@ -136,14 +127,12 @@ void ItemList::set_item_icon_transposed(int p_idx, const bool p_transposed) { } bool ItemList::is_item_icon_transposed(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].icon_transposed; } void ItemList::set_item_icon_region(int p_idx, const Rect2 &p_region) { - ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].icon_region = p_region; @@ -152,14 +141,12 @@ void ItemList::set_item_icon_region(int p_idx, const Rect2 &p_region) { } Rect2 ItemList::get_item_icon_region(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), Rect2()); return items[p_idx].icon_region; } void ItemList::set_item_icon_modulate(int p_idx, const Color &p_modulate) { - ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].icon_modulate = p_modulate; @@ -167,70 +154,61 @@ void ItemList::set_item_icon_modulate(int p_idx, const Color &p_modulate) { } Color ItemList::get_item_icon_modulate(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), Color()); return items[p_idx].icon_modulate; } void ItemList::set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color) { - ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].custom_bg = p_custom_bg_color; } Color ItemList::get_item_custom_bg_color(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), Color()); return items[p_idx].custom_bg; } void ItemList::set_item_custom_fg_color(int p_idx, const Color &p_custom_fg_color) { - ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].custom_fg = p_custom_fg_color; } Color ItemList::get_item_custom_fg_color(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), Color()); return items[p_idx].custom_fg; } -void ItemList::set_item_tag_icon(int p_idx, const Ref<Texture> &p_tag_icon) { - +void ItemList::set_item_tag_icon(int p_idx, const Ref<Texture2D> &p_tag_icon) { ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].tag_icon = p_tag_icon; update(); shape_changed = true; } -Ref<Texture> ItemList::get_item_tag_icon(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture>()); +Ref<Texture2D> ItemList::get_item_tag_icon(int p_idx) const { + ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture2D>()); return items[p_idx].tag_icon; } void ItemList::set_item_selectable(int p_idx, bool p_selectable) { - ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].selectable = p_selectable; } bool ItemList::is_item_selectable(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].selectable; } void ItemList::set_item_disabled(int p_idx, bool p_disabled) { - ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].disabled = p_disabled; @@ -238,13 +216,11 @@ void ItemList::set_item_disabled(int p_idx, bool p_disabled) { } bool ItemList::is_item_disabled(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].disabled; } void ItemList::set_item_metadata(int p_idx, const Variant &p_metadata) { - ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].metadata = p_metadata; @@ -253,16 +229,14 @@ void ItemList::set_item_metadata(int p_idx, const Variant &p_metadata) { } Variant ItemList::get_item_metadata(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), Variant()); return items[p_idx].metadata; } -void ItemList::select(int p_idx, bool p_single) { +void ItemList::select(int p_idx, bool p_single) { ERR_FAIL_INDEX(p_idx, items.size()); if (p_single || select_mode == SELECT_SINGLE) { - if (!items[p_idx].selectable || items[p_idx].disabled) { return; } @@ -274,15 +248,14 @@ void ItemList::select(int p_idx, bool p_single) { current = p_idx; ensure_selected_visible = false; } else { - if (items[p_idx].selectable && !items[p_idx].disabled) { items.write[p_idx].selected = true; } } update(); } -void ItemList::unselect(int p_idx) { +void ItemList::unselect(int p_idx) { ERR_FAIL_INDEX(p_idx, items.size()); if (select_mode != SELECT_MULTI) { @@ -295,12 +268,11 @@ void ItemList::unselect(int p_idx) { } void ItemList::unselect_all() { - - if (items.size() < 1) + if (items.size() < 1) { return; + } for (int i = 0; i < items.size(); i++) { - items.write[i].selected = false; } current = -1; @@ -308,7 +280,6 @@ void ItemList::unselect_all() { } bool ItemList::is_selected(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].selected; @@ -317,21 +288,19 @@ bool ItemList::is_selected(int p_idx) const { void ItemList::set_current(int p_current) { ERR_FAIL_INDEX(p_current, items.size()); - if (select_mode == SELECT_SINGLE) + if (select_mode == SELECT_SINGLE) { select(p_current, true); - else { + } else { current = p_current; update(); } } int ItemList::get_current() const { - return current; } void ItemList::move_item(int p_from_idx, int p_to_idx) { - ERR_FAIL_INDEX(p_from_idx, items.size()); ERR_FAIL_INDEX(p_to_idx, items.size()); @@ -348,11 +317,10 @@ void ItemList::move_item(int p_from_idx, int p_to_idx) { } int ItemList::get_item_count() const { - return items.size(); } -void ItemList::remove_item(int p_idx) { +void ItemList::remove_item(int p_idx) { ERR_FAIL_INDEX(p_idx, items.size()); items.remove(p_idx); @@ -362,7 +330,6 @@ void ItemList::remove_item(int p_idx) { } void ItemList::clear() { - items.clear(); current = -1; ensure_selected_visible = false; @@ -372,65 +339,58 @@ void ItemList::clear() { } void ItemList::set_fixed_column_width(int p_size) { - ERR_FAIL_COND(p_size < 0); fixed_column_width = p_size; update(); shape_changed = true; } -int ItemList::get_fixed_column_width() const { +int ItemList::get_fixed_column_width() const { return fixed_column_width; } void ItemList::set_same_column_width(bool p_enable) { - same_column_width = p_enable; update(); shape_changed = true; } -bool ItemList::is_same_column_width() const { +bool ItemList::is_same_column_width() const { return same_column_width; } void ItemList::set_max_text_lines(int p_lines) { - ERR_FAIL_COND(p_lines < 1); max_text_lines = p_lines; update(); shape_changed = true; } -int ItemList::get_max_text_lines() const { +int ItemList::get_max_text_lines() const { return max_text_lines; } void ItemList::set_max_columns(int p_amount) { - ERR_FAIL_COND(p_amount < 0); max_columns = p_amount; update(); shape_changed = true; } -int ItemList::get_max_columns() const { +int ItemList::get_max_columns() const { return max_columns; } void ItemList::set_select_mode(SelectMode p_mode) { - select_mode = p_mode; update(); } ItemList::SelectMode ItemList::get_select_mode() const { - return select_mode; } void ItemList::set_icon_mode(IconMode p_mode) { - ERR_FAIL_INDEX((int)p_mode, 2); icon_mode = p_mode; update(); @@ -438,28 +398,27 @@ void ItemList::set_icon_mode(IconMode p_mode) { } ItemList::IconMode ItemList::get_icon_mode() const { - return icon_mode; } void ItemList::set_fixed_icon_size(const Size2 &p_size) { - fixed_icon_size = p_size; update(); } Size2 ItemList::get_fixed_icon_size() const { - return fixed_icon_size; } -Size2 ItemList::Item::get_icon_size() const { - if (icon.is_null()) +Size2 ItemList::Item::get_icon_size() const { + if (icon.is_null()) { return Size2(); + } Size2 size_result = Size2(icon_region.size).abs(); - if (icon_region.size.x == 0 || icon_region.size.y == 0) + if (icon_region.size.x == 0 || icon_region.size.y == 0) { size_result = icon->get_size(); + } if (icon_transposed) { Size2 size_tmp = size_result; @@ -471,7 +430,6 @@ Size2 ItemList::Item::get_icon_size() const { } void ItemList::_gui_input(const Ref<InputEvent> &p_event) { - double prev_scroll = scroll_bar->get_value(); Ref<InputEventMouseMotion> mm = p_event; @@ -483,7 +441,6 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseButton> mb = p_event; if (defer_select_single >= 0 && mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) { - select(defer_select_single, true); emit_signal("multi_selected", defer_select_single, true); @@ -492,17 +449,15 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { } if (mb.is_valid() && (mb->get_button_index() == BUTTON_LEFT || (allow_rmb_select && mb->get_button_index() == BUTTON_RIGHT)) && mb->is_pressed()) { - search_string = ""; //any mousepress cancels Vector2 pos = mb->get_position(); - Ref<StyleBox> bg = get_stylebox("bg"); + Ref<StyleBox> bg = get_theme_stylebox("bg"); pos -= bg->get_offset(); pos.y += scroll_bar->get_value(); int closest = -1; for (int i = 0; i < items.size(); i++) { - Rect2 rc = items[i].rect_cache; if (i % current_columns == current_columns - 1) { rc.size.width = get_size().width; //not right but works @@ -515,7 +470,6 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { } if (closest != -1) { - int i = closest; if (select_mode == SELECT_MULTI && items[i].selected && mb->get_command()) { @@ -523,7 +477,6 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { emit_signal("multi_selected", i, false); } else if (select_mode == SELECT_MULTI && mb->get_shift() && current >= 0 && current < items.size() && current != i) { - int from = current; int to = i; if (i < current) { @@ -532,23 +485,21 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { for (int j = from; j <= to; j++) { bool selected = !items[j].selected; select(j, false); - if (selected) + if (selected) { emit_signal("multi_selected", j, true); + } } if (mb->get_button_index() == BUTTON_RIGHT) { - emit_signal("item_rmb_selected", i, get_local_mouse_position()); } } else { - if (!mb->is_doubleclick() && !mb->get_command() && select_mode == SELECT_MULTI && items[i].selectable && !items[i].disabled && items[i].selected && mb->get_button_index() == BUTTON_LEFT) { defer_select_single = i; return; } if (items[i].selected && mb->get_button_index() == BUTTON_RIGHT) { - emit_signal("item_rmb_selected", i, get_local_mouse_position()); } else { bool selected = items[i].selected; @@ -558,15 +509,14 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { if (!selected || allow_reselect) { if (select_mode == SELECT_SINGLE) { emit_signal("item_selected", i); - } else + } else { emit_signal("multi_selected", i, true); + } } if (mb->get_button_index() == BUTTON_RIGHT) { - emit_signal("item_rmb_selected", i, get_local_mouse_position()); } else if (/*select_mode==SELECT_SINGLE &&*/ mb->is_doubleclick()) { - emit_signal("item_activated", i); } } @@ -584,28 +534,21 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { emit_signal("nothing_selected"); } if (mb.is_valid() && mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) { - scroll_bar->set_value(scroll_bar->get_value() - scroll_bar->get_page() * mb->get_factor() / 8); } if (mb.is_valid() && mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) { - scroll_bar->set_value(scroll_bar->get_value() + scroll_bar->get_page() * mb->get_factor() / 8); } if (p_event->is_pressed() && items.size() > 0) { if (p_event->is_action("ui_up")) { - if (search_string != "") { - uint64_t now = OS::get_singleton()->get_ticks_msec(); uint64_t diff = now - search_time_msec; if (diff < uint64_t(ProjectSettings::get_singleton()->get("gui/timers/incremental_search_max_interval_msec")) * 2) { - for (int i = current - 1; i >= 0; i--) { - if (items[i].text.begins_with(search_string)) { - set_current(i); ensure_current_is_visible(); if (select_mode == SELECT_SINGLE) { @@ -629,18 +572,13 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { accept_event(); } } else if (p_event->is_action("ui_down")) { - if (search_string != "") { - uint64_t now = OS::get_singleton()->get_ticks_msec(); uint64_t diff = now - search_time_msec; if (diff < uint64_t(ProjectSettings::get_singleton()->get("gui/timers/incremental_search_max_interval_msec")) * 2) { - for (int i = current + 1; i < items.size(); i++) { - if (items[i].text.begins_with(search_string)) { - set_current(i); ensure_current_is_visible(); if (select_mode == SELECT_SINGLE) { @@ -663,7 +601,6 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { accept_event(); } } else if (p_event->is_action("ui_page_up")) { - search_string = ""; //any mousepress cancels for (int i = 4; i > 0; i--) { @@ -678,7 +615,6 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { } } } else if (p_event->is_action("ui_page_down")) { - search_string = ""; //any mousepress cancels for (int i = 4; i > 0; i--) { @@ -694,7 +630,6 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { } } } else if (p_event->is_action("ui_left")) { - search_string = ""; //any mousepress cancels if (current % current_columns != 0) { @@ -706,7 +641,6 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { accept_event(); } } else if (p_event->is_action("ui_right")) { - search_string = ""; //any mousepress cancels if (current % current_columns != (current_columns - 1) && current + 1 < items.size()) { @@ -720,7 +654,6 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { } else if (p_event->is_action("ui_cancel")) { search_string = ""; } else if (p_event->is_action("ui_select") && select_mode == SELECT_MULTI) { - if (current >= 0 && current < items.size()) { if (items[current].selectable && !items[current].disabled && !items[current].selected) { select(current, false); @@ -737,11 +670,9 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { emit_signal("item_activated", current); } } else { - Ref<InputEventKey> k = p_event; if (k.is_valid() && k->get_unicode()) { - uint64_t now = OS::get_singleton()->get_ticks_msec(); uint64_t diff = now - search_time_msec; uint64_t max_interval = uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000)); @@ -751,19 +682,22 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { search_string = ""; } - if (String::chr(k->get_unicode()) != search_string) + if (String::chr(k->get_unicode()) != search_string) { search_string += String::chr(k->get_unicode()); + } for (int i = current + 1; i <= items.size(); i++) { if (i == items.size()) { - if (current == 0 || current == -1) + if (current == 0 || current == -1) { break; - else + } else { i = 0; + } } - if (i == current) + if (i == current) { break; + } if (items[i].text.findn(search_string) == 0) { set_current(i); @@ -780,22 +714,20 @@ void ItemList::_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventPanGesture> pan_gesture = p_event; if (pan_gesture.is_valid()) { - scroll_bar->set_value(scroll_bar->get_value() + scroll_bar->get_page() * pan_gesture->get_delta().y / 8); } - if (scroll_bar->get_value() != prev_scroll) + if (scroll_bar->get_value() != prev_scroll) { accept_event(); //accept event if scroll changed + } } void ItemList::ensure_current_is_visible() { - ensure_selected_visible = true; update(); } static Rect2 _adjust_to_max_size(Size2 p_size, Size2 p_max_size) { - Size2 size = p_max_size; int tex_width = p_size.width * size.height / p_size.height; int tex_height = size.height; @@ -812,15 +744,13 @@ static Rect2 _adjust_to_max_size(Size2 p_size, Size2 p_max_size) { } void ItemList::_notification(int p_what) { - if (p_what == NOTIFICATION_RESIZED) { shape_changed = true; update(); } if (p_what == NOTIFICATION_DRAW) { - - Ref<StyleBox> bg = get_stylebox("bg"); + Ref<StyleBox> bg = get_theme_stylebox("bg"); int mw = scroll_bar->get_minimum_size().x; scroll_bar->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -mw); @@ -837,18 +767,18 @@ void ItemList::_notification(int p_what) { draw_style_box(bg, Rect2(Point2(), size)); - int hseparation = get_constant("hseparation"); - int vseparation = get_constant("vseparation"); - int icon_margin = get_constant("icon_margin"); - int line_separation = get_constant("line_separation"); + int hseparation = get_theme_constant("hseparation"); + int vseparation = get_theme_constant("vseparation"); + int icon_margin = get_theme_constant("icon_margin"); + int line_separation = get_theme_constant("line_separation"); - Ref<StyleBox> sbsel = has_focus() ? get_stylebox("selected_focus") : get_stylebox("selected"); - Ref<StyleBox> cursor = has_focus() ? get_stylebox("cursor") : get_stylebox("cursor_unfocused"); + Ref<StyleBox> sbsel = has_focus() ? get_theme_stylebox("selected_focus") : get_theme_stylebox("selected"); + Ref<StyleBox> cursor = has_focus() ? get_theme_stylebox("cursor") : get_theme_stylebox("cursor_unfocused"); - Ref<Font> font = get_font("font"); - Color guide_color = get_color("guide_color"); - Color font_color = get_color("font_color"); - Color font_color_selected = get_color("font_color_selected"); + Ref<Font> font = get_theme_font("font"); + Color guide_color = get_theme_color("guide_color"); + Color font_color = get_theme_color("font_color"); + Color font_color_selected = get_theme_color("font_color_selected"); int font_height = font->get_height(); Vector<int> line_size_cache; Vector<int> line_limit_cache; @@ -859,21 +789,18 @@ void ItemList::_notification(int p_what) { } if (has_focus()) { - VisualServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), true); - draw_style_box(get_stylebox("bg_focus"), Rect2(Point2(), size)); - VisualServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), false); + RenderingServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), true); + draw_style_box(get_theme_stylebox("bg_focus"), Rect2(Point2(), size)); + RenderingServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), false); } if (shape_changed) { - float max_column_width = 0; //1- compute item minimum sizes for (int i = 0; i < items.size(); i++) { - Size2 minsize; if (items[i].icon.is_valid()) { - if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) { minsize = fixed_icon_size * icon_scale; } else { @@ -890,7 +817,6 @@ void ItemList::_notification(int p_what) { } if (items[i].text != "") { - Size2 s = font->get_string_size(items[i].text); //s.width=MIN(s.width,fixed_column_width); @@ -908,8 +834,9 @@ void ItemList::_notification(int p_what) { } } - if (fixed_column_width > 0) + if (fixed_column_width > 0) { minsize.x = fixed_column_width; + } max_column_width = MAX(max_column_width, minsize.x); // elements need to adapt to the selected size @@ -923,8 +850,9 @@ void ItemList::_notification(int p_what) { //2-attempt best fit current_columns = 0x7FFFFFFF; - if (max_columns > 0) + if (max_columns > 0) { current_columns = max_columns; + } while (true) { //repeat until all fits @@ -934,7 +862,6 @@ void ItemList::_notification(int p_what) { int max_h = 0; separators.clear(); for (int i = 0; i < items.size(); i++) { - if (current_columns > 1 && items[i].rect_cache.size.width + ofs.x > fit_size) { //went past current_columns = MAX(col, 1); @@ -942,16 +869,17 @@ void ItemList::_notification(int p_what) { break; } - if (same_column_width) + if (same_column_width) { items.write[i].rect_cache.size.x = max_column_width; + } items.write[i].rect_cache.position = ofs; max_h = MAX(max_h, items[i].rect_cache.size.y); ofs.x += items[i].rect_cache.size.x + hseparation; col++; if (col == current_columns) { - - if (i < items.size() - 1) + if (i < items.size() - 1) { separators.push_back(ofs.y + max_h + vseparation / 2); + } for (int j = i; j >= 0 && col > 0; j--, col--) { items.write[j].rect_cache.size.y = max_h; @@ -969,10 +897,11 @@ void ItemList::_notification(int p_what) { } if (all_fit) { - float page = size.height - bg->get_minimum_size().height; + float page = MAX(0, size.height - bg->get_minimum_size().height); float max = MAX(page, ofs.y + max_h); - if (auto_height) + if (auto_height) { auto_height_value = ofs.y + max_h + bg->get_minimum_size().height; + } scroll_bar->set_max(max); scroll_bar->set_page(page); if (max <= page) { @@ -981,8 +910,9 @@ void ItemList::_notification(int p_what) { } else { scroll_bar->show(); - if (do_autoscroll_to_bottom) + if (do_autoscroll_to_bottom) { scroll_bar->set_value(max); + } } break; } @@ -993,8 +923,7 @@ void ItemList::_notification(int p_what) { } //ensure_selected_visible needs to be checked before we draw the list. - if (ensure_selected_visible && current >= 0 && current <= items.size()) { - + if (ensure_selected_visible && current >= 0 && current < items.size()) { Rect2 r = items[current].rect_cache; int from = scroll_bar->get_value(); int to = from + scroll_bar->get_page(); @@ -1035,14 +964,15 @@ void ItemList::_notification(int p_what) { } for (int i = first_item_visible; i < items.size(); i++) { - Rect2 rcache = items[i].rect_cache; - if (rcache.position.y > clip.position.y + clip.size.y) + if (rcache.position.y > clip.position.y + clip.size.y) { break; // done + } - if (!clip.intersects(rcache)) + if (!clip.intersects(rcache)) { continue; + } if (current_columns == 1) { rcache.size.width = width - rcache.position.x; @@ -1073,7 +1003,6 @@ void ItemList::_notification(int p_what) { Vector2 text_ofs; if (items[i].icon.is_valid()) { - Size2 icon_size; //= _adjust_to_max_size(items[i].get_icon_size(),fixed_icon_size) * icon_scale; @@ -1088,7 +1017,6 @@ void ItemList::_notification(int p_what) { Point2 pos = items[i].rect_cache.position + icon_ofs + base_ofs; if (icon_mode == ICON_MODE_TOP) { - pos.x += Math::floor((items[i].rect_cache.size.width - icon_size.width) / 2); pos.y += MIN( Math::floor((items[i].rect_cache.size.height - icon_size.height) / 2), @@ -1096,7 +1024,6 @@ void ItemList::_notification(int p_what) { text_ofs.y = icon_size.height + icon_margin; text_ofs.y += items[i].rect_cache.size.height - items[i].min_rect_cache.size.height; } else { - pos.y += Math::floor((items[i].rect_cache.size.height - icon_size.height) / 2); text_ofs.x = icon_size.width + icon_margin; } @@ -1110,8 +1037,9 @@ void ItemList::_notification(int p_what) { } Color modulate = items[i].icon_modulate; - if (items[i].disabled) + if (items[i].disabled) { modulate.a *= 0.5; + } // If the icon is transposed, we have to switch the size so that it is drawn correctly if (items[i].icon_transposed) { @@ -1125,41 +1053,40 @@ void ItemList::_notification(int p_what) { } if (items[i].tag_icon.is_valid()) { - draw_texture(items[i].tag_icon, items[i].rect_cache.position + base_ofs); } if (items[i].text != "") { - int max_len = -1; Vector2 size2 = font->get_string_size(items[i].text); - if (fixed_column_width) + if (fixed_column_width) { max_len = fixed_column_width; - else if (same_column_width) + } else if (same_column_width) { max_len = items[i].rect_cache.size.x; - else + } else { max_len = size2.x; + } Color modulate = items[i].selected ? font_color_selected : (items[i].custom_fg != Color() ? items[i].custom_fg : font_color); - if (items[i].disabled) + if (items[i].disabled) { modulate.a *= 0.5; + } if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) { - int ss = items[i].text.length(); float ofs = 0; int line = 0; for (int j = 0; j <= ss; j++) { - int cs = j < ss ? font->get_char_size(items[i].text[j], items[i].text[j + 1]).x : 0; if (ofs + cs > max_len || j == ss) { line_limit_cache.write[line] = j; line_size_cache.write[line] = ofs; line++; ofs = 0; - if (line >= max_text_lines) + if (line >= max_text_lines) { break; + } } else { ofs += cs; } @@ -1175,21 +1102,21 @@ void ItemList::_notification(int p_what) { FontDrawer drawer(font, Color(1, 1, 1)); for (int j = 0; j < ss; j++) { - if (j == line_limit_cache[line]) { line++; ofs = 0; - if (line >= max_text_lines) + if (line >= max_text_lines) { break; + } } ofs += drawer.draw_char(get_canvas_item(), text_ofs + Vector2(ofs + (max_len - line_size_cache[line]) / 2, line * (font_height + line_separation)).floor(), items[i].text[j], items[i].text[j + 1], modulate); } //special multiline mode } else { - - if (fixed_column_width > 0) + if (fixed_column_width > 0) { size2.x = MIN(size2.x, fixed_column_width); + } if (icon_mode == ICON_MODE_TOP) { text_ofs.x += (items[i].rect_cache.size.width - size2.x) / 2; @@ -1207,7 +1134,6 @@ void ItemList::_notification(int p_what) { } if (select_mode == SELECT_MULTI && i == current) { - Rect2 r = rcache; r.position += base_ofs; r.position.y -= vseparation / 2; @@ -1235,8 +1161,9 @@ void ItemList::_notification(int p_what) { } for (int i = first_visible_separator; i < separators.size(); i++) { - if (separators[i] > clip.position.y + clip.size.y) + if (separators[i] > clip.position.y + clip.size.y) { break; // done + } const int y = base_ofs.y + separators[i]; draw_line(Vector2(bg->get_margin(MARGIN_LEFT), y), Vector2(width, y), guide_color); @@ -1249,9 +1176,8 @@ void ItemList::_scroll_changed(double) { } int ItemList::get_item_at_position(const Point2 &p_pos, bool p_exact) const { - Vector2 pos = p_pos; - Ref<StyleBox> bg = get_stylebox("bg"); + Ref<StyleBox> bg = get_theme_stylebox("bg"); pos -= bg->get_offset(); pos.y += scroll_bar->get_value(); @@ -1259,7 +1185,6 @@ int ItemList::get_item_at_position(const Point2 &p_pos, bool p_exact) const { int closest_dist = 0x7FFFFFFF; for (int i = 0; i < items.size(); i++) { - Rect2 rc = items[i].rect_cache; if (i % current_columns == current_columns - 1) { rc.size.width = get_size().width - rc.position.x; //make sure you can still select the last item when clicking past the column @@ -1281,12 +1206,12 @@ int ItemList::get_item_at_position(const Point2 &p_pos, bool p_exact) const { } bool ItemList::is_pos_at_end_of_items(const Point2 &p_pos) const { - - if (items.empty()) + if (items.empty()) { return true; + } Vector2 pos = p_pos; - Ref<StyleBox> bg = get_stylebox("bg"); + Ref<StyleBox> bg = get_theme_stylebox("bg"); pos -= bg->get_offset(); pos.y += scroll_bar->get_value(); @@ -1295,7 +1220,6 @@ bool ItemList::is_pos_at_end_of_items(const Point2 &p_pos) const { } String ItemList::get_tooltip(const Point2 &p_pos) const { - int closest = get_item_at_position(p_pos, true); if (closest != -1) { @@ -1314,7 +1238,6 @@ String ItemList::get_tooltip(const Point2 &p_pos) const { } void ItemList::sort_items_by_text() { - items.sort(); update(); shape_changed = true; @@ -1330,7 +1253,6 @@ void ItemList::sort_items_by_text() { } int ItemList::find_metadata(const Variant &p_metadata) const { - for (int i = 0; i < items.size(); i++) { if (items[i].metadata == p_metadata) { return i; @@ -1341,22 +1263,18 @@ int ItemList::find_metadata(const Variant &p_metadata) const { } void ItemList::set_allow_rmb_select(bool p_allow) { - allow_rmb_select = p_allow; } bool ItemList::get_allow_rmb_select() const { - return allow_rmb_select; } void ItemList::set_allow_reselect(bool p_allow) { - allow_reselect = p_allow; } bool ItemList::get_allow_reselect() const { - return allow_reselect; } @@ -1383,22 +1301,21 @@ Vector<int> ItemList::get_selected_items() { bool ItemList::is_anything_selected() { for (int i = 0; i < items.size(); i++) { - if (items[i].selected) + if (items[i].selected) { return true; + } } return false; } void ItemList::_set_items(const Array &p_items) { - ERR_FAIL_COND(p_items.size() % 3); clear(); for (int i = 0; i < p_items.size(); i += 3) { - String text = p_items[i + 0]; - Ref<Texture> icon = p_items[i + 1]; + Ref<Texture2D> icon = p_items[i + 1]; bool disabled = p_items[i + 2]; int idx = get_item_count(); @@ -1408,10 +1325,8 @@ void ItemList::_set_items(const Array &p_items) { } Array ItemList::_get_items() const { - Array items; for (int i = 0; i < get_item_count(); i++) { - items.push_back(get_item_text(i)); items.push_back(get_item_icon(i)); items.push_back(is_item_disabled(i)); @@ -1421,7 +1336,6 @@ Array ItemList::_get_items() const { } Size2 ItemList::get_minimum_size() const { - if (auto_height) { return Size2(0, auto_height_value); } @@ -1429,24 +1343,20 @@ Size2 ItemList::get_minimum_size() const { } void ItemList::set_autoscroll_to_bottom(const bool p_enable) { - do_autoscroll_to_bottom = p_enable; } void ItemList::set_auto_height(bool p_enable) { - auto_height = p_enable; shape_changed = true; update(); } bool ItemList::has_auto_height() const { - return auto_height; } void ItemList::_bind_methods() { - ClassDB::bind_method(D_METHOD("add_item", "text", "icon", "selectable"), &ItemList::add_item, DEFVAL(Variant()), DEFVAL(true)); ClassDB::bind_method(D_METHOD("add_icon_item", "icon", "selectable"), &ItemList::add_icon_item, DEFVAL(true)); @@ -1456,7 +1366,7 @@ void ItemList::_bind_methods() { ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "icon"), &ItemList::set_item_icon); ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &ItemList::get_item_icon); - ClassDB::bind_method(D_METHOD("set_item_icon_transposed", "idx", "rect"), &ItemList::set_item_icon_transposed); + ClassDB::bind_method(D_METHOD("set_item_icon_transposed", "idx", "transposed"), &ItemList::set_item_icon_transposed); ClassDB::bind_method(D_METHOD("is_item_icon_transposed", "idx"), &ItemList::is_item_icon_transposed); ClassDB::bind_method(D_METHOD("set_item_icon_region", "idx", "rect"), &ItemList::set_item_icon_region); @@ -1542,7 +1452,6 @@ void ItemList::_bind_methods() { ClassDB::bind_method(D_METHOD("get_v_scroll"), &ItemList::get_v_scroll); - ClassDB::bind_method(D_METHOD("_scroll_changed"), &ItemList::_scroll_changed); ClassDB::bind_method(D_METHOD("_gui_input"), &ItemList::_gui_input); ClassDB::bind_method(D_METHOD("_set_items"), &ItemList::_set_items); @@ -1553,15 +1462,15 @@ void ItemList::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Multi"), "set_select_mode", "get_select_mode"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "max_text_lines"), "set_max_text_lines", "get_max_text_lines"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "max_text_lines", PROPERTY_HINT_RANGE, "1,10,1,or_greater"), "set_max_text_lines", "get_max_text_lines"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_height"), "set_auto_height", "has_auto_height"); ADD_GROUP("Columns", ""); - ADD_PROPERTY(PropertyInfo(Variant::INT, "max_columns"), "set_max_columns", "get_max_columns"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "max_columns", PROPERTY_HINT_RANGE, "0,10,1,or_greater"), "set_max_columns", "get_max_columns"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "same_column_width"), "set_same_column_width", "is_same_column_width"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_column_width"), "set_fixed_column_width", "get_fixed_column_width"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_column_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_fixed_column_width", "get_fixed_column_width"); ADD_GROUP("Icon", ""); ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_mode", PROPERTY_HINT_ENUM, "Top,Left"), "set_icon_mode", "get_icon_mode"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "icon_scale"), "set_icon_scale", "get_icon_scale"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "icon_scale"), "set_icon_scale", "get_icon_scale"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fixed_icon_size"), "set_fixed_icon_size", "get_fixed_icon_size"); BIND_ENUM_CONSTANT(ICON_MODE_TOP); @@ -1582,7 +1491,6 @@ void ItemList::_bind_methods() { } ItemList::ItemList() { - current = -1; select_mode = SELECT_SINGLE; @@ -1599,7 +1507,7 @@ ItemList::ItemList() { add_child(scroll_bar); shape_changed = true; - scroll_bar->connect("value_changed", this, "_scroll_changed"); + scroll_bar->connect("value_changed", callable_mp(this, &ItemList::_scroll_changed)); set_focus_mode(FOCUS_ALL); current_columns = 1; |