Skip to content

Instantly share code, notes, and snippets.

@lyf-is-coding
Last active July 9, 2022 14:58
Show Gist options
  • Save lyf-is-coding/a4896863fd1c4dbb74f21b59d8ecd4c4 to your computer and use it in GitHub Desktop.
Save lyf-is-coding/a4896863fd1c4dbb74f21b59d8ecd4c4 to your computer and use it in GitHub Desktop.
C++ ImGui Drag and drop item inside collapsing header
if (ImGui::CollapsingHeader(group.name.c_str()))
{
float window_width = ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x;
size_t buttonCounts = group.apps.size();
for (int index = 0; index < buttonCounts; ++index)
{
ImGui::PushID(index);
ImGui::Button(group.apps[index].name.c_str(), button_sz);
// HANDLE DRAG AND DROP BUTTON
// Our buttons are both drag sources and drag targets
if (ImGui::BeginDragDropSource())
{
// Set payload to carry the index of our button
ImGui::SetDragDropPayload("DND_BUTTON", &index, sizeof(int));
// Display preview
ImGui::Text("Moving %s", group.apps[index].name.c_str());
ImGui::EndDragDropSource();
}
if (ImGui::BeginDragDropTarget())
{
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DND_BUTTON"))
{
int payload_n = *(const int*)payload->Data;
std::swap(group.apps[index], group.apps[payload_n]);
}
ImGui::EndDragDropTarget();
}
ImGui::PopID();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment