@@ -419,7 +419,7 @@ void ItemList::select(int p_idx, bool p_single) {
419419void ItemList::deselect (int p_idx) {
420420 ERR_FAIL_INDEX (p_idx, items.size ());
421421
422- if (select_mode != SELECT_MULTI ) {
422+ if (select_mode == SELECT_SINGLE ) {
423423 items.write [p_idx].selected = false ;
424424 current = -1 ;
425425 } else {
@@ -746,7 +746,7 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) {
746746 return ;
747747 }
748748
749- if (items[i].selectable && (!items[i].selected || allow_reselect)) {
749+ if (items[i].selectable && (!items[i].selected || allow_reselect) && select_mode != SELECT_TOGGLE ) {
750750 select (i, select_mode == SELECT_SINGLE || !mb->is_command_or_control_pressed ());
751751
752752 if (select_mode == SELECT_SINGLE) {
@@ -756,6 +756,18 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) {
756756 }
757757 }
758758
759+ if (items[i].selectable && select_mode == SELECT_TOGGLE) {
760+ if (items[i].selected ) {
761+ deselect (i);
762+ current = i;
763+ emit_signal (SNAME (" multi_selected" ), i, false );
764+ } else {
765+ select (i, false );
766+ current = i;
767+ emit_signal (SNAME (" multi_selected" ), i, true );
768+ }
769+ }
770+
759771 emit_signal (SNAME (" item_clicked" ), i, get_local_mouse_position (), mb->get_button_index ());
760772
761773 if (mb->get_button_index () == MouseButton::LEFT && mb->is_double_click ()) {
@@ -929,7 +941,7 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) {
929941 }
930942 } else if (p_event->is_action (" ui_cancel" , true )) {
931943 search_string = " " ;
932- } else if (p_event->is_action (" ui_select" , true ) && select_mode == SELECT_MULTI) {
944+ } else if (p_event->is_action (" ui_select" , true ) && ( select_mode == SELECT_MULTI || select_mode == SELECT_TOGGLE) ) {
933945 if (current >= 0 && current < items.size ()) {
934946 if (CAN_SELECT (current) && !items[current].selected ) {
935947 select (current, false );
@@ -1353,7 +1365,7 @@ void ItemList::_notification(int p_what) {
13531365 }
13541366 }
13551367
1356- if (select_mode == SELECT_MULTI && i == current ) {
1368+ if (i == current && (select_mode == SELECT_MULTI || select_mode == SELECT_TOGGLE) ) {
13571369 Rect2 r = rcache;
13581370 r.position += base_ofs;
13591371
@@ -1913,7 +1925,7 @@ void ItemList::_bind_methods() {
19131925
19141926 ClassDB::bind_method (D_METHOD (" force_update_list_size" ), &ItemList::force_update_list_size);
19151927
1916- ADD_PROPERTY (PropertyInfo (Variant::INT, " select_mode" , PROPERTY_HINT_ENUM, " Single,Multi" ), " set_select_mode" , " get_select_mode" );
1928+ ADD_PROPERTY (PropertyInfo (Variant::INT, " select_mode" , PROPERTY_HINT_ENUM, " Single,Multi,Toggle " ), " set_select_mode" , " get_select_mode" );
19171929 ADD_PROPERTY (PropertyInfo (Variant::BOOL, " allow_reselect" ), " set_allow_reselect" , " get_allow_reselect" );
19181930 ADD_PROPERTY (PropertyInfo (Variant::BOOL, " allow_rmb_select" ), " set_allow_rmb_select" , " get_allow_rmb_select" );
19191931 ADD_PROPERTY (PropertyInfo (Variant::BOOL, " allow_search" ), " set_allow_search" , " get_allow_search" );
@@ -1936,6 +1948,7 @@ void ItemList::_bind_methods() {
19361948
19371949 BIND_ENUM_CONSTANT (SELECT_SINGLE);
19381950 BIND_ENUM_CONSTANT (SELECT_MULTI);
1951+ BIND_ENUM_CONSTANT (SELECT_TOGGLE);
19391952
19401953 ADD_SIGNAL (MethodInfo (" item_selected" , PropertyInfo (Variant::INT, " index" )));
19411954 ADD_SIGNAL (MethodInfo (" empty_clicked" , PropertyInfo (Variant::VECTOR2, " at_position" ), PropertyInfo (Variant::INT, " mouse_button_index" )));
0 commit comments