-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Description
Version/Branch of Dear ImGui: Version 1.90.7
Bonjour Omar,
There is a common user issue which is that developers may reuse the same ID, and will not understand why their widget is not responsive.
Example
#include "hello_imgui/hello_imgui.h"
#include "imgui.h"
#include "stdio.h"
char text1[500] = "Hello,";
char text2[500] = "world!";
int value = 0;
bool flag1 = false, flag2 = false;
float f1 = 0.f, f2 = 0.f;
void Gui()
{
// Only the first button will be clickable
if (ImGui::Button("button")) printf("Button1 pressed\n");
if (ImGui::Button("button")) printf("Button2 pressed\n");
// Those two inputs will share the same value (editing one will overwrite the other)
ImGui::InputText("text", text1, IM_ARRAYSIZE(text1));
ImGui::InputText("text", text2, IM_ARRAYSIZE(text2));
// The second radio button cannot be selected
ImGui::RadioButton("radio", &value, 0);
ImGui::RadioButton("radio", &value, 1);
// The second checkbox cannot be toggled
ImGui::Checkbox("checkbox", &flag1);
ImGui::Checkbox("checkbox", &flag2);
// Those two sliders will share the same value (editing one will overwrite the other)
ImGui::SliderFloat("slider", &f1, 0.f, 1.f);
ImGui::SliderFloat("slider", &f2, 0.f, 1.f);
}
int main() { HelloImGui::Run(Gui); }I did want to add an assert for Python users of Dear ImGui bundle, as they might not be able to understand the reason or the failure.
So I added a patch like the one you can see here:
pthom@ea95177
With this patch, the example code would throw an assert for each reused ID.
The patch adds this method:
// a version of GetID that warns if the ID was already used
IMGUI_API ImGuiID ImGuiWindow::GetID_AssertUnique(const char* str_id);The principle is to use this method in some rare and carefuly selected locations (ButtonEx, ArrowButtonEx, Checkbox, RadioButton , DragScalar, SliderScalar, Drag, InputTextEx), i.e. the most commonly used widgets, and to warn the user if they are using an existing ID.
The implementation is simple: just remember the already pushed IDs, and raise an assert if the user is raising the same ID in the same frame. This concerns only a very limited set of the IDs (i.e. those for the aforementioned widgets).
Would you be interested in such a change? If so, I can push a PR. I know that my current implementation can be improved and should not use a static variable (instead, perhaps rely on a member of ImGuiWindow)
I know that this change could have some implications in a lot of user code. But I would think that it is for the best because they would be warned that they were doing something bad.
As an encouraging hint, I tested it successfully in all of the numerous demonstrations of Dear ImGui Bundle, and it worked correctly
Feel free to close if you do not feel that this is useful.
Note: this feature could be protected by a compilation flag, in order to avoid potential regressions in user code.
Cheers!