Skip to content

Commit

Permalink
[DEBUG] Added code to detect using cursor change to extend boundaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed Aug 5, 2022
1 parent c911901 commit 9423e42
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
38 changes: 33 additions & 5 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6629,6 +6629,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
window->DC.CurrLineSize = window->DC.PrevLineSize = ImVec2(0.0f, 0.0f);
window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset = 0.0f;
window->DC.IsSameLine = false;
window->DC.IsSetPos = false;

window->DC.NavLayerCurrent = ImGuiNavLayer_Main;
window->DC.NavLayersActiveMask = window->DC.NavLayersActiveMaskNext;
Expand Down Expand Up @@ -6753,6 +6754,22 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
return !window->SkipItems;
}

void CheckUsedSetCursorPosWithSideEffect(ImGuiWindow* window, const char* context)
{
if (!window->DC.IsSetPos)
return;
window->DC.IsSetPos = false;
if (window->DC.CursorMaxPos.x == window->DC.CursorPos.x && window->DC.CursorMaxPos.y == window->DC.CursorPos.y)
return;

IMGUI_DEBUG_LOG("In Window '%s':\n"
"Code before %s was taking advantage of SetCursorPos/SetCursorScreenPos() extending boundaries!\n",
window->Name, context);

//IM_ASSERT(0);
//window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos);
}

void ImGui::End()
{
ImGuiContext& g = *GImGui;
Expand All @@ -6779,6 +6796,8 @@ void ImGui::End()
if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging
LogFinish();

CheckUsedSetCursorPosWithSideEffect(window, "End()");

// Pop from window stack
g.LastItemData = g.CurrentWindowStack.back().ParentLastItemDataBackup;
if (window->Flags & ImGuiWindowFlags_ChildMenu)
Expand Down Expand Up @@ -8422,7 +8441,7 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y)
window->DC.CurrLineSize.y = 0.0f;
window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y);
window->DC.CurrLineTextBaseOffset = 0.0f;
window->DC.IsSameLine = false;
window->DC.IsSameLine = window->DC.IsSetPos = false;

// Horizontal layout mode
if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)
Expand Down Expand Up @@ -8535,11 +8554,15 @@ ImVec2 ImGui::GetCursorScreenPos()
return window->DC.CursorPos;
}

// 2022/08/05: Setting cursor position also extend boundaries (via modifying CursorMaxPos) used to compute window size, group size etc.
// I believe this was is a judicious choice but it's probably being relied upon (it has been the case since 1.31 and 1.50)
// It would be sane if we requested user to use SetCursorPos() + Dummy(ImVec2(0,0)) to extend CursorMaxPos...
void ImGui::SetCursorScreenPos(const ImVec2& pos)
{
ImGuiWindow* window = GetCurrentWindow();
window->DC.CursorPos = pos;
window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos);
//window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos);
window->DC.IsSetPos = true;
}

// User generally sees positions in window coordinates. Internally we store CursorPos in absolute screen coordinates because it is more convenient.
Expand All @@ -8566,21 +8589,24 @@ void ImGui::SetCursorPos(const ImVec2& local_pos)
{
ImGuiWindow* window = GetCurrentWindow();
window->DC.CursorPos = window->Pos - window->Scroll + local_pos;
window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos);
//window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos);
window->DC.IsSetPos = true;
}

void ImGui::SetCursorPosX(float x)
{
ImGuiWindow* window = GetCurrentWindow();
window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + x;
window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x);
//window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x);
window->DC.IsSetPos = true;
}

void ImGui::SetCursorPosY(float y)
{
ImGuiWindow* window = GetCurrentWindow();
window->DC.CursorPos.y = window->Pos.y - window->Scroll.y + y;
window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y);
//window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y);
window->DC.IsSetPos = true;
}

ImVec2 ImGui::GetCursorStartPos()
Expand Down Expand Up @@ -8797,6 +8823,8 @@ void ImGui::EndGroup()
ImGuiGroupData& group_data = g.GroupStack.back();
IM_ASSERT(group_data.WindowID == window->ID); // EndGroup() in wrong window?

CheckUsedSetCursorPosWithSideEffect(window, "EndGroup()");

ImRect group_bb(group_data.BackupCursorPos, ImMax(window->DC.CursorMaxPos, group_data.BackupCursorPos));

window->DC.CursorPos = group_data.BackupCursorPos;
Expand Down
3 changes: 3 additions & 0 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // F

typedef void (*ImGuiErrorLogCallback)(void* user_data, const char* fmt, ...);

extern void CheckUsedSetCursorPosWithSideEffect(ImGuiWindow* window, const char* context);

//-----------------------------------------------------------------------------
// [SECTION] Context pointer
// See implementation of this variable in imgui.cpp for comments and details.
Expand Down Expand Up @@ -2022,6 +2024,7 @@ struct IMGUI_API ImGuiWindowTempData
float CurrLineTextBaseOffset; // Baseline offset (0.0f by default on a new line, generally == style.FramePadding.y when a framed item has been added).
float PrevLineTextBaseOffset;
bool IsSameLine;
bool IsSetPos;
ImVec1 Indent; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.)
ImVec1 ColumnsOffset; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API.
ImVec1 GroupOffset;
Expand Down
4 changes: 3 additions & 1 deletion imgui_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,7 @@ void ImGui::TableBeginRow(ImGuiTable* table)
table->RowIndentOffsetX = window->DC.Indent.x - table->HostIndentX; // Lock indent
window->DC.PrevLineTextBaseOffset = 0.0f;
window->DC.CurrLineSize = ImVec2(0.0f, 0.0f);
window->DC.IsSameLine = false;
window->DC.IsSameLine = window->DC.IsSetPos = false;
window->DC.CursorMaxPos.y = next_y1;

// Making the header BG color non-transparent will allow us to overlay it multiple times when handling smooth dragging.
Expand Down Expand Up @@ -2000,6 +2000,8 @@ void ImGui::TableEndCell(ImGuiTable* table)
ImGuiTableColumn* column = &table->Columns[table->CurrentColumn];
ImGuiWindow* window = table->InnerWindow;

CheckUsedSetCursorPosWithSideEffect(window, "TableNextRow()/TableNextColumn()/TableSetColumn()");

// Report maximum position so we can infer content size per column.
float* p_max_pos_x;
if (table->RowFlags & ImGuiTableRowFlags_Headers)
Expand Down

0 comments on commit 9423e42

Please sign in to comment.