-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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 events #4234
Comments
It's really hard to tell exactly what you're trying to accomplish based on your description. It'd be helpful to know what your end goal is.
|
Fully agree with your statement. So, in the picture the green bit is the video of the emulator. I would like to receive mouse events when the mouse pointer is over the image (the green bit) (ideally in x,y relative to the image). In most GUI frameworks, every widget has a virtual |
Ah that makes much more sense, thanks for clarifying! One of the biggest differences between Dear ImGui and many other UI frameworks is it is not inherently event-driven. Assuming you're submitting your screen as an Here's a quick example: // Using the font texture as an example, you'd obviously use your emulator's screen texture instead
ImGuiIO& io = ImGui::GetIO();
ImTextureID textureId = io.Fonts->TexID;
ImVec2 textureSize = ImVec2(io.Fonts->TexWidth, io.Fonts->TexHeight);
ImGui::Begin("GH-4233 Example");
ImGui::ImageButton(textureId, textureSize);
bool isHovered = ImGui::IsItemHovered();
bool isFocused = ImGui::IsItemFocused();
ImVec2 mousePositionAbsolute = ImGui::GetMousePos();
ImVec2 screenPositionAbsolute = ImGui::GetItemRectMin();
ImVec2 mousePositionRelative = ImVec2(mousePositionAbsolute.x - screenPositionAbsolute.x, mousePositionAbsolute.y - screenPositionAbsolute.y);
ImGui::Text("Is mouse over screen? %s", isHovered ? "Yes" : "No");
ImGui::Text("Is screen focused? %s", isFocused ? "Yes" : "No");
ImGui::Text("Position: %f, %f", mousePositionRelative.x, mousePositionRelative.y);
ImGui::Text("Mouse clicked: %s", ImGui::IsMouseDown(ImGuiMouseButton_Left) ? "Yes" : "No");
ImGui::End(); Note the use of A more built-out version for your Apple ][ emulator might look something like this pseudocode: ImGui::Begin("Apple ][");
ImGui::ImageButton(screenTexture, screenTextureSize);
if (ImGui::IsItemHovered())
{
ImVec2 mousePositionAbsolute = ImGui::GetMousePos();
ImVec2 screenPositionAbsolute = ImGui::GetItemRectMin();
ImVec2 mousePositionRelative = ImVec2(mousePositionAbsolute.x - screenPositionAbsolute.x, mousePositionAbsolute.y - screenPositionAbsolute.y);
SendMousePositionToEmulator(mousePositionRelative);
}
// If the emulator has focus, send it mouse button/keyboard events
if (ImGui::IsItemFocused())
{
SetEmulatorLeftMouseDownState(ImGui::IsMouseDown(ImGuiMouseButton_Left));
// ...etc for other mouse buttons
for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++)
{
SetEmulatorKeyState(i, io.KeysDown[i]);
}
}
// When the emulator looses focus, release all buttons
else
{
SetEmulatorLeftMouseDownState(false);
// ...etc for other mouse buttons
for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++)
{
SetEmulatorKeyState(i, false);
}
}
ImGui::End(); Note that the index for the If you need to have this be event-driven for some reason, you'd have to modify whatever platform backend you're using or capture the inputs some other way. (You'd probably still want to use |
I see. At the moment this is all event driven because I display the emulator window as the background of the ImGui application, so I get (residual) events from SDL when no other ImGui widget is focused. I see what you mean, is that I can ask any time what the mouse and keyboard state is, but I need to do some book-keeping to decide if an event has been generated or not. I guess I need to compute the delta and fire when it changes. So in a sense I need to decide if I stick to SDL events or I jump completely to ImGui state-polling. |
Yup, sounds like you got it! I'd probably go with whatever fits more naturally with your emulator, or just stick to SDL events since it sounds like you already have them working. If your goal is to be able to switch between having the app as a fullscreen background or as its own window without having the logic diverge, you might check out the fullscreen window example under the Examples menu in the demo and use that for your fullscreen background mode. (The example doesn't use this, but in your case you'd want to specify the Or if your goal is to be able to view the debugger windows without having them on top of your emulator's screen, you might check out the experimental docking branch. You don't need to use the docking features, but an added bonus of this branch is that it lets you pull windows outside of the main viewport. |
This is exactly what I was doing. I will have a look as you suggest and weigh the pros and cons. Thanks everybody. |
Closing this as answered, thanks for the thoughtful answers! |
I am using
1.84 WIP
.I would like to handle mouse events and following instructions I do not handle them if ImGui wants to (
io.WantCaptureMouse
).Basically I would like to know for a particular ImGui window
My problem is that as soon as the mouse is over a window, I get
io.WantCaptureMouse
and so I loose the event.Otherwise I guess I could use functions in this area
imgui/imgui.h
Lines 336 to 344 in 81eceb0
is this correct?
The text was updated successfully, but these errors were encountered: