1
0
mirror of https://github.com/ocornut/imgui.git synced 2025-02-20 04:21:01 +01:00

Updated Multi Select (markdown)

omar 2024-06-05 17:39:06 +02:00
parent 45090a144f
commit cbe27bbfb8

@ -4,7 +4,7 @@
![Multi-Select](https://github.com/ocornut/imgui/assets/8225057/442d5689-3e91-4771-9e25-498fbeb9ea32)
- **This system implements standard multi-selection idioms** (CTRL+Mouse/Keyboard, SHIFT+Mouse/Keyboard, etc) and supports a clipper being used. Handling this manually and correctly is tricky, this is why we provide the functionality. If you don't need SHIFT+Mouse/Keyboard range-select + clipping, you could technically implement a simple form of multi-selection yourself, by reacting to click/presses on Selectable() items.
- TreeNode() and Selectable() are supported but custom widgets may use it as well.
- TreeNode(), Selectable(), Checkbox() are supported but custom widgets may use it as well.
- **In the spirit of Dear ImGui design, your code owns actual selection data**. This is designed to allow all kinds of selection storage you may use in your application e.g. external selection (set/map/hash), intrusive selection (bool inside your objects) etc.
- The work involved to deal with multi-selection differs whether you want to only submit visible items and clip others, or submit all items regardless of their visibility. Clipping items is more efficient and will allow you to deal with large lists (1k~100k items). See "Usage flow" section below for details. If you are not sure, always start without clipping! You can work your way to the optimized version afterwards.
@ -24,7 +24,7 @@
```cpp
// Main API
ImGuiMultiSelectIO* BeginMultiSelect(ImGuiMultiSelectFlags flags, int current_selection_size = -1);
ImGuiMultiSelectIO* BeginMultiSelect(ImGuiMultiSelectFlags flags, int selection_size = -1, int items_count = -1);
ImGuiMultiSelectIO* EndMultiSelect();
void SetNextItemSelectionUserData(ImGuiSelectionUserData selection_user_data);
```
@ -42,6 +42,7 @@ struct ImGuiMultiSelectIO
ImGuiSelectionUserData NavIdItem; // ms:w, app:r / // (If using deletion) Last known SetNextItemSelectionUserData() value for NavId (if part of submitted items).
bool NavIdSelected; // ms:w, app:r / app:r // (If using deletion) Last known selection state for NavId (if part of submitted items).
bool RangeSrcReset; // app:w / ms:r // (If using deletion) Set before EndMultiSelect() to reset ResetSrcItem (e.g. if deleted selection).
int ItemsCount; // ms:w, app:r / app:r // 'int items_count' parameter to BeginMultiSelect() is copied here for convenience, allowing simpler calls to your ApplyRequests handler. Not used internally.
};
// Selection request item
@ -92,7 +93,7 @@ enum ImGuiSelectionRequestType
being syntactically easier to downcast. Feel free to reinterpret_cast and store a pointer inside.
- If you need to wrap this API for another language/framework, feel free to expose this as 'int' if simpler.
### With the ImGuiSelectionBasicStorage helper
### Using ImGuiSelectionBasicStorage helper
`ImGuiSelectionBasicStorage` is an optional helper to store multi-selection state + apply multi-selection requests.
- Used by our demos and provided as a convenience to easily implement basic multi-selection.
@ -105,8 +106,8 @@ static ImGuiSelectionBasicStorage selection; // Your selection
selection.AdapterData = (void*)&items; // Setup adapter so selection.ApplyRequests() function can convert indexes to identifiers.
selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { return ((vector<MyItem>*)self->AdapterData))[idx].ID; };
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ImGuiMultiSelectFlags_None);
selection.ApplyRequests(ms_io, items.Size);
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ImGuiMultiSelectFlags_None, selection.Size, items.Size);
selection.ApplyRequests(ms_io);
for (int idx = 0; idx < items.Size; idx++)
{
bool item_is_selected = selection.Contains(items[idx].ID);
@ -114,7 +115,7 @@ for (int idx = 0; idx < items.Size; idx++)
ImGui::Selectable(label, item_is_selected);
}
ms_io = ImGui::EndMultiSelect();
selection.ApplyRequests(ms_io, items.Size);
selection.ApplyRequests(ms_io);
```
To store a multi-selection, in your real application you could: