diff --git a/Core/ControlMapper.cpp b/Core/ControlMapper.cpp index 980cc01d85..e7f5fd2d63 100644 --- a/Core/ControlMapper.cpp +++ b/Core/ControlMapper.cpp @@ -13,7 +13,7 @@ static float MapAxisValue(float v) { return sign * Clamp(invDeadzone + (abs(v) - deadzone) / (1.0f - deadzone) * (sensitivity - invDeadzone), 0.0f, 1.0f); } -void ConvertAnalogStick(float &x, float &y) { +static void ConvertAnalogStick(float &x, float &y) { const bool isCircular = g_Config.bAnalogIsCircular; float norm = std::max(fabsf(x), fabsf(y)); @@ -34,10 +34,25 @@ void ConvertAnalogStick(float &x, float &y) { y = Clamp(y / norm * mappedNorm, -1.0f, 1.0f); } -void ControlMapper::SetCallbacks(std::function onVKeyDown, std::function onVKeyUp, std::function setPSPAxis) { +void ControlMapper::SetCallbacks(std::function onVKeyDown, std::function onVKeyUp, std::function setPSPAnalog) { onVKeyDown_ = onVKeyDown; onVKeyUp_ = onVKeyUp; - setPSPAxis_ = setPSPAxis; + setPSPAnalog_ = setPSPAnalog; +} + +void ControlMapper::SetPSPAxis(char axis, float value, int stick) { + static float history[2][2] = {}; + + int axisId = axis == 'X' ? 0 : 1; + + history[stick][axisId] = value; + + float x = history[stick][0]; + float y = history[stick][1]; + + ConvertAnalogStick(x, y); + + setPSPAnalog_(stick, x, y); } bool ControlMapper::Key(const KeyInput &key, bool *pauseTrigger) { @@ -119,7 +134,7 @@ void ControlMapper::setVKeyAnalog(char axis, int stick, int virtualKeyMin, int v if (maxDown) value += scale; if (setZero || minDown || maxDown) { - setPSPAxis_(axis, value, stick); + SetPSPAxis(axis, value, stick); } } @@ -245,29 +260,29 @@ void ControlMapper::processAxis(const AxisInput &axis, int direction) { float value = fabs(axis.value) * scale; switch (result) { case VIRTKEY_AXIS_X_MIN: - setPSPAxis_('X', -value, CTRL_STICK_LEFT); + SetPSPAxis('X', -value, CTRL_STICK_LEFT); break; case VIRTKEY_AXIS_X_MAX: - setPSPAxis_('X', value, CTRL_STICK_LEFT); + SetPSPAxis('X', value, CTRL_STICK_LEFT); break; case VIRTKEY_AXIS_Y_MIN: - setPSPAxis_('Y', -value, CTRL_STICK_LEFT); + SetPSPAxis('Y', -value, CTRL_STICK_LEFT); break; case VIRTKEY_AXIS_Y_MAX: - setPSPAxis_('Y', value, CTRL_STICK_LEFT); + SetPSPAxis('Y', value, CTRL_STICK_LEFT); break; case VIRTKEY_AXIS_RIGHT_X_MIN: - setPSPAxis_('X', -value, CTRL_STICK_RIGHT); + SetPSPAxis('X', -value, CTRL_STICK_RIGHT); break; case VIRTKEY_AXIS_RIGHT_X_MAX: - setPSPAxis_('X', value, CTRL_STICK_RIGHT); + SetPSPAxis('X', value, CTRL_STICK_RIGHT); break; case VIRTKEY_AXIS_RIGHT_Y_MIN: - setPSPAxis_('Y', -value, CTRL_STICK_RIGHT); + SetPSPAxis('Y', -value, CTRL_STICK_RIGHT); break; case VIRTKEY_AXIS_RIGHT_Y_MAX: - setPSPAxis_('Y', value, CTRL_STICK_RIGHT); + SetPSPAxis('Y', value, CTRL_STICK_RIGHT); break; } } diff --git a/Core/ControlMapper.h b/Core/ControlMapper.h index 815499878b..f332e35ffa 100644 --- a/Core/ControlMapper.h +++ b/Core/ControlMapper.h @@ -8,11 +8,8 @@ // Utilities for mapping input events to PSP inputs and virtual keys. // Main use is of course from EmuScreen.cpp, but also useful from control settings etc. - -// Maps analog stick input to a distorted space according to -// the deadzone and shape settings. -void ConvertAnalogStick(float &x, float &y); - +// At some point I want to refactor this from using callbacks to simply providing lists of events. +// Still it won't be able to be completely stateless due to the 2-D processing of analog sticks. class ControlMapper { public: @@ -22,13 +19,15 @@ public: void SetCallbacks( std::function onVKeyDown, std::function onVKeyUp, - std::function setPSPAxis); + std::function setPSPAnalog); private: void processAxis(const AxisInput &axis, int direction); void pspKey(int pspKeyCode, int flags); void setVKeyAnalog(char axis, int stick, int virtualKeyMin, int virtualKeyMax, bool setZero = true); + void SetPSPAxis(char axis, float value, int stick); + void onVKeyDown(int vkey); void onVKeyUp(int vkey); @@ -41,5 +40,5 @@ private: // Callbacks std::function onVKeyDown_; std::function onVKeyUp_; - std::function setPSPAxis_; + std::function setPSPAnalog_; }; diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 94299dc651..68f9627776 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -136,21 +136,7 @@ static void __EmuScreenVblank() // Handles control rotation due to internal screen rotation. // TODO: This should be a callback too, so we don't actually call the __Ctrl functions // from settings screens, etc. -static void SetPSPAxis(char axis, float value, int stick) { - // TODO: Can we move the rest of this logic into ControlMapping too? - - static float history[2][2] = {}; - - int axisId = axis == 'X' ? 0 : 1; - - history[stick][axisId] = value; - - float x = history[stick][0]; - float y = history[stick][1]; - - // It's a bit non-ideal to run through this twice, once for each axis, but... - ConvertAnalogStick(x, y); - +static void SetPSPAnalog(int stick, float x, float y) { switch (g_Config.iInternalScreenRotation) { case ROTATION_LOCKED_HORIZONTAL: // Standard rotation. No change. @@ -190,7 +176,7 @@ EmuScreen::EmuScreen(const Path &filename) controlMapper_.SetCallbacks( std::bind(&EmuScreen::onVKeyDown, this, _1), std::bind(&EmuScreen::onVKeyUp, this, _1), - &SetPSPAxis); + &SetPSPAnalog); // Make sure we don't leave it at powerdown after the last game. // TODO: This really should be handled elsewhere if it isn't.