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:
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
Looking at imgui.cpp line 115ff (if you are confused about the meaning or use of ID in ImGui), I think there is already a solution to your problem: Either use PushID() or PopID(), or add extra ID information to the label using "Yes##Foo".
Ouch! A very big sorry indeed! I did not see that.
For other users making the same mistake (not reading the troubleshooting tips in the cpp that is..), here is the help section covering this issue:
In my case, I used the final option.
This is indeed a common question especially for first-time users of immediate mode GUI library.
I have now added an "index" at the very top of the file, listing all sections in the file so hopefully people will be more aware of the existence of the FAQ.
and use label###ID if you don't want the label to be part of the ID
Most helpful comment
Ouch! A very big sorry indeed! I did not see that.
For other users making the same mistake (not reading the troubleshooting tips in the cpp that is..), here is the help section covering this issue:
to do so they need an unique ID. unique ID are typically derived from a string label, an indice or a pointer.
when you call Button("OK") the button shows "OK" and also use "OK" as an ID.
within a same Window, use PushID() / PopID() to easily create scopes and avoid ID conflicts.
so if you have a loop creating "multiple" items, you can use PushID() / PopID() with the index of each item, or their pointer, etc.
some functions like TreeNode() implicitly creates a scope for you by calling PushID()
depending on your use cases you may want to use strings, indices or pointers as ID. experiment and see what makes more sense!
In my case, I used the final option.