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
| 14511 | // If the item has no identifier: |
| 14512 | // - Currently always assume left mouse button. |
| 14513 | bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) |
| 14514 | { |
| 14515 | ImGuiContext& g = *GImGui; |
| 14516 | ImGuiWindow* window = g.CurrentWindow; |
| 14517 | |
| 14518 | // FIXME-DRAGDROP: While in the common-most "drag from non-zero active id" case we can tell the mouse button, |
| 14519 | // in both SourceExtern and id==0 cases we may requires something else (explicit flags or some heuristic). |
| 14520 | ImGuiMouseButton mouse_button = ImGuiMouseButton_Left; |
| 14521 | |
| 14522 | bool source_drag_active = false; |
| 14523 | ImGuiID source_id = 0; |
| 14524 | ImGuiID source_parent_id = 0; |
| 14525 | if ((flags & ImGuiDragDropFlags_SourceExtern) == 0) |
| 14526 | { |
| 14527 | source_id = g.LastItemData.ID; |
| 14528 | if (source_id != 0) |
| 14529 | { |
| 14530 | // Common path: items with ID |
| 14531 | if (g.ActiveId != source_id) |
| 14532 | return false; |
| 14533 | if (g.ActiveIdMouseButton != -1) |
| 14534 | mouse_button = g.ActiveIdMouseButton; |
| 14535 | if (g.IO.MouseDown[mouse_button] == false || window->SkipItems) |
| 14536 | return false; |
| 14537 | g.ActiveIdAllowOverlap = false; |
| 14538 | } |
| 14539 | else |
| 14540 | { |
| 14541 | // Uncommon path: items without ID |
| 14542 | if (g.IO.MouseDown[mouse_button] == false || window->SkipItems) |
| 14543 | return false; |
| 14544 | if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) == 0 && (g.ActiveId == 0 || g.ActiveIdWindow != window)) |
| 14545 | return false; |
| 14546 | |
| 14547 | // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to: |
| 14548 | // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag. |
| 14549 | if (!(flags & ImGuiDragDropFlags_SourceAllowNullID)) |
| 14550 | { |
| 14551 | IM_ASSERT(0); |
| 14552 | return false; |
| 14553 | } |
| 14554 | |
| 14555 | // Magic fallback to handle items with no assigned ID, e.g. Text(), Image() |
| 14556 | // We build a throwaway ID based on current ID stack + relative AABB of items in window. |
| 14557 | // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING/RESIZINGG OF THE WIDGET, so if your widget moves your dragging operation will be canceled. |
| 14558 | // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive. |
| 14559 | // Rely on keeping other window->LastItemXXX fields intact. |
| 14560 | source_id = g.LastItemData.ID = window->GetIDFromRectangle(g.LastItemData.Rect); |
| 14561 | KeepAliveID(source_id); |
| 14562 | bool is_hovered = ItemHoverable(g.LastItemData.Rect, source_id, g.LastItemData.ItemFlags); |
| 14563 | if (is_hovered && g.IO.MouseClicked[mouse_button]) |
| 14564 | { |
| 14565 | SetActiveID(source_id, window); |
| 14566 | FocusWindow(window); |
| 14567 | } |
| 14568 | if (g.ActiveId == source_id) // Allow the underlying widget to display/return hovered during the mouse release frame, else we would get a flicker. |
| 14569 | g.ActiveIdAllowOverlap = is_hovered; |
| 14570 | } |
nothing calls this directly
no test coverage detected