Przeglądaj źródła

Merge pull request #47469 from HEAVYPOLY/ios-pen-pressure

Add iOS pen pressure
Rémi Verschelde 4 lat temu
rodzic
commit
8ec14c917f

+ 1 - 0
main/input_default.cpp

@@ -403,6 +403,7 @@ void InputDefault::_parse_input_event_impl(const Ref<InputEvent> &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);
 		}

+ 24 - 4
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];

+ 3 - 0
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);
 

+ 25 - 0
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<InputEventMouseButton> 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<InputEventMouseMotion> 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;