diff --git a/imgui.h b/imgui.h index d5a3bda52..58409f611 100644 --- a/imgui.h +++ b/imgui.h @@ -2843,14 +2843,14 @@ struct ImGuiSelectionBasicStorage // Members ImGuiStorage Storage; // [Internal] Selection set. Think of this as similar to e.g. std::set int Size; // Number of selected items (== number of 1 in the Storage), maintained by this helper. - void* AdapterData; // Adapter to convert item index to item identifier // e.g. selection.AdapterData = (void*)my_items; ImGuiID (*AdapterIndexToStorageId)(ImGuiSelectionBasicStorage* self, int idx); // e.g. selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { return ((MyItems**)self->AdapterData)[idx]->ID; }; + void* UserData; // User data for use by adapter function // e.g. selection.UserData = (void*)my_items; // Methods: apply selection requests coming from BeginMultiSelect() and EndMultiSelect() functions IMGUI_API void ApplyRequests(ImGuiMultiSelectIO* ms_io, int items_count); // Methods: selection storage - ImGuiSelectionBasicStorage() { Clear(); AdapterData = NULL; AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage*, int idx) { return (ImGuiID)idx; }; } + ImGuiSelectionBasicStorage() { Clear(); UserData = NULL; AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage*, int idx) { return (ImGuiID)idx; }; } void Clear() { Storage.Data.resize(0); Size = 0; } void Swap(ImGuiSelectionBasicStorage& r) { Storage.Data.swap(r.Storage.Data); } bool Contains(ImGuiID id) const { return Storage.GetInt(id, 0) != 0; } diff --git a/imgui_demo.cpp b/imgui_demo.cpp index c984ee40f..efaf116dd 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -2872,8 +2872,8 @@ struct ExampleDualListBox void ApplySelectionRequests(ImGuiMultiSelectIO* ms_io, int side) { // In this example we store item id in selection (instead of item index) - Selections[side].AdapterData = Items[side].Data; - Selections[side].AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { ImGuiID* items = (ImGuiID*)self->AdapterData; return items[idx]; }; + Selections[side].UserData = Items[side].Data; + Selections[side].AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { ImGuiID* items = (ImGuiID*)self->UserData; return items[idx]; }; Selections[side].ApplyRequests(ms_io, Items[side].Size); } static int IMGUI_CDECL CompareItemsByValue(const void* lhs, const void* rhs) @@ -3135,8 +3135,8 @@ static void ShowDemoWindowMultiSelect() // Use a custom selection.Adapter: store item identifier in Selection (instead of index) static ImVector items; static ExampleSelectionWithDeletion selection; - selection.AdapterData = (void*)&items; - selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { ImVector* p_items = (ImVector*)self->AdapterData; return (*p_items)[idx]; }; // Index -> ID + selection.UserData = (void*)&items; + selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { ImVector* p_items = (ImVector*)self->UserData; return (*p_items)[idx]; }; // Index -> ID ImGui::Text("Added features:"); ImGui::BulletText("Dynamic list with Delete key support."); @@ -9847,8 +9847,8 @@ struct ExampleAssetsBrowser ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ms_flags, Selection.Size); // Use custom selection adapter: store ID in selection (recommended) - Selection.AdapterData = this; - Selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self_, int idx) { ExampleAssetsBrowser* self = (ExampleAssetsBrowser*)self_->AdapterData; return self->Items[idx].ID; }; + Selection.UserData = this; + Selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self_, int idx) { ExampleAssetsBrowser* self = (ExampleAssetsBrowser*)self_->UserData; return self->Items[idx].ID; }; Selection.ApplyRequests(ms_io, Items.Size); const bool want_delete = (ImGui::Shortcut(ImGuiKey_Delete, ImGuiInputFlags_Repeat) && (Selection.Size > 0)) || RequestDelete; diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 062b36452..7602b2cd1 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -7306,7 +7306,7 @@ static void DebugLogMultiSelectRequests(const char* function, const ImGuiMultiSe // Lifetime: don't hold on ImGuiMultiSelectIO* pointers over multiple frames or past any subsequent call to BeginMultiSelect() or EndMultiSelect(). // Passing 'current_selection_size' is currently optional: // - it is useful for shortcut routing with ImGuiMultiSelectFlags_ClearOnEscape: so we can have Escape be used to clear selection THEN to exit child window. -// - if it is costly for you to compute, but can easily tell if your selection is empty or not, you may alter the ImGuiMultiSelectFlags_ClearOnEscape flag based on that. +// - if it is costly for you to compute, but can easily tell if your selection is empty or not, you may pass 1, or use the ImGuiMultiSelectFlags_ClearOnEscape flag dynamically. ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags, int current_selection_size) { ImGuiContext& g = *GImGui; @@ -7347,6 +7347,7 @@ ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags, int cur storage->Window = window; ms->Storage = storage; + // Output to user ms->IO.RangeSrcItem = storage->RangeSrcItem; ms->IO.NavIdItem = storage->NavIdItem; ms->IO.NavIdSelected = (storage->NavIdSelected == 1) ? true : false;