ocornut / imgui

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

io.WantCaptureMouse doesn't work for scrollwheel #7588

Open YTN0 opened 6 months ago

YTN0 commented 6 months ago

Version/Branch of Dear ImGui:

Latest

Back-ends:

Custom

Compiler, OS:

Windows 11

Full config/build information:

No response

Details:

io.WantCaptureMouse always seems to return true for scrollwheel changes even when not "captured" within a window. This makes it difficult to tell if a scrollwheel action is intended for the window or the main (non-ImGui) UI.

Any recommended solutions for this?

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

ocornut commented 6 months ago

Merely hovering a window will generally set io.WantCaptureMouse, and mouse wheels are processed by hovered windows, so I am not sure I understand your question or reasoning. Could you clarify?

YTN0 commented 6 months ago

I have some imgui windows on the screen overlaying parts of the main rendered window.

For my mouse event processing, I send the mouse events to imgui first and if WantCaptureMouse is false, I then forward the event to my main mouse event handler.

For scrollwheel events., even when I am not over an imgui window (am over the main rendered screen) and I send the mouse scroll event to imgui, it returns "true" for WantCaptureMouse., so my main window (which should process the event) never gets it.

ocornut commented 6 months ago

For scrollwheel events., even when I am not over an imgui window (am over the main rendered screen) and I send the mouse scroll event to imgui, it returns "true" for WantCaptureMouse., so my main window (which should process the event) never gets it.

That's not the case in our vanilla demo, you can verify it in Demo->Inputs. And as a general fact wheel state or events have no effect on WantCaptureMouse. So I think you have another thing interfering which is causing WantCaptureMouse to be set.

When unsure, just display the live value using ImGui::Text() so you can investigate.

memononen commented 3 days ago

I ran into the same issue when I tried to implement mouse wheel zoom for my "background main view".

My code looks roughly like this:

ImGui::NewFrame();

// UI stuff

ImGui::Render();

if (!io.WantCaptureMouse) {
    // Handle 3D viewport mouse interaction
}
// Render 3D viewport

io.MouseWheel is always zero, because it is zeroed out in imGui::EndFrame() (which is called on Render). Other mouse events seem to work.

There's this fine print in the comments, though:

(You want to try calling EndFrame/Render as late as you can, to be able to use Dear ImGui in your own game rendering code)

So it sounds like the right solution is to call ImGui::Render() after rendering my 3D viewport.

[edit] Just wanted to clarify that calling ImGui::Render() after the 3D viewport rendering works.