1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-09-23 19:18:34 +02:00

MultiSelect: ImGuiSelectionBasicStorage: move function bodies to cpp file.

+ make ImGuiStorage::BuildSortByKey() less affected by msvc debug mode.
This commit is contained in:
ocornut 2024-06-11 19:45:21 +02:00
parent 2af3b2ac81
commit c07864f64a
3 changed files with 49 additions and 12 deletions

View File

@ -2567,10 +2567,12 @@ static int IMGUI_CDECL PairComparerByID(const void* lhs, const void* rhs)
}
// For quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once.
IM_MSVC_RUNTIME_CHECKS_OFF
void ImGuiStorage::BuildSortByKey()
{
ImQsort(Data.Data, (size_t)Data.Size, sizeof(ImGuiStoragePair), PairComparerByID);
}
IM_MSVC_RUNTIME_CHECKS_RESTORE
int ImGuiStorage::GetInt(ImGuiID key, int default_val) const
{

22
imgui.h
View File

@ -2840,16 +2840,14 @@ struct ImGuiSelectionBasicStorage
ImGuiID (*AdapterIndexToStorageId)(ImGuiSelectionBasicStorage* self, int idx); // e.g. selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { return ((MyItems**)self->UserData)[idx]->ID; };
// Methods
IMGUI_API void ApplyRequests(ImGuiMultiSelectIO* ms_io);// Apply selection requests coming from BeginMultiSelect() and EndMultiSelect() functions. It uses 'items_count' passed to BeginMultiSelect()
IMGUI_API ImGuiID GetNextSelectedItem(void** opaque_it); // Iterate selection with 'void* it = NULL; while (ImGuiId id = selection.GetNextSelectedItem(&it)) { ... }'
bool Contains(ImGuiID id) const { return _Storage.GetInt(id, 0) != 0; } // Query if an item id is in selection.
ImGuiID GetStorageIdFromIndex(int idx) { return AdapterIndexToStorageId(this, idx); } // Convert index to item id based on provided adapter.
// [Internal, rarely called directly by end-user]
ImGuiSelectionBasicStorage() { Size = 0; 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); int lhs_size = Size; Size = r.Size; r.Size = lhs_size; }
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--; } }
ImGuiSelectionBasicStorage();
IMGUI_API void ApplyRequests(ImGuiMultiSelectIO* ms_io); // Apply selection requests coming from BeginMultiSelect() and EndMultiSelect() functions. It uses 'items_count' passed to BeginMultiSelect()
IMGUI_API bool Contains(ImGuiID id) const; // Query if an item id is in selection.
IMGUI_API void Clear(); // Clear selection
IMGUI_API void Swap(ImGuiSelectionBasicStorage& r); // Swap two selections
IMGUI_API void SetItemSelected(ImGuiID id, bool selected); // Add/remove an item from selection (generally done by ApplyRequests() function)
IMGUI_API ImGuiID GetNextSelectedItem(void** opaque_it); // Iterate selection with 'void* it = NULL; while (ImGuiId id = selection.GetNextSelectedItem(&it)) { ... }'
inline ImGuiID GetStorageIdFromIndex(int idx) { return AdapterIndexToStorageId(this, idx); } // Convert index to item id based on provided adapter.
};
// Optional helper to apply multi-selection requests to existing randomly accessible storage.
@ -2861,8 +2859,8 @@ struct ImGuiSelectionExternalStorage
void (*AdapterSetItemSelected)(ImGuiSelectionExternalStorage* self, int idx, bool selected); // e.g. AdapterSetItemSelected = [](ImGuiSelectionExternalStorage* self, int idx, bool selected) { ((MyItems**)self->UserData)[idx]->Selected = selected; }
// Methods
ImGuiSelectionExternalStorage() { UserData = NULL; AdapterSetItemSelected = NULL; }
IMGUI_API void ApplyRequests(ImGuiMultiSelectIO* ms_io); // Generic function, using AdapterSetItemSelected()
IMGUI_API ImGuiSelectionExternalStorage();
IMGUI_API void ApplyRequests(ImGuiMultiSelectIO* ms_io); // Apply selection requests by using AdapterSetItemSelected() calls
};
//-----------------------------------------------------------------------------

View File

@ -7821,6 +7821,30 @@ void ImGui::DebugNodeMultiSelectState(ImGuiMultiSelectState* storage)
// - ImGuiSelectionExternalStorage
//-------------------------------------------------------------------------
ImGuiSelectionBasicStorage::ImGuiSelectionBasicStorage()
{
Size = 0;
UserData = NULL;
AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage*, int idx) { return (ImGuiID)idx; };
}
void ImGuiSelectionBasicStorage::Clear()
{
Size = 0;
_Storage.Data.resize(0);
}
void ImGuiSelectionBasicStorage::Swap(ImGuiSelectionBasicStorage& r)
{
ImSwap(Size, r.Size);
_Storage.Data.swap(r._Storage.Data);
}
bool ImGuiSelectionBasicStorage::Contains(ImGuiID id) const
{
return _Storage.GetInt(id, 0) != 0;
}
// GetNextSelectedItem() is an abstraction allowing us to change our underlying actual storage system without impacting user.
// (e.g. store unselected vs compact down, compact down on demand, use raw ImVector<ImGuiID> instead of ImGuiStorage...)
ImGuiID ImGuiSelectionBasicStorage::GetNextSelectedItem(void** opaque_it)
@ -7838,6 +7862,13 @@ ImGuiID ImGuiSelectionBasicStorage::GetNextSelectedItem(void** opaque_it)
return has_more ? it->key : 0;
}
void ImGuiSelectionBasicStorage::SetItemSelected(ImGuiID id, bool selected)
{
int* p_int = _Storage.GetIntRef(id, 0);
if (selected && *p_int == 0) { *p_int = 1; Size++; }
else if (!selected && *p_int != 0) { *p_int = 0; Size--; }
}
// Optimized for batch edits (with same value of 'selected')
static void ImGuiSelectionBasicStorage_BatchSetItemSelected(ImGuiSelectionBasicStorage* selection, ImGuiID id, bool selected, int size_before_amends)
{
@ -7914,6 +7945,12 @@ void ImGuiSelectionBasicStorage::ApplyRequests(ImGuiMultiSelectIO* ms_io)
//-------------------------------------------------------------------------
ImGuiSelectionExternalStorage::ImGuiSelectionExternalStorage()
{
UserData = NULL;
AdapterSetItemSelected = NULL;
}
// Apply requests coming from BeginMultiSelect() and EndMultiSelect().
// We also pull 'ms_io->ItemsCount' as passed for BeginMultiSelect() for consistency with ImGuiSelectionBasicStorage
// This makes no assumption about underlying storage.