From 847b1dde8c5c9dbad9fced2cbed378e1429ff0fb Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 4 Aug 2023 10:23:44 +0200 Subject: [PATCH] MultiSelect: (Breaking) Fix + Rename ImGuiMultiSelectFlags_NoMultiSelect to ImGuiMultiSelectFlags_SingleSelect as it seems easier to grasp. Feature was broken by "Tidying up..." June 30 commit. --- imgui.h | 6 +++--- imgui_demo.cpp | 2 +- imgui_widgets.cpp | 16 +++++++++------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/imgui.h b/imgui.h index 1ac315d70..aa9774759 100644 --- a/imgui.h +++ b/imgui.h @@ -2724,12 +2724,12 @@ struct ImColor #define IMGUI_HAS_MULTI_SELECT // Multi-Select/Range-Select WIP branch // <-- This is currently _not_ in the top of imgui.h to prevent merge conflicts. // Flags for BeginMultiSelect(). -// (we provide 'ImGuiMultiSelectFlags_NoMultiSelect' for consistency and flexiblity, but it essentially disable the main purpose of BeginMultiSelect(). -// If you use 'ImGuiMultiSelectFlags_NoMultiSelect' you can handle single-selection in a simpler way by just calling Selectable()/TreeNode() and reacting on clicks). +// (we provide 'ImGuiMultiSelectFlags_SingleSelect' for consistency and flexiblity to allow a single-selection to use same code/logic, but it essentially disable the biggest purpose of BeginMultiSelect(). +// If you use 'ImGuiMultiSelectFlags_SingleSelect' you can handle single-selection in a simpler way by just calling Selectable()/TreeNode() and reacting on clicks). enum ImGuiMultiSelectFlags_ { ImGuiMultiSelectFlags_None = 0, - ImGuiMultiSelectFlags_NoMultiSelect = 1 << 0, // Disable selecting more than one item. This is not very useful at this kind of selection can be implemented without BeginMultiSelect(), but this is available for consistency. + ImGuiMultiSelectFlags_SingleSelect = 1 << 0, // Disable selecting more than one item. This is available to allow single-selection code to use same code/logic is desired, but may not be very useful. ImGuiMultiSelectFlags_NoSelectAll = 1 << 1, // Disable CTRL+A shortcut to set RequestSelectAll ImGuiMultiSelectFlags_ClearOnEscape = 1 << 2, // Clear selection when pressing Escape while scope is focused. ImGuiMultiSelectFlags_ClearOnClickWindowVoid= 1 << 3, // Clear selection when clicking on empty location within host window (use if BeginMultiSelect() covers a whole window) diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 27aa35694..c532ff9b4 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -3106,7 +3106,7 @@ static void ShowDemoWindowMultiSelect() ImGui::Checkbox("Enable drag & drop", &use_drag_drop); ImGui::Checkbox("Show in a table", &show_in_table); ImGui::Checkbox("Show color button", &show_color_button); - ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoMultiSelect", &flags, ImGuiMultiSelectFlags_NoMultiSelect); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_SingleSelect", &flags, ImGuiMultiSelectFlags_SingleSelect); ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoSelectAll", &flags, ImGuiMultiSelectFlags_NoSelectAll); ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ClearOnEscape", &flags, ImGuiMultiSelectFlags_ClearOnEscape); ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ClearOnClickWindowVoid", &flags, ImGuiMultiSelectFlags_ClearOnClickWindowVoid); diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 8ffbd27c3..58467a1de 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -7180,7 +7180,7 @@ ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags) } // Shortcut: Select all (CTRL+A) - if (!(flags & ImGuiMultiSelectFlags_NoMultiSelect) && !(flags & ImGuiMultiSelectFlags_NoSelectAll)) + if (!(flags & ImGuiMultiSelectFlags_SingleSelect) && !(flags & ImGuiMultiSelectFlags_NoSelectAll)) if (Shortcut(ImGuiMod_Ctrl | ImGuiKey_A)) ms->BeginIO.RequestSelectAll = true; @@ -7332,7 +7332,7 @@ void ImGui::MultiSelectItemFooter(ImGuiID id, bool* p_selected, bool* p_pressed) void* item_data = (void*)g.NextItemData.SelectionUserData; - const bool is_multiselect = (ms->Flags & ImGuiMultiSelectFlags_NoMultiSelect) == 0; + const bool is_multiselect = (ms->Flags & ImGuiMultiSelectFlags_SingleSelect) == 0; bool is_ctrl = (ms->KeyMods & ImGuiMod_Ctrl) != 0; bool is_shift = (ms->KeyMods & ImGuiMod_Shift) != 0; @@ -7411,18 +7411,20 @@ void ImGui::MultiSelectItemFooter(ImGuiID id, bool* p_selected, bool* p_pressed) ms->EndIO.RangeDirection = +1; } - if (input_source == ImGuiInputSource_Mouse || g.NavActivateId == id) + if (!is_multiselect) { - if (is_multiselect && !is_ctrl) + ms->EndIO.RequestClear = true; + } + else if (input_source == ImGuiInputSource_Mouse || g.NavActivateId == id) + { + if (!is_ctrl) ms->EndIO.RequestClear = true; } else if (input_source == ImGuiInputSource_Keyboard || input_source == ImGuiInputSource_Gamepad) { - if (is_multiselect && is_shift && !is_ctrl) // Without Shift the RequestClear was done in BeginIO, not necessary to do again. + if (is_shift && !is_ctrl) // Without Shift the RequestClear was done in BeginIO, not necessary to do again. ms->EndIO.RequestClear = true; } - else if (!is_multiselect) - ms->EndIO.RequestClear = true; } // Update/store the selection state of the Source item (used by CTRL+SHIFT, when Source is unselected we perform a range unselect)