Skip to content

Commit 197e9c0

Browse files
committed
Tables: separate bg0 and bg1 draw channels, selectable above bg color + will facilitate drawing of borders in EndTable().
+ unused code to strip out dummy draw channel calls.
1 parent 52f24df commit 197e9c0

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

imgui_internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ struct ImGuiTabBar
18901890

18911891
#define IM_COL32_DISABLE IM_COL32(0,0,0,1) // Special sentinel code which cannot be used as a regular color.
18921892
#define IMGUI_TABLE_MAX_COLUMNS 64 // sizeof(ImU64) * 8. This is solely because we frequently encode columns set in a ImU64.
1893-
#define IMGUI_TABLE_MAX_DRAW_CHANNELS (1 + 2 + 64 * 2) // See TableUpdateDrawChannels()
1893+
#define IMGUI_TABLE_MAX_DRAW_CHANNELS (4 + 64 * 2) // See TableUpdateDrawChannels()
18941894

18951895
// [Internal] sizeof() ~ 100
18961896
// We use the terminology "Visible" to refer to a column that is not Hidden by user or settings. However it may still be out of view and clipped (see IsClipped).
@@ -2035,7 +2035,8 @@ struct ImGuiTable
20352035
ImS8 FreezeColumnsCount; // Actual frozen columns count (== FreezeColumnsRequest, or == 0 when no scrolling offset)
20362036
ImS8 RowCellDataCurrent; // Index of current RowCellData[] entry in current row
20372037
ImU8 DummyDrawChannel; // Redirect non-visible columns here.
2038-
ImU8 BgDrawChannelUnfrozen; // Index within DrawSplitter.Channels[]
2038+
ImU8 Bg1DrawChannelCurrent; // For Selectable() and other widgets drawing accross columns after the freezing line. Index within DrawSplitter.Channels[]
2039+
ImU8 Bg1DrawChannelUnfrozen;
20392040
bool IsLayoutLocked; // Set by TableUpdateLayout() which is called when beginning the first row.
20402041
bool IsInsideRow; // Set when inside TableBeginRow()/TableEndRow().
20412042
bool IsInitializing;

imgui_tables.cpp

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@
9393
//-----------------------------------------------------------------------------
9494

9595
// Configuration
96+
static const int TABLE_DRAW_CHANNEL_BG0 = 0;
97+
static const int TABLE_DRAW_CHANNEL_BG1_FROZEN = 1;
98+
static const int TABLE_DRAW_CHANNEL_UNCLIPPED = 2; // When using ImGuiTableFlags_NoClip
9699
static const float TABLE_BORDER_SIZE = 1.0f; // FIXME-TABLE: Currently hard-coded.
97100
static const float TABLE_RESIZE_SEPARATOR_HALF_THICKNESS = 4.0f; // Extend outside inner borders.
98101
static const float TABLE_RESIZE_SEPARATOR_FEEDBACK_TIMER = 0.06f; // Delay/timer before making the hover feedback (color+cursor) visible because tables/columns tends to be more cramped.
@@ -894,7 +897,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
894897
// Initial state
895898
ImGuiWindow* inner_window = table->InnerWindow;
896899
if (table->Flags & ImGuiTableFlags_NoClip)
897-
table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, 1);
900+
table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, TABLE_DRAW_CHANNEL_UNCLIPPED);
898901
else
899902
inner_window->DrawList->PushClipRect(inner_window->ClipRect.Min, inner_window->ClipRect.Max, false);
900903

@@ -1015,6 +1018,7 @@ void ImGui::EndTable()
10151018
// Draw borders
10161019
if ((flags & ImGuiTableFlags_Borders) != 0)
10171020
TableDrawBorders(table);
1021+
table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, 0);
10181022

10191023
// Store content width reference for each column (before attempting to merge draw calls)
10201024
const float backup_outer_cursor_pos_x = outer_window->DC.CursorPos.x;
@@ -1024,8 +1028,19 @@ void ImGui::EndTable()
10241028
if (table->RightMostVisibleColumn != -1)
10251029
max_pos_x = ImMax(max_pos_x, table->Columns[table->RightMostVisibleColumn].MaxX);
10261030

