-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pre-compute the size of widgets #3714
Comments
Right now there is no official method to measure widgets before they're drawn. Most of the time you can specify its size before it is drawn. There are methods that can be applied if you don't mind running logic twice. Out of the box I see two possible solutions:
*Widget can be drawn with two methods:
|
Note that most widgets size are standard,
Eg framed+label widgets can be calculated using CalcItemWidth() + style.ItemInnerSpacing.x + CalcTextSize(label), Button() are CalcTextSize() + FramePadding * 2.
|
Some clarification as I sent yesterday's message hastily from my phone: Widget heights are almost always
Widget widths are not as clearly defined, but:
|
We are currently missing - and we will eventually implement - a "null" layout mode which will allow submitting items for size measurement. |
Hi guys! I also faced this issue. I'm trying to center a button using advices from the comment above (#3714 (comment)), but I met the following problem: {
ImGui::Begin("Hello, world!");
ImDrawList* list = ImGui::GetWindowDrawList();
struct foo {
bool btn() { return ImGui::Button("btn",ImVec2{200,20}); };
} btn;
ImVec2 start = ImGui::GetCursorPos();
ImVec2 size;
list->ChannelsSplit(2);
ImGui::PushID("someotherid");
list->ChannelsSetCurrent(1);
if (btn.btn() ) {
std::cout << "btn 1 (invisible) pressed\n";
};
ImGui::PopID();
size = ImGui::GetItemRectSize();
ImVec2 available = ImGui::GetContentRegionMax();
start.x = (available.x-size.x)/2;
ImGui::SetCursorPos(start);
list->ChannelsSetCurrent(0);
if (btn.btn()){
std::cout << "btn 1 pressed\n";
};
ImGui::End();
} Am I doing something wrong? |
You should manually calculate expected width of a button before rendering it as described in #3714 (comment). |
My Issue/Question:
Is there a way to compute the size of a widget before it is drawn so you can affect it's layout intelligently? For example, if you want to call, say, InputInt, is there some way to precompute how much space it will take given a particular set of arguments/style settings? I searched through the demo code but was unable to find any such examples.
The text was updated successfully, but these errors were encountered: