Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What am I doing wrong when trying to set up DnD with tree nodes? #6387

Closed
GasimGasimzada opened this issue Apr 30, 2023 · 2 comments
Closed
Labels
drag drop drag and drop label/id and id stack implicit identifiers, pushid(), id stack

Comments

@GasimGasimzada
Copy link

GasimGasimzada commented Apr 30, 2023

Version/Branch of Dear ImGui:

Version: The version in docking that's in commit 4fdafe
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: Custom backend
Compiler: MSVC
Operating System: Windows

My Issue/Question:

I am not sure what I am doing wrong here. I have a scene hierarchy window that recursively renders tree nodes. I want to add DnD to it so that I can reparent nodes anywhere. However, the drop target do not seem to be working. Drop target logic does not get triggered (even breakpoints do not trigger it). For the sake of testing, I have removed all the other operations (click, right click, and rendering children) and kept it as simple as possible:

int treeNodeFlags = ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_FramePadding;
if (isLeaf) {
  treeNodeFlags |= ImGuiTreeNodeFlags_Leaf;
}

if (state.selectedEntity == entity) {
  treeNodeFlags |= ImGuiTreeNodeFlags_Selected;
}

auto nodeName = getNodeName(name, entity)

ImGui::TreeNodeEx(nodeName.c_str(), treeNodeFlags);

if (ImGui::BeginDragDropSource()) {
  // `entity` is a uint32_t
  ImGui::SetDragDropPayload("Entity", &entity, sizeof(Entity));
  ImGui::Text("%s", nodeName.c_str());
  ImGui::EndDragDropSource();
}

if (ImGui::BeginDragDropTarget()) {
  if (auto *payload = ImGui::AcceptDragDropPayload("Entity", 0)) {
    auto entity = *static_cast<Entity *>(payload->Data);
    std::cout << "Entity : " << entity << "\n";
  }
  ImGui::EndDragDropTarget();
}

ImGui::TreePop();
@GamingMinds-DanielC
Copy link
Contributor

Code looks good to me, I can't spot any mistakes. Looks pretty similar to the drag&drop handling in an outliner window I made (also using the docking branch). Since you mentioned custom backends, can you reproduce the issue with default backends as well? Just to make sure that the culprit isn't hiding in there.

Another idea: are your node names guaranteed unique? If not, repeating names could mess up the id stack and that might interfere with proper handling as well. In that case, surrounding your TreeNodeEx() with PushID() and PopID() might help.

One general hint I can give is to not repeat the payload string, use something like constexpr char const* Entity_Payload = "Entity"; instead. That's not the problem here, but can save headaches when it gets more complex.

@ocornut ocornut added the drag drop drag and drop label May 2, 2023
@GasimGasimzada
Copy link
Author

@GamingMinds-DanielC Thank you for the help! The issue was in IDs. I had some entities that had the same name. Adding PushID/PopID fixed my problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
drag drop drag and drop label/id and id stack implicit identifiers, pushid(), id stack
Projects
None yet
Development

No branches or pull requests

3 participants