1031+
#if 0
1032+
// Strip out dummy channel draw calls
1033+
// We have no way to prevent user submitting direct ImDrawList calls into a hidden column (but ImGui:: calls will be clipped out)
1034+
// FIXME-TABLE: The problem with this approach is we are effectively making it harder for users watching metrics to spot wasted vertices.
1035+
if (table->DummyDrawChannel != (ImU8)-1)
1036+
{
1037+
ImDrawChannel* dummy_channel = &table->DrawSplitter._Channels[table->DummyDrawChannel];
1038+
dummy_channel->_CmdBuffer.resize(0);
1039+
dummy_channel->_IdxBuffer.resize(0);
1040+
}
1041+
#endif
1042+
10271043
// Flatten channels and merge draw calls
1028-
table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, 0);
10291044
if ((table->Flags & ImGuiTableFlags_NoClip) == 0)
10301045
TableReorderDrawChannelsForMerge(table);
10311046
table->DrawSplitter.Merge(inner_window->DrawList);
@@ -1101,7 +1116,7 @@ void ImGui::TableDrawBorders(ImGuiTable* table)
11011116
{
11021117
ImGuiWindow* inner_window = table->InnerWindow;
11031118
ImGuiWindow* outer_window = table->OuterWindow;
1104-
table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, 0);
1119+
table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, TABLE_DRAW_CHANNEL_BG0);
11051120
if (inner_window->Hidden || !table->HostClipRect.Overlaps(table->InnerClipRect))
11061121
return;
11071122
ImDrawList* inner_drawlist = inner_window->DrawList;
@@ -1322,23 +1337,24 @@ void ImGui::TableSetColumnVisible(int column_n, bool visible)
13221337
// - We allocate 1 or 2 background draw channels. This is because we know PushTableBackground() is only used for
13231338
// horizontal spanning. If we allowed vertical spanning we'd need one background draw channel per merge group (1-4).
13241339
// Draw channel allocation (before merging):
1325-
// - NoClip --> 1+D+1 channels: background + foreground (same clip rect == 1 draw call)
1326-
// - Clip --> 1+D+N channels
1327-
// - FreezeRows --> 1+D+N*2 (unless scrolling value is zero)
1328-
// - FreezeRows || FreezeColunns --> 2+D+N*2 (unless scrolling value is zero)
1340+
// - NoClip --> 2+D+1 channels: bg0 + bg1 + foreground (same clip rect == 1 draw call) (FIXME-TABLE: could merge bg1 and foreground?)
1341+
// - Clip --> 2+D+N channels
1342+
// - FreezeRows --> 2+D+N*2 (unless scrolling value is zero)
1343+
// - FreezeRows || FreezeColunns --> 3+D+N*2 (unless scrolling value is zero)
13291344
// Where D is 1 if any column is clipped or hidden (dummy channel) otherwise 0.
13301345
void ImGui::TableUpdateDrawChannels(ImGuiTable* table)
13311346
{
13321347
const int freeze_row_multiplier = (table->FreezeRowsCount > 0) ? 2 : 1;
13331348
const int channels_for_row = (table->Flags & ImGuiTableFlags_NoClip) ? 1 : table->ColumnsVisibleCount;
1334-
const int channels_for_bg = 1 * freeze_row_multiplier;
1349+
const int channels_for_bg = 1 + 1 * freeze_row_multiplier;
13351350
const int channels_for_dummy = (table->ColumnsVisibleCount < table->ColumnsCount || table->VisibleUnclippedMaskByIndex != table->VisibleMaskByIndex) ? +1 : 0;
13361351
const int channels_total = channels_for_bg + (channels_for_row * freeze_row_multiplier) + channels_for_dummy;
13371352
table->DrawSplitter.Split(table->InnerWindow->DrawList, channels_total);
13381353
table->DummyDrawChannel = (ImU8)((channels_for_dummy > 0) ? channels_total - 1 : -1);
1339-
table->BgDrawChannelUnfrozen = (ImU8)((table->FreezeRowsCount > 0) ? channels_for_row + 1 : 0);
1354+
table->Bg1DrawChannelCurrent = TABLE_DRAW_CHANNEL_BG1_FROZEN;
1355+
table->Bg1DrawChannelUnfrozen = (ImU8)((table->FreezeRowsCount > 0) ? 2 + channels_for_row : TABLE_DRAW_CHANNEL_BG1_FROZEN);
13401356

1341-
int draw_channel_current = 1;
1357+
int draw_channel_current = 2;
13421358
for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
13431359
{
13441360
ImGuiTableColumn* column = &table->Columns[column_n];
@@ -1475,15 +1491,16 @@ void ImGui::TableReorderDrawChannelsForMerge(ImGuiTable* table)
14751491
// 2. Rewrite channel list in our preferred order
14761492
if (merge_group_mask != 0)
14771493
{
1478-
// We skip background channel 0 from the shuffling since it won't move (see channels allocation in TableUpdateDrawChannels()).
1479-
const int LEADING_DRAW_CHANNELS = 1;
1494+
// We skip channel 0 (Bg0) and 1 (Bg1 frozen) from the shuffling since they won't move - see channels allocation in TableUpdateDrawChannels().
1495+
const int LEADING_DRAW_CHANNELS = 2;
14801496
g.DrawChannelsTempMergeBuffer.resize(splitter->_Count - LEADING_DRAW_CHANNELS); // Use shared temporary storage so the allocation gets amortized
14811497
ImDrawChannel* dst_tmp = g.DrawChannelsTempMergeBuffer.Data;
1482-
ImBitArray<IMGUI_TABLE_MAX_DRAW_CHANNELS> remaining_mask; // We need 131-bit of storage
1498+
ImBitArray<IMGUI_TABLE_MAX_DRAW_CHANNELS> remaining_mask; // We need 132-bit of storage
14831499
remaining_mask.ClearBits();
14841500
remaining_mask.SetBitRange(LEADING_DRAW_CHANNELS, splitter->_Count - 1);
1485-
remaining_mask.ClearBit(table->BgDrawChannelUnfrozen);
1486-
int remaining_count = splitter->_Count - ((table->BgDrawChannelUnfrozen == 0) ? 1 : 2);
1501+
remaining_mask.ClearBit(table->Bg1DrawChannelUnfrozen);
1502+
IM_ASSERT(has_freeze_v == false || table->Bg1DrawChannelUnfrozen != TABLE_DRAW_CHANNEL_BG1_FROZEN);
1503+
int remaining_count = splitter->_Count - (has_freeze_v ? LEADING_DRAW_CHANNELS + 1 : LEADING_DRAW_CHANNELS);
14871504
//ImRect host_rect = (table->InnerWindow == table->OuterWindow) ? table->InnerClipRect : table->HostClipRect;
14881505
ImRect host_rect = table->HostClipRect;
14891506
for (int merge_group_n = 0; merge_group_n < IM_ARRAYSIZE(merge_groups); merge_group_n++)
@@ -1531,9 +1548,9 @@ void ImGui::TableReorderDrawChannelsForMerge(ImGuiTable* table)
15311548
}
15321549
}
15331550

1534-
// BgDrawChannelFrozen is always channel 0, but make sure BgDrawChannelUnfrozen appears in the middle of our groups
1535-
if (merge_group_n == 1 && table->BgDrawChannelUnfrozen != 0)
1536-
memcpy(dst_tmp++, &splitter->_Channels[table->BgDrawChannelUnfrozen], sizeof(ImDrawChannel));
1551+
// Make sure Bg1DrawChannelUnfrozen appears in the middle of our groups (whereas Bg0 and Bg1 frozen are fixed to 0 and 1)
1552+
if (merge_group_n == 1 && has_freeze_v)
1553+
memcpy(dst_tmp++, &splitter->_Channels[table->Bg1DrawChannelUnfrozen], sizeof(ImDrawChannel));
15371554
}
15381555

