ocornut / imgui

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

Window size + custom rendering #7634

Open frosthuuund opened 5 months ago

frosthuuund commented 5 months ago

Version/Branch of Dear ImGui:

Version 1.90.6, Branch: master (release) (master/docking/etc.)

Back-ends:

imgui_impl_dx9.cpp + imgui_impl_win32.cpp

Compiler, OS:

win11 + vs2022

Full config/build information:

No response

Details:

My Issue/Question: Hello, im doing things with custom rendering and im struggling with window size. I drew a perfect rectangle which scales with window size, but i can't make window fit perfectly outside canvas. So basically, i want to be able resize the window, the canvas should scale with window size and both canvas and window have 1:1 aspect ratio. Any solution about that? I gave up.

Screenshots/Video:

image

Minimal, Complete and Verifiable Example code:

GamingMinds-DanielC commented 5 months ago

It would be far easier to point you in the right direction if you posted the code that isn't working, it often is just a small thing that is wrong or missing. So without knowing what you tried so far, take a look at ImGui::GetContentRegionAvail() (called from within the window).

ocornut commented 5 months ago

As suggested by above reply, you are not sharing or explaining enough so we can only guess.

Maybe "Demo->Examples->Constrained Window Resize" can be useful. One issue is that currently the ImGuiSizeCallbackData callback deals with window size, not accounting for the fact that decoration (title bar, etc.) and window padding will be desirable. Generally, Window Size = Contents Size + WindowPadding * 2 + TitleBarSize. Title Bar is GetFrameHeight() high. Based on it you can adapt the linked demo to your need.

We should rework ImGuiSizeCallbackData to conveniently provide decoration sizes as part of the structure to ease computation, or add new fields to allow the user to manipulate/overwrite a "Client size".

DynamicalCreator commented 5 months ago

As ocornut said, Demo->Examples->Constrained Window Resize is what you are probably looking for. You still need to handle the decoration size as he also said.

Code taken from imgui_demo.cpp:

struct CustomConstraints
    {
       static void AspectRatio(ImGuiSizeCallbackData* data)
        {
            float aspect_ratio = *(float*)data->UserData;
            data->DesiredSize.y = (float)(int)(data->DesiredSize.x / aspect_ratio);
        }
        static void Square(ImGuiSizeCallbackData* data)
        {
            data->DesiredSize.x = data->DesiredSize.y = IM_MAX(data->DesiredSize.x, data->DesiredSize.y);
        }
    };

// lock the window size to be always square
ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Square);

// or if you need some other aspect ratio like 16:9
// float aspect_ratio = 16.0f / 9.0f;
// ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::AspectRatio, (void*)&aspect_ratio);

// your window here
if (ImGui::Begin("Canvas"))
{
    // code
}