Tables: Fix sort direction (issue 3023). Remove SortOrder from ImGuiTableSortSpecsColumn. Made sort arrow smaller. Added debug stuff in metrics.

This commit is contained in:
Omar 2020-02-17 15:09:50 +01:00 committed by ocornut
parent 643cf6fc8c
commit ae6fc48f60
3 changed files with 22 additions and 19 deletions

View File

@ -1849,10 +1849,9 @@ 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)
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)

View File

@ -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.

View File

@ -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();
}