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
| 9769 | // If the item has no identifier: |
| 9770 | // - Currently always assume left mouse button. |
| 9771 | bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) |
| 9772 | { |
| 9773 | ImGuiContext& g = *GImGui; |
| 9774 | ImGuiWindow* window = g.CurrentWindow; |
| 9775 | |
| 9776 | // FIXME-DRAGDROP: While in the common-most "drag from non-zero active id" case we can tell the mouse button, |
| 9777 | // in both SourceExtern and id==0 cases we may requires something else (explicit flags or some heuristic). |
| 9778 | ImGuiMouseButton mouse_button = ImGuiMouseButton_Left; |
| 9779 | |
| 9780 | bool source_drag_active = false; |
| 9781 | ImGuiID source_id = 0; |
| 9782 | ImGuiID source_parent_id = 0; |
| 9783 | if (!(flags & ImGuiDragDropFlags_SourceExtern)) |
| 9784 | { |
| 9785 | source_id = window->DC.LastItemId; |
| 9786 | if (source_id != 0) |
| 9787 | { |
| 9788 | // Common path: items with ID |
| 9789 | if (g.ActiveId != source_id) |
| 9790 | return false; |
| 9791 | if (g.ActiveIdMouseButton != -1) |
| 9792 | mouse_button = g.ActiveIdMouseButton; |
| 9793 | if (g.IO.MouseDown[mouse_button] == false) |
| 9794 | return false; |
| 9795 | g.ActiveIdAllowOverlap = false; |
| 9796 | } |
| 9797 | else |
| 9798 | { |
| 9799 | // Uncommon path: items without ID |
| 9800 | if (g.IO.MouseDown[mouse_button] == false) |
| 9801 | return false; |
| 9802 | |
| 9803 | // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to: |
| 9804 | // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag, C) Swallow your programmer pride. |
| 9805 | if (!(flags & ImGuiDragDropFlags_SourceAllowNullID)) |
| 9806 | { |
| 9807 | IM_ASSERT(0); |
| 9808 | return false; |
| 9809 | } |
| 9810 | |
| 9811 | // Early out |
| 9812 | if ((window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect) == 0 && (g.ActiveId == 0 || g.ActiveIdWindow != window)) |
| 9813 | return false; |
| 9814 | |
| 9815 | // Magic fallback (=somehow reprehensible) to handle items with no assigned ID, e.g. Text(), Image() |
| 9816 | // We build a throwaway ID based on current ID stack + relative AABB of items in window. |
| 9817 | // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING OF THE WIDGET, so if your widget moves your dragging operation will be canceled. |
| 9818 | // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive. |
| 9819 | source_id = window->DC.LastItemId = window->GetIDFromRectangle(window->DC.LastItemRect); |
| 9820 | bool is_hovered = ItemHoverable(window->DC.LastItemRect, source_id); |
| 9821 | if (is_hovered && g.IO.MouseClicked[mouse_button]) |
| 9822 | { |
| 9823 | SetActiveID(source_id, window); |
| 9824 | FocusWindow(window); |
| 9825 | } |
| 9826 | if (g.ActiveId == source_id) // Allow the underlying widget to display/return hovered during the mouse release frame, else we would get a flicker. |
| 9827 | g.ActiveIdAllowOverlap = is_hovered; |
| 9828 | } |
nothing calls this directly
no test coverage detected