From ae6fc48f6048b171996ad985bd20c23aef221a40 Mon Sep 17 00:00:00 2001 From: Omar Date: Mon, 17 Feb 2020 15:09:50 +0100 Subject: [PATCH] Tables: Fix sort direction (issue 3023). Remove SortOrder from ImGuiTableSortSpecsColumn. Made sort arrow smaller. Added debug stuff in metrics. --- imgui.h | 19 +++++++++---------- imgui_demo.cpp | 6 +++--- imgui_tables.cpp | 16 ++++++++++------ 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/imgui.h b/imgui.h index 067c4f21c..cc380ea03 100644 --- a/imgui.h +++ b/imgui.h @@ -1846,23 +1846,22 @@ struct ImGuiPayload // Sorting specification for one column of a table (sizeof == 8 bytes) struct ImGuiTableSortSpecsColumn { - ImGuiID ColumnUserID; // User id of the column (if specified by a TableSetupColumn() call) - ImU8 ColumnIndex; // Index of the column - ImU8 SortOrder; // Index within parent ImGuiTableSortSpecs (always stored in order starting from 0, tables sorted on a single criteria will always have a 0 here) - ImS8 SortSign; // +1 or -1 (you can use this or SortDirection, whichever is more convenient for your sort function) - ImS8 SortDirection; // ImGuiSortDirection_Ascending or ImGuiSortDirection_Descending (you can use this or SortSign, whichever is more convenient for your sort function) + ImGuiID ColumnUserID; // User id of the column (if specified by a TableSetupColumn() call) + ImU8 ColumnIndex; // Index of the column + ImU8 SortOrder; // Index within parent ImGuiTableSortSpecs (always stored in order starting from 0, tables sorted on a single criteria will always have a 0 here) + ImGuiSortDirection SortDirection : 8; // ImGuiSortDirection_Ascending or ImGuiSortDirection_Descending (you can use this or SortSign, whichever is more convenient for your sort function) - ImGuiTableSortSpecsColumn() { ColumnUserID = 0; ColumnIndex = 0; SortOrder = 0; SortSign = +1; SortDirection = ImGuiSortDirection_Ascending; } + ImGuiTableSortSpecsColumn() { ColumnUserID = 0; ColumnIndex = 0; SortOrder = 0; SortDirection = ImGuiSortDirection_Ascending; } }; // Sorting specifications for a table (often handling sort specs for a single column, occasionally more) // Obtained by calling TableGetSortSpecs() struct ImGuiTableSortSpecs { - const ImGuiTableSortSpecsColumn* Specs; // Pointer to sort spec array. - int SpecsCount; // Sort spec count. Most often 1 unless e.g. ImGuiTableFlags_MultiSortable is enabled. - bool SpecsChanged; // Set to true by TableGetSortSpecs() call if the specs have changed since the previous call. Use this to sort again! - ImU64 ColumnsMask; // Set to the mask of column indexes included in the Specs array. e.g. (1 << N) when column N is sorted. + const ImGuiTableSortSpecsColumn* Specs; // Pointer to sort spec array. + int SpecsCount; // Sort spec count. Most often 1 unless e.g. ImGuiTableFlags_MultiSortable is enabled. + bool SpecsChanged; // Set to true by TableGetSortSpecs() call if the specs have changed since the previous call. Use this to sort again! + ImU64 ColumnsMask; // Set to the mask of column indexes included in the Specs array. e.g. (1 << N) when column N is sorted. ImGuiTableSortSpecs() { Specs = NULL; SpecsCount = 0; SpecsChanged = false; ColumnsMask = 0x00; } }; diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 22338f79c..b91392ae6 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -3271,10 +3271,10 @@ struct MyItem case MyItemColumnID_Description: delta = (strcmp(a->Name, b->Name)); break; default: IM_ASSERT(0); break; } - if (delta < 0) - return -1 * sort_spec->SortSign; if (delta > 0) - return +1 * sort_spec->SortSign; + return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? +1 : -1; + if (delta < 0) + return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? -1 : +1; } // qsort() is instable so always return a way to differenciate items. diff --git a/imgui_tables.cpp b/imgui_tables.cpp index fedf1b35d..c2b219d94 100644 --- a/imgui_tables.cpp +++ b/imgui_tables.cpp @@ -2011,7 +2011,7 @@ void ImGui::TableHeader(const char* label) float w_sort_text = 0.0f; if ((table->Flags & ImGuiTableFlags_Sortable) && !(column->Flags & ImGuiTableColumnFlags_NoSort)) { - const float ARROW_SCALE = 0.75f; + const float ARROW_SCALE = 0.65f; w_arrow = ImFloor(g.FontSize * ARROW_SCALE + g.Style.FramePadding.x);// table->CellPadding.x); if (column->SortOrder != -1) { @@ -2036,7 +2036,7 @@ void ImGui::TableHeader(const char* label) PopStyleColor(); x += w_sort_text; } - RenderArrow(window->DrawList, ImVec2(x, y), col, column->SortDirection == ImGuiSortDirection_Ascending ? ImGuiDir_Down : ImGuiDir_Up, ARROW_SCALE); + RenderArrow(window->DrawList, ImVec2(x, y), col, column->SortDirection == ImGuiSortDirection_Ascending ? ImGuiDir_Up : ImGuiDir_Down, ARROW_SCALE); } // Handle clicking on column header to adjust Sort Order @@ -2126,7 +2126,6 @@ const ImGuiTableSortSpecs* ImGui::TableGetSortSpecs() sort_spec->ColumnUserID = column->UserID; sort_spec->ColumnIndex = (ImU8)column_n; sort_spec->SortOrder = (ImU8)column->SortOrder; - sort_spec->SortSign = (column->SortDirection == ImGuiSortDirection_Ascending) ? +1 : -1; sort_spec->SortDirection = column->SortDirection; table->SortSpecs.ColumnsMask |= (ImU64)1 << column_n; } @@ -2298,7 +2297,7 @@ void ImGui::TableSaveSettings(ImGuiTable* table) // We skip saving some data in the .ini file when they are unnecessary to restore our state // FIXME-TABLE: We don't have logic to easily compare SortOrder to DefaultSortOrder yet. if (column->IndexDisplayOrder != n) - settings->SaveFlags |= ImGuiTableFlags_Reorderable;; + settings->SaveFlags |= ImGuiTableFlags_Reorderable; if (column_settings->SortOrder != -1) settings->SaveFlags |= ImGuiTableFlags_Sortable; if (column_settings->Visible != ((column->Flags & ImGuiTableColumnFlags_DefaultHide) == 0)) @@ -2446,11 +2445,13 @@ void ImGui::DebugNodeTable(ImGuiTable* table) "Active: %d, Clipped: %d, DrawChannels: %d,%d\n" "WidthGiven/Requested: %.1f/%.1f, Weight: %.2f\n" "ContentWidth: RowsFrozen %d, RowsUnfrozen %d, HeadersUsed/Desired %d/%d\n" + "SortOrder: %d, SortDir: %s\n" "UserID: 0x%08X, Flags: 0x%04X: %s%s%s%s..", n, column->IndexDisplayOrder, name ? name : "NULL", column->MinX - table->WorkRect.Min.x, column->MaxX - table->WorkRect.Min.x, column->IsActive, column->IsClipped, column->DrawChannelRowsBeforeFreeze, column->DrawChannelRowsAfterFreeze, column->WidthGiven, column->WidthRequested, column->ResizeWeight, column->ContentWidthRowsFrozen, column->ContentWidthRowsUnfrozen, column->ContentWidthHeadersUsed, column->ContentWidthHeadersDesired, + column->SortOrder, (column->SortDirection == ImGuiSortDirection_Ascending) ? "Ascending" : (column->SortDirection == ImGuiSortDirection_Descending) ? "Descending" : "None", column->UserID, column->Flags, (column->Flags & ImGuiTableColumnFlags_WidthFixed) ? "WidthFixed " : "", (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? "WidthStretch " : "", @@ -2465,8 +2466,11 @@ void ImGui::DebugNodeTable(ImGuiTable* table) for (int n = 0; n < settings->ColumnsCount; n++) { ImGuiTableColumnSettings* column_settings = &settings->GetColumnSettings()[n]; - BulletText("Column %d Order %d SortOrder %d Visible %d UserID 0x%08X WidthOrWeight %.3f", - n, column_settings->DisplayOrder, column_settings->SortOrder, column_settings->Visible, column_settings->UserID, column_settings->WidthOrWeight); + ImGuiSortDirection sort_dir = (column_settings->SortOrder != -1) ? column_settings->SortDirection : ImGuiSortDirection_None; + BulletText("Column %d Order %d SortOrder %d %s Visible %d UserID 0x%08X WidthOrWeight %.3f", + n, column_settings->DisplayOrder, column_settings->SortOrder, + (sort_dir == ImGuiSortDirection_Ascending) ? "Asc" : (sort_dir == ImGuiSortDirection_Descending) ? "Des" : "---", + column_settings->Visible, column_settings->UserID, column_settings->WidthOrWeight); } TreePop(); }