15391556
// Append unmergeable channels that we didn't reorder at the end of the list
@@ -1748,7 +1765,7 @@ void ImGui::TableEndRow(ImGuiTable* table)
17481765
// always followed by a change of clipping rectangle we perform the smallest overwrite possible here.
17491766
if ((table->Flags & ImGuiTableFlags_NoClip) == 0)
17501767
window->DrawList->_CmdHeader.ClipRect = table->BgClipRectForDrawCmd.ToVec4();
1751-
table->DrawSplitter.SetCurrentChannel(window->DrawList, table->IsUnfrozen ? table->BgDrawChannelUnfrozen : 0);
1768+
table->DrawSplitter.SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_BG0);
17521769
}
17531770

17541771
// Draw row background
@@ -1805,6 +1822,7 @@ void ImGui::TableEndRow(ImGuiTable* table)
18051822
float y0 = ImMax(table->RowPosY2 + 1, window->InnerClipRect.Min.y);
18061823
table->BgClipRect.Min.y = table->BgClipRectForDrawCmd.Min.y = ImMin(y0, window->InnerClipRect.Max.y);
18071824
table->BgClipRect.Max.y = table->BgClipRectForDrawCmd.Max.y = window->InnerClipRect.Max.y;
1825+
table->Bg1DrawChannelCurrent = table->Bg1DrawChannelUnfrozen;
18081826
IM_ASSERT(table->BgClipRectForDrawCmd.Min.y <= table->BgClipRectForDrawCmd.Max.y);
18091827

18101828
float row_height = table->RowPosY2 - table->RowPosY1;
@@ -1860,7 +1878,9 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_n)
18601878
window->SkipItems = column->IsSkipItems;
18611879
if (table->Flags & ImGuiTableFlags_NoClip)
18621880
{
1863-
table->DrawSplitter.SetCurrentChannel(window->DrawList, 1);
1881+
// FIXME: if we end up drawing all borders/bg in EndTable, could remove this and just assert that channel hasn't changed.
1882+
table->DrawSplitter.SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_UNCLIPPED);
1883+
//IM_ASSERT(table->DrawSplitter._Current == TABLE_DRAW_CHANNEL_UNCLIPPED);
18641884
}
18651885
else
18661886
{
@@ -2025,7 +2045,7 @@ void ImGui::PushTableBackground()
20252045
// Optimization: avoid SetCurrentChannel() + PushClipRect()
20262046
table->HostBackupClipRect = window->ClipRect;
20272047
SetWindowClipRectBeforeSetChannel(window, table->BgClipRectForDrawCmd);
2028-
table->DrawSplitter.SetCurrentChannel(window->DrawList, table->IsUnfrozen ? table->BgDrawChannelUnfrozen : 0);
2048+
table->DrawSplitter.SetCurrentChannel(window->DrawList, table->Bg1DrawChannelCurrent);
20292049
}
20302050

20312051
void ImGui::PopTableBackground()

0 commit comments

Comments
 (0)