Two childs not overlapping eachother #4493

Closed
opened 2021-08-27 20:37:16 +00:00 by AbbyIOM · 7 comments
AbbyIOM commented 2021-08-27 20:37:16 +00:00 (Migrated from github.com)

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

https://user-images.githubusercontent.com/89664487/131185803-881f6e16-571b-43b3-bb4e-e3636434a282.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();
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** https://user-images.githubusercontent.com/89664487/131185803-881f6e16-571b-43b3-bb4e-e3636434a282.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 commented 2021-08-27 21:07:33 +00:00 (Migrated from github.com)

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().

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 commented 2021-08-27 21:15:54 +00:00 (Migrated from github.com)

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).

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).
AbbyIOM commented 2021-08-27 21:20:44 +00:00 (Migrated from github.com)

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.

> 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 commented 2021-08-30 11:09:27 +00:00 (Migrated from github.com)

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();
Pushed a fix for this case. Here's some minimal working test: ```cpp 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(); ```
cfillion commented 2023-04-23 11:23:13 +00:00 (Migrated from github.com)

// 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

> // 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](https://user-images.githubusercontent.com/4297676/233826061-ebcd7e4c-d84d-411c-9365-b29be6e4fed5.png) <details> <summary>Minimal code snippet to duplicate</summary> ```cpp 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); ``` </details> An example of the problem occurring in a real-world scenario ([script](https://forum.cockos.com/showthread.php?t=278176) by @GoranKovac): user-positioned child windows (`InputTextMultiline`) are dynamically submitted and any of them may underlap later child windows (the toolbars): ![Screencap](https://cdn.discordapp.com/attachments/1093484466317561966/1096563877237493790/whyyyyyyyy.gif)
ocornut commented 2023-04-25 08:25:11 +00:00 (Migrated from github.com)

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).
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 commented 2023-04-25 14:39:22 +00:00 (Migrated from github.com)

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)

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 in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#4493
No description provided.