Skip to content

Commit

Permalink
android tilt and pressure
Browse files Browse the repository at this point in the history
  • Loading branch information
HEAVYPOLY committed Aug 29, 2024
1 parent 01c78d8 commit ba4fec5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
6 changes: 5 additions & 1 deletion platform/android/android_input_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void AndroidInputHandler::_release_mouse_event_info(bool p_source_mouse_relative
mouse_event_info.valid = false;
}

void AndroidInputHandler::process_mouse_event(int p_event_action, int p_event_android_buttons_mask, Point2 p_event_pos, Vector2 p_delta, bool p_double_click, bool p_source_mouse_relative) {
void AndroidInputHandler::process_mouse_event(int p_event_action, int p_event_android_buttons_mask, Point2 p_event_pos, Vector2 p_delta, float p_pressure, Vector2 p_tilt, bool p_double_click, bool p_source_mouse_relative) {
int event_buttons_mask = _android_button_mask_to_godot_button_mask(p_event_android_buttons_mask);
switch (p_event_action) {
case AMOTION_EVENT_ACTION_HOVER_MOVE: // hover move
Expand All @@ -267,6 +267,8 @@ void AndroidInputHandler::process_mouse_event(int p_event_action, int p_event_an
ev->set_position(p_event_pos);
ev->set_global_position(p_event_pos);
ev->set_relative(p_event_pos - hover_prev_pos);
ev->set_pressure(p_pressure);
ev->set_tilt(p_tilt);
input->parse_input_event(ev);
hover_prev_pos = p_event_pos;
} break;
Expand Down Expand Up @@ -311,6 +313,8 @@ void AndroidInputHandler::process_mouse_event(int p_event_action, int p_event_an
hover_prev_pos = p_event_pos;
}
ev->set_button_mask(event_buttons_mask);
ev->set_pressure(p_pressure);
ev->set_tilt(p_tilt);
input->parse_input_event(ev);
} break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ public class GodotLib {
* Forward touch events.
*/
public static native void dispatchTouchEvent(int event, int pointer, int pointerCount, float[] positions, boolean doubleTap);

/**
* Dispatch mouse events
*/
public static native void dispatchMouseEvent(int event, int buttonMask, float x, float y, float deltaX, float deltaY, boolean doubleClick, boolean sourceMouseRelative);
public static native void dispatchMouseEvent(int event, int buttonMask, float x, float y, float deltaX, float deltaY, float pressure, float tiltX, float tiltY, boolean doubleClick, boolean sourceMouseRelative);

public static native void magnify(float x, float y, float factor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,27 +485,28 @@ static boolean handleMouseEvent(final MotionEvent event) {
final float y = event.getY();
final int buttonsMask = event.getButtonState();

float verticalFactor = 0;
float horizontalFactor = 0;

// If event came from RotaryEncoder (Bezel or Crown rotate event on Wear OS smart watches),
// convert it to mouse wheel event.
if (event.isFromSource(InputDevice.SOURCE_ROTARY_ENCODER)) {
if (rotaryInputAxis == ROTARY_INPUT_HORIZONTAL_AXIS) {
horizontalFactor = -event.getAxisValue(MotionEvent.AXIS_SCROLL);
} else {
// If rotaryInputAxis is not ROTARY_INPUT_HORIZONTAL_AXIS then use default ROTARY_INPUT_VERTICAL_AXIS axis.
verticalFactor = -event.getAxisValue(MotionEvent.AXIS_SCROLL);
}
} else {
verticalFactor = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
horizontalFactor = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
float verticalFactor = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
float horizontalFactor = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
float pressure = event.getPressure();
float tiltX = event.getAxisValue(MotionEvent.AXIS_TILT_X);
float tiltY = event.getAxisValue(MotionEvent.AXIS_TILT_Y);

// Check if the event has tilt data
if (Float.isNaN(tiltX) || Float.isNaN(tiltY)) {
tiltX = 0; // Default value if tilt data is not available
tiltY = 0; // Default value if tilt data is not available
}

// Check if the event has pressure data
if (Float.isNaN(pressure)) {
pressure = 1; // Default value if pressure data is not available
}

boolean sourceMouseRelative = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
sourceMouseRelative = event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE);
}
return handleMouseEvent(eventAction, buttonsMask, x, y, horizontalFactor, verticalFactor, false, sourceMouseRelative);
return handleMouseEvent(eventAction, buttonsMask, x, y, horizontalFactor, verticalFactor, pressure, tiltX, tiltY, false, sourceMouseRelative);
}

static boolean handleMouseEvent(int eventAction, int buttonsMask, float x, float y) {
Expand Down Expand Up @@ -551,6 +552,11 @@ static boolean handleMouseEvent(int eventAction, int buttonsMask, float x, float
return false;
}

static boolean handleMouseEvent(int eventAction, int buttonsMask, float x, float y, float deltaX, float deltaY, float pressure, float tiltX, float tiltY, boolean doubleClick, boolean sourceMouseRelative) {
GodotLib.dispatchMouseEvent(eventAction, buttonsMask, x, y, deltaX, deltaY, pressure, tiltX, tiltY, doubleClick, sourceMouseRelative);
return true;
}

static boolean handleTouchEvent(final MotionEvent event) {
final int pointerCount = event.getPointerCount();
if (pointerCount == 0) {
Expand Down

0 comments on commit ba4fec5

Please sign in to comment.