Fix MOUSE_MODE_CAPTURED not working correctly with popups

This commit is contained in:
MinusKube 2022-09-07 19:31:57 +02:00
parent e7a0a97c0b
commit 06b86fad0d
9 changed files with 92 additions and 37 deletions

View File

@ -397,7 +397,10 @@ void DisplayServerX11::mouse_set_mode(MouseMode p_mode) {
if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) {
//flush pending motion events //flush pending motion events
_flush_mouse_motion(); _flush_mouse_motion();
WindowID window_id = windows.has(last_focused_window) ? last_focused_window : MAIN_WINDOW_ID; WindowID window_id = _get_focused_window_or_popup();
if (!windows.has(window_id)) {
window_id = MAIN_WINDOW_ID;
}
WindowData &window = windows[window_id]; WindowData &window = windows[window_id];
if (XGrabPointer( if (XGrabPointer(
@ -433,7 +436,11 @@ void DisplayServerX11::warp_mouse(const Point2i &p_position) {
if (mouse_mode == MOUSE_MODE_CAPTURED) { if (mouse_mode == MOUSE_MODE_CAPTURED) {
last_mouse_pos = p_position; last_mouse_pos = p_position;
} else { } else {
WindowID window_id = windows.has(last_focused_window) ? last_focused_window : MAIN_WINDOW_ID; WindowID window_id = _get_focused_window_or_popup();
if (!windows.has(window_id)) {
window_id = MAIN_WINDOW_ID;
}
XWarpPointer(x11_display, None, windows[window_id].x11_window, XWarpPointer(x11_display, None, windows[window_id].x11_window,
0, 0, 0, 0, (int)p_position.x, (int)p_position.y); 0, 0, 0, 0, (int)p_position.x, (int)p_position.y);
} }
@ -3181,6 +3188,15 @@ void DisplayServerX11::_window_changed(XEvent *event) {
} }
} }
DisplayServer::WindowID DisplayServerX11::_get_focused_window_or_popup() const {
const List<WindowID>::Element *E = popup_list.back();
if (E) {
return E->get();
}
return last_focused_window;
}
void DisplayServerX11::_dispatch_input_events(const Ref<InputEvent> &p_event) { void DisplayServerX11::_dispatch_input_events(const Ref<InputEvent> &p_event) {
static_cast<DisplayServerX11 *>(get_singleton())->_dispatch_input_event(p_event); static_cast<DisplayServerX11 *>(get_singleton())->_dispatch_input_event(p_event);
} }
@ -3936,7 +3952,11 @@ void DisplayServerX11::process_events() {
// The X11 API requires filtering one-by-one through the motion // The X11 API requires filtering one-by-one through the motion
// notify events, in order to figure out which event is the one // notify events, in order to figure out which event is the one
// generated by warping the mouse pointer. // generated by warping the mouse pointer.
WindowID focused_window_id = windows.has(last_focused_window) ? last_focused_window : MAIN_WINDOW_ID; WindowID focused_window_id = _get_focused_window_or_popup();
if (!windows.has(focused_window_id)) {
focused_window_id = MAIN_WINDOW_ID;
}
while (true) { while (true) {
if (mouse_mode == MOUSE_MODE_CAPTURED && event.xmotion.x == windows[focused_window_id].size.width / 2 && event.xmotion.y == windows[focused_window_id].size.height / 2) { if (mouse_mode == MOUSE_MODE_CAPTURED && event.xmotion.x == windows[focused_window_id].size.width / 2 && event.xmotion.y == windows[focused_window_id].size.height / 2) {
//this is likely the warp event since it was warped here //this is likely the warp event since it was warped here

View File

@ -284,6 +284,8 @@ class DisplayServerX11 : public DisplayServer {
Context context = CONTEXT_ENGINE; Context context = CONTEXT_ENGINE;
WindowID _get_focused_window_or_popup() const;
void _send_window_event(const WindowData &wd, WindowEvent p_event); void _send_window_event(const WindowData &wd, WindowEvent p_event);
static void _dispatch_input_events(const Ref<InputEvent> &p_event); static void _dispatch_input_events(const Ref<InputEvent> &p_event);
void _dispatch_input_event(const Ref<InputEvent> &p_event); void _dispatch_input_event(const Ref<InputEvent> &p_event);

View File

@ -187,6 +187,8 @@ private:
Point2i _get_native_screen_position(int p_screen) const; Point2i _get_native_screen_position(int p_screen) const;
static void _displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplayChangeSummaryFlags flags, void *user_info); static void _displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplayChangeSummaryFlags flags, void *user_info);
WindowID _get_focused_window_or_popup() const;
static void _dispatch_input_events(const Ref<InputEvent> &p_event); static void _dispatch_input_events(const Ref<InputEvent> &p_event);
void _dispatch_input_event(const Ref<InputEvent> &p_event); void _dispatch_input_event(const Ref<InputEvent> &p_event);
void _push_input(const Ref<InputEvent> &p_event); void _push_input(const Ref<InputEvent> &p_event);

View File

@ -316,6 +316,15 @@ void DisplayServerMacOS::_displays_arrangement_changed(CGDirectDisplayID display
} }
} }
DisplayServer::WindowID DisplayServerMacOS::_get_focused_window_or_popup() const {
const List<WindowID>::Element *E = popup_list.back();
if (E) {
return E->get();
}
return last_focused_window;
}
void DisplayServerMacOS::_dispatch_input_events(const Ref<InputEvent> &p_event) { void DisplayServerMacOS::_dispatch_input_events(const Ref<InputEvent> &p_event) {
((DisplayServerMacOS *)(get_singleton()))->_dispatch_input_event(p_event); ((DisplayServerMacOS *)(get_singleton()))->_dispatch_input_event(p_event);
} }
@ -1827,7 +1836,10 @@ void DisplayServerMacOS::mouse_set_mode(MouseMode p_mode) {
return; return;
} }
WindowID window_id = windows.has(last_focused_window) ? last_focused_window : MAIN_WINDOW_ID; WindowID window_id = _get_focused_window_or_popup();
if (!windows.has(window_id)) {
window_id = MAIN_WINDOW_ID;
}
WindowData &wd = windows[window_id]; WindowData &wd = windows[window_id];
if (p_mode == MOUSE_MODE_CAPTURED) { if (p_mode == MOUSE_MODE_CAPTURED) {
// Apple Docs state that the display parameter is not used. // Apple Docs state that the display parameter is not used.
@ -1942,7 +1954,10 @@ void DisplayServerMacOS::warp_mouse(const Point2i &p_position) {
_THREAD_SAFE_METHOD_ _THREAD_SAFE_METHOD_
if (mouse_mode != MOUSE_MODE_CAPTURED) { if (mouse_mode != MOUSE_MODE_CAPTURED) {
WindowID window_id = windows.has(last_focused_window) ? last_focused_window : MAIN_WINDOW_ID; WindowID window_id = _get_focused_window_or_popup();
if (!windows.has(window_id)) {
window_id = MAIN_WINDOW_ID;
}
WindowData &wd = windows[window_id]; WindowData &wd = windows[window_id];
// Local point in window coords. // Local point in window coords.

View File

@ -104,7 +104,10 @@ void DisplayServerWindows::_set_mouse_mode_impl(MouseMode p_mode) {
if (windows.has(MAIN_WINDOW_ID) && (p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_CONFINED || p_mode == MOUSE_MODE_CONFINED_HIDDEN)) { if (windows.has(MAIN_WINDOW_ID) && (p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_CONFINED || p_mode == MOUSE_MODE_CONFINED_HIDDEN)) {
// Mouse is grabbed (captured or confined). // Mouse is grabbed (captured or confined).
WindowID window_id = windows.has(last_focused_window) ? last_focused_window : MAIN_WINDOW_ID; WindowID window_id = _get_focused_window_or_popup();
if (!windows.has(window_id)) {
window_id = MAIN_WINDOW_ID;
}
WindowData &wd = windows[window_id]; WindowData &wd = windows[window_id];
@ -119,11 +122,15 @@ void DisplayServerWindows::_set_mouse_mode_impl(MouseMode p_mode) {
ClientToScreen(wd.hWnd, &pos); ClientToScreen(wd.hWnd, &pos);
SetCursorPos(pos.x, pos.y); SetCursorPos(pos.x, pos.y);
SetCapture(wd.hWnd); SetCapture(wd.hWnd);
_register_raw_input_devices(window_id);
} }
} else { } else {
// Mouse is free to move around (not captured or confined). // Mouse is free to move around (not captured or confined).
ReleaseCapture(); ReleaseCapture();
ClipCursor(nullptr); ClipCursor(nullptr);
_register_raw_input_devices(INVALID_WINDOW_ID);
} }
if (p_mode == MOUSE_MODE_HIDDEN || p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_CONFINED_HIDDEN) { if (p_mode == MOUSE_MODE_HIDDEN || p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_CONFINED_HIDDEN) {
@ -139,6 +146,37 @@ void DisplayServerWindows::_set_mouse_mode_impl(MouseMode p_mode) {
} }
} }
DisplayServer::WindowID DisplayServerWindows::_get_focused_window_or_popup() const {
const List<WindowID>::Element *E = popup_list.back();
if (E) {
return E->get();
}
return last_focused_window;
}
void DisplayServerWindows::_register_raw_input_devices(WindowID p_target_window) {
use_raw_input = true;
RAWINPUTDEVICE rid[1] = {};
rid[0].usUsagePage = 0x01;
rid[0].usUsage = 0x02;
rid[0].dwFlags = 0;
if (p_target_window != INVALID_WINDOW_ID && windows.has(p_target_window)) {
// Follow the defined window
rid[0].hwndTarget = windows[p_target_window].hWnd;
} else {
// Follow the keyboard focus
rid[0].hwndTarget = 0;
}
if (RegisterRawInputDevices(rid, 1, sizeof(rid[0])) == FALSE) {
// Registration failed.
use_raw_input = false;
}
}
bool DisplayServerWindows::tts_is_speaking() const { bool DisplayServerWindows::tts_is_speaking() const {
ERR_FAIL_COND_V(!tts, false); ERR_FAIL_COND_V(!tts, false);
return tts->is_speaking(); return tts->is_speaking();
@ -194,7 +232,9 @@ DisplayServer::MouseMode DisplayServerWindows::mouse_get_mode() const {
void DisplayServerWindows::warp_mouse(const Point2i &p_position) { void DisplayServerWindows::warp_mouse(const Point2i &p_position) {
_THREAD_SAFE_METHOD_ _THREAD_SAFE_METHOD_
if (!windows.has(last_focused_window)) { WindowID window_id = _get_focused_window_or_popup();
if (!windows.has(window_id)) {
return; // No focused window? return; // No focused window?
} }
@ -205,7 +245,7 @@ void DisplayServerWindows::warp_mouse(const Point2i &p_position) {
POINT p; POINT p;
p.x = p_position.x; p.x = p_position.x;
p.y = p_position.y; p.y = p_position.y;
ClientToScreen(windows[last_focused_window].hWnd, &p); ClientToScreen(windows[window_id].hWnd, &p);
SetCursorPos(p.x, p.y); SetCursorPos(p.x, p.y);
} }
@ -2527,7 +2567,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
old_y = coords.y; old_y = coords.y;
} }
if (windows[window_id].window_has_focus && mm->get_relative() != Vector2()) { if ((windows[window_id].window_has_focus || windows[window_id].is_popup) && mm->get_relative() != Vector2()) {
Input::get_singleton()->parse_input_event(mm); Input::get_singleton()->parse_input_event(mm);
} }
} }
@ -3799,19 +3839,7 @@ DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, Win
return; return;
} }
use_raw_input = true; _register_raw_input_devices(INVALID_WINDOW_ID);
RAWINPUTDEVICE Rid[1];
Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x02;
Rid[0].dwFlags = 0;
Rid[0].hwndTarget = 0;
if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE) {
// Registration failed.
use_raw_input = false;
}
#if defined(VULKAN_ENABLED) #if defined(VULKAN_ENABLED)
if (rendering_driver == "vulkan") { if (rendering_driver == "vulkan") {

View File

@ -466,6 +466,8 @@ class DisplayServerWindows : public DisplayServer {
void _update_real_mouse_position(WindowID p_window); void _update_real_mouse_position(WindowID p_window);
void _set_mouse_mode_impl(MouseMode p_mode); void _set_mouse_mode_impl(MouseMode p_mode);
WindowID _get_focused_window_or_popup() const;
void _register_raw_input_devices(WindowID p_target_window);
void _process_activate_event(WindowID p_window_id, WPARAM wParam, LPARAM lParam); void _process_activate_event(WindowID p_window_id, WPARAM wParam, LPARAM lParam);
void _process_key_events(); void _process_key_events();

View File

@ -568,7 +568,7 @@ public:
bool is_input_disabled() const; bool is_input_disabled() const;
Vector2 get_mouse_position() const; Vector2 get_mouse_position() const;
virtual void warp_mouse(const Vector2 &p_position); void warp_mouse(const Vector2 &p_position);
void set_physics_object_picking(bool p_enable); void set_physics_object_picking(bool p_enable);
bool get_physics_object_picking(); bool get_physics_object_picking();

View File

@ -986,18 +986,6 @@ DisplayServer::WindowID Window::get_window_id() const {
return window_id; return window_id;
} }
void Window::warp_mouse(const Vector2 &p_position) {
Transform2D xform = get_screen_transform();
Vector2 gpos = xform.xform(p_position);
if (transient_parent && !transient_parent->is_embedding_subwindows()) {
Transform2D window_trans = Transform2D().translated(get_position() + (transient_parent->get_visible_rect().size - transient_parent->get_real_size()));
gpos = window_trans.xform(gpos);
}
Input::get_singleton()->warp_mouse(gpos);
}
void Window::set_wrap_controls(bool p_enable) { void Window::set_wrap_controls(bool p_enable) {
wrap_controls = p_enable; wrap_controls = p_enable;
if (wrap_controls) { if (wrap_controls) {

View File

@ -254,8 +254,6 @@ public:
void set_use_font_oversampling(bool p_oversampling); void set_use_font_oversampling(bool p_oversampling);
bool is_using_font_oversampling() const; bool is_using_font_oversampling() const;
void warp_mouse(const Vector2 &p_position) override;
void set_wrap_controls(bool p_enable); void set_wrap_controls(bool p_enable);
bool is_wrapping_controls() const; bool is_wrapping_controls() const;
void child_controls_changed(); void child_controls_changed();