From abfa8487ebd0df379149570b7da3b8d552bb5cd9 Mon Sep 17 00:00:00 2001 From: omar Date: Tue, 14 Jan 2020 16:18:55 +0100 Subject: [PATCH] RangeSelect/MultiSelect: Renamed SetNextItemMultiSelectData() to SetNextItemSelectionData() --- imgui.h | 6 +++--- imgui_demo.cpp | 12 ++++++++++-- imgui_internal.h | 2 +- imgui_widgets.cpp | 12 ++++++------ 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/imgui.h b/imgui.h index f17ed7a6..bfd4a8f1 100644 --- a/imgui.h +++ b/imgui.h @@ -620,7 +620,7 @@ namespace ImGui // When enabled, Selectable() and TreeNode() functions will return true when selection needs toggling. IMGUI_API ImGuiMultiSelectData* BeginMultiSelect(ImGuiMultiSelectFlags flags, void* range_ref, bool range_ref_is_selected); IMGUI_API ImGuiMultiSelectData* EndMultiSelect(); - IMGUI_API void SetNextItemMultiSelectData(void* item_data); + IMGUI_API void SetNextItemSelectionData(void* item_data); // Widgets: List Boxes // - This is essentially a thin wrapper to using BeginChild/EndChild with some stylistic changes. @@ -2353,7 +2353,7 @@ struct ImGuiListClipper // performance penalty, but requires a little more work on the code. If you only have a few hundreds elements in your possible selection set, // you may as well not bother with clipping, as the cost should be negligible (as least on imgui side). // If you are not sure, always start without clipping and you can work your way to the more optimized version afterwards. -// - The void* Src/Dst value represent a selectable object. They are the values you pass to SetNextItemMultiSelectData(). +// - The void* Src/Dst value represent a selectable object. They are the values you pass to SetNextItemSelectionData(). // Storing an integer index is the easiest thing to do, as SetRange requests will give you two end points. But the code never assume that sortable integers are used. // - In the spirit of imgui design, your code own the selection data. So this is designed to handle all kind of selection data: instructive (store a bool inside each object), // external array (store an array aside from your objects), set (store only selected items in a hash/map/set), using intervals (store indices in an interval tree), etc. @@ -2364,7 +2364,7 @@ struct ImGuiListClipper // 3) Set RangeSrcPassedBy=true if the RangeSrc item is part of the items clipped before the first submitted/visible item. [Only required if you are using a clipper in step 4] // This is because for range-selection we need to know if we are currently "inside" or "outside" the range. // If you are using integer indices everywhere, this is easy to compute: if (clipper.DisplayStart > (int)data->RangeSrc) { data->RangeSrcPassedBy = true; } -// 4) Submit your items with SetNextItemMultiSelectData() + Selectable()/TreeNode() calls. +// 4) Submit your items with SetNextItemSelectionData() + Selectable()/TreeNode() calls. // Call IsItemSelectionToggled() to query with the selection state has been toggled, in which you need the info immediately (before EndMultiSelect()) for your display. // When cannot reliably return a "IsItemSelected()" value because we need to consider clipped (unprocessed) item, this is why we return a toggle event instead. // 5) Call EndMultiSelect(). Save the value of ->RangeSrc for the next frame (you may convert the value in a format that is safe for persistance) diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 9e48d8e9..e3b8ac19 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -1260,7 +1260,7 @@ static void ShowDemoWindowWidgets() }; int COUNT = 1000; - HelpMarker("Hold CTRL and click to select multiple items. Hold SHIFT to select a range."); + HelpMarker("Hold CTRL and click to select multiple items. Hold SHIFT to select a range. Keyboard is also supported."); ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", (unsigned int *)&ImGui::GetIO().ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard); if (ImGui::BeginListBox("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20))) @@ -1268,6 +1268,7 @@ static void ShowDemoWindowWidgets() ImGuiMultiSelectData* multi_select_data = ImGui::BeginMultiSelect(ImGuiMultiSelectFlags_None, (void*)(intptr_t)selection_ref, selection.GetSelected((int)selection_ref)); if (multi_select_data->RequestClear) { selection.Clear(); } if (multi_select_data->RequestSelectAll) { selection.SelectAll(COUNT); } + ImVec2 color_button_sz(ImGui::GetFontSize(), ImGui::GetFontSize()); ImGuiListClipper clipper; clipper.Begin(COUNT); while (clipper.Step()) @@ -1280,7 +1281,14 @@ static void ShowDemoWindowWidgets() char label[64]; sprintf(label, "Object %05d (category: %s)", n, random_names[n % IM_ARRAYSIZE(random_names)]); bool item_is_selected = selection.GetSelected(n); - ImGui::SetNextItemMultiSelectData((void*)(intptr_t)n); + + // Emit a color button, to test that Shift+LeftArrow landing on an item that is not part + // of the selection scope doesn't erroneously alter our selection. + ImVec4 dummy_col = ImColor((ImU32)ImGui::GetID(label)); + ImGui::ColorButton("##", dummy_col, ImGuiColorEditFlags_NoTooltip, color_button_sz); + ImGui::SameLine(); + + ImGui::SetNextItemSelectionData((void*)(intptr_t)n); if (ImGui::Selectable(label, item_is_selected)) selection.SetSelected(n, !item_is_selected); ImGui::PopID(); diff --git a/imgui_internal.h b/imgui_internal.h index 7df58b80..807a82a2 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -1095,7 +1095,7 @@ struct ImGuiNextItemData ImGuiCond OpenCond; bool OpenVal; // Set by SetNextItemOpen() ImGuiID MultiSelectScopeId; // Set by SetNextItemMultiSelectData() - void* MultiSelectData; + void* SelectionData; // Set by SetNextItemSelectionData() (note that NULL/0 is a valid value) ImGuiNextItemData() { memset(this, 0, sizeof(*this)); } inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; } // Also cleared manually by ItemAdd()! diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 0e8eaafe..6a407916 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -6359,7 +6359,7 @@ bool ImGui::Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags //------------------------------------------------------------------------- // - BeginMultiSelect() // - EndMultiSelect() -// - SetNextItemMultiSelectData() +// - SetNextItemSelectionData() // - MultiSelectItemHeader() [Internal] // - MultiSelectItemFooter() [Internal] //------------------------------------------------------------------------- @@ -6429,11 +6429,11 @@ ImGuiMultiSelectData* ImGui::EndMultiSelect() return &state->Out; } -void ImGui::SetNextItemMultiSelectData(void* item_data) +void ImGui::SetNextItemSelectionData(void* item_data) { ImGuiContext& g = *GImGui; IM_ASSERT(g.MultiSelectScopeId != 0); - g.NextItemData.MultiSelectData = item_data; + g.NextItemData.SelectionData = item_data; g.NextItemData.MultiSelectScopeId = g.MultiSelectScopeId; } @@ -6442,8 +6442,8 @@ void ImGui::MultiSelectItemHeader(ImGuiID id, bool* p_selected) ImGuiContext& g = *GImGui; ImGuiMultiSelectState* state = &g.MultiSelectState; - IM_ASSERT(g.NextItemData.MultiSelectScopeId == g.MultiSelectScopeId && "Forgot to call SetNextItemMultiSelectData() prior to item, required in BeginMultiSelect()/EndMultiSelect() scope"); - void* item_data = g.NextItemData.MultiSelectData; + IM_ASSERT(g.NextItemData.MultiSelectScopeId == g.MultiSelectScopeId && "Forgot to call SetNextItemSelectionData() prior to item, required in BeginMultiSelect()/EndMultiSelect() scope"); + void* item_data = g.NextItemData.SelectionData; // Apply Clear/SelectAll requests requested by BeginMultiSelect(). // This is only useful if the user hasn't processed them already, and this only works if the user isn't using the clipper. @@ -6482,7 +6482,7 @@ void ImGui::MultiSelectItemFooter(ImGuiID id, bool* p_selected, bool* p_pressed) ImGuiWindow* window = g.CurrentWindow; ImGuiMultiSelectState* state = &g.MultiSelectState; - void* item_data = g.NextItemData.MultiSelectData; + void* item_data = g.NextItemData.SelectionData; g.NextItemData.MultiSelectScopeId = 0; bool selected = *p_selected;