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
| 12485 | // If the item has no identifier: |
| 12486 | // - Currently always assume left mouse button. |
| 12487 | bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) |
| 12488 | { |
| 12489 | ImGuiContext& g = *GImGui; |
| 12490 | ImGuiWindow* window = g.CurrentWindow; |
| 12491 | |
| 12492 | // FIXME-DRAGDROP: While in the common-most "drag from non-zero active id" case we can tell the mouse button, |
| 12493 | // in both SourceExtern and id==0 cases we may requires something else (explicit flags or some heuristic). |
| 12494 | ImGuiMouseButton mouse_button = ImGuiMouseButton_Left; |
| 12495 | |
| 12496 | bool source_drag_active = false; |
| 12497 | ImGuiID source_id = 0; |
| 12498 | ImGuiID source_parent_id = 0; |
| 12499 | if (!(flags & ImGuiDragDropFlags_SourceExtern)) |
| 12500 | { |
| 12501 | source_id = g.LastItemData.ID; |
| 12502 | if (source_id != 0) |
| 12503 | { |
| 12504 | // Common path: items with ID |
| 12505 | if (g.ActiveId != source_id) |
| 12506 | return false; |
| 12507 | if (g.ActiveIdMouseButton != -1) |
| 12508 | mouse_button = g.ActiveIdMouseButton; |
| 12509 | if (g.IO.MouseDown[mouse_button] == false || window->SkipItems) |
| 12510 | return false; |
| 12511 | g.ActiveIdAllowOverlap = false; |
| 12512 | } |
| 12513 | else |
| 12514 | { |
| 12515 | // Uncommon path: items without ID |
| 12516 | if (g.IO.MouseDown[mouse_button] == false || window->SkipItems) |
| 12517 | return false; |
| 12518 | if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) == 0 && (g.ActiveId == 0 || g.ActiveIdWindow != window)) |
| 12519 | return false; |
| 12520 | |
| 12521 | // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to: |
| 12522 | // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag. |
| 12523 | if (!(flags & ImGuiDragDropFlags_SourceAllowNullID)) |
| 12524 | { |
| 12525 | IM_ASSERT(0); |
| 12526 | return false; |
| 12527 | } |
| 12528 | |
| 12529 | // Magic fallback to handle items with no assigned ID, e.g. Text(), Image() |
| 12530 | // We build a throwaway ID based on current ID stack + relative AABB of items in window. |
| 12531 | // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING/RESIZINGG OF THE WIDGET, so if your widget moves your dragging operation will be canceled. |
| 12532 | // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive. |
| 12533 | // Rely on keeping other window->LastItemXXX fields intact. |
| 12534 | source_id = g.LastItemData.ID = window->GetIDFromRectangle(g.LastItemData.Rect); |
| 12535 | KeepAliveID(source_id); |
| 12536 | bool is_hovered = ItemHoverable(g.LastItemData.Rect, source_id); |
| 12537 | if (is_hovered && g.IO.MouseClicked[mouse_button]) |
| 12538 | { |
| 12539 | SetActiveID(source_id, window); |
| 12540 | FocusWindow(window); |
| 12541 | } |
| 12542 | if (g.ActiveId == source_id) // Allow the underlying widget to display/return hovered during the mouse release frame, else we would get a flicker. |
| 12543 | g.ActiveIdAllowOverlap = is_hovered; |
| 12544 | } |
nothing calls this directly
no test coverage detected