How to allow window resize to set an aspect ratio, for a video preview window? #5627

Closed
opened 2022-09-01 06:21:43 +00:00 by moebiussurfing · 4 comments
moebiussurfing commented 2022-09-01 06:21:43 +00:00 (Migrated from github.com)

Version: 1.88
Branch: docking
openFrameworks / Windows

My Issue/Question:

I want to draw a window that can be resized (with the draggable top right bottom triangle)
I draw an fbo inside.
But when I set the window shape, (after calculating the window height related to the fbo aspect ratio),
the draggable triangle disappears.
Probably it's related to the passed ImGuiCond_Always condition... ?

Any idea/workaround to improve this?

Here's some pseudo / openFrameworks code;

ImGuiWindowFlags window_flags = ImGuiWindowFlags_None;
if (bAutoResize) window_flags += ImGuiWindowFlags_AlwaysAutoResize;

static bool bready = false;
static ofRectangle win;
static float spx;
static float spy;
static float wWin;
static float hWin;

//--

// target

float ratio = fbo.getWidth() / fbo.getHeight();

static float wWinTar = wWin;
static float hWinTar = wWin * ratio;

//static float wWinTar = wWin + (2 * spx); // taking care of spacing..
//static float hWinTar = (wWin * ratio) + (2 * spy);

//--

//TODO: 
// Fix: resize triangle dissapears when setting size. maybe bc the cond...

// Other approaches
//ImVec2 size_min(wWinTar, hWinTar);
//ImVec2 size_max(-1,-1);
//if (bready && bLock) ImGui::SetNextWindowSizeConstraints(size_min, size_max);

//if (bready) ImGui::SetNextWindowContentSize(ImVec2(wWinTar, hWinTar));

//--

ImVec2 vTar(wWinTar, hWinTar);

if (bReady) ImGui::SetNextWindowSize(vTar, ImGuiCond_Always);

