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

Accessing IDs in different states, and passing an explicit ID into ImageButton() #1390

Closed
JaminGrey opened this issue Oct 23, 2017 · 5 comments
Labels
label/id and id stack implicit identifiers, pushid(), id stack popups

Comments

@JaminGrey
Copy link

I have four ImageButtons(), and they popup a Modal popup when clicked.
It is possible for two or more four ImageButton()'s to share the same image.

Because the imageID is used as their ID, if they share the same image then they can't be clicked.
So I wrap them in a PushID() unique to each button.
But because they are now in an inner PushID(), they can't call the popup modal by ID, because it's in a different ID stack-frame.

My solution is to wrap them, but call outside of them:
ImGuiID backgroundSelectionPopup = ImGui::GetCurrentContext()->CurrentWindow->GetID(BackgroundSelectionPopup);

I then, in the inner PushID() stack-frame, call ImGui::OpenPopupEx(backgroundSelectionPopup, false); myself when the buttons are clicked.

This works fine, but feels hackish. Works fine for me in the present, though, so no complaints #here.

I think I read that you are already considering how to make IDs callable from different stack-frames, so I'm just presenting my scenario as a real-world use-case. I would also suggest making an overload of ImageButton() that takes an explicit ID.

@ocornut ocornut added the popups label Oct 24, 2017
@ocornut
Copy link
Owner

ocornut commented Oct 24, 2017

Linking to #331
I think the solution will be to expose more popups functions with ImGuiID and a dedicated helper function to calculate identifiers in a file-system-path manner, e.g. "/rootid" "../id_in_parent_scope", but I haven't yet worked on identifying the potential issues there.

I agree ImageButton() would need an explicit ID.

@Flix01
Copy link

Flix01 commented Oct 24, 2017

I agree ImageButton() would need an explicit ID.

One possible solution to solve it, is to create an API that allows ImageButton() to display text next to the image. This way we can use the text as ID, and still fall back to no-text mode by passing something like: "###dummyLabel" as text.

P.S. Some time ago I posted a possible implementation here: #1096, but I've not tested it much, to be honest. So I think it would be better to rewrite it from scratch.

@ocornut ocornut added the label/id and id stack implicit identifiers, pushid(), id stack label Apr 6, 2020
@ocornut
Copy link
Owner

ocornut commented Jun 23, 2020

Closing this for now, as this issue seems duplicate of two separate issues:

In the test_engine repository we're using a function ImHashDecoratedPath() which

  • treat a leading / as resetting the seed.
  • ignore subsequent / separators (using "\/" to inhibit a dedicated forward slash)
  • it's missing support for leading ../ sequences, in order to support it it means the function cannot be a loose function anymore but moved to a spot where it depends on the ID stack.
    We'll work some more on this and will later post here and in Can not open modal popup from menu / Issue with ID stack #331.

@ocornut ocornut closed this as completed Jun 23, 2020
@xarthurx
Copy link

xarthurx commented Apr 1, 2021

Would it be possible to provide an example of how to use this ImageButtonEx() with OpenPopupEx()?
I encounter a similar issue and follows from the Imgui_Demo to #2464 to here, but cannot figure our how to use the ImGuiID in ImageButtonEx.

ocornut added a commit that referenced this issue Aug 3, 2022
…d' parameter + removed 'int frame_padding = -1' parameter. (#5533, #4471, #2464, #1390).

Also removed frame_padding parameter from ImageButtonEx(), amend e0ec69d.
@ocornut
Copy link
Owner

ocornut commented Aug 22, 2024

FYI the old ImageButton() entry point which was made obsolete on Aug 3, 2022, has now been commented out.

For reference:

#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
// Legacy API obsoleted in 1.89. Two differences with new ImageButton()
// - old ImageButton() used ImTextureId as item id (created issue with multiple buttons with same image, transient texture id values, opaque computation of ID)
// - new ImageButton() requires an explicit 'const char* str_id'
// - old ImageButton() had frame_padding' override argument.
// - new ImageButton() always use style.FramePadding.
bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col)
{
    // Default to using texture ID as ID. User can still push string/integer prefixes.
    PushID((void*)(intptr_t)user_texture_id);
    if (frame_padding >= 0)
        PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2((float)frame_padding, (float)frame_padding));
    bool ret = ImageButton("", user_texture_id, size, uv0, uv1, bg_col, tint_col);
    if (frame_padding >= 0)
        PopStyleVar();
    PopID();
    return ret;
}
#endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS

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 popups
Projects
None yet
Development

No branches or pull requests

4 participants