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

Drag drop does not work with Child #7539

Closed
BraunSilas opened this issue Apr 29, 2024 · 6 comments
Closed

Drag drop does not work with Child #7539

BraunSilas opened this issue Apr 29, 2024 · 6 comments
Labels
drag drop drag and drop

Comments

@BraunSilas
Copy link

Version/Branch of Dear ImGui:

v1.90.4 / Main

Back-ends:

imgui_impl_sdl2.cpp + imgui_impl_sdlrenderer2.cpp

Compiler, OS:

Windows 10 + CLion + Bundled MinGW

Full config/build information:

No response

Details:

Hi,
I am trying to drag drop a child;
I have been successfully dropping onto a child. But whenever I try to drag a child ImGui::BeginDragDropSource() returns false;
i have gone trough some iterations of testing.
First I got an Assert error for line 12890 refering to this:

// If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to:
            // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag.
            if (!(flags & ImGuiDragDropFlags_SourceAllowNullID))
            {
                IM_ASSERT(0);
                return false;
            }

i figured that a child (even with id) might fall under the group of items without unique identifyer but so I included the flag: "ImGuiDragDropFlags_SourceAllowNullID"
This avoided the Assert, yet BeginDragDropSource() still returned false;

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

Here is the way I tested it

ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(5, 10, 30, 150));
    ImGui::BeginChild(2664735, ImVec2(((ImGui::GetWindowWidth() - 10) * 0.5) - 8, ImGui::GetWindowHeight()-120),
                      ImGuiChildFlags_Border);

    ImGui::EndChild();
    ImGui::PopStyleColor();
    if(ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID)){
        SDL_Log("drag");
        ImGui::EndDragDropSource();
    }
    if(ImGui::BeginDragDropSource()){
        SDL_Log("drop");
        ImGui::EndDragDropTarget();
    }
@ocornut ocornut added the drag drop drag and drop label Apr 29, 2024
@ocornut
Copy link
Owner

ocornut commented Apr 29, 2024

It doesn't indeed, but I'm not sure we can make it work as it would be a relatively ambiguous behavior.
What's inside your child? Do you want the drag to happen when clicking and interacting with any item within the child?

You might submit an invisible button covering the child window as the last item in the window, and use as a drop source. Have you tried that? (not same problem, but similar idea as what's used in #3379)

@BraunSilas
Copy link
Author

interesting post(#3379). Here is a picture

Git Issue screeshot

Each of the blocks on saying "Wait" are movement commands for a robot. Each of those commands has multiple inputs and so on and I want to be able to reorder them by dragging them on top or inbetween of each other.

To reflect on your response...

I can put a button within the child on the lowest z level just above the child (cause I need to be able to reach the widgets in the child. Is that right?
I am gonna give it a shot.

@ocornut
Copy link
Owner

ocornut commented Apr 30, 2024

You can use an InvisibleButton() just before calling EndChild() and use this. Make sure the button covers entire child region. You can call ImGui::DebugDrawItemRect() after InvisibleButton() to ensure your geometry is correct.

@BraunSilas
Copy link
Author

BraunSilas commented Apr 30, 2024

git 3

I did that and you can see the red rectangle;
The Problem I have is:
I have a child for the left gray bar on each of the routines and a child with all of the other elements in it.(see image)
the structure of the code is somthng like this:

BeginChild(); //LeftGrayBar
EndChild();
BeginChild(); //with all other elements
EndChild();
InvisibleButton();

The button is only reachable in the gaps between the 2 children.
It seems like the children have a higher z value.
the same is the case if I but the Button before the children.

I solved it now by putting the invisible button in all of the children, so it will be active on all layers.
not pretty but it works.

@ocornut
Copy link
Owner

ocornut commented Apr 30, 2024

You can add the undocumented ImGuiButtonFlags_FlattenChildren to InvisibleButton() to solve this.

However, I think your nested use of BeginChild() is unnecessary and may be inefficient. Child windows are meant to be scrolling area.

Instead of

BeginChild(); //LeftGrayBar
EndChild();
BeginChild(); //with all other elements
EndChild();
InvisibleButton();

You can use:

BeginGroup(); //LeftGrayBar
//..
EndGroup();
SameLine();
BeginGroup(); //with all other elements
EndGroup();
InvisibleButton();

BeginGroup() will lock the X coordinates. You can also use Indent() to adjust it.

@BraunSilas
Copy link
Author

Good to know, thank you.
I am gonna run with the hacked Solution for now to save time refactoring the layout but I'll use it from now on.
Thanks @ocornut
Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
drag drop drag and drop
Projects
None yet
Development

No branches or pull requests

2 participants