Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling mouse capture on 3D viewport window implemented using ImGui::Image() #4831

Open
tksuoran opened this issue Dec 26, 2021 · 7 comments

Comments

@tksuoran
Copy link

Version/Branch of Dear ImGui:

Version: 1.86
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: custom (erhe)
Compiler: Visual Studio 2022
Operating System: Windows 10

My Issue/Question:

I currently use ImGuiIO::WantCaptureMouse to see if I should process mouse events or not. When I am docking a window to another one, WantCaptureMouse seems not to be set.

Is there a way to detect that docking is active? Or should I do something differently?

@ocornut
Copy link
Owner

ocornut commented Dec 26, 2021

If confirmed that seems like a bug to me.

@ocornut
Copy link
Owner

ocornut commented Dec 26, 2021

When I am docking a window to another one, WantCaptureMouse seems not to be set.

I cannot seem to repro this. If I open the Demo under Inputs you can visualize io.WantCaptureMouse (or a simple Text() call anywhere can do the same thing) and it seems to be always 1 at the right time.

Please clarify your statement with a repro and a video.

@tksuoran
Copy link
Author

I've confirmed that ImGuiIO::WantCaptureMouse works and the issue is in my own logic. Thank you for the support and sorry for the trouble.

@tksuoran tksuoran reopened this Dec 27, 2021
@tksuoran tksuoran changed the title ImGuiIO::WantCaptureMouse is not set when docking a window Need a way to detect when docking a window Dec 27, 2021
@tksuoran
Copy link
Author

It looks like ImGuiIO::WantCaptureMouse always true when hovering ImGui window. Is this right?

I have a 3D scene viewport imgui window, and user interacts with it using mouse. For example dragging mouse turns the camera:

2021_11_26

My existing logic uses ImGuiIO::WantCaptureMouse except when mouse is hovering on viewport window, so that I can use mouse to interact with the window. This works mostly, but fails when docking another window to the viewport window.

I wonder what exactly would be the most correct way to deal with the issue. Options that I have considered:

  • Add ImGui window flag to ask ImGui not to set capture mouse within that window. I would still want the title/border work though.
  • Add ImGui io flag to tell when docking is in progress
  • Filter events in the application using some other logic

I think I can get the last option to work, but I am curious if the first two options would make any sense at all.

Thanks again!

I was considering detecting when docking was in progr
I could work around this by rejecting mouse drags which start outside viewport window. I was just wondering if being able to detect

@ocornut
Copy link
Owner

ocornut commented Dec 27, 2021

It looks like ImGuiIO::WantCaptureMouse always true when hovering ImGui window. Is this right?

Yes, that's one of the condition which would make this flag true (there are other).

My existing logic uses ImGuiIO::WantCaptureMouse except when mouse is hovering on viewport window

Sounds good.

This works mostly, but fails when docking another window to the viewport window.

Then please focus on making a repro and proving and fixing this because it should work.... assuming you are using IsItemHovered() on that 3d viewport window... Anything else suggested (e.g. including this topic "Need to detect when docking a window" is a XY Problem in the first place. You are focusing on a probably incorrect workaround/solution. You should be able to use the information provided by IsItemHovered().

Add ImGui window flag to ask ImGui not to set capture mouse within that window. I would still want the title/border work though.

Inside your window you are likely submitting an Image() call so you can use IsItemHovered() after that Image() call and use that value for your input filtering and title/border will still go through dear imgui. Technically you may overwrite the WantCaptureMouse flag as well, by calling ImGui::CaptureMouseFromApp(false) but it'll be equivalent to testing io.WantCaptureMouse && !was_my_image_hovered as you already are trying to do (and without details/repro we can't magically guess what you are doing wrong).

Note that this is all because you want to use your own input data when clicking on that 3D viewport... you may as well simply use dear imgui inputs... Replace Image() by ImageButton() (without border) to make it interactive, then if (IsItemActive()) and you can simply poll Dear ImGui mouse data from there.

If in doubt about the meaning of some values 1) check demo 2) simply display them on the screen...

@tksuoran
Copy link
Author

Thank you very much. IsItemHovered() works perfectly.

It works a bit differently (better!) from what I had expected:

  • What I (incorrectly) thought IsItemHovered() does: tells me if mouse cursor is currently on top of an item.
  • Actual behavior seems to be somewhat more complicated: If I start dragging in window A and continue the same drag over to another window B, IsItemHovered() for an image in window B keeps returning false until mouse button is released.

I had implemented tracking in which window the drag was started in my application code and got the mouse working, but I replaced that as unnecessary, because IsItemHovered() works in a useful way.

@tksuoran tksuoran changed the title Need a way to detect when docking a window Handling mouse capture on 3D viewport window implemented using ImGui::Image() Jan 1, 2022
@tksuoran
Copy link
Author

tksuoran commented Jan 1, 2022

I have one more issue:

Combining io.WantCaptureMouse and ImGui::IsItemHovered() does not seem to be able to cover the case where I resize the window from the bottom right corner triangle. What would be a suggested way to deal with this?

Actual behavior when resizing a viewport window from the bottom right triangle:

  • Window resizes
  • App processes mouse drag also in the viewport

Expected behavior:

  • Window resizes
  • App can determine that the input event should be discarded

I have intentionally zero window padding, and so the resizing triangle does overlap with the image.

Thanks again!

@tksuoran tksuoran reopened this Jan 1, 2022
ocornut added a commit that referenced this issue May 23, 2022
…FrameWantCaptureMouse() and SetNextFrameWantCaptureKeyboard(). Added demo. (#5304, #4831, #4480, #533)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants