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

Multiple Scrolling Region #894

Closed
vivienneanthony opened this issue Nov 5, 2016 · 3 comments
Closed

Multiple Scrolling Region #894

vivienneanthony opened this issue Nov 5, 2016 · 3 comments
Labels
label/id and id stack implicit identifiers, pushid(), id stack

Comments

@vivienneanthony
Copy link

vivienneanthony commented Nov 5, 2016

Hello,

I'm trying to implement multiple scrolling regions.

Visually
[Career] [Tier1]
............ [Tier2]
............ [Tier3]

I tried the following code


 // Set four columns for tiers


                    ImGui::Columns(2);

                    ImGui::PushItemWidth(100.0f);

                    ImGui::Text("Career");

                    ImGui::NextColumn();

                    ImGui::Text("Tiers");

                    for(unsigned int idx=0; idx<4; idx++)
                    {

                        static const char * label= "###label"+idx;
                       
                        ImGui::BeginChild(label,ImVec2(0,100),true);
                        ImGui::Spacing();
                        ImGui::Text("test");

                        ImGui::EndChild();
                        ImGui::Separator();
                    }

I'm not sure what is wrong. It's nothing major but I am wondering if anyone tried it before.

Vivienne

@ocornut
Copy link
Owner

ocornut commented Nov 5, 2016

Vivienne,

The problem here is that your 4 calls to BeginChild use the same identifier, so it is appending 4 times to the same child.

In C++ when you assign to a static variable when declaring it, the assignment is only done the first time.
Furthermore, "label"+idx isn't doing what you think it is doing here. Raw char* are pointers so by doing pointer_to_literal_string + offset you are offsetting the pointer.

So "###label" + 1 with return the pointer to "##label, here.
You may want to read about C string, or use std::string if you are unconfortable with it, or use sprintf() to generate a temporary string into a buffer.

char buf[32];
sprintf(buf, "label%d", idx);

The other less obvious and tricky issue here is that when passing a string identifier to BeginChild(), the identifier isn't being hashed using the ID stack of imgui. That might change in the future.

So you can do:

char buf[32];
sprintf(buf, "label%d", idx);
BeginChild(buf,...)

or

PushId(idx);
BeginChild(GetId("anything")); // Unique id made of idx+anything, read docs in imgui.cpp about id stack
...
PopId()
ImGui::Begin("Bug #894");
ImGui::Columns(2);
//ImGui::PushItemWidth(100.0f);
ImGui::Text("Career");
ImGui::NextColumn();
ImGui::Text("Tiers");
for (int idx=0; idx<4; idx++)
{
    ImGui::PushID(idx);
    ImGui::BeginChild(ImGui::GetID("child"),ImVec2(0,100),true);
    ImGui::Spacing();
    ImGui::Text("test");
    ImGui::EndChild();
    ImGui::PopID();
    ImGui::Separator();
}
ImGui::End();

capture

PS: the PushItemWidth() call appears to be useless here.

@vivienneanthony
Copy link
Author

Thank you for the clarification.

@ocornut
Copy link
Owner

ocornut commented Nov 6, 2016

No problem. By the way I applied the change to BeginChild() that I wanted to do already, which is that the stack-id is applied over the string child label.

So in the example above:

    ImGui::PushID(idx);
    ImGui::BeginChild(ImGui::GetID("child"),ImVec2(0,100),true);
    ImGui::Spacing();
    ImGui::Text("test");
    ImGui::EndChild();
    ImGui::PopID();

It is now possible to just pass a string to BeginChild():

    ImGui::PushID(idx);
    ImGui::BeginChild("child", ImVec2(0,100),true);
    ImGui::Spacing();
    ImGui::Text("test");
    ImGui::EndChild();
    ImGui::PopID();

Which is the common pattern.

@ocornut ocornut closed this as completed Nov 6, 2016
ocornut added a commit that referenced this issue Sep 26, 2018
…y not applying the ID stack to the provided string to uniquely identify the child window. This was undoing an intentional change introduced in 1.50 and broken in 1.60. (#1698, #894, #713) + reworked the Begin/BeginChild comments in imgui.h.
@ocornut ocornut added the label/id and id stack implicit identifiers, pushid(), id stack label Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
label/id and id stack implicit identifiers, pushid(), id stack
Projects
None yet
Development

No branches or pull requests

2 participants