Repository navigation
Description
Version/Branch of Dear ImGui:
Version: master
Branch: master
Back-end/Renderer/Compiler/OS
Back-ends: imgui_impl_sdl.cpp + imgui_impl_opengl3.cpp
Operating System: Fedora 34
My Issue/Question:
Hi,
I'm going to measure widget width (#3714) and align it.
I'm able to do so rendering one widget two times with the following steps:
- save cursor position
- push temporaty ID
- render widget (with 0 alpha)
- save its size
- pop id
- set cursor position to x = (availableRegionW - itemWidth), y = start pos.Y
- render widget
the problem is, that this first (invisible) widget is rendered in foreground?... and covers this second (visible).
The solution is to put the visible widget inside of Child.
my question is: is it possible to do it another way so that the second widget could be rendered in a normal way (without any other steps before)
Screenshots/Video
with steps above (without Push..Alpha to show both buttons):

when mouse over the common region of both buttons

with child (mouse over common region of two buttons)

Standalone, minimal, complete and verifiable example: (see #2261)
// Here's some code anyone can copy and paste to reproduce your issue
{
ImGui::Begin("Hello, world!");
struct foo {
bool btn() { return ImGui::Button("btn",ImVec2{200,20}); };
} btn;
ImVec2 start = ImGui::GetCursorPos();
ImVec2 size;
ImGui::PushID("someotherid");
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0);
if (btn.btn() ) {
std::cout << "btn 1 (invisible) pressed\n";
};
ImGui::PopStyleVar();
ImGui::PopID();
size = ImGui::GetItemRectSize();
ImVec2 available = ImGui::GetContentRegionMax();
start.x = (available.x-size.x)/2;
ImGui::SetCursorPos(start);
if (ImGui::BeginChild("container")) {
if (btn.btn()){
std::cout << "btn 1 pressed\n";
}
}
ImGui::EndChild();
ImGui::End();
}
Activity
ocornut commentedon Sep 29, 2021
This scream like XY Problem please start by describing what you are trying to do in the first place.
It also doesn't make sense to do any of that if your button has a fixed size of (200,20) so your code is not adequately representing your problem. The solution is likely that you should be able to tell the compute of a widget yourself given they are pretty standardized.
gucio321 commentedon Sep 29, 2021
@ocornut thanx for your reply!
I'm going to add a common method for aligning widgets (in fact for getting its widths).
I'm reffering to this comment: #3714 (comment)
gucio321 commentedon Sep 29, 2021
well, so as I can read:
defaultposition, get its size, move cursor back, add dummy, render centered widget.the problem was, that the second widget (e.g. button) (both widgets has the same id) wasn't return true on user interaction. Instead, user was able to interact with this invisible widget
the solution I really need is to render my first widget behind other imgui widgets to be able to safely measure it.
ocornut commentedon Sep 30, 2021
That's not really possible (other than output the "first" widget in a dummy window then hide the window...).
What we are missing a dummy layout mode where ItemSize() would record size and ItemAdd() always return false. This is desirable for many other features and we'll do it eventually (It's one of the last thing missing to allow #846 to be an independent extension).