Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ sealed class NetworkCommand {
RightThumbstickY = originalState.RightThumbstickY
LeftTrigger = originalState.LeftTrigger
RightTrigger = originalState.RightTrigger
Pitch = originalState.Pitch
Roll = originalState.Roll
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.github.kitswas.virtualgamepadmobile.ui.screens

import android.annotation.SuppressLint
import android.app.Activity
import android.content.res.Configuration
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorManager
import android.util.Log
import android.widget.Toast
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -57,6 +61,32 @@
DrawGamepad(screenWidth, screenHeight, gamepadState, buttonConfigs)

val activity = LocalContext.current.findActivity()
val sensorService = activity?.getSystemService(Activity.SENSOR_SERVICE)
if (sensorService is SensorManager) {
val rotationSensor = sensorService.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)
sensorService.registerListener(object : android.hardware.SensorEventListener {

override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
// If you need to process this as well, it would be a good idea
// to wrap the values from this as well as onSensorChanged() into
// a custom SensorEvent class and then put it on a channel.
}

override fun onSensorChanged(event: SensorEvent?) {
if (event?.sensor == rotationSensor) {
val rotationMatrix = FloatArray(9)
SensorManager.getRotationMatrixFromVector(rotationMatrix, event?.values)

Check warning on line 78 in app/src/main/java/io/github/kitswas/virtualgamepadmobile/ui/screens/Gamepad.kt

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this useless null-safe access ?., it always succeeds.

See more on https://sonarcloud.io/project/issues?id=kitswas_VirtualGamePad-Mobile&issues=AZ1JyT3z5heiKBF9rOul&open=AZ1JyT3z5heiKBF9rOul&pullRequest=22
// TODO rotate

Check warning on line 79 in app/src/main/java/io/github/kitswas/virtualgamepadmobile/ui/screens/Gamepad.kt

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this TODO comment.

See more on https://sonarcloud.io/project/issues?id=kitswas_VirtualGamePad-Mobile&issues=AZ1JyT3z5heiKBF9rOum&open=AZ1JyT3z5heiKBF9rOum&pullRequest=22
val orientation = FloatArray(3)
SensorManager.getOrientation(rotationMatrix, orientation)
gamepadState.Pitch = orientation[1];
gamepadState.Roll = orientation[2];
}
}

}, rotationSensor, 20000);
}

// disconnect on back press
androidx.lifecycle.compose.LocalLifecycleOwner.current.lifecycle
.addObserver(androidx.lifecycle.LifecycleEventObserver { _, event ->
Expand Down