Hi Omar,
I have an issue with ImageButtons. I have following method in my app:
void Foo(int index, vec4 uv) {
static int hov_index = -1;
ImGui::PushID(index);
if (ImGui::ImageButton((void*)(intptr_t)(hov_index == index ? icon_texture_flat_ : icon_texture_), ImVec2(24, 24), ImVec2(uv.x, uv.w), ImVec2(uv.z, uv.y), 0 )) {
Foonction();
}
ImGui::PopID();
if (ImGui::IsItemHovered()) {
hov_index = (ImGui::IsMouseDown(0) ? index : -1);
}
}
I wanted to achieve than if user press and hold the button, image is changed (flat and normal icon), which worked perfectly. The problem was that from that point ImageButton never returned true. After debugging a bit, I presumed that since ImGuiID is generated from texture id (and I have two different textures), it actually never recognizes that the item is pressed.
My solution was to overload the ImGui::ImageButton as ImageButton(ImTextureID user_texture_id, ImTextureID user_texture_id_down ....
copy/paste complete ImageButton definition, then change last line in definition to: window->DrawList->AddImage(!held ? user_texture_id : user_texture_id_down, image_bb.Min, image_bb.Max, uv0, uv1, GetColorU32(tint_col));
From that point it worked as expected.
I have two questions:
PushID((void*)(intptr_t)user_texture_id); if for example I PushID myself? Shouldn't ImGui check if there is a pushed ID already by the user, and in case there is none, use your existent solution of adding texture_id as ID?UI: saw there is something similar at https://github.com/ocornut/imgui/issues/1390
Thanks
Shouldn't ImGui check if there is a pushed ID already by the user,
That's not possible, there's always a pushed id in the stack.
This is a flaw of ImageButton(), the code is very small I suggest just created your own custom function (in your own source file, never in imgui.cpp).
Thank you for your fast response.
I suggest just created your own custom function (in your own source file, never in imgui.cpp).
That was exactly what I did :)
I'm adding a note that this is an issue with ImageButton.
I would like to later redesign the API of Image() and ImageButton() they are old and have other problems.
Someone came to me yesterday pointing out that this flaw is even more problematic as with some back-end it is possible that the ImTextureId be unstable from frame to frame, therefore making it impossible to use ImageButton() as-is.