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

Use current mouse button state instead of saved values. #92424

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3023,6 +3023,10 @@ void Node3DEditorViewport::_notification(int p_what) {
update_preview_node = false;
} break;

case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
set_freelook_active(false);
} break;

case NOTIFICATION_ENTER_TREE: {
surface->connect(SceneStringName(draw), callable_mp(this, &Node3DEditorViewport::_draw));
surface->connect(SceneStringName(gui_input), callable_mp(this, &Node3DEditorViewport::_sinput));
Expand Down
51 changes: 37 additions & 14 deletions platform/linuxbsd/x11/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,34 @@ Point2i DisplayServerX11::mouse_get_position() 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) {
Expand Down Expand Up @@ -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));
}

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) {
WindowData &wd = windows[p_window];
// X11 functions don't know what const is
Expand Down Expand Up @@ -4750,12 +4765,20 @@ void DisplayServerX11::process_events() {
} else if (mb->get_button_index() == MouseButton::MIDDLE) {
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_global_position(mb->get_position());

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];

if (event.type == ButtonPress) {
Expand Down
2 changes: 0 additions & 2 deletions platform/linuxbsd/x11/display_server_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ class DisplayServerX11 : public DisplayServer {
Point2i last_click_pos = Point2i(-100, -100);
uint64_t last_click_ms = 0;
MouseButton last_click_button_index = MouseButton::NONE;
BitField<MouseButtonMask> last_button_state;
bool app_focused = false;
uint64_t time_since_no_focus = 0;

Expand Down Expand Up @@ -292,7 +291,6 @@ class DisplayServerX11 : public DisplayServer {

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 _flush_mouse_motion();

Expand Down
2 changes: 0 additions & 2 deletions platform/macos/display_server_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ class DisplayServerMacOS : public DisplayServer {

CGEventSourceRef event_source;
MouseMode mouse_mode = MOUSE_MODE_VISIBLE;
BitField<MouseButtonMask> last_button_state;

bool drop_events = false;
bool in_dispatch_input_event = false;
Expand Down Expand Up @@ -302,7 +301,6 @@ class DisplayServerMacOS : public DisplayServer {
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 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 void clipboard_set(const String &p_text) override;
Expand Down
22 changes: 18 additions & 4 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1421,11 +1421,25 @@
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> 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;
}

Expand Down
24 changes: 6 additions & 18 deletions platform/macos/godot_content_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,6 @@ - (void)processMouseEvent:(NSEvent *)event index:(MouseButton)index pressed:(boo
}

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;
mb.instantiate();
Expand All @@ -394,7 +384,7 @@ - (void)processMouseEvent:(NSEvent *)event index:(MouseButton)index pressed:(boo
mb->set_pressed(pressed);
mb->set_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) {
mb->set_double_click([event clickCount] == 2);
}
Expand Down Expand Up @@ -745,7 +735,6 @@ - (void)processScrollEvent:(NSEvent *)event button:(MouseButton)button factor:(d

DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
MouseButtonMask mask = mouse_button_to_mask(button);
BitField<MouseButtonMask> last_button_state = ds->mouse_get_button_state();

Ref<InputEventMouseButton> sc;
sc.instantiate();
Expand All @@ -757,9 +746,9 @@ - (void)processScrollEvent:(NSEvent *)event button:(MouseButton)button factor:(d
sc->set_pressed(true);
sc->set_position(wd.mouse_pos);
sc->set_global_position(wd.mouse_pos);
last_button_state.set_flag(mask);
sc->set_button_mask(last_button_state);
ds->mouse_set_button_state(last_button_state);
BitField<MouseButtonMask> scroll_mask = ds->mouse_get_button_state();
scroll_mask.set_flag(mask);
sc->set_button_mask(scroll_mask);

Input::get_singleton()->parse_input_event(sc);

Expand All @@ -770,9 +759,8 @@ - (void)processScrollEvent:(NSEvent *)event button:(MouseButton)button factor:(d
sc->set_pressed(false);
sc->set_position(wd.mouse_pos);
sc->set_global_position(wd.mouse_pos);
last_button_state.clear_flag(mask);
sc->set_button_mask(last_button_state);
ds->mouse_set_button_state(last_button_state);
scroll_mask.clear_flag(mask);
sc->set_button_mask(scroll_mask);

Input::get_singleton()->parse_input_event(sc);
}
Expand Down
41 changes: 30 additions & 11 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,24 @@ Point2i DisplayServerWindows::mouse_get_position() 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;
}

Expand Down Expand Up @@ -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_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);

Expand Down Expand Up @@ -4103,7 +4121,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
mm->set_tilt(windows[window_id].last_tilt);
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_global_position(Vector2(coords.x, coords.y));
Expand Down Expand Up @@ -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_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.
coords.x = GET_X_LPARAM(lParam);
Expand Down Expand Up @@ -4378,7 +4396,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
mm->set_tilt(windows[window_id].last_tilt);
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_global_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
Expand Down Expand Up @@ -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_meta_pressed(mods.has_flag(WinKeyModifierMask::META));

if (mb->is_pressed()) {
last_button_state.set_flag(mouse_button_to_mask(mb->get_button_index()));
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 {
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)));

Expand All @@ -4571,7 +4591,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
SetCapture(hWnd);
}
} else {
if (--pressrc <= 0 || last_button_state.is_empty()) {
if (--pressrc <= 0 || mouse_get_button_state().is_empty()) {
if (mouse_mode != MOUSE_MODE_CAPTURED) {
ReleaseCapture();
}
Expand All @@ -4596,8 +4616,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA
// Send release for mouse wheel.
Ref<InputEventMouseButton> mbd = mb->duplicate();
mbd->set_window_id(window_id);
last_button_state.clear_flag(mouse_button_to_mask(mbd->get_button_index()));
mbd->set_button_mask(last_button_state);
mbd->set_button_mask(mouse_get_button_state());
mbd->set_pressed(false);
Input::get_singleton()->parse_input_event(mbd);
}
Expand Down
2 changes: 1 addition & 1 deletion platform/windows/display_server_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ class DisplayServerWindows : public DisplayServer {

MouseMode mouse_mode;
int restore_mouse_trails = 0;
BitField<MouseButtonMask> last_button_state;

bool use_raw_input = false;
bool drop_events = false;
bool in_dispatch_input_event = false;
Expand Down
Loading