Drag N Drop and tree nodes


#1

I have some code that prints out objects in my scene in a hierarchy. It uses trees. When I add some drag and drop code, drags aren’t picking up the right object. It happens with the last few objects that don’t seem to pick up the right thing.

		if (ImGui::Begin("Game Ojects", &showGameObjects))
		{

			//show a list of game objects
			ImGui::LabelText("", "Game Objects in Scene.");


			ImGui::PushStyleVar(ImGuiStyleVar_IndentSpacing, ImGui::GetFontSize() * 3);
			ImGuiTreeNodeFlags node_flags;
			bool pop = false;

			for (int i = 0; i < meshList.size(); i++)
			{
				//if it has a parent, do nothing because it will get picked up during the recursion and put in a child node
				

				if (meshList[i].hasChildren)
				{
					node_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
					pop = true;
				}

				if (!meshList[i].hasChildren)
				{
					node_flags |= ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen;
					pop = false;
				}

				if (!meshList[i].immediateParentID.empty())
				{
				}
				else if (ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, meshList[i].name.c_str()))
				{
					treeChildren(meshList[i].name);

					if(pop)
					ImGui::TreePop();
				}

				if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None))
				{
					ImGui::SetDragDropPayload("DND_Object", &i, sizeof(int));
					ImGui::Text("Make %s a child", meshList[i].name.c_str());
					ImGui::EndDragDropSource();
				}

			}
			ImGui::PopStyleVar();
			
		}
		
		ImGui::End();

void treeChildren(std::string name)
{

ImGuiTreeNodeFlags node_flags;
bool pop = false;

for (int k = 0; k < meshList.size(); k++)
{
	if (meshList[k].immediateParentID == name)
	{
		if (meshList[k].hasChildren)
		{
			node_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
			pop = true;
		}

		if (!meshList[k].hasChildren)
		{
			node_flags |= ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen;
			pop = false;
		}
		if (ImGui::TreeNodeEx((void*)(intptr_t)k, node_flags, meshList[k].name.c_str()) && meshList[k].hasChildren)
		{
			treeChildren( meshList[k].name);
			if(pop)
			ImGui::TreePop();
		}
	}

	if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None))
	{
		ImGui::SetDragDropPayload("DND_Object", &k, sizeof(int));
		ImGui::Text("Make %s a child", meshList[k].name.c_str());
		ImGui::EndDragDropSource();
	}
	//fdfd
}

}


#2

I don’t know if it is related to your issue, but here node_flags is not initialized, and one branch is assigning to it the other adding flags. it looks all a bit fishy
.