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
| 10456 | // If the item has no identifier: |
| 10457 | // - Currently always assume left mouse button. |
| 10458 | bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) |
| 10459 | { |
| 10460 | ImGuiContext& g = *GImGui; |
| 10461 | ImGuiWindow* window = g.CurrentWindow; |
| 10462 | |
| 10463 | // FIXME-DRAGDROP: While in the common-most "drag from non-zero active id" case we can tell the mouse button, |
| 10464 | // in both SourceExtern and id==0 cases we may requires something else (explicit flags or some heuristic). |
| 10465 | ImGuiMouseButton mouse_button = ImGuiMouseButton_Left; |
| 10466 | |
| 10467 | bool source_drag_active = false; |
| 10468 | ImGuiID source_id = 0; |
| 10469 | ImGuiID source_parent_id = 0; |
| 10470 | if (!(flags & ImGuiDragDropFlags_SourceExtern)) |
| 10471 | { |
| 10472 | source_id = g.LastItemData.ID; |
| 10473 | if (source_id != 0) |
| 10474 | { |
| 10475 | // Common path: items with ID |
| 10476 | if (g.ActiveId != source_id) |
| 10477 | return false; |
| 10478 | if (g.ActiveIdMouseButton != -1) |
| 10479 | mouse_button = g.ActiveIdMouseButton; |
| 10480 | if (g.IO.MouseDown[mouse_button] == false || window->SkipItems) |
| 10481 | return false; |
| 10482 | g.ActiveIdAllowOverlap = false; |
| 10483 | } |
| 10484 | else |
| 10485 | { |
| 10486 | // Uncommon path: items without ID |
| 10487 | if (g.IO.MouseDown[mouse_button] == false || window->SkipItems) |
| 10488 | return false; |
| 10489 | if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) == 0 && (g.ActiveId == 0 || g.ActiveIdWindow != window)) |
| 10490 | return false; |
| 10491 | |
| 10492 | // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to: |
| 10493 | // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag. |
| 10494 | if (!(flags & ImGuiDragDropFlags_SourceAllowNullID)) |
| 10495 | { |
| 10496 | IM_ASSERT(0); |
| 10497 | return false; |
| 10498 | } |
| 10499 | |
| 10500 | // Magic fallback to handle items with no assigned ID, e.g. Text(), Image() |
| 10501 | // We build a throwaway ID based on current ID stack + relative AABB of items in window. |
| 10502 | // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING/RESIZINGG OF THE WIDGET, so if your widget moves your dragging operation will be canceled. |
| 10503 | // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive. |
| 10504 | // Rely on keeping other window->LastItemXXX fields intact. |
| 10505 | source_id = g.LastItemData.ID = window->GetIDFromRectangle(g.LastItemData.Rect); |
| 10506 | bool is_hovered = ItemHoverable(g.LastItemData.Rect, source_id); |
| 10507 | if (is_hovered && g.IO.MouseClicked[mouse_button]) |
| 10508 | { |
| 10509 | SetActiveID(source_id, window); |
| 10510 | FocusWindow(window); |
| 10511 | } |
| 10512 | if (g.ActiveId == source_id) // Allow the underlying widget to display/return hovered during the mouse release frame, else we would get a flicker. |
| 10513 | g.ActiveIdAllowOverlap = is_hovered; |
| 10514 | } |
| 10515 | if (g.ActiveId != source_id) |
nothing calls this directly
no test coverage detected