MCPcopy Create free account
hub / github.com/KebsCS/KBotExt / TableSortSpecsSanitize

Method TableSortSpecsSanitize

KBotExt/imgui/imgui_tables.cpp:2764–2828  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2762}
2763
2764void ImGui::TableSortSpecsSanitize(ImGuiTable* table)
2765{
2766 IM_ASSERT(table->Flags & ImGuiTableFlags_Sortable);
2767
2768 // Clear SortOrder from hidden column and verify that there's no gap or duplicate.
2769 int sort_order_count = 0;
2770 ImU64 sort_order_mask = 0x00;
2771 for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
2772 {
2773 ImGuiTableColumn* column = &table->Columns[column_n];
2774 if (column->SortOrder != -1 && !column->IsEnabled)
2775 column->SortOrder = -1;
2776 if (column->SortOrder == -1)
2777 continue;
2778 sort_order_count++;
2779 sort_order_mask |= ((ImU64)1 << column->SortOrder);
2780 IM_ASSERT(sort_order_count < (int)sizeof(sort_order_mask) * 8);
2781 }
2782
2783 const bool need_fix_linearize = ((ImU64)1 << sort_order_count) != (sort_order_mask + 1);
2784 const bool need_fix_single_sort_order = (sort_order_count > 1) && !(table->Flags & ImGuiTableFlags_SortMulti);
2785 if (need_fix_linearize || need_fix_single_sort_order)
2786 {
2787 ImU64 fixed_mask = 0x00;
2788 for (int sort_n = 0; sort_n < sort_order_count; sort_n++)
2789 {
2790 // Fix: Rewrite sort order fields if needed so they have no gap or duplicate.
2791 // (e.g. SortOrder 0 disappeared, SortOrder 1..2 exists --> rewrite then as SortOrder 0..1)
2792 int column_with_smallest_sort_order = -1;
2793 for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
2794 if ((fixed_mask & ((ImU64)1 << (ImU64)column_n)) == 0 && table->Columns[column_n].SortOrder != -1)
2795 if (column_with_smallest_sort_order == -1 || table->Columns[column_n].SortOrder < table->Columns[column_with_smallest_sort_order].SortOrder)
2796 column_with_smallest_sort_order = column_n;
2797 IM_ASSERT(column_with_smallest_sort_order != -1);
2798 fixed_mask |= ((ImU64)1 << column_with_smallest_sort_order);
2799 table->Columns[column_with_smallest_sort_order].SortOrder = (ImGuiTableColumnIdx)sort_n;
2800
2801 // Fix: Make sure only one column has a SortOrder if ImGuiTableFlags_MultiSortable is not set.
2802 if (need_fix_single_sort_order)
2803 {
2804 sort_order_count = 1;
2805 for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
2806 if (column_n != column_with_smallest_sort_order)
2807 table->Columns[column_n].SortOrder = -1;
2808 break;
2809 }
2810 }
2811 }
2812
2813 // Fallback default sort order (if no column with the ImGuiTableColumnFlags_DefaultSort flag)
2814 if (sort_order_count == 0 && !(table->Flags & ImGuiTableFlags_SortTristate))
2815 for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
2816 {
2817 ImGuiTableColumn* column = &table->Columns[column_n];
2818 if (column->IsEnabled && !(column->Flags & ImGuiTableColumnFlags_NoSort))
2819 {
2820 sort_order_count = 1;
2821 column->SortOrder = 0;

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected