Merge pull request #92424 from bruvzg/mouse_state

Use current mouse button state instead of saved values.
This commit is contained in:
Rémi Verschelde 2024-06-03 10:35:46 +02:00
commit e716ea673e
No known key found for this signature in database
GPG key ID: C3336907360768E1
8 changed files with 96 additions and 52 deletions

View file

@ -3023,6 +3023,10 @@ void Node3DEditorViewport::_notification(int p_what) {
update_preview_node = false; update_preview_node = false;
} break; } break;
case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
set_freelook_active(false);
} break;
case NOTIFICATION_ENTER_TREE: { case NOTIFICATION_ENTER_TREE: {
surface->connect(SceneStringName(draw), callable_mp(this, &Node3DEditorViewport::_draw)); surface->connect(SceneStringName(draw), callable_mp(this, &Node3DEditorViewport::_draw));
surface->connect(SceneStringName(gui_input), callable_mp(this, &Node3DEditorViewport::_sinput)); surface->connect(SceneStringName(gui_input), callable_mp(this, &Node3DEditorViewport::_sinput));

View file

@ -502,7 +502,34 @@ Point2i DisplayServerX11::mouse_get_position() const {
} }
BitField<MouseButtonMask> DisplayServerX11::mouse_get_button_state() const { BitField<MouseButtonMask> DisplayServerX11::mouse_get_button_state() const {
return last_button_state; int number_of_screens = XScreenCount(x11_display);
for (int i = 0; i < number_of_screens; i++) {
Window root, child;
int root_x, root_y, win_x, win_y;
unsigned int mask;
if (XQueryPointer(x11_display, XRootWindow(x11_display, i), &root, &child, &root_x, &root_y, &win_x, &win_y, &mask)) {
BitField<MouseButtonMask> last_button_state = 0;
if (mask & Button1Mask) {
last_button_state.set_flag(MouseButtonMask::LEFT);
}
if (mask & Button2Mask) {
last_button_state.set_flag(MouseButtonMask::MIDDLE);
}
if (mask & Button3Mask) {
last_button_state.set_flag(MouseButtonMask::RIGHT);
}
if (mask & Button4Mask) {
last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1);
}
if (mask & Button5Mask) {
last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2);
}
return last_button_state;
}
}
return 0;
} }
void DisplayServerX11::clipboard_set(const String &p_text) { void DisplayServerX11::clipboard_set(const String &p_text) {
@ -3351,18 +3378,6 @@ void DisplayServerX11::_get_key_modifier_state(unsigned int p_x11_state, Ref<Inp
state->set_meta_pressed((p_x11_state & Mod4Mask)); state->set_meta_pressed((p_x11_state & Mod4Mask));
} }
BitField<MouseButtonMask> DisplayServerX11::_get_mouse_button_state(MouseButton p_x11_button, int p_x11_type) {
MouseButtonMask mask = mouse_button_to_mask(p_x11_button);
if (p_x11_type == ButtonPress) {
last_button_state.set_flag(mask);
} else {
last_button_state.clear_flag(mask);
}
return last_button_state;
}
void DisplayServerX11::_handle_key_event(WindowID p_window, XKeyEvent *p_event, LocalVector<XEvent> &p_events, uint32_t &p_event_index, bool p_echo) { void DisplayServerX11::_handle_key_event(WindowID p_window, XKeyEvent *p_event, LocalVector<XEvent> &p_events, uint32_t &p_event_index, bool p_echo) {
WindowData &wd = windows[p_window]; WindowData &wd = windows[p_window];
// X11 functions don't know what const is // X11 functions don't know what const is
@ -4750,12 +4765,20 @@ void DisplayServerX11::process_events() {
} else if (mb->get_button_index() == MouseButton::MIDDLE) { } else if (mb->get_button_index() == MouseButton::MIDDLE) {
mb->set_button_index(MouseButton::RIGHT); mb->set_button_index(MouseButton::RIGHT);
} }
mb->set_button_mask(_get_mouse_button_state(mb->get_button_index(), event.xbutton.type));
mb->set_position(Vector2(event.xbutton.x, event.xbutton.y)); mb->set_position(Vector2(event.xbutton.x, event.xbutton.y));
mb->set_global_position(mb->get_position()); mb->set_global_position(mb->get_position());
mb->set_pressed((event.type == ButtonPress)); mb->set_pressed((event.type == ButtonPress));
if (mb->is_pressed() && mb->get_button_index() >= MouseButton::WHEEL_UP && mb->get_button_index() <= MouseButton::WHEEL_RIGHT) {
MouseButtonMask mask = mouse_button_to_mask(mb->get_button_index());
BitField<MouseButtonMask> scroll_mask = mouse_get_button_state();
scroll_mask.set_flag(mask);
mb->set_button_mask(scroll_mask);
} else {
mb->set_button_mask(mouse_get_button_state());
}
const WindowData &wd = windows[window_id]; const WindowData &wd = windows[window_id];
if (event.type == ButtonPress) { if (event.type == ButtonPress) {

View file

@ -263,7 +263,6 @@ class DisplayServerX11 : public DisplayServer {
Point2i last_click_pos = Point2i(-100, -100); Point2i last_click_pos = Point2i(-100, -100);
uint64_t last_click_ms = 0; uint64_t last_click_ms = 0;
MouseButton last_click_button_index = MouseButton::NONE; MouseButton last_click_button_index = MouseButton::NONE;
BitField<MouseButtonMask> last_button_state;
bool app_focused = false; bool app_focused = false;
uint64_t time_since_no_focus = 0; uint64_t time_since_no_focus = 0;
@ -292,7 +291,6 @@ class DisplayServerX11 : public DisplayServer {
Rect2i _screen_get_rect(int p_screen) const; Rect2i _screen_get_rect(int p_screen) const;
BitField<MouseButtonMask> _get_mouse_button_state(MouseButton p_x11_button, int p_x11_type);
void _get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state); void _get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state);
void _flush_mouse_motion(); void _flush_mouse_motion();

View file

@ -164,7 +164,6 @@ private:
CGEventSourceRef event_source; CGEventSourceRef event_source;
MouseMode mouse_mode = MOUSE_MODE_VISIBLE; MouseMode mouse_mode = MOUSE_MODE_VISIBLE;
BitField<MouseButtonMask> last_button_state;
bool drop_events = false; bool drop_events = false;
bool in_dispatch_input_event = false; bool in_dispatch_input_event = false;
@ -302,7 +301,6 @@ public:
bool update_mouse_wrap(WindowData &p_wd, NSPoint &r_delta, NSPoint &r_mpos, NSTimeInterval p_timestamp); bool update_mouse_wrap(WindowData &p_wd, NSPoint &r_delta, NSPoint &r_mpos, NSTimeInterval p_timestamp);
virtual void warp_mouse(const Point2i &p_position) override; virtual void warp_mouse(const Point2i &p_position) override;
virtual Point2i mouse_get_position() const override; virtual Point2i mouse_get_position() const override;
void mouse_set_button_state(BitField<MouseButtonMask> p_state);
virtual BitField<MouseButtonMask> mouse_get_button_state() const override; virtual BitField<MouseButtonMask> mouse_get_button_state() const override;
virtual void clipboard_set(const String &p_text) override; virtual void clipboard_set(const String &p_text) override;

View file

@ -1421,11 +1421,25 @@ Point2i DisplayServerMacOS::mouse_get_position() const {
return Vector2i(); return Vector2i();
} }
void DisplayServerMacOS::mouse_set_button_state(BitField<MouseButtonMask> p_state) {
last_button_state = p_state;
}
BitField<MouseButtonMask> DisplayServerMacOS::mouse_get_button_state() const { BitField<MouseButtonMask> DisplayServerMacOS::mouse_get_button_state() const {
BitField<MouseButtonMask> last_button_state = 0;
NSUInteger buttons = [NSEvent pressedMouseButtons];
if (buttons & (1 << 0)) {
last_button_state.set_flag(MouseButtonMask::LEFT);
}
if (buttons & (1 << 1)) {
last_button_state.set_flag(MouseButtonMask::RIGHT);
}
if (buttons & (1 << 2)) {
last_button_state.set_flag(MouseButtonMask::MIDDLE);
}
if (buttons & (1 << 3)) {
last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1);
}
if (buttons & (1 << 4)) {
last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2);
}
return last_button_state; return last_button_state;
} }

View file

@ -370,16 +370,6 @@
} }
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
BitField<MouseButtonMask> last_button_state = ds->mouse_get_button_state();
MouseButtonMask mask = mouse_button_to_mask(index);
if (pressed) {
last_button_state.set_flag(mask);
} else {
last_button_state.clear_flag(mask);
}
ds->mouse_set_button_state(last_button_state);
Ref<InputEventMouseButton> mb; Ref<InputEventMouseButton> mb;
mb.instantiate(); mb.instantiate();
@ -394,7 +384,7 @@
mb->set_pressed(pressed); mb->set_pressed(pressed);
mb->set_position(wd.mouse_pos); mb->set_position(wd.mouse_pos);
mb->set_global_position(wd.mouse_pos); mb->set_global_position(wd.mouse_pos);
mb->set_button_mask(last_button_state); mb->set_button_mask(ds->mouse_get_button_state());
if (!outofstream && index == MouseButton::LEFT && pressed) { if (!outofstream && index == MouseButton::LEFT && pressed) {
mb->set_double_click([event clickCount] == 2); mb->set_double_click([event clickCount] == 2);
} }
@ -745,7 +735,6 @@
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
MouseButtonMask mask = mouse_button_to_mask(button); MouseButtonMask mask = mouse_button_to_mask(button);
BitField<MouseButtonMask> last_button_state = ds->mouse_get_button_state();
Ref<InputEventMouseButton> sc; Ref<InputEventMouseButton> sc;
sc.instantiate(); sc.instantiate();
@ -757,9 +746,9 @@
sc->set_pressed(true); sc->set_pressed(true);
sc->set_position(wd.mouse_pos); sc->set_position(wd.mouse_pos);
sc->set_global_position(wd.mouse_pos); sc->set_global_position(wd.mouse_pos);
last_button_state.set_flag(mask); BitField<MouseButtonMask> scroll_mask = ds->mouse_get_button_state();
sc->set_button_mask(last_button_state); scroll_mask.set_flag(mask);
ds->mouse_set_button_state(last_button_state); sc->set_button_mask(scroll_mask);
Input::get_singleton()->parse_input_event(sc); Input::get_singleton()->parse_input_event(sc);
@ -770,9 +759,8 @@
sc->set_pressed(false); sc->set_pressed(false);
sc->set_position(wd.mouse_pos); sc->set_position(wd.mouse_pos);
sc->set_global_position(wd.mouse_pos); sc->set_global_position(wd.mouse_pos);
last_button_state.clear_flag(mask); scroll_mask.clear_flag(mask);
sc->set_button_mask(last_button_state); sc->set_button_mask(scroll_mask);
ds->mouse_set_button_state(last_button_state);
Input::get_singleton()->parse_input_event(sc); Input::get_singleton()->parse_input_event(sc);
} }

