While trying to create a mechanic, I decided to try to do it via Drag’n’Drop. And was surprised to find that in the original ImGUI it exists by default, but in Gamma there are only DragDropFlags.
Am I missing something or has it just not been imported? Is this theoretically possible or is there some difficulty that prevents it from being imported?
Demo Code
IMGUI_DEMO_MARKER("Widgets/Drag and drop");
if (ImGui::TreeNode("Drag and Drop"))
{
IMGUI_DEMO_MARKER("Widgets/Drag and drop/Standard widgets");
if (ImGui::TreeNode("Drag and drop in standard widgets"))
{
// ColorEdit widgets automatically act as drag source and drag target.
// They are using standardized payload strings IMGUI_PAYLOAD_TYPE_COLOR_3F and IMGUI_PAYLOAD_TYPE_COLOR_4F
// to allow your own widgets to use colors in their drag and drop interaction.
// Also see 'Demo->Widgets->Color/Picker Widgets->Palette' demo.
HelpMarker("You can drag from the color squares.");
static float col1[3] = { 1.0f, 0.0f, 0.2f };
static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f };
ImGui::ColorEdit3("color 1", col1);
ImGui::ColorEdit4("color 2", col2);
ImGui::TreePop();
}
IMGUI_DEMO_MARKER("Widgets/Drag and drop/Copy-swap items");
if (ImGui::TreeNode("Drag and drop to copy/swap items"))
{
enum Mode
{
Mode_Copy,
Mode_Move,
Mode_Swap
};
static int mode = 0;
if (ImGui::RadioButton("Copy", mode == Mode_Copy)) { mode = Mode_Copy; } ImGui::SameLine();
if (ImGui::RadioButton("Move", mode == Mode_Move)) { mode = Mode_Move; } ImGui::SameLine();
if (ImGui::RadioButton("Swap", mode == Mode_Swap)) { mode = Mode_Swap; }
static const char* names[9] =
{
"Bobby", "Beatrice", "Betty",
"Brianna", "Barry", "Bernard",
"Bibi", "Blaine", "Bryn"
};
for (int n = 0; n < IM_ARRAYSIZE(names); n++)
{
ImGui::PushID(n);
if ((n % 3) != 0)
ImGui::SameLine();
ImGui::Button(names[n], ImVec2(60, 60));
// Our buttons are both drag sources and drag targets here!
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None))
{
// Set payload to carry the index of our item (could be anything)
ImGui::SetDragDropPayload("DND_DEMO_CELL", &n, sizeof(int));
// Display preview (could be anything, e.g. when dragging an image we could decide to display
// the filename and a small preview of the image, etc.)
if (mode == Mode_Copy) { ImGui::Text("Copy %s", names[n]); }
if (mode == Mode_Move) { ImGui::Text("Move %s", names[n]); }
if (mode == Mode_Swap) { ImGui::Text("Swap %s", names[n]); }
ImGui::EndDragDropSource();
}
if (ImGui::BeginDragDropTarget())
{
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DND_DEMO_CELL"))
{
IM_ASSERT(payload->DataSize == sizeof(int));
int payload_n = *(const int*)payload->Data;
if (mode == Mode_Copy)
{
names[n] = names[payload_n];
}
if (mode == Mode_Move)
{
names[n] = names[payload_n];
names[payload_n] = "";
}
if (mode == Mode_Swap)
{
const char* tmp = names[n];
names[n] = names[payload_n];
names[payload_n] = tmp;
}
}
ImGui::EndDragDropTarget();
}
ImGui::PopID();
}
ImGui::TreePop();
}