MCPcopy Create free account
hub / github.com/BlitterStudio/amiberry / ApplyRequests

Method ApplyRequests

external/imgui/imgui_widgets.cpp:8688–8744  ·  view source on GitHub ↗

Apply requests coming from BeginMultiSelect() and EndMultiSelect(). - Enable 'Demo->Tools->Debug Log->Selection' to see selection requests as they happen. - Honoring SetRange requests requires that you can iterate/interpolate between RangeFirstItem and RangeLastItem. - In this demo we often submit indices to SetNextItemSelectionUserData() + store the same indices in persistent selection. - Your co

Source from the content-addressed store, hash-verified

8686// if (req.Type == ImGuiSelectionRequestType_SetRange) { for (int n = (int)ms_io->RangeFirstItem; n <= (int)ms_io->RangeLastItem; n++) { SetItemSelected(n, ms_io->Selected); } }
8687// }
8688void ImGuiSelectionBasicStorage::ApplyRequests(ImGuiMultiSelectIO* ms_io)
8689{
8690 // For convenience we obtain ItemsCount as passed to BeginMultiSelect(), which is optional.
8691 // It makes sense when using ImGuiSelectionBasicStorage to simply pass your items count to BeginMultiSelect().
8692 // Other scheme may handle SetAll differently.
8693 IM_ASSERT(ms_io->ItemsCount != -1 && "Missing value for items_count in BeginMultiSelect() call!");
8694 IM_ASSERT(AdapterIndexToStorageId != NULL);
8695
8696 // This is optimized/specialized to cope with very large selections (e.g. 100k+ items)
8697 // - A simpler version could call SetItemSelected() directly instead of ImGuiSelectionBasicStorage_BatchSetItemSelected() + ImGuiSelectionBasicStorage_BatchFinish().
8698 // - Optimized select can append unsorted, then sort in a second pass. Optimized unselect can clear in-place then compact in a second pass.
8699 // - A more optimal version wouldn't even use ImGuiStorage but directly a ImVector<ImGuiID> to reduce bandwidth, but this is a reasonable trade off to reuse code.
8700 // - There are many ways this could be better optimized. The worse case scenario being: using BoxSelect2d in a grid, box-select scrolling down while wiggling
8701 // left and right: it affects coarse clipping + can emit multiple SetRange with 1 item each.
8702 // FIXME-OPT: For each block of consecutive SetRange request:
8703 // - add all requests to a sorted list, store ID, selected, offset in ImGuiStorage.
8704 // - rewrite sorted storage a single time.
8705 for (ImGuiSelectionRequest& req : ms_io->Requests)
8706 {
8707 if (req.Type == ImGuiSelectionRequestType_SetAll)
8708 {
8709 Clear();
8710 if (req.Selected)
8711 {
8712 _Storage.Data.reserve(ms_io->ItemsCount);
8713 const int size_before_amends = _Storage.Data.Size;
8714 for (int idx = 0; idx < ms_io->ItemsCount; idx++, _SelectionOrder++)
8715 ImGuiSelectionBasicStorage_BatchSetItemSelected(this, GetStorageIdFromIndex(idx), req.Selected, size_before_amends, _SelectionOrder);
8716 ImGuiSelectionBasicStorage_BatchFinish(this, req.Selected, size_before_amends);
8717 }
8718 }
8719 else if (req.Type == ImGuiSelectionRequestType_SetRange)
8720 {
8721 const int selection_changes = (int)req.RangeLastItem - (int)req.RangeFirstItem + 1;
8722 //ImGuiContext& g = *GImGui; IMGUI_DEBUG_LOG_SELECTION("Req %d/%d: set %d to %d\n", ms_io->Requests.index_from_ptr(&req), ms_io->Requests.Size, selection_changes, req.Selected);
8723 if (selection_changes == 1 || (selection_changes < Size / 100))
8724 {
8725 // Multiple sorted insertion + copy likely to be faster.
8726 // Technically we could do a single copy with a little more work (sort sequential SetRange requests)
8727 for (int idx = (int)req.RangeFirstItem; idx <= (int)req.RangeLastItem; idx++)
8728 SetItemSelected(GetStorageIdFromIndex(idx), req.Selected);
8729 }
8730 else
8731 {
8732 // Append insertion + single sort likely be faster.
8733 // Use req.RangeDirection to set order field so that Shift+Clicking from 1 to 5 is different than Shift+Clicking from 5 to 1
8734 const int size_before_amends = _Storage.Data.Size;
8735 int selection_order = _SelectionOrder + ((req.RangeDirection < 0) ? selection_changes - 1 : 0);
8736 for (int idx = (int)req.RangeFirstItem; idx <= (int)req.RangeLastItem; idx++, selection_order += req.RangeDirection)
8737 ImGuiSelectionBasicStorage_BatchSetItemSelected(this, GetStorageIdFromIndex(idx), req.Selected, size_before_amends, selection_order);
8738 if (req.Selected)
8739 _SelectionOrder += selection_changes;
8740 ImGuiSelectionBasicStorage_BatchFinish(this, req.Selected, size_before_amends);
8741 }
8742 }
8743 }
8744}
8745

Callers 3

DrawMethod · 0.80

Calls 4

ClearFunction · 0.85
reserveMethod · 0.45

Tested by

no test coverage detected