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

[macOS] Handle Trackpad / Magic Mouse swipe events and simulate XBUTTON1 / XBUTTON2 buttons. #87277

Merged
merged 1 commit into from
Feb 13, 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
2 changes: 1 addition & 1 deletion platform/macos/godot_content_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

- (void)processScrollEvent:(NSEvent *)event button:(MouseButton)button factor:(double)factor;
- (void)processPanEvent:(NSEvent *)event dx:(double)dx dy:(double)dy;
- (void)processMouseEvent:(NSEvent *)event index:(MouseButton)index pressed:(bool)pressed;
- (void)processMouseEvent:(NSEvent *)event index:(MouseButton)index pressed:(bool)pressed outofstream:(bool)outofstream;
- (void)setWindowID:(DisplayServer::WindowID)wid;
- (void)updateLayerDelegate;
- (void)cancelComposition;
Expand Down
49 changes: 34 additions & 15 deletions platform/macos/godot_content_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ - (void)cursorUpdate:(NSEvent *)event {
ds->cursor_update_shape();
}

- (void)processMouseEvent:(NSEvent *)event index:(MouseButton)index pressed:(bool)pressed {
- (void)processMouseEvent:(NSEvent *)event index:(MouseButton)index pressed:(bool)pressed outofstream:(bool)outofstream {
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
if (!ds || !ds->has_window(window_id)) {
return;
Expand All @@ -377,14 +377,18 @@ - (void)processMouseEvent:(NSEvent *)event index:(MouseButton)index pressed:(boo
Ref<InputEventMouseButton> mb;
mb.instantiate();
mb->set_window_id(window_id);
ds->update_mouse_pos(wd, [event locationInWindow]);
if (outofstream) {
ds->update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]);
} else {
ds->update_mouse_pos(wd, [event locationInWindow]);
}
ds->get_key_modifier_state([event modifierFlags], mb);
mb->set_button_index(index);
mb->set_pressed(pressed);
mb->set_position(wd.mouse_pos);
mb->set_global_position(wd.mouse_pos);
mb->set_button_mask(last_button_state);
if (index == MouseButton::LEFT && pressed) {
if (!outofstream && index == MouseButton::LEFT && pressed) {
mb->set_double_click([event clickCount] == 2);
}

Expand All @@ -394,10 +398,10 @@ - (void)processMouseEvent:(NSEvent *)event index:(MouseButton)index pressed:(boo
- (void)mouseDown:(NSEvent *)event {
if (([event modifierFlags] & NSEventModifierFlagControl)) {
mouse_down_control = true;
[self processMouseEvent:event index:MouseButton::RIGHT pressed:true];
[self processMouseEvent:event index:MouseButton::RIGHT pressed:true outofstream:false];
} else {
mouse_down_control = false;
[self processMouseEvent:event index:MouseButton::LEFT pressed:true];
[self processMouseEvent:event index:MouseButton::LEFT pressed:true outofstream:false];
}
}

Expand All @@ -407,9 +411,9 @@ - (void)mouseDragged:(NSEvent *)event {

- (void)mouseUp:(NSEvent *)event {
if (mouse_down_control) {
[self processMouseEvent:event index:MouseButton::RIGHT pressed:false];
[self processMouseEvent:event index:MouseButton::RIGHT pressed:false outofstream:false];
} else {
[self processMouseEvent:event index:MouseButton::LEFT pressed:false];
[self processMouseEvent:event index:MouseButton::LEFT pressed:false outofstream:false];
}
}

Expand Down Expand Up @@ -456,24 +460,24 @@ - (void)mouseMoved:(NSEvent *)event {
}

- (void)rightMouseDown:(NSEvent *)event {
[self processMouseEvent:event index:MouseButton::RIGHT pressed:true];
[self processMouseEvent:event index:MouseButton::RIGHT pressed:true outofstream:false];
}

- (void)rightMouseDragged:(NSEvent *)event {
[self mouseMoved:event];
}

- (void)rightMouseUp:(NSEvent *)event {
[self processMouseEvent:event index:MouseButton::RIGHT pressed:false];
[self processMouseEvent:event index:MouseButton::RIGHT pressed:false outofstream:false];
}

- (void)otherMouseDown:(NSEvent *)event {
if ((int)[event buttonNumber] == 2) {
[self processMouseEvent:event index:MouseButton::MIDDLE pressed:true];
[self processMouseEvent:event index:MouseButton::MIDDLE pressed:true outofstream:false];
} else if ((int)[event buttonNumber] == 3) {
[self processMouseEvent:event index:MouseButton::MB_XBUTTON1 pressed:true];
[self processMouseEvent:event index:MouseButton::MB_XBUTTON1 pressed:true outofstream:false];
} else if ((int)[event buttonNumber] == 4) {
[self processMouseEvent:event index:MouseButton::MB_XBUTTON2 pressed:true];
[self processMouseEvent:event index:MouseButton::MB_XBUTTON2 pressed:true outofstream:false];
} else {
return;
}
Expand All @@ -485,16 +489,31 @@ - (void)otherMouseDragged:(NSEvent *)event {

- (void)otherMouseUp:(NSEvent *)event {
if ((int)[event buttonNumber] == 2) {
[self processMouseEvent:event index:MouseButton::MIDDLE pressed:false];
[self processMouseEvent:event index:MouseButton::MIDDLE pressed:false outofstream:false];
} else if ((int)[event buttonNumber] == 3) {
[self processMouseEvent:event index:MouseButton::MB_XBUTTON1 pressed:false];
[self processMouseEvent:event index:MouseButton::MB_XBUTTON1 pressed:false outofstream:false];
} else if ((int)[event buttonNumber] == 4) {
[self processMouseEvent:event index:MouseButton::MB_XBUTTON2 pressed:false];
[self processMouseEvent:event index:MouseButton::MB_XBUTTON2 pressed:false outofstream:false];
} else {
return;
}
}

- (void)swipeWithEvent:(NSEvent *)event {
// Swipe gesture on Trackpad/Magic Mouse, or physical back/forward mouse buttons.
if ([event phase] == NSEventPhaseEnded || [event phase] == NSEventPhaseChanged) {
if (Math::is_equal_approx([event deltaX], 1.0)) {
// Swipe left (back).
[self processMouseEvent:event index:MouseButton::MB_XBUTTON1 pressed:true outofstream:true];
[self processMouseEvent:event index:MouseButton::MB_XBUTTON1 pressed:false outofstream:true];
} else if (Math::is_equal_approx([event deltaX], -1.0)) {
// Swipe right (forward).
[self processMouseEvent:event index:MouseButton::MB_XBUTTON2 pressed:true outofstream:true];
[self processMouseEvent:event index:MouseButton::MB_XBUTTON2 pressed:false outofstream:true];
}
}
}

- (void)mouseExited:(NSEvent *)event {
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
if (!ds || !ds->has_window(window_id)) {
Expand Down
Loading