Skip to content

Buttons with identical labels do not work #74

@UUSim

Description

@UUSim

Before rattling any discussion: This could be considered improper usage of ImGui, however in my ignorance I did use ImGui like this. I noticed the issue, and I think you should now about it.

Onto the issue:
Having Buttons with the same label, can cause unpredictable results.
In my case, with 2 buttons controlling two separate settings:

ImGui::Text("Settting 1:");
ImGui::SameLine();
if (ImGui::Button((setting1enabled) ? "Yes" : "No")){
    setting1enabled = !setting1enabled;
}

ImGui::Text("Settting 2:");
ImGui::SameLine();
if (ImGui::Button((setting2enabled) ? "Yes" : "No")){
    setting2enabled = !setting2enabled;
}

In both settings are true, the label is identical ("Yes") for both buttons.
When the 2nd button is then clicked, ImGui::Button will return false because
window->GetID(label); (imgui.cpp: line 3076) will return the ID of the 1st item with that label.
Since it will check the mouse coordinates against the coordinates of the wrong button, it will report as not being pressed.

When setting1enabled = false (or setting1enabled != setting2enabled), the label on each button will be different, and everything works as expected.

In case more than two buttons have an identical label, button Bn will only respond if the labels of B0,B1,...,Bn-1 != Bn (I have not tried this, but this is what will happen most likely).

Idea for a solution:

  • Allow supplying an extra ID to the ImGui::Button function call (ID=-1 by default).
  • If the optional ID is given, use that instead of the text label to index the Button in ImGui's storage (hash table?).

This problem may also apply to other widgets.
Note: You may close this issue I you regard this issue as being unintended library usage. That being said, I think it would be nice if users could choose to use it like this anyway

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions