ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
60.97k stars 10.29k forks source link

Inputs gets messed up with bigger supported window resolutions (1920x1080, 2560x1440, ...) #5750

Closed PsychoCoffee closed 2 years ago

PsychoCoffee commented 2 years ago

Version/Branch of Dear ImGui:

Version: 1.89 WIP Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: .cpp: imgui.cpp, imgui_draw.cpp, imgui_impl_dx11.cpp, imgui_impl_win32.cpp, imgui_tables.cpp, imgui_widgets.cpp, .h: imconfig.h, imgui.h, imgui_impl_dx11.h, imgui_impl_win32.h, imgui_internal.h, imstb_rectpack.h, imstb_textedit.h, imstb_truetype.h Compiler: (Don't know, must be on default for Visual Studio 2022) Operating System: Windows 10

My Issue/Question: Working with ImGui Buttons on bigger supported screen resolutions (1920x1080, or 2560x1440) messes up ImGui. What happens is that the buttons don't register when you click directly on them, but on a random part of the screen. Also, the ImGui Text also looks under scaled, and everything else. Is there an easy way to auto scale text / font with a provided resolution?

Screenshots/Video 1 2

// There's lots of code to paste for the main menu, but here is the first screen:
//Initialization:
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO();
    ImGui_ImplWin32_Init(hwnd);
    ImGui_ImplDX11_Init(device.Get(), deviceContext.Get());
    ImGui::StyleColorsDark();
//Actual part of the code
ImGui_ImplDX11_NewFrame();
        ImGui_ImplWin32_NewFrame();
        ImGui::NewFrame();
        ImGuiStyle& styleMenuMain = ImGui::GetStyle();
        drawMainMenuStyle();
        ImGui::SetNextWindowSize(ImVec2(1000, 1000));
        ImGui::SetNextWindowPos(ImVec2(150, 310));
        //ImGui::SetNextWindowFocus();
        if (ImGui::Begin("##mainMenuButtons", NULL, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_AlwaysAutoResize)) {
            ImGui::NewLine();
                if (ImGui::Button("New Game")) {
                    ImGui::OpenPopup("##Select Dialog NEWGAME01");
                }
                ImGui::NewLine();
                if (ImGui::Button("Save Game")) {
                    ImGui::OpenPopup("##Select Dialog SAVEGAME01");
            ImGui::NewLine();
                if (ImGui::Button("Load Game")) {
                    ImGui::OpenPopup("##Select Dialog LOADGAME01");
            }
            ImGui::NewLine();
                if (ImGui::Button("Options"))
                {
                    ImGui::OpenPopup("##Select Dialog OPTIONS01");
                }
            ImGui::NewLine();
                if (ImGui::Button("Quit Game")) {
                    ImGui::OpenPopup("##Confirm Dialog EXIT01");
            }
        }

void drawMainMenuStyle() {
    ImGuiStyle& styleMenuMain = ImGui::GetStyle();
    float alphaL = 0.0f;
    styleMenuMain.Colors[ImGuiCol_WindowBg] = ImVec4(0, 0, 0, alphaL);
    styleMenuMain.Colors[ImGuiCol_Border] = ImVec4(0, 0, 0, alphaL);
    styleMenuMain.Colors[ImGuiCol_TitleBg] = ImVec4(0, 0, 0, alphaL);
    styleMenuMain.Colors[ImGuiCol_TitleBgActive] = ImVec4(0, 0, 0, alphaL);
    styleMenuMain.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0, 0, 0, alphaL);
    styleMenuMain.Colors[ImGuiCol_Button] = ImVec4(0.35, 0.33, 0.34, alphaL);
    styleMenuMain.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.30, 0.28, 0.29, 0.5f);
    styleMenuMain.Colors[ImGuiCol_ButtonActive] = ImVec4(0.28, 0.26, 0.27, 0.8f);
}
PathogenDavid commented 2 years ago

This typically indicates that the size of your swap chain does not match the size of the window.

If you compare the sizes used between the swap chain and Dear ImGui do they match? IE:

DXGI_SWAP_CHAIN_DESC description;
if (g_pSwapChain->GetDesc(&description) != S_OK) // Substitute g_pSwapChain for your swap chain
    printf("GetDesc failed!\n");
ImVec2 dearImGuiSize = ImGui::GetIO().DisplaySize;
printf("DXGI: %ux%u Dear ImGui: %dx%d\n", description.BufferDesc.Width, description.BufferDesc.Height, (int)dearImGuiSize.x, (int)dearImGuiSize.y);

Most likely the swap chain is wrong and Dear ImGui is correct. Make sure you're handling WM_SIZE correctly.

PsychoCoffee commented 2 years ago

This typically indicates that the size of your swap chain does not match the size of the window.

If you compare the sizes used between the swap chain and Dear ImGui do they match? IE:

DXGI_SWAP_CHAIN_DESC description;
if (g_pSwapChain->GetDesc(&description) != S_OK) // Substitute g_pSwapChain for your swap chain
    printf("GetDesc failed!\n");
ImVec2 dearImGuiSize = ImGui::GetIO().DisplaySize;
printf("DXGI: %ux%u Dear ImGui: %dx%d\n", description.BufferDesc.Width, description.BufferDesc.Height, (int)dearImGuiSize.x, (int)dearImGuiSize.y);

Most likely the swap chain is wrong and Dear ImGui is correct. Make sure you're handling WM_SIZE correctly.

I don't know exactly where to look, but I put the code in the part where I initialize DirectX11. When I run the program now, it is instantly giving me CreateContext errors. "No current context, did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"

When it comes to viewports and swapchains, I know nothing (hence why I am getting this error).

DXGI_SWAP_CHAIN_DESC scd = { 0 };

        scd.BufferDesc.Width = this->windowWidth;
        scd.BufferDesc.Height = this->windowHeight;
        scd.BufferDesc.RefreshRate.Numerator = 60;
        scd.BufferDesc.RefreshRate.Denominator = 1;
        scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
        scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

        scd.SampleDesc.Count = 1;
        scd.SampleDesc.Quality = 0;

        scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
        scd.BufferCount = 1;
        scd.OutputWindow = hwnd;

        ImGuiContext* ctx1 = ImGui::GetCurrentContext();
        ImGui::SetCurrentContext(ctx1);
        ImVec2 dearImGuiSize = ImGui::GetIO().DisplaySize;
        printf("DXGI: %ux%u Dear ImGui: %dx%d\n", scd.BufferDesc.Width, scd.BufferDesc.Height, (int)dearImGuiSize.x, (int)dearImGuiSize.y);

        if(windowedMode)
            scd.Windowed = TRUE;
        else
            scd.Windowed = FALSE;

        scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
        scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
ocornut commented 2 years ago

I don't know exactly where to look,

Add the print code in your main loop to display the value after resizing. The purpose of this code is to display and compare both values while running.

When it comes to viewports and swapchains, I know nothing (hence why I am getting this error).

Refer to examples/example_win32_directx11/. You app needs to react to WM_SIZE event and resize its buffers.

ocornut commented 2 years ago

Closing as answered. @PsychoCoffee let us know if that solved your issue.