-
-
Notifications
You must be signed in to change notification settings - Fork 971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Android] Stylus support #3111
base: main
Are you sure you want to change the base?
[Android] Stylus support #3111
Conversation
// To get azimuth angle, we need to use orientation property. Orientation value range is [-PI, PI] (https://developer.android.com/develop/ui/compose/touch-input/stylus-input/advanced-stylus-features#orientation) | ||
// To shift range into [0, 2PI], we add 2PI if orientation is less than 0. | ||
if (orientation < 0) { | ||
orientation += 2 * PI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't that shift it to [PI, 3PI]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's possible that I missed something, but that is why there's an if expression. I'm only shifting angles from [-PI, 0]
- that should result in new range being [PI, 2PI]
. Combined with second half we get range [0, 2PI]
- a full circle.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it doesn't matter since now we use formula with modulo (mentioned in the comment below)
val azimuthAngle = if (orientation >= 3 * PI / 2) { | ||
orientation - 3 * PI / 2 | ||
} else { | ||
orientation + PI / 2 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it simply be orientation - 3PI/2 (mod 2PI)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stylusData.tiltX = tilts.first | ||
stylusData.tiltY = tilts.second | ||
stylusData.altitudeAngle = altitudeAngle | ||
stylusData.azimuthAngle = azimuthAngle | ||
stylusData.pressure = pressure |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to assign a new object here. Since it's passed referentially to the EventDataBuilder,
changes like that may modify an already scheduled but not yet sent event. (This would also warrant a name change)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that it will be too expensive, but we can get back to it.
Done in 29d2d67
fun createStylusDataObject(stylusData: StylusData): WritableMap { | ||
val stylusDataObject = Arguments.createMap() | ||
|
||
stylusDataObject.putDouble("tiltX", stylusData.tiltX) | ||
stylusDataObject.putDouble("tiltY", stylusData.tiltY) | ||
stylusDataObject.putDouble("altitudeAngle", stylusData.altitudeAngle) | ||
stylusDataObject.putDouble("azimuthAngle", stylusData.azimuthAngle) | ||
stylusDataObject.putDouble("pressure", stylusData.pressure) | ||
|
||
return stylusDataObject | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fun createStylusDataObject(stylusData: StylusData): WritableMap { | |
val stylusDataObject = Arguments.createMap() | |
stylusDataObject.putDouble("tiltX", stylusData.tiltX) | |
stylusDataObject.putDouble("tiltY", stylusData.tiltY) | |
stylusDataObject.putDouble("altitudeAngle", stylusData.altitudeAngle) | |
stylusDataObject.putDouble("azimuthAngle", stylusData.azimuthAngle) | |
stylusDataObject.putDouble("pressure", stylusData.pressure) | |
return stylusDataObject | |
} | |
fun createStylusDataObject(stylusData: StylusData) = Arguments.createMap().apply { | |
putDouble("tiltX", stylusData.tiltX) | |
putDouble("tiltY", stylusData.tiltY) | |
putDouble("altitudeAngle", stylusData.altitudeAngle) | |
putDouble("azimuthAngle", stylusData.azimuthAngle) | |
putDouble("pressure", stylusData.pressure) | |
} |
This can also be moved to a method on StylusData
as toReadableMap
. I think WritableMap
can be assigned to ReadableMap
and it makes it clear that it shouldn't be modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 1ab4c37
@@ -11,6 +11,7 @@ import com.swmansion.gesturehandler.react.RNViewConfigurationHelper | |||
class HoverGestureHandler : GestureHandler<HoverGestureHandler>() { | |||
private var handler: Handler? = null | |||
private var finishRunnable = Runnable { finish() } | |||
var stylusData: StylusData = StylusData(0.0, 0.0, 0.0, 0.0, -1.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var stylusData: StylusData = StylusData(0.0, 0.0, 0.0, 0.0, -1.0) | |
var stylusData: StylusData = StylusData(0.0, 0.0, 0.0, 0.0, -1.0) | |
private set |
And please move the magic numbers to be the default values in the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (stylusData.pressure != -1.0) { | ||
putMap("stylusData", GestureUtils.createStylusDataObject(stylusData)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the pressure when hovering?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is 0
if (state == STATE_UNDETERMINED) { | ||
stylusData.pressure = -1.0 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be inside onReset
? And please make a new object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in b896f2e
var tiltX: Double, | ||
var tiltY: Double, | ||
var altitudeAngle: Double, | ||
var azimuthAngle: Double, | ||
var pressure: Double |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var tiltX: Double, | |
var tiltY: Double, | |
var altitudeAngle: Double, | |
var azimuthAngle: Double, | |
var pressure: Double | |
val tiltX: Double, | |
val tiltY: Double, | |
val altitudeAngle: Double, | |
val azimuthAngle: Double, | |
val pressure: Double |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in b896f2e
@@ -45,6 +45,7 @@ class PanGestureHandler(context: Context?) : GestureHandler<PanGestureHandler>() | |||
private var activateAfterLongPress = DEFAULT_ACTIVATE_AFTER_LONG_PRESS | |||
private val activateDelayed = Runnable { activate() } | |||
private var handler: Handler? = null | |||
var stylusData: StylusData = StylusData(0.0, 0.0, 0.0, 0.0, -1.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var stylusData: StylusData = StylusData(0.0, 0.0, 0.0, 0.0, -1.0) | |
var stylusData: StylusData = StylusData(0.0, 0.0, 0.0, 0.0, -1.0) | |
private set |
And the same deal with numbers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Description
This PR adds stylus support on Android.
Note
You can read more about this feature in #3107
Test plan
Tested on StylusData example