Skip to content

Commit f2df804

Browse files
committed
Tables: four small fixes.
Fixed last item flags leaking to disabled column, affecting IsItemHovered(). (ocornut#3651). Validate and fix invalid DisplayOrder data from ini file. Allow TableHeaderRows() to function will missing TableSetupColumn() calls. Made TableHeader() use AllowItemOverlap mode to allow submit subsequent item in same cell, since it covers the whole cell area.
1 parent 738606a commit f2df804

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

imgui.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,6 +4749,7 @@ bool ImGui::IsItemEdited()
47494749
}
47504750

47514751
// Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority.
4752+
// FIXME: Although this is exposed, its interaction and ideal idiom with using ImGuiButtonFlags_AllowItemOverlap flag are extremely confusing, need rework.
47524753
void ImGui::SetItemAllowOverlap()
47534754
{
47544755
ImGuiContext& g = *GImGui;

imgui_tables.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,8 @@ const char* ImGui::TableGetColumnName(int column_n)
13691369

13701370
const char* ImGui::TableGetColumnName(const ImGuiTable* table, int column_n)
13711371
{
1372-
IM_ASSERT(table->IsLayoutLocked == true || column_n <= table->DeclColumnsCount); // NameOffset is invalid otherwise
1372+
if (table->IsLayoutLocked == false && column_n >= table->DeclColumnsCount)
1373+
return ""; // NameOffset is invalid at this point
13731374
const ImGuiTableColumn* column = &table->Columns[column_n];
13741375
if (column->NameOffset == -1)
13751376
return "";
@@ -1762,7 +1763,6 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_n)
17621763
window->DC.CursorMaxPos.x = window->DC.CursorPos.x;
17631764
window->DC.ColumnsOffset.x = start_x - window->Pos.x - window->DC.Indent.x; // FIXME-WORKRECT
17641765
window->DC.CurrLineTextBaseOffset = table->RowTextBaseline;
1765-
window->DC.LastItemId = 0;
17661766
window->DC.NavLayerCurrent = (ImGuiNavLayer)column->NavLayerCurrent;
17671767

17681768
window->WorkRect.Min.y = window->DC.CursorPos.y;
@@ -1775,6 +1775,12 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_n)
17751775
window->DC.CursorPos.y = ImMax(window->DC.CursorPos.y, table->RowPosY2);
17761776

17771777
window->SkipItems = column->IsSkipItems;
1778+
if (column->IsSkipItems)
1779+
{
1780+
window->DC.LastItemId = 0;
1781+
window->DC.LastItemStatusFlags = 0;
1782+
}
1783+
17781784
if (table->Flags & ImGuiTableFlags_NoClip)
17791785
{
17801786
// FIXME: if we end up drawing all borders/bg in EndTable, could remove this and just assert that channel hasn't changed.
@@ -2638,8 +2644,10 @@ void ImGui::TableHeader(const char* label)
26382644
//GetForegroundDrawList()->AddRect(cell_r.Min, cell_r.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG]
26392645
//GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG]
26402646

2647+
// Using AllowItemOverlap mode because we cover the whole cell, and we want user to be able to submit subsequent items.
26412648
bool hovered, held;
2642-
bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_None);
2649+
bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_AllowItemOverlap);
2650+
SetItemAllowOverlap();
26432651
if (hovered || selected)
26442652
{
26452653
const ImU32 col = GetColorU32(held ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
@@ -3025,6 +3033,7 @@ void ImGui::TableLoadSettings(ImGuiTable* table)
30253033

30263034
// Serialize ImGuiTableSettings/ImGuiTableColumnSettings into ImGuiTable/ImGuiTableColumn
30273035
ImGuiTableColumnSettings* column_settings = settings->GetColumnSettings();
3036+
ImU64 display_order_mask = 0;
30283037
for (int data_n = 0; data_n < settings->ColumnsCount; data_n++, column_settings++)
30293038
{
30303039
int column_n = column_settings->Index;
@@ -3044,12 +3053,19 @@ void ImGui::TableLoadSettings(ImGuiTable* table)
30443053
column->DisplayOrder = column_settings->DisplayOrder;
30453054
else
30463055
column->DisplayOrder = (ImGuiTableColumnIdx)column_n;
3056+
display_order_mask |= (ImU64)1 << column->DisplayOrder;
30473057
column->IsEnabled = column->IsEnabledNextFrame = column_settings->IsEnabled;
30483058
column->SortOrder = column_settings->SortOrder;
30493059
column->SortDirection = column_settings->SortDirection;
30503060
}
30513061

3052-
// FIXME-TABLE: Need to validate .ini data
3062+
// Validate and fix invalid display order data
3063+
const ImU64 expected_display_order_mask = (settings->ColumnsCount == 64) ? ~0 : ((ImU64)1 << settings->ColumnsCount) - 1;
3064+
if (display_order_mask != expected_display_order_mask)
3065+
for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
3066+
table->Columns[column_n].DisplayOrder = (ImGuiTableColumnIdx)column_n;
3067+
3068+
// Rebuild index
30533069
for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
30543070
table->DisplayOrderToIndex[table->Columns[column_n].DisplayOrder] = (ImGuiTableColumnIdx)column_n;
30553071
}

0 commit comments

Comments
 (0)