Skip to content

Commit

Permalink
Drag and Drop: BeginDragDropSource() with ImGuiDragDropFlags_SourceAl…
Browse files Browse the repository at this point in the history
…lowNullID doesn't lose tooltip when scrolling. (#143)

Reduced amount of self critical commentary since it'll appear like a hack for users but it isn't more a hack than many other things.
  • Loading branch information
ocornut committed Nov 10, 2021
1 parent f8a806a commit 2c29e39
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Other Changes:
by the clipper to display. (#3841)
- Clipper: fixed content height declaration slightly mismatching the value of when not using a clipper.
(an additional ItemSpacing.y was declared, affecting scrollbar range).
- Drag and Drop: BeginDragDropSource() with ImGuiDragDropFlags_SourceAllowNullID doesn't lose
tooltip when scrolling. (#143)
- Backends: Vulkan: Call vkCmdSetScissor() at the end of render with a full-viewport to reduce
likehood of issues with people using VK_DYNAMIC_STATE_SCISSOR in their app without calling
vkCmdSetScissor() explicitly every frame. (#4644)
Expand Down
8 changes: 4 additions & 4 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3121,7 +3121,7 @@ ImGuiID ImGuiWindow::GetIDNoKeepAlive(int n)
ImGuiID ImGuiWindow::GetIDFromRectangle(const ImRect& r_abs)
{
ImGuiID seed = IDStack.back();
const int r_rel[4] = { (int)(r_abs.Min.x - Pos.x), (int)(r_abs.Min.y - Pos.y), (int)(r_abs.Max.x - Pos.x), (int)(r_abs.Max.y - Pos.y) };
ImRect r_rel = ImGui::WindowRectAbsToRel(this, r_abs);
ImGuiID id = ImHashData(&r_rel, sizeof(r_rel), seed);
ImGui::KeepAliveID(id);
return id;
Expand Down Expand Up @@ -10276,16 +10276,16 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags)
return false;

// If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to:
// A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag, C) Swallow your programmer pride.
// A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag.
if (!(flags & ImGuiDragDropFlags_SourceAllowNullID))
{
IM_ASSERT(0);
return false;
}

// Magic fallback (=somehow reprehensible) to handle items with no assigned ID, e.g. Text(), Image()
// Magic fallback to handle items with no assigned ID, e.g. Text(), Image()
// We build a throwaway ID based on current ID stack + relative AABB of items in window.
// THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING OF THE WIDGET, so if your widget moves your dragging operation will be canceled.
// THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING/RESIZINGG OF THE WIDGET, so if your widget moves your dragging operation will be canceled.
// We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive.
// Rely on keeping other window->LastItemXXX fields intact.
source_id = g.LastItemData.ID = window->GetIDFromRectangle(g.LastItemData.Rect);
Expand Down

0 comments on commit 2c29e39

Please sign in to comment.