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

Two childs not overlapping eachother #4493

Closed
AbbyIOM opened this issue Aug 27, 2021 · 7 comments
Closed

Two childs not overlapping eachother #4493

AbbyIOM opened this issue Aug 27, 2021 · 7 comments
Labels

Comments

@AbbyIOM
Copy link

AbbyIOM commented Aug 27, 2021

Thank you!

My Issue/Question:

I've got two childs, one child(1) has a button/text init the second child(2) has a move button that moves the child(2) on top of the other child(1), my problem is that even though the child() is on top of the child(1) the button/text is still visible.

Screenshots/Video

2021-08-27.22-36-14.mp4
// Here's some code anyone can copy and paste to reproduce your issue
            ImGui::SetNextWindowPos(ImVec2(100, 200));
            ImGui::BeginChild("Child #1", ImVec2(200, 200), true);
            ImGui::Text("Child #1 Text");
            ImGui::EndChild();


            ImGui::SetNextWindowPos(ImVec2(movePos, 200));
            ImGui::BeginChild("Child #2", ImVec2(200, 200), true);
            ImGui::SetCursorPosY(50);
            if (ImGui::Button("Move Child #2 on Child #1"))
                move = !move;
            ImGui::EndChild();
@ocornut
Copy link
Owner

ocornut commented Aug 27, 2021

The ChildBg rectangle is drawn on the parent to save a draw call. If you look for that code drawing the background you’ll find there’s a rule for it you may be able to workaround.

Check the code that calls RenderWindowDecorations().

@ocornut
Copy link
Owner

ocornut commented Aug 27, 2021

Detecting overlapping childs would be a O(log N) operation I don’t fancy but since our heuristic lastest so long (since 1.71) without another report, I think we could add to checking of previous child as part of the heuristic and it would fix a good portionncases (including yours which technically is the first reported case).

@ocornut ocornut added the bug label Aug 27, 2021
@AbbyIOM
Copy link
Author

AbbyIOM commented Aug 27, 2021

The ChildBg rectangle is drawn on the parent to save a draw call. If you look for that code drawing the background you’ll find there’s a rule for it you may be able to workaround.

Check the code that calls RenderWindowDecorations().

Thank you for the fast reply! Will do.

@ocornut
Copy link
Owner

ocornut commented Aug 30, 2021

Pushed a fix for this case.

Here's some minimal working test:

ImGui::Text("Hello from parent");
ImVec2 p = ImGui::GetCursorScreenPos();
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.5f, 0.0f, 0.0f, 1.0f));
ImGui::SetNextWindowPos(ImVec2(p.x+100, p.y+0));
ImGui::BeginChild("Child #1", ImVec2(200, 200), true);
ImGui::Text("Child #1 Text");
ImGui::Button("Child #1 long button");
ImGui::EndChild();
ImGui::PopStyleColor();

ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.0f, 0.5f, 0.0f, 1.0f));
ImGui::SetNextWindowPos(ImVec2(p.x+150, p.y+0));
ImGui::BeginChild("Child #2", ImVec2(200, 200), true);
ImGui::SetCursorPosY(50);
ImGui::Button("Some button");
ImGui::EndChild();
ImGui::PopStyleColor();

@ocornut ocornut closed this as completed Aug 30, 2021
AnClark pushed a commit to AnClark/imgui-vst-mod that referenced this issue Aug 30, 2021
AnClark pushed a commit to AnClark/imgui-vst-mod that referenced this issue Aug 30, 2021
@cfillion
Copy link
Contributor

// Child windows can render their decoration (bg color, border, scrollbars, etc.) within their parent to save a draw call (since 1.71)
// FIXME: [...] Please get in contact if you are affected (github #4493)

This optimization causes z-order issues when a child window overlaps another submitted earlier, before the previous.

Screenshot

Minimal code snippet to duplicate
if(ImGui::BeginChildFrame(ImGui::GetID("under1"), { -FLT_MIN, 28 })) {
  ImGui::Text("aaaaaaaaaaaaaaaaa");
  ImGui::EndChildFrame();
}
if(ImGui::BeginChildFrame(ImGui::GetID("under2"), { -FLT_MIN, 28 })) {
  ImGui::Text("bbbbbbbbbbbbbbbbb");
  ImGui::EndChildFrame();
}

ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(0, 0, 0, 0xcc));
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0xff, 0, 0xff, 0xff));
// overlaps under1 but not under2
ImGui::SetCursorPos({ 10, 32 });
if(ImGui::BeginChild("over1", { 50, 20 })) {
  ImGui::Text("over");
  ImGui::EndChild();
}
// overlaps both under1 and under2 but not over1
ImGui::SetCursorPos({ 70, 32 });
if(ImGui::BeginChild("over2", { 50, 50 })) {
  ImGui::Text("over");
  ImGui::EndChild();
}
ImGui::PopStyleColor(2);

An example of the problem occurring in a real-world scenario (script by @GoranKovac): user-positioned child windows (InputTextMultiline) are dynamically submitted and any of them may underlap later child windows (the toolbars):

Screencap

@ocornut
Copy link
Owner

ocornut commented Apr 25, 2023

This optimization is quite meaningful, so considering we haven't been getting too many requests for that and considering your specific use case, I would suggest to aim for a workaround.

  • You may submit child without decoration and draw background and border yourself? Considering the application here it seems like this would be trivial? May need to pass ImGuiWindowFlags_AlwaysUseWindowPadding to child.
  • Or you may trick the heuristic by leaving the parent empty of vertices (e.g adding an intermediate invisible child window to hold all those).

@ocornut
Copy link
Owner

ocornut commented Apr 25, 2023

If you can think of a way to amend the existing heuristic I'm also open to that.

(Btw there was a minor typo in the code parent_is_empty was parent_is_not_empty, I reworked the condition accordingly so the name is not misleading anymore. That'll be in an upcoming commit but it has no effect on compiled code)

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

No branches or pull requests

3 participants