Skip to content

Commit

Permalink
Add iOS Apple Pencil pressure
Browse files Browse the repository at this point in the history
  • Loading branch information
HEAVYPOLY committed Apr 3, 2021
1 parent b38a369 commit f60b90d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions main/input_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
28 changes: 24 additions & 4 deletions platform/iphone/godot_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,12 @@ - (void)touchesBegan:(NSSet *)touchesSet withEvent:(UIEvent *)event {
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);
}
}
}
}
Expand All @@ -353,7 +358,14 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
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);
}
}
}
}
Expand All @@ -367,7 +379,11 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
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);
}
}
}
}
Expand All @@ -379,7 +395,11 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
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];
Expand Down
3 changes: 3 additions & 0 deletions platform/iphone/os_iphone.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ class OSIPhone : public OS_Unix {

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

Expand Down
25 changes: 25 additions & 0 deletions platform/iphone/os_iphone.mm
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,31 @@
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;
Expand Down

0 comments on commit f60b90d

Please sign in to comment.