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