ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
57.74k stars 9.9k forks source link

stack #7690

Open TomyFrank opened 2 weeks ago

TomyFrank commented 2 weeks ago

Version/Branch of Dear ImGui:

Version 1.9, Branch: master

Back-ends:

imgui_win32_opengl3

Compiler, OS:

Windows 10

Full config/build information:

No response

Details:

I'm struggling understanding how Dear ImGui stacks work and still have some problems although I've read the link about it on FAQ. You can see the example code in the code section. My thoughts:

  1. At the beginning, when no buton is clicked, there's a stack (stack_0) containing:
    ("my window", "open popup_1")
    "my window"
  2. When "open popup_1" is clicked, a new stack (let's call it stack_1) is made for "popup_1" with that ID at the top and the popup is run/displayed.
  3. When "open popup_2" is clicked, a new stack (let's call it stack_2) is made for "popup_2" with that ID at the top and since we're running f(), the second popup is also run/displayed.

Which of my thought are right please?

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

 ImGui::ShowIDStackToolWindow();
 ImGui::Begin("my window");

 auto f = [] {
   if (ImGui::BeginPopupModal("popup_2")) {
     ImGui::SetItemDefaultFocus();
     if (ImGui::Button("Close popup"))
       ImGui::CloseCurrentPopup();
     ImGui::EndPopup();
   }
 };

 if (ImGui::Button("open popup_1"))
   ImGui::OpenPopup("popup_1");

 if (ImGui::BeginPopupModal("popup_1")) {
   ImGui::SetItemDefaultFocus();
   if (ImGui::Button("open popup_2"))
     ImGui::OpenPopup("popup_2");
   f();
   if (ImGui::Button("Close popup"))
     ImGui::CloseCurrentPopup();
   ImGui::EndPopup();
 }
 ImGui::End();
ocornut commented 2 weeks ago

What’s the actual problem you are aiming to solve?

It’s difficult to answer your questions with a simple yes/no answer, there are most things rights but vocabulary can be slippery or misleading.

Some out of order elements to help understand this:

The back of the stack is used as a seed for computing ids at that level of the stack.

The specific ID at the top of the window is based on its name, so in the specific case of popups it would be based on a mangled name that doesn’t match the ID as perceived in the OpenPopup(), but that usually doesn’t matter to the end user.

“open popup_1” and “open popup_2” being buttons aka “leaf widget” their id is computed but never stored as part of a stack.

For historical reasons and simplicity IDStack[] is story inside each window but it would be possible to use a shared IDstack[] stack for all windows, as long as Begin() appends to it that’s work (see the difference between PushID() and PushIDOverride() which ignore the current back of the back).