if (ImGui::Begin(name.c_str(), p_open, window_flags);
{
    // get window props
    spx = ImGui::GetStyle().ItemSpacing.x;
    spy = ImGui::GetStyle().ItemSpacing.y;
    auto r = ofRectangle(
    ImGui::GetWindowPos().x,
    ImGui::GetWindowPos().y,
    ImGui::GetWindowWidth(),
    ImGui::GetWindowHeight());
    wWin= r.getWidth();
    hWin = r.getHeight();
    bReady = true; 

    DrawFboPreview(fbo);

    ImGui::End();
}

Regards

Version: 1.88 Branch: docking openFrameworks / Windows **My Issue/Question:** I want to draw a window that can be resized (with the draggable top right bottom triangle) I draw an fbo inside. But when I set the window shape, (after calculating the window height related to the fbo aspect ratio), the draggable triangle disappears. Probably it's related to the passed **ImGuiCond_Always** condition... ? Any idea/workaround to improve this? Here's some pseudo / **openFrameworks** code; ```.cpp ImGuiWindowFlags window_flags = ImGuiWindowFlags_None; if (bAutoResize) window_flags += ImGuiWindowFlags_AlwaysAutoResize; static bool bready = false; static ofRectangle win; static float spx; static float spy; static float wWin; static float hWin; //-- // target float ratio = fbo.getWidth() / fbo.getHeight(); static float wWinTar = wWin; static float hWinTar = wWin * ratio; //static float wWinTar = wWin + (2 * spx); // taking care of spacing.. //static float hWinTar = (wWin * ratio) + (2 * spy); //-- //TODO: // Fix: resize triangle dissapears when setting size. maybe bc the cond... // Other approaches //ImVec2 size_min(wWinTar, hWinTar); //ImVec2 size_max(-1,-1); //if (bready && bLock) ImGui::SetNextWindowSizeConstraints(size_min, size_max); //if (bready) ImGui::SetNextWindowContentSize(ImVec2(wWinTar, hWinTar)); //-- ImVec2 vTar(wWinTar, hWinTar); if (bReady) ImGui::SetNextWindowSize(vTar, ImGuiCond_Always); if (ImGui::Begin(name.c_str(), p_open, window_flags); { // get window props spx = ImGui::GetStyle().ItemSpacing.x; spy = ImGui::GetStyle().ItemSpacing.y; auto r = ofRectangle( ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); wWin= r.getWidth(); hWin = r.getHeight(); bReady = true; DrawFboPreview(fbo); ImGui::End(); } ``` Regards
ocornut commented 2022-09-01 06:25:58 +00:00 (Migrated from github.com)

This is demonstrated in Demo>Examples>Constrained Resized, see the ratio resizing example there and how to use a callback in SetNextWindowSizeConstraints().

Note that window size include title screen and other decoration, so if you need client size to be resized according to a ratio it needs a bit of extra calculation.

with WindowPadding=0: WindowSize = ClientSize + TitleBarSize which is GetFrameHeight() GetTextLineWithPadding(). EDIT

This is demonstrated in Demo>Examples>Constrained Resized, see the ratio resizing example there and how to use a callback in SetNextWindowSizeConstraints(). Note that window size include title screen and other decoration, so if you need client size to be resized according to a ratio it needs a bit of extra calculation. with WindowPadding=0: WindowSize = ClientSize + TitleBarSize which is GetFrameHeight() ~~GetTextLineWithPadding().~~ *EDIT*
moebiussurfing commented 2022-09-01 06:27:09 +00:00 (Migrated from github.com)

thanks, you are so kind.

thanks, you are so kind.
ocornut commented 2022-09-01 06:28:53 +00:00 (Migrated from github.com)

I am going to amend the existing examples to clarify this calculation of client size and use non-square ratio.

I am going to amend the existing examples to clarify this calculation of client size and use non-square ratio.
ocornut commented 2022-09-01 09:21:52 +00:00 (Migrated from github.com)

I added a more explicit example for this in 2569c64.

It is missing handling of window decoration (title bar). The correct and generic way to do it is a little tricky and I think the library should eventually provide more information and option to the user. In most situation, you can use:

float frame_height = ImGui::GetFrameHeight();
ImVec2 extra_size_needed = ImVec2(0.0f, frame_height); // For title-bar. Multiply by 2 if you have a menu.

And then perform the aspect ratio calculation by doing:

ComputeRatioedSize(CurrentSize - ExtraSizeNeeded) + ExtraSizeNeeded.

I will aim to improve examples but they need better support for the library.

Also depending on your style and need to display all pixels of a texture precisely, if your window has zero WindowPadding, it is probable you need to account for border size:

extra_size_needed  += ImVec2(ImGui::GetStyle().WindowBorderSize * 2, ImGui::GetStyle().WindowBorderSize * 2);

The reason being that by design we made border size not affect layout (it simplifies a few things).

I sense we need to improve a few things in the library.
We have a known table issue #3692 which is fairly severe/noticeable and will need us to refactor how window handle their decoration size. This will also lead us to #3518.

I added a more explicit example for this in 2569c64. It is missing handling of window decoration (title bar). The correct and generic way to do it is a little tricky and I think the library should eventually provide more information and option to the user. In most situation, you can use: ``` float frame_height = ImGui::GetFrameHeight(); ImVec2 extra_size_needed = ImVec2(0.0f, frame_height); // For title-bar. Multiply by 2 if you have a menu. ``` And then perform the aspect ratio calculation by doing: ComputeRatioedSize(CurrentSize - ExtraSizeNeeded) + ExtraSizeNeeded. I will aim to improve examples but they need better support for the library. Also depending on your style and need to display all pixels of a texture precisely, if your window has zero WindowPadding, it is probable you need to account for border size: ``` extra_size_needed += ImVec2(ImGui::GetStyle().WindowBorderSize * 2, ImGui::GetStyle().WindowBorderSize * 2); ``` The reason being that by design we made border size not affect layout (it simplifies a few things). I sense we need to improve a few things in the library. We have a known table issue #3692 which is fairly severe/noticeable and will need us to refactor how window handle their decoration size. This will also lead us to #3518.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#5627
No description provided.