MCPcopy Create free account
hub / github.com/Aegel5/SimpleSwitcher / BeginDragDropSource

Method BeginDragDropSource

imgui_tiny_app/imgui/imgui.cpp:15528–15645  ·  view source on GitHub ↗

When this returns true you need to: a) call SetDragDropPayload() exactly once, b) you may render the payload visual/description, c) call EndDragDropSource() If the item has an identifier: - This assume/require the item to be activated (typically via ButtonBehavior). - Therefore if you want to use this with a mouse button other than left mouse button, it is up to the item itself to activate with an

Source from the content-addressed store, hash-verified

15526// If the item has no identifier:
15527// - Currently always assume left mouse button.
15528bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags)
15529{
15530 ImGuiContext& g = *GImGui;
15531 ImGuiWindow* window = g.CurrentWindow;
15532
15533 // FIXME-DRAGDROP: While in the common-most "drag from non-zero active id" case we can tell the mouse button,
15534 // in both SourceExtern and id==0 cases we may requires something else (explicit flags or some heuristic).
15535 ImGuiMouseButton mouse_button = ImGuiMouseButton_Left;
15536
15537 bool source_drag_active = false;
15538 ImGuiID source_id = 0;
15539 ImGuiID source_parent_id = 0;
15540 if ((flags & ImGuiDragDropFlags_SourceExtern) == 0)
15541 {
15542 source_id = g.LastItemData.ID;
15543 if (source_id != 0)
15544 {
15545 // Common path: items with ID
15546 if (g.ActiveId != source_id)
15547 return false;
15548 if (g.ActiveIdMouseButton != -1)
15549 mouse_button = g.ActiveIdMouseButton;
15550 if (g.IO.MouseDown[mouse_button] == false || window->SkipItems)
15551 return false;
15552 g.ActiveIdAllowOverlap = false;
15553 }
15554 else
15555 {
15556 // Uncommon path: items without ID
15557 if (g.IO.MouseDown[mouse_button] == false || window->SkipItems)
15558 return false;
15559 if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) == 0 && (g.ActiveId == 0 || g.ActiveIdWindow != window))
15560 return false;
15561
15562 // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to:
15563 // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag.
15564 if (!(flags & ImGuiDragDropFlags_SourceAllowNullID))
15565 {
15566 IM_ASSERT(0);
15567 return false;
15568 }
15569
15570 // Magic fallback to handle items with no assigned ID, e.g. Text(), Image()
15571 // We build a throwaway ID based on current ID stack + relative AABB of items in window.
15572 // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING/RESIZING OF THE WIDGET, so if your widget moves your dragging operation will be canceled.
15573 // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive.
15574 // Rely on keeping other window->LastItemXXX fields intact.
15575 source_id = g.LastItemData.ID = window->GetIDFromRectangle(g.LastItemData.Rect);
15576 KeepAliveID(source_id);
15577 bool is_hovered = ItemHoverable(g.LastItemData.Rect, source_id, g.LastItemData.ItemFlags);
15578 if (is_hovered && g.IO.MouseClicked[mouse_button])
15579 {
15580 SetActiveID(source_id, window);
15581 FocusWindow(window);
15582 }
15583 if (g.ActiveId == source_id) // Allow the underlying widget to display/return hovered during the mouse release frame, else we would get a flicker.
15584 g.ActiveIdAllowOverlap = is_hovered;
15585 }

Callers

nothing calls this directly

Calls 3

ImHashStrFunction · 0.85
BeginTooltipFunction · 0.85
GetIDFromRectangleMethod · 0.80

Tested by

no test coverage detected