Skip to content

Commit

Permalink
Canvas: Use ImDrawCallback_ImCanvas macro as draw callback sentinel (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
thedmd committed Sep 19, 2023
1 parent eb1b2f8 commit 69c54d9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ v0.10.0 (WIP):
From this point ed::EndCreate() and ed::EndDelete() can only be called when
ed::BeginCreate() and ed::BeginDelete() calls were successful.

CHANGE: Canvas: Use ImDrawCallback_ImCanvas macro as draw callback sentinel (#256), thanks @nspitko

BUGFIX: Examples: Call ed::EndCreate() and ed::EndDelete() only when ed::BeginCreate() and ed::BeginDelete() returned true

BUGFIX: Canvas: Ensure SentinelDrawCallback cleanup (#255)
Expand Down
17 changes: 8 additions & 9 deletions imgui_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
static constexpr bool value = (sizeof(yes_type) == sizeof(test<mixin>(0))); \
}

// Special sentinel value. This needs to be unique, so allow it to be overridden in the user's ImGui config
# ifndef ImDrawCallback_ImCanvas
# define ImDrawCallback_ImCanvas (ImDrawCallback)(-2)
# endif

namespace ImCanvasDetails {

DECLARE_HAS_MEMBER(HasFringeScale, _FringeScale);
Expand Down Expand Up @@ -68,12 +73,6 @@ struct VtxCurrentOffsetRef
}
};

static void SentinelDrawCallback(const ImDrawList* parent_list, const ImDrawCmd* cmd)
{
// This is a sentinel draw callback, it's only purpose is to mark draw list command.
IM_ASSERT(false && "This draw callback should never be called.");
}

} // namespace ImCanvasDetails

// Returns a reference to _FringeScale extension to ImDrawList
Expand Down Expand Up @@ -444,7 +443,7 @@ void ImGuiEx::Canvas::EnterLocalSpace()
//
// More investigation is needed. To get to the bottom of this.
if ((!m_DrawList->CmdBuffer.empty() && m_DrawList->CmdBuffer.back().ElemCount > 0) || m_DrawList->_Splitter._Count > 1)
m_DrawList->AddCallback(&ImCanvasDetails::SentinelDrawCallback, nullptr);
m_DrawList->AddCallback(ImDrawCallback_ImCanvas, nullptr);

# if defined(IMGUI_HAS_VIEWPORT)
auto window = ImGui::GetCurrentWindow();
Expand Down Expand Up @@ -556,9 +555,9 @@ void ImGuiEx::Canvas::LeaveLocalSpace()
// Remove sentinel draw command if present
if (m_DrawListCommadBufferSize > 0)
{
if (m_DrawList->CmdBuffer.size() > m_DrawListCommadBufferSize && m_DrawList->CmdBuffer[m_DrawListCommadBufferSize].UserCallback == &ImCanvasDetails::SentinelDrawCallback)
if (m_DrawList->CmdBuffer.size() > m_DrawListCommadBufferSize && m_DrawList->CmdBuffer[m_DrawListCommadBufferSize].UserCallback == ImDrawCallback_ImCanvas)
m_DrawList->CmdBuffer.erase(m_DrawList->CmdBuffer.Data + m_DrawListCommadBufferSize);
else if (m_DrawList->CmdBuffer.size() >= m_DrawListCommadBufferSize && m_DrawList->CmdBuffer[m_DrawListCommadBufferSize - 1].UserCallback == &ImCanvasDetails::SentinelDrawCallback)
else if (m_DrawList->CmdBuffer.size() >= m_DrawListCommadBufferSize && m_DrawList->CmdBuffer[m_DrawListCommadBufferSize - 1].UserCallback == ImDrawCallback_ImCanvas)
m_DrawList->CmdBuffer.erase(m_DrawList->CmdBuffer.Data + m_DrawListCommadBufferSize - 1);
}

Expand Down

0 comments on commit 69c54d9

Please sign in to comment.