diff --git a/Core/Debugger/WebSocket/InputSubscriber.cpp b/Core/Debugger/WebSocket/InputSubscriber.cpp index b35c8413e7..facedff0fc 100644 --- a/Core/Debugger/WebSocket/InputSubscriber.cpp +++ b/Core/Debugger/WebSocket/InputSubscriber.cpp @@ -254,8 +254,7 @@ void WebSocketInputState::AnalogSend(DebuggerRequest &req) { if (!AnalogValue(req, &x, "x") || !AnalogValue(req, &y, "y")) return; - __CtrlSetAnalogX(x, stick == "left" ? CTRL_STICK_LEFT : CTRL_STICK_RIGHT); - __CtrlSetAnalogY(y, stick == "left" ? CTRL_STICK_LEFT : CTRL_STICK_RIGHT); + __CtrlSetAnalogXY(stick == "left" ? CTRL_STICK_LEFT : CTRL_STICK_RIGHT, x, y); req.Respond(); } diff --git a/Core/HLE/sceCtrl.cpp b/Core/HLE/sceCtrl.cpp index a87cba554f..fe78433792 100644 --- a/Core/HLE/sceCtrl.cpp +++ b/Core/HLE/sceCtrl.cpp @@ -188,18 +188,14 @@ void __CtrlButtonUp(u32 buttonBit) ctrlCurrent.buttons &= ~buttonBit; } -void __CtrlSetAnalogX(float x, int stick) +void __CtrlSetAnalogXY(int stick, float x, float y) { - u8 scaled = clamp_u8((int)ceilf(x * 127.5f + 127.5f)); + u8 scaledX = clamp_u8((int)ceilf(x * 127.5f + 127.5f)); + // TODO: We might have too many negations of Y... + u8 scaledY = clamp_u8((int)ceilf(-y * 127.5f + 127.5f)); std::lock_guard guard(ctrlMutex); - ctrlCurrent.analog[stick][CTRL_ANALOG_X] = scaled; -} - -void __CtrlSetAnalogY(float y, int stick) -{ - u8 scaled = clamp_u8((int)ceilf(-y * 127.5f + 127.5f)); - std::lock_guard guard(ctrlMutex); - ctrlCurrent.analog[stick][CTRL_ANALOG_Y] = scaled; + ctrlCurrent.analog[stick][CTRL_ANALOG_X] = scaledX; + ctrlCurrent.analog[stick][CTRL_ANALOG_Y] = scaledY; } void __CtrlSetRapidFire(bool state) diff --git a/Core/HLE/sceCtrl.h b/Core/HLE/sceCtrl.h index 039b4e7404..6a77cd0b9d 100644 --- a/Core/HLE/sceCtrl.h +++ b/Core/HLE/sceCtrl.h @@ -72,10 +72,9 @@ void __CtrlButtonDown(u32 buttonBit); void __CtrlButtonUp(u32 buttonBit); // Call this to set the position of an analog stick, ideally when it changes. -// Position value should be from -1 to 1, inclusive, in a square (no need to force to a circle.) +// X and Y values should be from -1 to 1, inclusive, in a square (no need to force to a circle.) // No deadzone filtering is done (but note that this applies to the actual PSP as well.) -void __CtrlSetAnalogX(float value, int stick = CTRL_STICK_LEFT); -void __CtrlSetAnalogY(float value, int stick = CTRL_STICK_LEFT); +void __CtrlSetAnalogXY(int stick, float x, float y); // Call this to enable rapid-fire. This will cause buttons other than arrows to alternate. void __CtrlSetRapidFire(bool state); diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 9e24e53bd4..39ce5c92f0 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -170,8 +170,7 @@ static void SetPSPAxis(char axis, float value, int stick) { // It's a bit non-ideal to run through this twice, once for each axis, but... ConvertAnalogStick(x, y); - __CtrlSetAnalogX(x, stick); - __CtrlSetAnalogY(y, stick); + __CtrlSetAnalogXY(stick, x, y); } EmuScreen::EmuScreen(const Path &filename) @@ -729,14 +728,12 @@ void EmuScreen::onVKeyUp(int virtualKeyCode) { case VIRTKEY_ANALOG_ROTATE_CW: autoRotatingAnalogCW_ = false; - __CtrlSetAnalogX(0.0f, 0); - __CtrlSetAnalogY(0.0f, 0); + __CtrlSetAnalogXY(0, 0.0f, 0.0f); break; case VIRTKEY_ANALOG_ROTATE_CCW: autoRotatingAnalogCCW_ = false; - __CtrlSetAnalogX(0.0f, 0); - __CtrlSetAnalogY(0.0f, 0); + __CtrlSetAnalogXY(0, 0.0f, 0.0f); break; default: @@ -986,12 +983,14 @@ void EmuScreen::update() { if (autoRotatingAnalogCW_) { const float now = time_now_d(); // Clamp to a square - __CtrlSetAnalogX(std::min(1.0f, std::max(-1.0f, 1.42f*cosf(now*-g_Config.fAnalogAutoRotSpeed))), 0); - __CtrlSetAnalogY(std::min(1.0f, std::max(-1.0f, 1.42f*sinf(now*-g_Config.fAnalogAutoRotSpeed))), 0); + float x = std::min(1.0f, std::max(-1.0f, 1.42f * cosf(now * -g_Config.fAnalogAutoRotSpeed))); + float y = std::min(1.0f, std::max(-1.0f, 1.42f * sinf(now * -g_Config.fAnalogAutoRotSpeed))); + __CtrlSetAnalogXY(0, x, y); } else if (autoRotatingAnalogCCW_) { const float now = time_now_d(); - __CtrlSetAnalogX(std::min(1.0f, std::max(-1.0f, 1.42f*cosf(now*g_Config.fAnalogAutoRotSpeed))), 0); - __CtrlSetAnalogY(std::min(1.0f, std::max(-1.0f, 1.42f*sinf(now*g_Config.fAnalogAutoRotSpeed))), 0); + float x = std::min(1.0f, std::max(-1.0f, 1.42f * cosf(now * g_Config.fAnalogAutoRotSpeed))); + float y = std::min(1.0f, std::max(-1.0f, 1.42f * sinf(now * g_Config.fAnalogAutoRotSpeed))); + __CtrlSetAnalogXY(0, x, y); } // This is here to support the iOS on screen back button. diff --git a/UI/GamepadEmu.cpp b/UI/GamepadEmu.cpp index d38bac4b92..69359e1432 100644 --- a/UI/GamepadEmu.cpp +++ b/UI/GamepadEmu.cpp @@ -199,8 +199,7 @@ void AnalogRotationButton::Touch(const TouchInput &input) { autoRotating_ = true; } else if (lastDown && !down) { autoRotating_ = false; - __CtrlSetAnalogX(0.0f, 0); - __CtrlSetAnalogY(0.0f, 0); + __CtrlSetAnalogXY(0, 0.0f, 0.0f); } } @@ -215,8 +214,9 @@ void AnalogRotationButton::Update() { if (autoRotating_) { float speed = clockWise_ ? -g_Config.fAnalogAutoRotSpeed : g_Config.fAnalogAutoRotSpeed; // Clamp to a square - __CtrlSetAnalogX(std::min(1.0f, std::max(-1.0f, 1.42f*cosf(now*speed))), 0); - __CtrlSetAnalogY(std::min(1.0f, std::max(-1.0f, 1.42f*sinf(now*speed))), 0); + __CtrlSetAnalogXY(0, + std::min(1.0f, std::max(-1.0f, 1.42f*cosf(now*speed))), + std::min(1.0f, std::max(-1.0f, 1.42f*sinf(now*speed)))); } } @@ -441,8 +441,7 @@ void PSPStick::Touch(const TouchInput &input) { dragPointerId_ = -1; centerX_ = bounds_.centerX(); centerY_ = bounds_.centerY(); - __CtrlSetAnalogX(0.0f, stick_); - __CtrlSetAnalogY(0.0f, stick_); + __CtrlSetAnalogXY(stick_, 0.0f, 0.0f); return; } if (input.flags & TOUCH_DOWN) { @@ -492,11 +491,9 @@ void PSPStick::ProcessTouch(float x, float y, bool down) { dx = std::min(1.0f, std::max(-1.0f, dx)); dy = std::min(1.0f, std::max(-1.0f, dy)); - __CtrlSetAnalogX(dx, stick_); - __CtrlSetAnalogY(-dy, stick_); + __CtrlSetAnalogXY(stick_, dx, -dy); } else { - __CtrlSetAnalogX(0.0f, stick_); - __CtrlSetAnalogY(0.0f, stick_); + __CtrlSetAnalogXY(stick_, 0.0f, 0.0f); } } diff --git a/UI/TiltEventProcessor.cpp b/UI/TiltEventProcessor.cpp index 2f66906239..8beb768508 100644 --- a/UI/TiltEventProcessor.cpp +++ b/UI/TiltEventProcessor.cpp @@ -96,8 +96,7 @@ void TiltEventProcessor::TranslateTiltToInput(const Tilt &tilt) { } void TiltEventProcessor::GenerateAnalogStickEvent(const Tilt &tilt) { - __CtrlSetAnalogX(clamp(tilt.x_), CTRL_STICK_LEFT); - __CtrlSetAnalogY(clamp(tilt.y_), CTRL_STICK_LEFT); + __CtrlSetAnalogXY(CTRL_STICK_LEFT, clamp(tilt.x_), clamp(tilt.y_)); tiltAnalogSet = true; } @@ -182,8 +181,7 @@ void TiltEventProcessor::ResetTiltEvents() { tiltButtonsDown = 0; if (tiltAnalogSet) { - __CtrlSetAnalogX(0.0f, CTRL_STICK_LEFT); - __CtrlSetAnalogY(0.0f, CTRL_STICK_LEFT); + __CtrlSetAnalogXY(CTRL_STICK_LEFT, 0.0f, 0.0f); tiltAnalogSet = false; } } diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index c5f6f9a0fc..35e3f0b86a 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -752,10 +752,12 @@ static void retro_input(void) } } - __CtrlSetAnalogX(input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X) / 32767.0f, CTRL_STICK_LEFT); - __CtrlSetAnalogY(input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y) / -32767.0f, CTRL_STICK_LEFT); - __CtrlSetAnalogX(input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X) / 32767.0f, CTRL_STICK_RIGHT); - __CtrlSetAnalogY(input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y) / -32767.0f, CTRL_STICK_RIGHT); + float x_left = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X) / 32767.0f; + float y_left = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y) / -32767.0f; + float x_right = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X) / 32767.0f; + float y_right = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y) / -32767.0f; + __CtrlSetAnalogXY(CTRL_STICK_LEFT, x_left, y_left); + __CtrlSetAnalogXY(CTRL_STICK_RIGHT, x_right, y_right); } void retro_run(void)