View file

@ -660,6 +660,24 @@ Point2i DisplayServerWindows::mouse_get_position() const {
} }
BitField<MouseButtonMask> DisplayServerWindows::mouse_get_button_state() const { BitField<MouseButtonMask> DisplayServerWindows::mouse_get_button_state() const {
BitField<MouseButtonMask> last_button_state = 0;
if (GetAsyncKeyState(VK_LBUTTON) & (1 << 15)) {
last_button_state.set_flag(MouseButtonMask::LEFT);
}
if (GetAsyncKeyState(VK_RBUTTON) & (1 << 15)) {
last_button_state.set_flag(MouseButtonMask::RIGHT);
}
if (GetAsyncKeyState(VK_MBUTTON) & (1 << 15)) {
last_button_state.set_flag(MouseButtonMask::MIDDLE);
}
if (GetAsyncKeyState(VK_XBUTTON1) & (1 << 15)) {
last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1);
}
if (GetAsyncKeyState(VK_XBUTTON2) & (1 << 15)) {
last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2);
}
return last_button_state; return last_button_state;
} }
@ -3999,7 +4017,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
mm->set_pressure((raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN) ? 1.0f : 0.0f); mm->set_pressure((raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN) ? 1.0f : 0.0f);
mm->set_button_mask(last_button_state); mm->set_button_mask(mouse_get_button_state());
Point2i c(windows[window_id].width / 2, windows[window_id].height / 2); Point2i c(windows[window_id].width / 2, windows[window_id].height / 2);
@ -4103,7 +4121,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
mm->set_tilt(windows[window_id].last_tilt); mm->set_tilt(windows[window_id].last_tilt);
mm->set_pen_inverted(windows[window_id].last_pen_inverted); mm->set_pen_inverted(windows[window_id].last_pen_inverted);
mm->set_button_mask(last_button_state); mm->set_button_mask(mouse_get_button_state());
mm->set_position(Vector2(coords.x, coords.y)); mm->set_position(Vector2(coords.x, coords.y));
mm->set_global_position(Vector2(coords.x, coords.y)); mm->set_global_position(Vector2(coords.x, coords.y));
@ -4248,7 +4266,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
mm->set_alt_pressed(mods.has_flag(WinKeyModifierMask::ALT)); mm->set_alt_pressed(mods.has_flag(WinKeyModifierMask::ALT));
mm->set_meta_pressed(mods.has_flag(WinKeyModifierMask::META)); mm->set_meta_pressed(mods.has_flag(WinKeyModifierMask::META));
mm->set_button_mask(last_button_state); mm->set_button_mask(mouse_get_button_state());
POINT coords; // Client coords. POINT coords; // Client coords.
coords.x = GET_X_LPARAM(lParam); coords.x = GET_X_LPARAM(lParam);
@ -4378,7 +4396,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
mm->set_tilt(windows[window_id].last_tilt); mm->set_tilt(windows[window_id].last_tilt);
mm->set_pen_inverted(windows[window_id].last_pen_inverted); mm->set_pen_inverted(windows[window_id].last_pen_inverted);
mm->set_button_mask(last_button_state); mm->set_button_mask(mouse_get_button_state());
mm->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); mm->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
mm->set_global_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); mm->set_global_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
@ -4552,12 +4570,14 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
mb->set_alt_pressed(mods.has_flag(WinKeyModifierMask::ALT)); mb->set_alt_pressed(mods.has_flag(WinKeyModifierMask::ALT));
mb->set_meta_pressed(mods.has_flag(WinKeyModifierMask::META)); mb->set_meta_pressed(mods.has_flag(WinKeyModifierMask::META));
if (mb->is_pressed()) { if (mb->is_pressed() && mb->get_button_index() >= MouseButton::WHEEL_UP && mb->get_button_index() <= MouseButton::WHEEL_RIGHT) {
last_button_state.set_flag(mouse_button_to_mask(mb->get_button_index())); MouseButtonMask mask = mouse_button_to_mask(mb->get_button_index());
BitField<MouseButtonMask> scroll_mask = mouse_get_button_state();
scroll_mask.set_flag(mask);
mb->set_button_mask(scroll_mask);
} else { } else {
last_button_state.clear_flag(mouse_button_to_mask(mb->get_button_index())); mb->set_button_mask(mouse_get_button_state());
} }
mb->set_button_mask(last_button_state);
mb->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))); mb->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
@ -4571,7 +4591,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
SetCapture(hWnd); SetCapture(hWnd);
} }
} else { } else {
if (--pressrc <= 0 || last_button_state.is_empty()) { if (--pressrc <= 0 || mouse_get_button_state().is_empty()) {
if (mouse_mode != MOUSE_MODE_CAPTURED) { if (mouse_mode != MOUSE_MODE_CAPTURED) {
ReleaseCapture(); ReleaseCapture();
} }
@ -4596,8 +4616,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
// Send release for mouse wheel. // Send release for mouse wheel.
Ref<InputEventMouseButton> mbd = mb->duplicate(); Ref<InputEventMouseButton> mbd = mb->duplicate();
mbd->set_window_id(window_id); mbd->set_window_id(window_id);
last_button_state.clear_flag(mouse_button_to_mask(mbd->get_button_index())); mbd->set_button_mask(mouse_get_button_state());
mbd->set_button_mask(last_button_state);
mbd->set_pressed(false); mbd->set_pressed(false);
Input::get_singleton()->parse_input_event(mbd); Input::get_singleton()->parse_input_event(mbd);
} }

View file

@ -478,7 +478,7 @@ class DisplayServerWindows : public DisplayServer {
MouseMode mouse_mode; MouseMode mouse_mode;
int restore_mouse_trails = 0; int restore_mouse_trails = 0;
BitField<MouseButtonMask> last_button_state;
bool use_raw_input = false; bool use_raw_input = false;
bool drop_events = false; bool drop_events = false;
bool in_dispatch_input_event = false; bool in_dispatch_input_event = false;