1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-11-28 01:20:55 +01:00

MultiSelect: added ImGuiSelectionBasicStorage::GetStorageIdFromIndex() indirection to be easier on the reader.

Tempting to make it a virtual.
This commit is contained in:
ocornut 2024-06-05 17:16:06 +02:00
parent ab995d3d4f
commit c94cf6f01f
3 changed files with 9 additions and 8 deletions

View File

@ -2823,7 +2823,7 @@ struct ImGuiSelectionRequest
// 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.
// - USING THIS IS NOT MANDATORY. This is only a helper and not a required API. Advanced users are likely to implement their own.
// - USING THIS IS NOT MANDATORY. This is only a helper and not a required API.
// To store a multi-selection, in your application you could:
// - A) Use this helper as a convenience. We use our simple key->value ImGuiStorage as a std::set<ImGuiID> replacement.
// - B) Use your own external storage: e.g. std::set<MyObjectId>, std::vector<MyObjectId>, interval trees, etc.
@ -2845,7 +2845,7 @@ struct ImGuiSelectionBasicStorage
// Members
ImGuiStorage Storage; // [Internal] Selection set. Think of this as similar to e.g. std::set<ImGuiID>
int Size; // Number of selected items (== number of 1 in the Storage), maintained by this helper.
ImGuiID (*AdapterIndexToStorageId)(ImGuiSelectionBasicStorage* self, int idx); // e.g. selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { return ((MyItems**)self->AdapterData)[idx]->ID; };
ImGuiID (*AdapterIndexToStorageId)(ImGuiSelectionBasicStorage* self, int idx); // e.g. selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { return ((MyItems**)self->UserData)[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. Uses 'items_count' based to BeginMultiSelect()
@ -2857,6 +2857,7 @@ struct ImGuiSelectionBasicStorage
void Swap(ImGuiSelectionBasicStorage& r) { Storage.Data.swap(r.Storage.Data); }
bool Contains(ImGuiID id) const { return Storage.GetInt(id, 0) != 0; }
void SetItemSelected(ImGuiID id, bool v) { int* p_int = Storage.GetIntRef(id, 0); if (v && *p_int == 0) { *p_int = 1; Size++; } else if (!v && *p_int != 0) { *p_int = 0; Size--; } }
ImGuiID GetStorageIdFromIndex(int idx) { return AdapterIndexToStorageId(this, idx); }
};
//-----------------------------------------------------------------------------

View File

@ -2798,12 +2798,12 @@ struct ExampleSelectionWithDeletion : ImGuiSelectionBasicStorage
// If focused item is selected: land on first unselected item after focused item.
for (int idx = focused_idx + 1; idx < items_count; idx++)
if (!Contains(AdapterIndexToStorageId(this, idx)))
if (!Contains(GetStorageIdFromIndex(idx)))
return idx;
// If focused item is selected: otherwise return last unselected item before focused item.
for (int idx = IM_MIN(focused_idx, items_count) - 1; idx >= 0; idx--)
if (!Contains(AdapterIndexToStorageId(this, idx)))
if (!Contains(GetStorageIdFromIndex(idx)))
return idx;
return -1;
@ -2822,7 +2822,7 @@ struct ExampleSelectionWithDeletion : ImGuiSelectionBasicStorage
int item_next_idx_to_select = -1;
for (int idx = 0; idx < items.Size; idx++)
{
if (!Contains(AdapterIndexToStorageId(this, idx)))
if (!Contains(GetStorageIdFromIndex(idx)))
new_items.push_back(items[idx]);
if (item_curr_idx_to_select == idx)
item_next_idx_to_select = new_items.Size - 1;
@ -2832,7 +2832,7 @@ struct ExampleSelectionWithDeletion : ImGuiSelectionBasicStorage
// Update selection
Clear();
if (item_next_idx_to_select != -1 && ms_io->NavIdSelected)
SetItemSelected(AdapterIndexToStorageId(this, item_next_idx_to_select), true);
SetItemSelected(GetStorageIdFromIndex(item_next_idx_to_select), true);
}
};

View File

@ -7846,11 +7846,11 @@ void ImGuiSelectionBasicStorage::ApplyRequests(ImGuiMultiSelectIO* ms_io)
{
Storage.Data.reserve(ms_io->ItemsCount);
for (int idx = 0; idx < ms_io->ItemsCount; idx++)
SetItemSelected(AdapterIndexToStorageId(this, idx), true);
SetItemSelected(GetStorageIdFromIndex(idx), true);
}
if (req.Type == ImGuiSelectionRequestType_SetRange)
for (int idx = (int)req.RangeFirstItem; idx <= (int)req.RangeLastItem; idx++)
SetItemSelected(AdapterIndexToStorageId(this, idx), req.Selected);
SetItemSelected(GetStorageIdFromIndex(idx), req.Selected);
}
}