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

Cannot create multiple buttons with same texture? #5533

Closed
Krish2882005 opened this issue Aug 1, 2022 · 5 comments
Closed

Cannot create multiple buttons with same texture? #5533

Krish2882005 opened this issue Aug 1, 2022 · 5 comments
Labels
drawing/drawlist label/id and id stack implicit identifiers, pushid(), id stack

Comments

@Krish2882005
Copy link

Krish2882005 commented Aug 1, 2022

Hello

I am creating a texture which loads an icon like so

SDL_Texture* Texture = Load("SomePath.png");

I then create two buttons with the same texture

ImGui::ImageButton(Texture, SomeSize);

ImGui::ImageButton(Texture, SomeSize);

When doing this, only the first button works... Isn't the SDL_Texture pointer the id for the image button?

@Krish2882005
Copy link
Author

We can use PushID to give ID to the button

@ocornut
Copy link
Owner

ocornut commented Aug 1, 2022

It is an API oversight see #4471 #1390,
I am reopening this because I would like to rework/promote a new API with an explicit identifier (there is a ImageButtonEx() in imgui_internal.h already)

@ocornut ocornut reopened this Aug 1, 2022
@ocornut ocornut added drawing/drawlist label/id and id stack implicit identifiers, pushid(), id stack labels Aug 3, 2022
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 3, 2022

I have pushed a refactor in 4a2ae06

Changed signature of ImageButton() function: (#5533, #4471, #2464, #1390)

  • Added const char* str_id parameter + removed int frame_padding = -1 parameter.
  • Old signature: bool ImageButton(ImTextureID tex_id, ImVec2 size, ImVec2 uv0 = ImVec2(0,0), ImVec2 uv1 = ImVec2(1,1), int frame_padding = -1, ImVec4 bg_col = ImVec4(0,0,0,0), ImVec4 tint_col = ImVec4(1,1,1,1));
    • used the ImTextureID value to create an ID. This was inconsistent with other functions, led to ID conflicts, and caused problems with engines using transient ImTextureID values.
    • had a FramePadding override which was inconsistent with other functions and made the already-long signature even longer.
  • New signature: bool ImageButton(const char* str_id, ImTextureID tex_id, ImVec2 size, ImVec2 uv0 = ImVec2(0,0), ImVec2 uv1 = ImVec2(1,1), ImVec4 bg_col = ImVec4(0,0,0,0), ImVec4 tint_col = ImVec4(1,1,1,1));
    • requires an explicit identifier. You may still use e.g. PushID() calls and then pass an empty identifier.
    • always uses style.FramePadding for padding, to be consistent with other buttons. You may use PushStyleVar() to alter this.
  • As always we are keeping a redirection function available (will obsolete later).

@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

@luis605
Copy link

luis605 commented Sep 15, 2024

What is the new usage of ImageButton? imgui_demo doesn't seem to be update.

What is str_id and what do I need to pass as that argument? For what I understand, ImageButtons with the same texture would have had the same id and they wouldn't be selected correctly, am I right? And I believe that I can pass an empty string if the texture is only used once or if I manually push an ID, correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
drawing/drawlist label/id and id stack implicit identifiers, pushid(), id stack
Projects
None yet
Development

No branches or pull requests

3 participants