Merge pull request #18250 from hrydgard/separate-accelerometer-events

Separate out accelerometer events from joystick axis events
This commit is contained in:
Henrik Rydgård 2023-09-27 12:11:02 +02:00 committed by GitHub
commit 1fff976e48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 23 additions and 54 deletions

View file

@ -31,7 +31,7 @@ enum InputDeviceID {
DEVICE_ID_XINPUT_1 = 21,
DEVICE_ID_XINPUT_2 = 22,
DEVICE_ID_XINPUT_3 = 23,
DEVICE_ID_ACCELEROMETER = 30,
DEVICE_ID_ACCELEROMETER = 30, // no longer used
DEVICE_ID_XR_HMD = 39,
DEVICE_ID_XR_CONTROLLER_LEFT = 40,
DEVICE_ID_XR_CONTROLLER_RIGHT = 41,

View file

@ -305,7 +305,7 @@ enum InputAxis {
JOYSTICK_AXIS_MOUSE_REL_X = 26,
JOYSTICK_AXIS_MOUSE_REL_Y = 27,
// Mobile device accelerometer/gyro
// Mobile device accelerometer/gyro. NOTE: These are no longer passed around internally, only used for the plugin API.
JOYSTICK_AXIS_ACCELEROMETER_X = 40,
JOYSTICK_AXIS_ACCELEROMETER_Y = 41,
JOYSTICK_AXIS_ACCELEROMETER_Z = 42,

View file

@ -55,6 +55,7 @@ bool NativeIsRestarting();
void NativeTouch(const TouchInput &touch);
bool NativeKey(const KeyInput &key);
void NativeAxis(const AxisInput *axis, size_t count);
void NativeAccelerometer(float tiltX, float tiltY, float tiltZ);
// Called when it's process a frame, including rendering. If the device can keep up, this
// will be called sixty times per second. Main thread.

View file

@ -19,6 +19,12 @@ static u32 tiltButtonsDown = 0;
float rawTiltAnalogX;
float rawTiltAnalogY;
float g_currentYAngle = 0.0f;
float GetCurrentYAngle() {
return g_currentYAngle;
}
// These functions generate tilt events given the current Tilt amount,
// and the deadzone radius.
void GenerateAnalogStickEvent(float analogX, float analogY);
@ -73,6 +79,7 @@ void ProcessTilt(bool landscape, float calibrationAngle, float x, float y, float
Lin::Vec3 down = Lin::Vec3(x, y, z).normalized();
float angleAroundX = atan2(down.z, down.y);
g_currentYAngle = angleAroundX; // TODO: Should smooth this out over time a bit.
float yAngle = angleAroundX - calibrationAngle;
float xAngle = asinf(down.x);

View file

@ -1,5 +1,7 @@
#pragma once
#include "Common/Math/lin/vec3.h"
namespace TiltEventProcessor {
// generates a tilt in the correct coordinate system based on
@ -7,6 +9,8 @@ namespace TiltEventProcessor {
void ProcessTilt(bool landscape, const float calibrationAngle, float x, float y, float z, bool invertX, bool invertY, float xSensitivity, float ySensitivity);
void ResetTiltEvents();
float GetCurrentYAngle();
// Lets you preview the amount of tilt in TiltAnalogSettingsScreen.
extern float rawTiltAnalogX;
extern float rawTiltAnalogY;

View file

@ -731,18 +731,7 @@ void MainUI::updateAccelerometer() {
// TODO: Toggle it depending on whether it is enabled
QAccelerometerReading *reading = acc->reading();
if (reading) {
AxisInput axis[3];
for (int i = 0; i < 3; i++) {
axis[i].deviceId = DEVICE_ID_ACCELEROMETER;
}
axis[0].axisId = JOYSTICK_AXIS_ACCELEROMETER_X;
axis[0].value = reading->x();
axis[1].axisId = JOYSTICK_AXIS_ACCELEROMETER_Y;
axis[1].value = reading->y();
axis[2].axisId = JOYSTICK_AXIS_ACCELEROMETER_Z;
axis[2].value = reading->z();
NativeAxis(axis, 3);
NativeAccelerometer(reading->x(), reading->y(), reading->z());
}
#endif
}

View file

@ -1336,22 +1336,12 @@ static void ProcessOneAxisEvent(const AxisInput &axis) {
}
void NativeAxis(const AxisInput *axes, size_t count) {
// figure out what the current tilt orientation is by checking the axis event
// This is static, since we need to remember where we last were (in terms of orientation)
static float tiltX;
static float tiltY;
static float tiltZ;
for (size_t i = 0; i < count; i++) {
ProcessOneAxisEvent(axes[i]);
switch (axes[i].axisId) {
case JOYSTICK_AXIS_ACCELEROMETER_X: tiltX = axes[i].value; break;
case JOYSTICK_AXIS_ACCELEROMETER_Y: tiltY = axes[i].value; break;
case JOYSTICK_AXIS_ACCELEROMETER_Z: tiltZ = axes[i].value; break;
default: break;
}
}
}
void NativeAccelerometer(float tiltX, float tiltY, float tiltZ) {
if (g_Config.iTiltInputType == TILT_NULL) {
// if tilt events are disabled, don't do anything special.
return;
@ -1377,6 +1367,10 @@ void NativeAxis(const AxisInput *axes, size_t count) {
TiltEventProcessor::ProcessTilt(landscape, tiltBaseAngleY, tiltX, tiltY, tiltZ,
g_Config.bInvertTiltX, g_Config.bInvertTiltY,
xSensitivity, ySensitivity);
HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_ACCELEROMETER_X] = tiltX;
HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_ACCELEROMETER_Y] = tiltY;
HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_ACCELEROMETER_Z] = tiltZ;
}
void System_PostUIMessage(const std::string &message, const std::string &value) {

View file

@ -137,22 +137,8 @@ void TiltAnalogSettingsScreen::CreateViews() {
settings->Add(new Choice(di->T("Back")))->OnClick.Handle<UIScreen>(this, &UIScreen::OnBack);
}
void TiltAnalogSettingsScreen::axis(const AxisInput &axis) {
UIDialogScreenWithGameBackground::axis(axis);
if (axis.deviceId == DEVICE_ID_ACCELEROMETER) {
switch (axis.axisId) {
case JOYSTICK_AXIS_ACCELEROMETER_X: down_.x = axis.value; break;
case JOYSTICK_AXIS_ACCELEROMETER_Y: down_.y = axis.value; break;
case JOYSTICK_AXIS_ACCELEROMETER_Z: down_.z = axis.value; break;
default: break;
}
}
}
UI::EventReturn TiltAnalogSettingsScreen::OnCalibrate(UI::EventParams &e) {
Lin::Vec3 down = down_.normalized();
g_Config.fTiltBaseAngleY = atan2(down.z, down.x);
g_Config.fTiltBaseAngleY = TiltEventProcessor::GetCurrentYAngle();
return UI::EVENT_DONE;
}

View file

@ -29,7 +29,6 @@ public:
TiltAnalogSettingsScreen(const Path &gamePath) : UIDialogScreenWithGameBackground(gamePath) {}
void CreateViews() override;
void axis(const AxisInput &axis) override;
void update() override;
const char *tag() const override { return "TiltAnalogSettings"; }

View file

@ -1240,18 +1240,7 @@ extern "C" jboolean Java_org_ppsspp_ppsspp_NativeApp_mouseWheelEvent(
extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_accelerometer(JNIEnv *, jclass, float x, float y, float z) {
if (!renderer_inited)
return;
AxisInput axis[3];
for (int i = 0; i < 3; i++) {
axis[i].deviceId = DEVICE_ID_ACCELEROMETER;
}
axis[0].axisId = JOYSTICK_AXIS_ACCELEROMETER_X;
axis[0].value = x;
axis[1].axisId = JOYSTICK_AXIS_ACCELEROMETER_Y;
axis[1].value = y;
axis[2].axisId = JOYSTICK_AXIS_ACCELEROMETER_Z;
axis[2].value = z;
NativeAxis(axis, 3);
NativeAccelerometer(x, y, z);
}
extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendMessageFromJava(JNIEnv *env, jclass, jstring message, jstring param) {