Imgui Inputs are offset if Application window size is different than the Render texture size.
Version: 1.89.1 Branch: master
Back-ends: imgui_impl_dx12.cpp + imgui_impl_win32.cpp
Operating System: Windows 11
How to scale Imgui window if the Texture resolution is different than the window size?
I have a fixed texture resolution which I am setting the Imgui render target to ( 1280 * 720 ). But my application window size is dependent on the resolution of the display being used.
I tried a couple of things suggested in the discussions.
ImGui::GetStyle().ScaleAllSizes();
ImGui::GetIO().FontGlobalScale;
These fix the text sizes according to the window but the imgui buttons and inputs (Which require mouse input) are not working and are offset by some value. (Meaning they are not taking inputs when I hover over them, I have to hover over in the window at a slightly offset location for them to work.)
In this image I am NOT hovering over the color option. The render target resolution in 720p and the window size is 0.9 * the output display size.
In this image the window and Render target sizes are the same.
I may have missed these in other discussions/issues.
The premade backends handle the default case (matching render target size). For your custom case you would need a custom backend. That's not as complicated as it sounds, you can build on the premade ones. The way I would approach it in your case is:
- copy
imgui_impl_win32.cppto a custom version (corresponding header as well, better to untangle from the start) - in the handling of
WM_MOUSEMOVE, scale and offset coordinates according to your needs - depending on your needs, either adjust
ImGui_ImplWin32_UpdateMouseDataas well or removeImGuiBackendFlags_HasSetMousePosfromImGui_ImplWin32_Init
Should be easy enough, but you would have to merge later fixes to the premade backend to your custom version yourself. At least, if those fixes are relevant to you.
Nowadays it would be easier to peek into the event queue and modify values.
This is similar to issues some DPI methods are facing, we could consider exposing some kind of transform honored on dear imgui side, but it would need to make sense for multi-viewport and multi-monitors. Albeit a different issue, #2176 would optimally be best solved by decorelating OS coordinates from Dear ImGui coordinates.
I am tagging this with dpi tag - although the issue posed is not directly DPI related, it is essentially the same problem needed to support hi-dpi on Mac which requires a scaling of inputs.
I ve had this problem with Android
You can detect window size changes
Before this ImGui::NewFrame();
if(glwh){
glw=glWidth;
glh=glHeight;
glwh= false;
}
if(glWidth!=glw){
float w_w,y_y;
w_w=(float)glWidth/(float)glw;
y_y=(float)glHeight/(float)glh;
//You can adjust it through this
io.DisplayFramebufferScale=ImVec2(w_w, y_y);
}
ImGui::NewFrame();