Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

[iOS] Add support for `long_press` touch events

Pāvels Nadtočajevs7a3da76e1352
diff --git a/doc/classes/InputEventScreenTouch.xml b/doc/classes/InputEventScreenTouch.xml
index 90f5d03a1af..1c019dc175a 100644
--- a/doc/classes/InputEventScreenTouch.xml
+++ b/doc/classes/InputEventScreenTouch.xml
@@ -21,7 +21,7 @@
</member>
<member name="long_press" type="bool" setter="set_long_press" getter="is_long_press" default="false">
If [code]true[/code], the touch's state is a long press.
- [b]Note:[/b] This is only supported on Android.
+ [b]Note:[/b] This is only supported on Android and iOS.
</member>
<member name="position" type="Vector2" setter="set_position" getter="get_position" default="Vector2(0, 0)">
The touch position in the viewport the node is in, using the coordinate system of this viewport.
diff --git a/drivers/apple_embedded/display_server_apple_embedded.h b/drivers/apple_embedded/display_server_apple_embedded.h
index 925d9da66cd..daebb989cd4 100644
--- a/drivers/apple_embedded/display_server_apple_embedded.h
+++ b/drivers/apple_embedded/display_server_apple_embedded.h
@@ -131,7 +131,7 @@ public:
// MARK: Touches and Apple Pencil
- void touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_double_click);
+ void touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_double_click, bool p_long_press);
void touch_drag(int p_idx, int p_prev_x, int p_prev_y, int p_x, int p_y, float p_pressure, Vector2 p_tilt);
void touches_canceled(int p_idx);
diff --git a/drivers/apple_embedded/display_server_apple_embedded.mm b/drivers/apple_embedded/display_server_apple_embedded.mm
index 38e45156033..7c6f0125365 100644
--- a/drivers/apple_embedded/display_server_apple_embedded.mm
+++ b/drivers/apple_embedded/display_server_apple_embedded.mm
@@ -269,7 +269,7 @@ void DisplayServerAppleEmbedded::_window_callback(const Callable &p_callable, co
// MARK: Touches
-void DisplayServerAppleEmbedded::touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_double_click) {
+void DisplayServerAppleEmbedded::touch_press(int p_idx, int p_x, int p_y, bool p_pressed, bool p_double_click, bool p_long_press) {
Ref<InputEventScreenTouch> ev;
ev.instantiate();
@@ -277,6 +277,7 @@ void DisplayServerAppleEmbedded::touch_press(int p_idx, int p_x, int p_y, bool p
ev->set_pressed(p_pressed);
ev->set_position(Vector2(p_x, p_y));
ev->set_double_tap(p_double_click);
+ ev->set_long_press(p_long_press);
perform_event(ev);
}
@@ -302,7 +303,7 @@ void DisplayServerAppleEmbedded::perform_event(const Ref<InputEvent> &p_event) {
}
void DisplayServerAppleEmbedded::touches_canceled(int p_idx) {
- touch_press(p_idx, -1, -1, false, false);
+ touch_press(p_idx, -1, -1, false, false, false);
}
// MARK: Keyboard
diff --git a/drivers/apple_embedded/godot_view_apple_embedded.mm b/drivers/apple_embedded/godot_view_apple_embedded.mm
index 89983aec0a3..c212388ea5d 100644
--- a/drivers/apple_embedded/godot_view_apple_embedded.mm
+++ b/drivers/apple_embedded/godot_view_apple_embedded.mm
@@ -45,6 +45,8 @@ static const float earth_gravity = 9.80665;
@interface GDTView () {
UITouch *godot_touches[max_touches];
CGFloat last_edr_headroom;
+ NSTimeInterval begin_timestamp[max_touches];
+ CGPoint begin_delta[max_touches];
}
@property(assign, nonatomic) BOOL isActive;
@@ -358,7 +360,9 @@ static const float earth_gravity = 9.80665;
int tid = [self getTouchIDForTouch:touch];
ERR_FAIL_COND(tid == -1);
CGPoint touchPoint = [touch locationInView:self];
- DisplayServerAppleEmbedded::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, true, touch.tapCount > 1);
+ begin_timestamp[tid] = [touch timestamp];
+ begin_delta[tid] = CGPointMake(0, 0);
+ DisplayServerAppleEmbedded::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, true, touch.tapCount > 1, false);
}
}
@@ -368,6 +372,17 @@ static const float earth_gravity = 9.80665;
ERR_FAIL_COND(tid == -1);
CGPoint touchPoint = [touch locationInView:self];
CGPoint prev_point = [touch previousLocationInView:self];
+ if (begin_timestamp[tid] != 0.0) {
+ begin_delta[tid].x = fmax(begin_delta[tid].x, Math::abs(touchPoint.x - prev_point.x));
+ begin_delta[tid].y = fmax(begin_delta[tid].y, Math::abs(touchPoint.y - prev_point.y));
+ if ([touch timestamp] - begin_timestamp[tid] >= 0.5) {
+ begin_timestamp[tid] = 0.0;
+ if (begin_delta[tid].x <= 10.0 && begin_delta[tid].y <= 10.0) {
+ DisplayServerAppleEmbedded::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, true, touch.tapCount > 1, true);
+ return;
+ }
+ }
+ }
CGFloat alt = [touch altitudeAngle];
CGVector azim = [touch azimuthUnitVectorInView:self];
DisplayServerAppleEmbedded::get_singleton()->touch_drag(tid, prev_point.x * self.contentScaleFactor, prev_point.y * self.contentScaleFactor, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, [touch force] / [touch maximumPossibleForce], Vector2(azim.dx, azim.dy) * Math::cos(alt));
@@ -380,7 +395,7 @@ static const float earth_gravity = 9.80665;
ERR_FAIL_COND(tid == -1);
[self removeTouch:touch];
CGPoint touchPoint = [touch locationInView:self];
- DisplayServerAppleEmbedded::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, false, false);
+ DisplayServerAppleEmbedded::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, false, false, false);
}
}