Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Toggle selection mode to ItemList #99355

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/classes/ItemList.xml
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@
<constant name="SELECT_MULTI" value="1" enum="SelectMode">
Allows selecting multiple items by holding [kbd]Ctrl[/kbd] or [kbd]Shift[/kbd].
</constant>
<constant name="SELECT_TOGGLE" value="2" enum="SelectMode">
Allows selecting multiple items by toggling them on and off.
</constant>
</constants>
<theme_items>
<theme_item name="font_color" data_type="color" type="Color" default="Color(0.65, 0.65, 0.65, 1)">
Expand Down
23 changes: 18 additions & 5 deletions scene/gui/item_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ void ItemList::select(int p_idx, bool p_single) {
void ItemList::deselect(int p_idx) {
ERR_FAIL_INDEX(p_idx, items.size());

if (select_mode != SELECT_MULTI) {
if (select_mode == SELECT_SINGLE) {
items.write[p_idx].selected = false;
current = -1;
} else {
Expand Down Expand Up @@ -746,7 +746,7 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) {
return;
}

if (items[i].selectable && (!items[i].selected || allow_reselect)) {
if (items[i].selectable && (!items[i].selected || allow_reselect) && select_mode != SELECT_TOGGLE) {
select(i, select_mode == SELECT_SINGLE || !mb->is_command_or_control_pressed());

if (select_mode == SELECT_SINGLE) {
Expand All @@ -756,6 +756,18 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) {
}
}

if (items[i].selectable && select_mode == SELECT_TOGGLE) {
if (items[i].selected) {
deselect(i);
current = i;
emit_signal(SNAME("multi_selected"), i, false);
} else {
select(i, false);
current = i;
emit_signal(SNAME("multi_selected"), i, true);
}
}

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()) {
Expand Down Expand Up @@ -929,7 +941,7 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) {
}
} else if (p_event->is_action("ui_cancel", true)) {
search_string = "";
} else if (p_event->is_action("ui_select", true) && select_mode == SELECT_MULTI) {
} else if (p_event->is_action("ui_select", true) && (select_mode == SELECT_MULTI || select_mode == SELECT_TOGGLE)) {
if (current >= 0 && current < items.size()) {
if (CAN_SELECT(current) && !items[current].selected) {
select(current, false);
Expand Down Expand Up @@ -1353,7 +1365,7 @@ void ItemList::_notification(int p_what) {
}
}

if (select_mode == SELECT_MULTI && i == current) {
if (i == current && (select_mode == SELECT_MULTI || select_mode == SELECT_TOGGLE)) {
Rect2 r = rcache;
r.position += base_ofs;

Expand Down Expand Up @@ -1913,7 +1925,7 @@ void ItemList::_bind_methods() {

ClassDB::bind_method(D_METHOD("force_update_list_size"), &ItemList::force_update_list_size);

ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Multi"), "set_select_mode", "get_select_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Multi,Toggle"), "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::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
Expand All @@ -1936,6 +1948,7 @@ void ItemList::_bind_methods() {

BIND_ENUM_CONSTANT(SELECT_SINGLE);
BIND_ENUM_CONSTANT(SELECT_MULTI);
BIND_ENUM_CONSTANT(SELECT_TOGGLE);

ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
ADD_SIGNAL(MethodInfo("empty_clicked", PropertyInfo(Variant::VECTOR2, "at_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
Expand Down
3 changes: 2 additions & 1 deletion scene/gui/item_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class ItemList : public Control {

enum SelectMode {
SELECT_SINGLE,
SELECT_MULTI
SELECT_MULTI,
SELECT_TOGGLE,
};

private:
Expand Down