Skip to content

Center in game window at the start of the program. #1694

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

Closed
DasOhmoff opened this issue Mar 24, 2018 · 5 comments
Closed

Center in game window at the start of the program. #1694

DasOhmoff opened this issue Mar 24, 2018 · 5 comments
Labels

Comments

@DasOhmoff
Copy link

Hello, first of all I just want to mention that this is a great library and that I really like it :)
That being said, I have a little problem, I would like to know how to set the window position automatically to the middle of the screen on the start of the program.

My code:

ImGui::SetNextWindowPosCenter(ImGuiCond_Once | ImGuiCond_Appearing);
ImGui::Begin("Test Window.", &showWindow, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_ShowBorders);

ImGui::InputText("World Width", width, 6);
ImGui::InputText("World Height", height, 6);
            
ImGui::Button("Cancel");
ImGui::SameLine();
ImGui::Button("Create");

ImGui::End();

The problem is that ImGuiCond_Once does not seem to work as I though it would. Here is my screen when I open the test window in the program for the first time:

After I close the test window in the program and reopen it, it works just how it is supposed to work.

So basically to center the test window, I have to close and reopen it.
How do I set the test window position to the center without having to close and reopen it?

@lachlanpage
Copy link

from #1122

"You can position windows using SetNextWindowPos(). You can disable window moving, and within a window use SetCursorScreenPos() to position the next widgets. Please read on imgui_demo.cpp, execute the demo app and read imgui.h to find details."

or you can manually edit the imgui.ini file

@DasOhmoff
Copy link
Author

This is not the problem...

@ocornut
Copy link
Owner

ocornut commented Apr 4, 2018

Hello @DasOhmoff, thanks for reporting this. Looks like something broke since I introduced this feature (in this situation the positioning is applied before the window had a chance to calculate its own size).

Will look into it..

(PS 1: you are using an old version, SetNextWindowPosCenter() was obsolete in September)
(PS 2: combining multiple Cond as flags is described as illegal don't combine multiple values into flags. this is not the cause of your issue but letting you know I will change this to the ImGuiCond values become linear values, as described in the comments, and your OR-ing will become problematic)

@DasOhmoff
Copy link
Author

Thank you my friend :) .

ocornut added a commit that referenced this issue Apr 4, 2018
…fixes SetNextWindowPos() given a non-zero pivot. (#1694)
@ocornut
Copy link
Owner

ocornut commented Apr 4, 2018

This is an unusually complex/tricky matter, so although I have no pushed a fix which I believe will fix your issue, it probably has subtle side effects on other uses and I expect to return to it at some point. For now I believe your issue is fixed if you grab latest.

FYI I believe SetNextWindowPosCenter() on appearing never worked with non-popup/modal windows in the first place, and this is how I tested it (with a modal).

Repro

if (1 && show_another_window)
{
    ImGui::SetNextWindowPosCenter(ImGuiCond_Once);
    ImGui::Begin("Test #1694", &show_another_window, 0);//ImGuiWindowFlags_AlwaysAutoResize);
    char width[6] = "", height[6] = "";
    ImGui::InputText("World Width", width, 6);
    ImGui::InputText("World Height", height, 6);
    ImGui::Button("Cancel");
    ImGui::SameLine();
    ImGui::Button("Create");
    ImGui::End();
}

@ocornut ocornut closed this as completed Apr 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants