Hide the calls to ConvertAnalogStick inside ControlMapper.

This commit is contained in:
Henrik Rydgård 2021-07-09 11:01:56 +02:00
parent fac4c2a90b
commit 42f7ab7341
3 changed files with 35 additions and 35 deletions

View file

@ -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<void(int)> onVKeyDown, std::function<void(int)> onVKeyUp, std::function<void(char, float, int)> setPSPAxis) {
void ControlMapper::SetCallbacks(std::function<void(int)> onVKeyDown, std::function<void(int)> onVKeyUp, std::function<void(int, float, float)> 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;
}
}

View file

@ -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<void(int)> onVKeyDown,
std::function<void(int)> onVKeyUp,
std::function<void(char, float, int)> setPSPAxis);
std::function<void(int, float, float)> 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<void(int)> onVKeyDown_;
std::function<void(int)> onVKeyUp_;
std::function<void(char, float, int)> setPSPAxis_;
std::function<void(int, float, float)> setPSPAnalog_;
};

View file

@ -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.