Add iOS Apple Pencil pressure

This commit is contained in:
Vaughan Ling 2021-03-30 09:42:28 -07:00
parent b38a36923a
commit f60b90d92e
4 changed files with 53 additions and 4 deletions

View File

@ -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);
}

View File

@ -339,9 +339,14 @@ static const int max_touches = 8;
int tid = [self getTouchIDForTouch:touch];
ERR_FAIL_COND(tid == -1);
CGPoint touchPoint = [touch locationInView:self];
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);
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
@ -353,9 +358,16 @@ static const int max_touches = 8;
ERR_FAIL_COND(tid == -1);
CGPoint touchPoint = [touch locationInView:self];
CGPoint prev_point = [touch previousLocationInView:self];
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);
}
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
@ -367,9 +379,13 @@ static const int max_touches = 8;
ERR_FAIL_COND(tid == -1);
[self removeTouch:touch];
CGPoint touchPoint = [touch locationInView:self];
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);
}
}
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
@ -379,9 +395,13 @@ static const int max_touches = 8;
UITouch *touch = [tlist objectAtIndex:i];
int tid = [self getTouchIDForTouch:touch];
ERR_FAIL_COND(tid == -1);
if (touch.type == UITouchTypeStylus) {
OSIPhone::get_singleton()->pencil_cancelled(tid);
} else {
OSIPhone::get_singleton()->touches_cancelled(tid);
}
}
}
[self clearTouches];
}

View File

@ -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);

View File

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