From f60b90d92ec224f96a1cafff5946ee2e842e310d Mon Sep 17 00:00:00 2001 From: Vaughan Ling Date: Tue, 30 Mar 2021 09:42:28 -0700 Subject: [PATCH] Add iOS Apple Pencil pressure --- main/input_default.cpp | 1 + platform/iphone/godot_view.mm | 28 ++++++++++++++++++++++++---- platform/iphone/os_iphone.h | 3 +++ platform/iphone/os_iphone.mm | 25 +++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/main/input_default.cpp b/main/input_default.cpp index f62f17b80c6..1ca6462da68 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -403,6 +403,7 @@ void InputDefault::_parse_input_event_impl(const Ref &p_event, bool motion_event->set_relative(sd->get_relative()); motion_event->set_speed(sd->get_speed()); motion_event->set_button_mask(mouse_button_mask); + motion_event->set_pressure(1.f); _parse_input_event_impl(motion_event, true); } diff --git a/platform/iphone/godot_view.mm b/platform/iphone/godot_view.mm index 893a444bca6..b8d48b2d654 100644 --- a/platform/iphone/godot_view.mm +++ b/platform/iphone/godot_view.mm @@ -339,7 +339,12 @@ static const int max_touches = 8; int tid = [self getTouchIDForTouch:touch]; ERR_FAIL_COND(tid == -1); CGPoint touchPoint = [touch locationInView:self]; - OSIPhone::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, true, touch.tapCount > 1); + + if (touch.type == UITouchTypeStylus) { + OSIPhone::get_singleton()->pencil_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, true, touch.tapCount > 1); + } else { + OSIPhone::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, true, touch.tapCount > 1); + } } } } @@ -353,7 +358,14 @@ static const int max_touches = 8; ERR_FAIL_COND(tid == -1); CGPoint touchPoint = [touch locationInView:self]; CGPoint prev_point = [touch previousLocationInView:self]; - OSIPhone::get_singleton()->touch_drag(tid, prev_point.x * self.contentScaleFactor, prev_point.y * self.contentScaleFactor, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor); + CGFloat force = touch.force; + // Vector2 tilt = touch.azimuthUnitVector; + + if (touch.type == UITouchTypeStylus) { + OSIPhone::get_singleton()->pencil_drag(tid, prev_point.x * self.contentScaleFactor, prev_point.y * self.contentScaleFactor, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, force); + } else { + OSIPhone::get_singleton()->touch_drag(tid, prev_point.x * self.contentScaleFactor, prev_point.y * self.contentScaleFactor, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor); + } } } } @@ -367,7 +379,11 @@ static const int max_touches = 8; ERR_FAIL_COND(tid == -1); [self removeTouch:touch]; CGPoint touchPoint = [touch locationInView:self]; - OSIPhone::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, false, false); + if (touch.type == UITouchTypeStylus) { + OSIPhone::get_singleton()->pencil_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, false, false); + } else { + OSIPhone::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, false, false); + } } } } @@ -379,7 +395,11 @@ static const int max_touches = 8; UITouch *touch = [tlist objectAtIndex:i]; int tid = [self getTouchIDForTouch:touch]; ERR_FAIL_COND(tid == -1); - OSIPhone::get_singleton()->touches_cancelled(tid); + if (touch.type == UITouchTypeStylus) { + OSIPhone::get_singleton()->pencil_cancelled(tid); + } else { + OSIPhone::get_singleton()->touches_cancelled(tid); + } } } [self clearTouches]; diff --git a/platform/iphone/os_iphone.h b/platform/iphone/os_iphone.h index 054f2ed68dc..77921f79f32 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -127,9 +127,12 @@ public: virtual int get_screen_dpi(int p_screen = -1) const; + void pencil_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_doubleclick); void touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_doubleclick); + void pencil_drag(int p_idx, int p_prev_x, int p_prev_y, int p_x, int p_y, float p_force); void touch_drag(int p_idx, int p_prev_x, int p_prev_y, int p_x, int p_y); void touches_cancelled(int p_idx); + void pencil_cancelled(int p_idx); void key(uint32_t p_key, bool p_pressed); void set_virtual_keyboard_height(int p_height); diff --git a/platform/iphone/os_iphone.mm b/platform/iphone/os_iphone.mm index 9874dbe61ed..f268a25985e 100644 --- a/platform/iphone/os_iphone.mm +++ b/platform/iphone/os_iphone.mm @@ -220,6 +220,31 @@ void OSIPhone::key(uint32_t p_key, bool p_pressed) { perform_event(ev); }; +void OSIPhone::pencil_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_doubleclick) { + Ref ev; + ev.instance(); + ev->set_button_index(1); + ev->set_pressed(p_pressed); + ev->set_position(Vector2(p_x, p_y)); + ev->set_global_position(Vector2(p_x, p_y)); + ev->set_doubleclick(p_doubleclick); + perform_event(ev); +}; + +void OSIPhone::pencil_drag(int p_idx, int p_prev_x, int p_prev_y, int p_x, int p_y, float p_force) { + Ref ev; + ev.instance(); + ev->set_pressure(p_force); + ev->set_position(Vector2(p_x, p_y)); + ev->set_global_position(Vector2(p_x, p_y)); + ev->set_relative(Vector2(p_x - p_prev_x, p_y - p_prev_y)); + perform_event(ev); +}; + +void OSIPhone::pencil_cancelled(int p_idx) { + pencil_press(p_idx, -1, -1, false, false); +} + void OSIPhone::touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_doubleclick) { if (GLOBAL_DEF("debug/disable_touch", false)) { return;