Drag and Drop
Dear ImGui provides a flexible drag and drop system that works with any widget. You can drag data between widgets, windows, and even across different Dear ImGui contexts.Overview
The drag and drop system consists of two parts:- Drag Source: The widget being dragged (uses
BeginDragDropSource()) - Drop Target: The widget receiving the drop (uses
BeginDragDropTarget())
Basic Workflow
After rendering a widget, call
BeginDragDropSource(). If it returns true, set payload and call EndDragDropSource().After rendering target widget, call
BeginDragDropTarget(). If it returns true, accept payload and call EndDragDropTarget().Simple Drag and Drop Example
Drag and Drop Functions
BeginDragDropSource
Call after submitting an item to make it draggable:SetDragDropPayload
Set the data to be carried during drag:type: User-defined string (max 32 characters, strings starting with_are reserved)data: Pointer to your datasize: Size of data in bytescond: When to set payload (usually 0 for always)
BeginDragDropTarget
Call after submitting an item to make it a drop target:AcceptDragDropPayload
Accept a payload of a given type:NULL if payload type doesn’t match or if mouse button not released.
GetDragDropPayload
Peek into current payload from anywhere:NULL when drag and drop is inactive.
ImGuiPayload Structure
Dragging Between Widgets
Simple List Reordering
Dragging Custom Data
Advanced Usage
Peek Before Delivery
Preview payload without accepting it:Custom Preview Rendering
Multiple Payload Types
Accept multiple types in one target:Drag and Drop with Tables
Color Drag and Drop
ImGui widgets already support color drag and drop:Best Practices
Complete Example: File Tree
Reference
- Drag and Drop API (line 968)
- See
imgui_demo.cppunder “Widgets->Drag and Drop” for more examples