diff options
Diffstat (limited to 'scene/gui/item_list.cpp')
-rw-r--r-- | scene/gui/item_list.cpp | 859 |
1 files changed, 477 insertions, 382 deletions
diff --git a/scene/gui/item_list.cpp b/scene/gui/item_list.cpp index 6deb39b9e2..8b22f3722a 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-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -83,6 +83,9 @@ int ItemList::add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable) { } void ItemList::set_item_text(int p_idx, const String &p_text) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].text = p_text; @@ -97,6 +100,9 @@ String ItemList::get_item_text(int p_idx) const { } void ItemList::set_item_text_direction(int p_idx, Control::TextDirection p_text_direction) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3); if (items[p_idx].text_direction != p_text_direction) { @@ -119,6 +125,9 @@ void ItemList::clear_item_opentype_features(int p_idx) { } void ItemList::set_item_opentype_feature(int p_idx, const String &p_name, int p_value) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); int32_t tag = TS->name_to_tag(p_name); if (!items[p_idx].opentype_features.has(tag) || (int)items[p_idx].opentype_features[tag] != p_value) { @@ -138,6 +147,9 @@ int ItemList::get_item_opentype_feature(int p_idx, const String &p_name) const { } void ItemList::set_item_language(int p_idx, const String &p_language) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); if (items[p_idx].language != p_language) { items.write[p_idx].language = p_language; @@ -152,6 +164,9 @@ String ItemList::get_item_language(int p_idx) const { } void ItemList::set_item_tooltip_enabled(int p_idx, const bool p_enabled) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].tooltip_enabled = p_enabled; } @@ -162,6 +177,9 @@ bool ItemList::is_item_tooltip_enabled(int p_idx) const { } void ItemList::set_item_tooltip(int p_idx, const String &p_tooltip) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].tooltip = p_tooltip; @@ -175,6 +193,9 @@ String ItemList::get_item_tooltip(int p_idx) const { } void ItemList::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].icon = p_icon; @@ -189,6 +210,9 @@ Ref<Texture2D> ItemList::get_item_icon(int p_idx) const { } void ItemList::set_item_icon_transposed(int p_idx, const bool p_transposed) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].icon_transposed = p_transposed; @@ -203,6 +227,9 @@ bool ItemList::is_item_icon_transposed(int p_idx) const { } void ItemList::set_item_icon_region(int p_idx, const Rect2 &p_region) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].icon_region = p_region; @@ -217,6 +244,9 @@ Rect2 ItemList::get_item_icon_region(int p_idx) const { } void ItemList::set_item_icon_modulate(int p_idx, const Color &p_modulate) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].icon_modulate = p_modulate; @@ -230,6 +260,9 @@ Color ItemList::get_item_icon_modulate(int p_idx) const { } void ItemList::set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].custom_bg = p_custom_bg_color; @@ -243,6 +276,9 @@ Color ItemList::get_item_custom_bg_color(int p_idx) const { } void ItemList::set_item_custom_fg_color(int p_idx, const Color &p_custom_fg_color) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].custom_fg = p_custom_fg_color; @@ -256,6 +292,9 @@ Color ItemList::get_item_custom_fg_color(int p_idx) const { } void ItemList::set_item_tag_icon(int p_idx, const Ref<Texture2D> &p_tag_icon) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].tag_icon = p_tag_icon; @@ -270,6 +309,9 @@ Ref<Texture2D> ItemList::get_item_tag_icon(int p_idx) const { } void ItemList::set_item_selectable(int p_idx, bool p_selectable) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].selectable = p_selectable; @@ -281,6 +323,9 @@ bool ItemList::is_item_selectable(int p_idx) const { } void ItemList::set_item_disabled(int p_idx, bool p_disabled) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].disabled = p_disabled; @@ -293,6 +338,9 @@ bool ItemList::is_item_disabled(int p_idx) const { } void ItemList::set_item_metadata(int p_idx, const Variant &p_metadata) { + if (p_idx < 0) { + p_idx += get_item_count(); + } ERR_FAIL_INDEX(p_idx, items.size()); items.write[p_idx].metadata = p_metadata; @@ -537,6 +585,9 @@ Size2 ItemList::Item::get_icon_size() const { void ItemList::gui_input(const Ref<InputEvent> &p_event) { ERR_FAIL_COND(p_event.is_null()); +#define CAN_SELECT(i) (items[i].selectable && !items[i].disabled) +#define IS_SAME_ROW(i, row) (i / current_columns == row) + double prev_scroll = scroll_bar->get_value(); Ref<InputEventMouseMotion> mm = p_event; @@ -555,7 +606,7 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { return; } - if (mb.is_valid() && (mb->get_button_index() == MouseButton::LEFT || (allow_rmb_select && mb->get_button_index() == MouseButton::RIGHT)) && mb->is_pressed()) { + if (mb.is_valid() && mb->is_pressed()) { search_string = ""; //any mousepress cancels Vector2 pos = mb->get_position(); Ref<StyleBox> bg = get_theme_stylebox(SNAME("bg")); @@ -580,7 +631,7 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { } } - if (closest != -1) { + if (closest != -1 && (mb->get_button_index() == MouseButton::LEFT || (allow_rmb_select && mb->get_button_index() == MouseButton::RIGHT))) { int i = closest; if (select_mode == SELECT_MULTI && items[i].selected && mb->is_command_pressed()) { @@ -594,55 +645,47 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { SWAP(from, to); } for (int j = from; j <= to; j++) { + if (!CAN_SELECT(j)) { + continue; + } bool selected = !items[j].selected; select(j, false); if (selected) { emit_signal(SNAME("multi_selected"), j, true); } } + emit_signal(SNAME("item_clicked"), i, get_local_mouse_position(), mb->get_button_index()); - if (mb->get_button_index() == MouseButton::RIGHT) { - emit_signal(SNAME("item_rmb_selected"), i, get_local_mouse_position()); - } } else { if (!mb->is_double_click() && !mb->is_command_pressed() && select_mode == SELECT_MULTI && items[i].selectable && !items[i].disabled && items[i].selected && mb->get_button_index() == MouseButton::LEFT) { defer_select_single = i; return; } - if (items[i].selected && mb->get_button_index() == MouseButton::RIGHT) { - emit_signal(SNAME("item_rmb_selected"), i, get_local_mouse_position()); - } else { - bool selected = items[i].selected; - + if (!items[i].selected || allow_reselect) { select(i, select_mode == SELECT_SINGLE || !mb->is_command_pressed()); - if (!selected || allow_reselect) { - if (select_mode == SELECT_SINGLE) { - emit_signal(SNAME("item_selected"), i); - } else { - emit_signal(SNAME("multi_selected"), i, true); - } + if (select_mode == SELECT_SINGLE) { + emit_signal(SNAME("item_selected"), i); + } else { + emit_signal(SNAME("multi_selected"), i, true); } + } - if (mb->get_button_index() == MouseButton::RIGHT) { - emit_signal(SNAME("item_rmb_selected"), i, get_local_mouse_position()); - } else if (/*select_mode==SELECT_SINGLE &&*/ mb->is_double_click()) { - emit_signal(SNAME("item_activated"), i); - } + emit_signal(SNAME("item_clicked"), i, get_local_mouse_position(), mb->get_button_index()); + + if (mb->get_button_index() == MouseButton::LEFT && mb->is_double_click()) { + emit_signal(SNAME("item_activated"), i); } } return; + } else if (closest != -1) { + emit_signal(SNAME("item_clicked"), closest, get_local_mouse_position(), mb->get_button_index()); + } else { + // Since closest is null, more likely we clicked on empty space, so send signal to interested controls. Allows, for example, implement items deselecting. + emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), mb->get_button_index()); } - if (mb->get_button_index() == MouseButton::RIGHT) { - emit_signal(SNAME("rmb_clicked"), mb->get_position()); - - return; - } - - // Since closest is null, more likely we clicked on empty space, so send signal to interested controls. Allows, for example, implement items deselecting. - emit_signal(SNAME("nothing_selected")); } if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed()) { scroll_bar->set_value(scroll_bar->get_value() - scroll_bar->get_page() * mb->get_factor() / 8); @@ -659,7 +702,7 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { 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)) { + if (CAN_SELECT(i) && items[i].text.begins_with(search_string)) { set_current(i); ensure_current_is_visible(); if (select_mode == SELECT_SINGLE) { @@ -675,7 +718,15 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { } if (current >= current_columns) { - set_current(current - current_columns); + int next = current - current_columns; + while (next >= 0 && !CAN_SELECT(next)) { + next = next - current_columns; + } + if (next < 0) { + accept_event(); + return; + } + set_current(next); ensure_current_is_visible(); if (select_mode == SELECT_SINGLE) { emit_signal(SNAME("item_selected"), current); @@ -689,7 +740,7 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { 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)) { + if (CAN_SELECT(i) && items[i].text.begins_with(search_string)) { set_current(i); ensure_current_is_visible(); if (select_mode == SELECT_SINGLE) { @@ -704,7 +755,15 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { } if (current < items.size() - current_columns) { - set_current(current + current_columns); + int next = current + current_columns; + while (next < items.size() && !CAN_SELECT(next)) { + next = next + current_columns; + } + if (next >= items.size()) { + accept_event(); + return; + } + set_current(next); ensure_current_is_visible(); if (select_mode == SELECT_SINGLE) { emit_signal(SNAME("item_selected"), current); @@ -715,7 +774,7 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { search_string = ""; //any mousepress cancels for (int i = 4; i > 0; i--) { - if (current - current_columns * i >= 0) { + if (current - current_columns * i >= 0 && CAN_SELECT(current - current_columns * i)) { set_current(current - current_columns * i); ensure_current_is_visible(); if (select_mode == SELECT_SINGLE) { @@ -729,7 +788,7 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { search_string = ""; //any mousepress cancels for (int i = 4; i > 0; i--) { - if (current + current_columns * i < items.size()) { + if (current + current_columns * i < items.size() && CAN_SELECT(current + current_columns * i)) { set_current(current + current_columns * i); ensure_current_is_visible(); if (select_mode == SELECT_SINGLE) { @@ -744,7 +803,16 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { search_string = ""; //any mousepress cancels if (current % current_columns != 0) { - set_current(current - 1); + int current_row = current / current_columns; + int next = current - 1; + while (!CAN_SELECT(next)) { + next = next - 1; + } + if (next < 0 || !IS_SAME_ROW(next, current_row)) { + accept_event(); + return; + } + set_current(next); ensure_current_is_visible(); if (select_mode == SELECT_SINGLE) { emit_signal(SNAME("item_selected"), current); @@ -755,7 +823,16 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { search_string = ""; //any mousepress cancels if (current % current_columns != (current_columns - 1) && current + 1 < items.size()) { - set_current(current + 1); + int current_row = current / current_columns; + int next = current + 1; + while (!CAN_SELECT(next)) { + next = next + 1; + } + if (items.size() <= next || !IS_SAME_ROW(next, current_row)) { + accept_event(); + return; + } + set_current(next); ensure_current_is_visible(); if (select_mode == SELECT_SINGLE) { emit_signal(SNAME("item_selected"), current); @@ -831,6 +908,9 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) { if (scroll_bar->get_value() != prev_scroll) { accept_event(); //accept event if scroll changed } + +#undef CAN_SELECT +#undef IS_SAME_ROW } void ItemList::ensure_current_is_visible() { @@ -855,445 +935,451 @@ 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(); - } + switch (p_what) { + case NOTIFICATION_RESIZED: { + shape_changed = true; + update(); + } break; + + case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: + case NOTIFICATION_TRANSLATION_CHANGED: + case NOTIFICATION_THEME_CHANGED: { + for (int i = 0; i < items.size(); i++) { + _shape(i); + } + shape_changed = true; + update(); + } break; - if ((p_what == NOTIFICATION_LAYOUT_DIRECTION_CHANGED) || (p_what == NOTIFICATION_TRANSLATION_CHANGED) || (p_what == NOTIFICATION_THEME_CHANGED)) { - for (int i = 0; i < items.size(); i++) { - _shape(i); - } - shape_changed = true; - update(); - } + case NOTIFICATION_DRAW: { + Ref<StyleBox> bg = get_theme_stylebox(SNAME("bg")); - if (p_what == NOTIFICATION_DRAW) { - Ref<StyleBox> bg = get_theme_stylebox(SNAME("bg")); + int mw = scroll_bar->get_minimum_size().x; + scroll_bar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -mw); + scroll_bar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0); + scroll_bar->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, bg->get_margin(SIDE_TOP)); + scroll_bar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -bg->get_margin(SIDE_BOTTOM)); - int mw = scroll_bar->get_minimum_size().x; - scroll_bar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -mw); - scroll_bar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0); - scroll_bar->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, bg->get_margin(SIDE_TOP)); - scroll_bar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -bg->get_margin(SIDE_BOTTOM)); + Size2 size = get_size(); - Size2 size = get_size(); + int width = size.width - bg->get_minimum_size().width; + if (scroll_bar->is_visible()) { + width -= mw; + } - int width = size.width - bg->get_minimum_size().width; - if (scroll_bar->is_visible()) { - width -= mw; - } + draw_style_box(bg, Rect2(Point2(), size)); - draw_style_box(bg, Rect2(Point2(), size)); + int hseparation = get_theme_constant(SNAME("h_separation")); + int vseparation = get_theme_constant(SNAME("v_separation")); + int icon_margin = get_theme_constant(SNAME("icon_margin")); + int line_separation = get_theme_constant(SNAME("line_separation")); + Color font_outline_color = get_theme_color(SNAME("font_outline_color")); + int outline_size = get_theme_constant(SNAME("outline_size")); - int hseparation = get_theme_constant(SNAME("hseparation")); - int vseparation = get_theme_constant(SNAME("vseparation")); - int icon_margin = get_theme_constant(SNAME("icon_margin")); - int line_separation = get_theme_constant(SNAME("line_separation")); - Color font_outline_color = get_theme_color(SNAME("font_outline_color")); - int outline_size = get_theme_constant(SNAME("outline_size")); + Ref<StyleBox> sbsel = has_focus() ? get_theme_stylebox(SNAME("selected_focus")) : get_theme_stylebox(SNAME("selected")); + Ref<StyleBox> cursor = has_focus() ? get_theme_stylebox(SNAME("cursor")) : get_theme_stylebox(SNAME("cursor_unfocused")); + bool rtl = is_layout_rtl(); - Ref<StyleBox> sbsel = has_focus() ? get_theme_stylebox(SNAME("selected_focus")) : get_theme_stylebox(SNAME("selected")); - Ref<StyleBox> cursor = has_focus() ? get_theme_stylebox(SNAME("cursor")) : get_theme_stylebox(SNAME("cursor_unfocused")); - bool rtl = is_layout_rtl(); + Color guide_color = get_theme_color(SNAME("guide_color")); + Color font_color = get_theme_color(SNAME("font_color")); + Color font_selected_color = get_theme_color(SNAME("font_selected_color")); - Color guide_color = get_theme_color(SNAME("guide_color")); - Color font_color = get_theme_color(SNAME("font_color")); - Color font_selected_color = get_theme_color(SNAME("font_selected_color")); + if (has_focus()) { + RenderingServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), true); + draw_style_box(get_theme_stylebox(SNAME("bg_focus")), Rect2(Point2(), size)); + RenderingServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), false); + } - if (has_focus()) { - RenderingServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), true); - draw_style_box(get_theme_stylebox(SNAME("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.0; - if (shape_changed) { - float max_column_width = 0.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 { + minsize = items[i].get_icon_size() * icon_scale; + } - //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 { - minsize = items[i].get_icon_size() * icon_scale; + if (!items[i].text.is_empty()) { + if (icon_mode == ICON_MODE_TOP) { + minsize.y += icon_margin; + } else { + minsize.x += icon_margin; + } + } } if (!items[i].text.is_empty()) { - if (icon_mode == ICON_MODE_TOP) { - minsize.y += icon_margin; - } else { - minsize.x += icon_margin; + int max_width = -1; + if (fixed_column_width) { + max_width = fixed_column_width; + } else if (same_column_width) { + max_width = items[i].rect_cache.size.x; } - } - } + items.write[i].text_buf->set_width(max_width); + Size2 s = items[i].text_buf->get_size(); - if (!items[i].text.is_empty()) { - int max_width = -1; - if (fixed_column_width) { - max_width = fixed_column_width; - } else if (same_column_width) { - max_width = items[i].rect_cache.size.x; - } - items.write[i].text_buf->set_width(max_width); - Size2 s = items[i].text_buf->get_size(); + if (icon_mode == ICON_MODE_TOP) { + minsize.x = MAX(minsize.x, s.width); + if (max_text_lines > 0) { + minsize.y += s.height + line_separation * max_text_lines; + } else { + minsize.y += s.height; + } - if (icon_mode == ICON_MODE_TOP) { - minsize.x = MAX(minsize.x, s.width); - if (max_text_lines > 0) { - minsize.y += s.height + line_separation * max_text_lines; } else { - minsize.y += s.height; + minsize.y = MAX(minsize.y, s.height); + minsize.x += s.width; } + } - } else { - minsize.y = MAX(minsize.y, s.height); - minsize.x += s.width; + 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 + minsize.y += vseparation; + minsize.x += hseparation; + items.write[i].rect_cache.size = minsize; + items.write[i].min_rect_cache.size = minsize; } - if (fixed_column_width > 0) { - minsize.x = fixed_column_width; + int fit_size = size.x - bg->get_minimum_size().width - mw; + + //2-attempt best fit + current_columns = 0x7FFFFFFF; + if (max_columns > 0) { + current_columns = max_columns; } - max_column_width = MAX(max_column_width, minsize.x); - // elements need to adapt to the selected size - minsize.y += vseparation; - minsize.x += hseparation; - items.write[i].rect_cache.size = minsize; - items.write[i].min_rect_cache.size = minsize; - } + while (true) { + //repeat until all fits + bool all_fit = true; + Vector2 ofs; + int col = 0; + 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); + all_fit = false; + break; + } - int fit_size = size.x - bg->get_minimum_size().width - mw; + 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) { + separators.push_back(ofs.y + max_h + vseparation / 2); + } - //2-attempt best fit - current_columns = 0x7FFFFFFF; - if (max_columns > 0) { - current_columns = max_columns; - } + for (int j = i; j >= 0 && col > 0; j--, col--) { + items.write[j].rect_cache.size.y = max_h; + } - while (true) { - //repeat until all fits - bool all_fit = true; - Vector2 ofs; - int col = 0; - 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); - all_fit = false; - break; + ofs.x = 0; + ofs.y += max_h + vseparation; + col = 0; + max_h = 0; + } } - if (same_column_width) { - items.write[i].rect_cache.size.x = max_column_width; + for (int j = items.size() - 1; j >= 0 && col > 0; j--, col--) { + items.write[j].rect_cache.size.y = max_h; } - 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) { - 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; + if (all_fit) { + float page = MAX(0, size.height - bg->get_minimum_size().height); + float max = MAX(page, ofs.y + max_h); + 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) { + scroll_bar->set_value(0); + scroll_bar->hide(); + } else { + scroll_bar->show(); - ofs.x = 0; - ofs.y += max_h + vseparation; - col = 0; - max_h = 0; + if (do_autoscroll_to_bottom) { + scroll_bar->set_value(max); + } + } + break; } } - for (int j = items.size() - 1; j >= 0 && col > 0; j--, col--) { - items.write[j].rect_cache.size.y = max_h; - } + update_minimum_size(); + shape_changed = false; + } - if (all_fit) { - float page = MAX(0, size.height - bg->get_minimum_size().height); - float max = MAX(page, ofs.y + max_h); - 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) { - scroll_bar->set_value(0); - scroll_bar->hide(); - } else { - scroll_bar->show(); + //ensure_selected_visible needs to be checked before we draw the list. + 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(); - if (do_autoscroll_to_bottom) { - scroll_bar->set_value(max); - } - } - break; + if (r.position.y < from) { + scroll_bar->set_value(r.position.y); + } else if (r.position.y + r.size.y > to) { + scroll_bar->set_value(r.position.y + r.size.y - (to - from)); } } - update_minimum_size(); - shape_changed = false; - } - - //ensure_selected_visible needs to be checked before we draw the list. - 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(); + ensure_selected_visible = false; - if (r.position.y < from) { - scroll_bar->set_value(r.position.y); - } else if (r.position.y + r.size.y > to) { - scroll_bar->set_value(r.position.y + r.size.y - (to - from)); - } - } + Vector2 base_ofs = bg->get_offset(); + base_ofs.y -= int(scroll_bar->get_value()); - ensure_selected_visible = false; + const Rect2 clip(-base_ofs, size); // visible frame, don't need to draw outside of there - Vector2 base_ofs = bg->get_offset(); - base_ofs.y -= int(scroll_bar->get_value()); - - const Rect2 clip(-base_ofs, size); // visible frame, don't need to draw outside of there - - int first_item_visible; - { - // do a binary search to find the first item whose rect reaches below clip.position.y - int lo = 0; - int hi = items.size(); - while (lo < hi) { - const int mid = (lo + hi) / 2; - const Rect2 &rcache = items[mid].rect_cache; - if (rcache.position.y + rcache.size.y < clip.position.y) { - lo = mid + 1; - } else { - hi = mid; + int first_item_visible; + { + // do a binary search to find the first item whose rect reaches below clip.position.y + int lo = 0; + int hi = items.size(); + while (lo < hi) { + const int mid = (lo + hi) / 2; + const Rect2 &rcache = items[mid].rect_cache; + if (rcache.position.y + rcache.size.y < clip.position.y) { + lo = mid + 1; + } else { + hi = mid; + } } - } - // we might have ended up with column 2, or 3, ..., so let's find the first column - while (lo > 0 && items[lo - 1].rect_cache.position.y == items[lo].rect_cache.position.y) { - lo -= 1; - } - first_item_visible = lo; - } - - 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) { - break; // done + // we might have ended up with column 2, or 3, ..., so let's find the first column + while (lo > 0 && items[lo - 1].rect_cache.position.y == items[lo].rect_cache.position.y) { + lo -= 1; + } + first_item_visible = lo; } - if (!clip.intersects(rcache)) { - continue; - } + for (int i = first_item_visible; i < items.size(); i++) { + Rect2 rcache = items[i].rect_cache; - if (current_columns == 1) { - rcache.size.width = width - rcache.position.x; - } + if (rcache.position.y > clip.position.y + clip.size.y) { + break; // done + } - if (items[i].selected) { - Rect2 r = rcache; - r.position += base_ofs; - r.position.y -= vseparation / 2; - r.size.y += vseparation; - r.position.x -= hseparation / 2; - r.size.x += hseparation; - - if (rtl) { - r.position.x = size.width - r.position.x - r.size.x; + if (!clip.intersects(rcache)) { + continue; } - draw_style_box(sbsel, r); - } - if (items[i].custom_bg.a > 0.001) { - Rect2 r = rcache; - r.position += base_ofs; - - // Size rect to make the align the temperature colors - r.position.y -= vseparation / 2; - r.size.y += vseparation; - r.position.x -= hseparation / 2; - r.size.x += hseparation; - - if (rtl) { - r.position.x = size.width - r.position.x - r.size.x; + if (current_columns == 1) { + rcache.size.width = width - rcache.position.x; } - draw_rect(r, items[i].custom_bg); - } + if (items[i].selected) { + Rect2 r = rcache; + r.position += base_ofs; + r.position.y -= vseparation / 2; + r.size.y += vseparation; + r.position.x -= hseparation / 2; + r.size.x += hseparation; - 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; + if (rtl) { + r.position.x = size.width - r.position.x - r.size.x; + } - if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) { - icon_size = fixed_icon_size * icon_scale; - } else { - icon_size = items[i].get_icon_size() * icon_scale; + draw_style_box(sbsel, r); } + if (items[i].custom_bg.a > 0.001) { + Rect2 r = rcache; + r.position += base_ofs; - Vector2 icon_ofs; + // Size rect to make the align the temperature colors + r.position.y -= vseparation / 2; + r.size.y += vseparation; + r.position.x -= hseparation / 2; + r.size.x += hseparation; - Point2 pos = items[i].rect_cache.position + icon_ofs + base_ofs; + if (rtl) { + r.position.x = size.width - r.position.x - r.size.x; + } - if (icon_mode == ICON_MODE_TOP) { - pos.x += Math::floor((items[i].rect_cache.size.width - icon_size.width) / 2); - pos.y += icon_margin; - text_ofs.y = icon_size.height + icon_margin * 2; - } else { - pos.y += Math::floor((items[i].rect_cache.size.height - icon_size.height) / 2); - text_ofs.x = icon_size.width + icon_margin; + draw_rect(r, items[i].custom_bg); } - Rect2 draw_rect = Rect2(pos, icon_size); + 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; - if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) { - Rect2 adj = _adjust_to_max_size(items[i].get_icon_size() * icon_scale, icon_size); - draw_rect.position += adj.position; - draw_rect.size = adj.size; - } + if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) { + icon_size = fixed_icon_size * icon_scale; + } else { + icon_size = items[i].get_icon_size() * icon_scale; + } - Color modulate = items[i].icon_modulate; - if (items[i].disabled) { - modulate.a *= 0.5; - } + Vector2 icon_ofs; - // If the icon is transposed, we have to switch the size so that it is drawn correctly - if (items[i].icon_transposed) { - Size2 size_tmp = draw_rect.size; - draw_rect.size.x = size_tmp.y; - draw_rect.size.y = size_tmp.x; - } + Point2 pos = items[i].rect_cache.position + icon_ofs + base_ofs; - Rect2 region = (items[i].icon_region.size.x == 0 || items[i].icon_region.size.y == 0) ? Rect2(Vector2(), items[i].icon->get_size()) : Rect2(items[i].icon_region); + if (icon_mode == ICON_MODE_TOP) { + pos.x += Math::floor((items[i].rect_cache.size.width - icon_size.width) / 2); + pos.y += icon_margin; + text_ofs.y = icon_size.height + icon_margin * 2; + } else { + pos.y += Math::floor((items[i].rect_cache.size.height - icon_size.height) / 2); + text_ofs.x = icon_size.width + icon_margin; + } - if (rtl) { - draw_rect.position.x = size.width - draw_rect.position.x - draw_rect.size.x; - } - draw_texture_rect_region(items[i].icon, draw_rect, region, modulate, items[i].icon_transposed); - } + Rect2 draw_rect = Rect2(pos, icon_size); - if (items[i].tag_icon.is_valid()) { - Point2 draw_pos = items[i].rect_cache.position; - if (rtl) { - draw_pos.x = size.width - draw_pos.x - items[i].tag_icon->get_width(); - } - draw_texture(items[i].tag_icon, draw_pos + base_ofs); - } + if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) { + Rect2 adj = _adjust_to_max_size(items[i].get_icon_size() * icon_scale, icon_size); + draw_rect.position += adj.position; + draw_rect.size = adj.size; + } - if (!items[i].text.is_empty()) { - int max_len = -1; + Color modulate = items[i].icon_modulate; + if (items[i].disabled) { + modulate.a *= 0.5; + } - Vector2 size2 = items[i].text_buf->get_size(); - if (fixed_column_width) { - max_len = fixed_column_width; - } else if (same_column_width) { - max_len = items[i].rect_cache.size.x; - } else { - max_len = size2.x; - } + // If the icon is transposed, we have to switch the size so that it is drawn correctly + if (items[i].icon_transposed) { + Size2 size_tmp = draw_rect.size; + draw_rect.size.x = size_tmp.y; + draw_rect.size.y = size_tmp.x; + } - Color modulate = items[i].selected ? font_selected_color : (items[i].custom_fg != Color() ? items[i].custom_fg : font_color); - if (items[i].disabled) { - modulate.a *= 0.5; - } + Rect2 region = (items[i].icon_region.size.x == 0 || items[i].icon_region.size.y == 0) ? Rect2(Vector2(), items[i].icon->get_size()) : Rect2(items[i].icon_region); - if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) { - text_ofs += base_ofs; - text_ofs += items[i].rect_cache.position; + if (rtl) { + draw_rect.position.x = size.width - draw_rect.position.x - draw_rect.size.x; + } + draw_texture_rect_region(items[i].icon, draw_rect, region, modulate, items[i].icon_transposed); + } + if (items[i].tag_icon.is_valid()) { + Point2 draw_pos = items[i].rect_cache.position; if (rtl) { - text_ofs.x = size.width - text_ofs.x - max_len; + draw_pos.x = size.width - draw_pos.x - items[i].tag_icon->get_width(); } + draw_texture(items[i].tag_icon, draw_pos + base_ofs); + } - items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_CENTER); + if (!items[i].text.is_empty()) { + int max_len = -1; - if (outline_size > 0 && font_outline_color.a > 0) { - items[i].text_buf->draw_outline(get_canvas_item(), text_ofs, outline_size, font_outline_color); + Vector2 size2 = items[i].text_buf->get_size(); + if (fixed_column_width) { + max_len = fixed_column_width; + } else if (same_column_width) { + max_len = items[i].rect_cache.size.x; + } else { + max_len = size2.x; } - items[i].text_buf->draw(get_canvas_item(), text_ofs, modulate); - } else { - if (fixed_column_width > 0) { - size2.x = MIN(size2.x, fixed_column_width); + Color modulate = items[i].selected ? font_selected_color : (items[i].custom_fg != Color() ? items[i].custom_fg : font_color); + if (items[i].disabled) { + modulate.a *= 0.5; } - if (icon_mode == ICON_MODE_TOP) { - text_ofs.x += (items[i].rect_cache.size.width - size2.x) / 2; - } else { - text_ofs.y += (items[i].rect_cache.size.height - size2.y) / 2; - } + if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) { + text_ofs += base_ofs; + text_ofs += items[i].rect_cache.position; - text_ofs += base_ofs; - text_ofs += items[i].rect_cache.position; + if (rtl) { + text_ofs.x = size.width - text_ofs.x - max_len; + } - if (rtl) { - text_ofs.x = size.width - text_ofs.x - max_len; - } + items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_CENTER); - items.write[i].text_buf->set_width(max_len); + if (outline_size > 0 && font_outline_color.a > 0) { + items[i].text_buf->draw_outline(get_canvas_item(), text_ofs, outline_size, font_outline_color); + } - if (rtl) { - items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_RIGHT); + items[i].text_buf->draw(get_canvas_item(), text_ofs, modulate); } else { - items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_LEFT); + 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; + } else { + text_ofs.y += (items[i].rect_cache.size.height - size2.y) / 2; + } + + text_ofs += base_ofs; + text_ofs += items[i].rect_cache.position; + + if (rtl) { + text_ofs.x = size.width - text_ofs.x - max_len; + } + + items.write[i].text_buf->set_width(width - text_ofs.x); + + if (rtl) { + items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_RIGHT); + } else { + items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_LEFT); + } + + if (outline_size > 0 && font_outline_color.a > 0) { + items[i].text_buf->draw_outline(get_canvas_item(), text_ofs, outline_size, font_outline_color); + } + + if (width - text_ofs.x > 0) { + items[i].text_buf->draw(get_canvas_item(), text_ofs, modulate); + } } + } - if (outline_size > 0 && font_outline_color.a > 0) { - items[i].text_buf->draw_outline(get_canvas_item(), text_ofs, outline_size, font_outline_color); + if (select_mode == SELECT_MULTI && i == current) { + Rect2 r = rcache; + r.position += base_ofs; + r.position.y -= vseparation / 2; + r.size.y += vseparation; + r.position.x -= hseparation / 2; + r.size.x += hseparation; + + if (rtl) { + r.position.x = size.width - r.position.x - r.size.x; } - items[i].text_buf->draw(get_canvas_item(), text_ofs, modulate); + draw_style_box(cursor, r); } } - if (select_mode == SELECT_MULTI && i == current) { - Rect2 r = rcache; - r.position += base_ofs; - r.position.y -= vseparation / 2; - r.size.y += vseparation; - r.position.x -= hseparation / 2; - r.size.x += hseparation; - - if (rtl) { - r.position.x = size.width - r.position.x - r.size.x; + int first_visible_separator = 0; + { + // do a binary search to find the first separator that is below clip_position.y + int lo = 0; + int hi = separators.size(); + while (lo < hi) { + const int mid = (lo + hi) / 2; + if (separators[mid] < clip.position.y) { + lo = mid + 1; + } else { + hi = mid; + } } - - draw_style_box(cursor, r); + first_visible_separator = lo; } - } - int first_visible_separator = 0; - { - // do a binary search to find the first separator that is below clip_position.y - int lo = 0; - int hi = separators.size(); - while (lo < hi) { - const int mid = (lo + hi) / 2; - if (separators[mid] < clip.position.y) { - lo = mid + 1; - } else { - hi = mid; + for (int i = first_visible_separator; i < separators.size(); i++) { + if (separators[i] > clip.position.y + clip.size.y) { + break; // done } - } - first_visible_separator = lo; - } - for (int i = first_visible_separator; i < separators.size(); i++) { - 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(SIDE_LEFT), y), Vector2(width, y), guide_color); } - - const int y = base_ofs.y + separators[i]; - draw_line(Vector2(bg->get_margin(SIDE_LEFT), y), Vector2(width, y), guide_color); - } + } break; } } @@ -1492,6 +1578,9 @@ bool ItemList::_set(const StringName &p_name, const Variant &p_value) { } else if (components[1] == "disabled") { set_item_disabled(item_index, p_value); return true; + } else if (components[1] == "selectable") { + set_item_selectable(item_index, p_value); + return true; } } #ifndef DISABLE_DEPRECATED @@ -1528,6 +1617,9 @@ bool ItemList::_get(const StringName &p_name, Variant &r_ret) const { } else if (components[1] == "disabled") { r_ret = is_item_disabled(item_index); return true; + } else if (components[1] == "selectable") { + r_ret = is_item_selectable(item_index); + return true; } } return false; @@ -1541,6 +1633,10 @@ void ItemList::_get_property_list(List<PropertyInfo> *p_list) const { pi.usage &= ~(get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0); p_list->push_back(pi); + pi = PropertyInfo(Variant::BOOL, vformat("item_%d/selectable", i)); + pi.usage &= ~(is_item_selectable(i) ? PROPERTY_USAGE_STORAGE : 0); + p_list->push_back(pi); + pi = PropertyInfo(Variant::BOOL, vformat("item_%d/disabled", i)); pi.usage &= ~(!is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0); p_list->push_back(pi); @@ -1652,7 +1748,7 @@ void ItemList::_bind_methods() { ClassDB::bind_method(D_METHOD("ensure_current_is_visible"), &ItemList::ensure_current_is_visible); - ClassDB::bind_method(D_METHOD("get_v_scroll"), &ItemList::get_v_scroll); + ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &ItemList::get_v_scroll_bar); ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &ItemList::set_text_overrun_behavior); ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &ItemList::get_text_overrun_behavior); @@ -1680,11 +1776,10 @@ void ItemList::_bind_methods() { BIND_ENUM_CONSTANT(SELECT_MULTI); ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index"))); - ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::VECTOR2, "at_position"))); + ADD_SIGNAL(MethodInfo("empty_clicked", PropertyInfo(Variant::VECTOR2, "at_position"), PropertyInfo(Variant::INT, "mouse_button_index"))); + ADD_SIGNAL(MethodInfo("item_clicked", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::VECTOR2, "at_position"), PropertyInfo(Variant::INT, "mouse_button_index"))); ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "selected"))); ADD_SIGNAL(MethodInfo("item_activated", PropertyInfo(Variant::INT, "index"))); - ADD_SIGNAL(MethodInfo("rmb_clicked", PropertyInfo(Variant::VECTOR2, "at_position"))); - ADD_SIGNAL(MethodInfo("nothing_selected")); GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000); ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/incremental_search_max_interval_msec", PropertyInfo(Variant::INT, "gui/timers/incremental_search_max_interval_msec", PROPERTY_HINT_RANGE, "0,10000,1,or_greater")); // No negative numbers |