Skip to content

Commit

Permalink
RangeSelect/MultiSelect: added ImGuiSelectionBasicStorage::GetStorage…
Browse files Browse the repository at this point in the history
…IdFromIndex() indirection to be easier on the reader.

Tempting to make it a virtual.
  • Loading branch information
ocornut committed Jun 5, 2024
1 parent 683bcee commit 991dbd6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -2769,7 +2769,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()
Expand All @@ -2781,6 +2781,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); }
};

//-----------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2774,12 +2774,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;
Expand All @@ -2798,7 +2798,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;
Expand All @@ -2808,7 +2808,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);
}
};

Expand Down
4 changes: 2 additions & 2 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7710,11 +7710,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);
}
}

Expand Down

0 comments on commit 991dbd6

Please sign in to comment.