Skip to content

Commit

Permalink
Merge pull request #14596 from hrydgard/analog-setup-screen
Browse files Browse the repository at this point in the history
Replace the "Test Analogs" screen with a new screen that lets you directly try the settings.
  • Loading branch information
hrydgard authored Jul 9, 2021
2 parents 69f9670 + d4c5ba9 commit 5bd9e93
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 160 deletions.
8 changes: 4 additions & 4 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ static ConfigSetting controlSettings[] = {
ConfigSetting("TouchButtonOpacity", &g_Config.iTouchButtonOpacity, 65, true, true),
ConfigSetting("TouchButtonHideSeconds", &g_Config.iTouchButtonHideSeconds, 20, true, true),
ConfigSetting("AutoCenterTouchAnalog", &g_Config.bAutoCenterTouchAnalog, false, true, true),
ConfigSetting("AnalogAutoRotSpeed", &g_Config.fAnalogAutoRotSpeed, 15.0f, true, true),
ConfigSetting("AnalogAutoRotSpeed", &g_Config.fAnalogAutoRotSpeed, 8.0f, true, true),

// Snap touch control position
ConfigSetting("TouchSnapToGrid", &g_Config.bTouchSnapToGrid, false, true, true),
Expand Down Expand Up @@ -962,10 +962,10 @@ static ConfigSetting controlSettings[] = {
ConfigSetting("AnalogRotationCWKeyX", "AnalogRotationKeyCWY", "AnalogRotationKeyCWScale", "ShowAnalogRotationCWKey", &g_Config.touchAnalogRotationCWKey, defaultTouchPosHide, true, true),
ConfigSetting("AnalogRotationCCWKeyX", "AnalogRotationKeyCCWY", "AnalogRotationKeyCCWScale", "ShowAnalogRotationCCWKey", &g_Config.touchAnalogRotationCCWKey, defaultTouchPosHide, true, true),

ConfigSetting("AnalogDeadzone", &g_Config.fAnalogDeadzone, 0.0f, true, true),
ConfigSetting("AnalogDeadzone", &g_Config.fAnalogDeadzone, 0.15f, true, true),
ConfigSetting("AnalogInverseDeadzone", &g_Config.fAnalogInverseDeadzone, 0.0f, true, true),
ConfigSetting("AnalogSensitivity", &g_Config.fAnalogSensitivity, 1.0f, true, true),
ConfigSetting("AnalogIsCircular", &g_Config.bAnalogIsCircular, true, true, true),
ConfigSetting("AnalogSensitivity", &g_Config.fAnalogSensitivity, 1.1f, true, true),
ConfigSetting("AnalogIsCircular", &g_Config.bAnalogIsCircular, false , true, true),

ConfigSetting("AnalogLimiterDeadzone", &g_Config.fAnalogLimiterDeadzone, 0.6f, true, true),

Expand Down
49 changes: 48 additions & 1 deletion Core/ControlMapper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <algorithm>

#include "Common/Math/math_util.h"
#include "Common/TimeUtil.h"

#include "Core/KeyMap.h"
#include "Core/ControlMapper.h"
#include "Core/Config.h"
Expand All @@ -13,7 +15,7 @@ static float MapAxisValue(float v) {
return sign * Clamp(invDeadzone + (abs(v) - deadzone) / (1.0f - deadzone) * (sensitivity - invDeadzone), 0.0f, 1.0f);
}

static void ConvertAnalogStick(float &x, float &y) {
void ConvertAnalogStick(float &x, float &y) {
const bool isCircular = g_Config.bAnalogIsCircular;

float norm = std::max(fabsf(x), fabsf(y));
Expand All @@ -40,6 +42,11 @@ void ControlMapper::SetCallbacks(std::function<void(int)> onVKeyDown, std::funct
setPSPAnalog_ = setPSPAnalog;
}

void ControlMapper::SetRawCallback(std::function<void(int, float, float)> setRawAnalog) {
setRawAnalog_ = setRawAnalog;
}


void ControlMapper::SetPSPAxis(char axis, float value, int stick) {
static float history[2][2] = {};

Expand All @@ -50,6 +57,10 @@ void ControlMapper::SetPSPAxis(char axis, float value, int stick) {
float x = history[stick][0];
float y = history[stick][1];

if (setRawAnalog_) {
setRawAnalog_(stick, x, y);
}

ConvertAnalogStick(x, y);

setPSPAnalog_(stick, x, y);
Expand Down Expand Up @@ -94,6 +105,23 @@ bool ControlMapper::Axis(const AxisInput &axis) {
return false;
}

void ControlMapper::Update() {
if (autoRotatingAnalogCW_) {
const double now = time_now_d();
// Clamp to a square
float x = std::min(1.0f, std::max(-1.0f, 1.42f * (float)cos(now * -g_Config.fAnalogAutoRotSpeed)));
float y = std::min(1.0f, std::max(-1.0f, 1.42f * (float)sin(now * -g_Config.fAnalogAutoRotSpeed)));

setPSPAnalog_(0, x, y);
} else if (autoRotatingAnalogCCW_) {
const double now = time_now_d();
float x = std::min(1.0f, std::max(-1.0f, 1.42f * (float)cos(now * g_Config.fAnalogAutoRotSpeed)));
float y = std::min(1.0f, std::max(-1.0f, 1.42f * (float)sin(now * g_Config.fAnalogAutoRotSpeed)));

setPSPAnalog_(0, x, y);
}
}


inline bool IsAnalogStickKey(int key) {
switch (key) {
Expand Down Expand Up @@ -203,6 +231,15 @@ void ControlMapper::onVKeyDown(int vkey) {
setVKeyAnalog('Y', CTRL_STICK_RIGHT, VIRTKEY_AXIS_RIGHT_Y_MIN, VIRTKEY_AXIS_RIGHT_Y_MAX, false);
break;

case VIRTKEY_ANALOG_ROTATE_CW:
autoRotatingAnalogCW_ = true;
autoRotatingAnalogCCW_ = false;
break;
case VIRTKEY_ANALOG_ROTATE_CCW:
autoRotatingAnalogCW_ = false;
autoRotatingAnalogCCW_ = true;
break;

default:
if (onVKeyDown_)
onVKeyDown_(vkey);
Expand Down Expand Up @@ -238,6 +275,16 @@ void ControlMapper::onVKeyUp(int vkey) {
setVKeyAnalog('Y', CTRL_STICK_RIGHT, VIRTKEY_AXIS_RIGHT_Y_MIN, VIRTKEY_AXIS_RIGHT_Y_MAX, false);
break;

case VIRTKEY_ANALOG_ROTATE_CW:
autoRotatingAnalogCW_ = false;
__CtrlSetAnalogXY(0, 0.0f, 0.0f);
break;

case VIRTKEY_ANALOG_ROTATE_CCW:
autoRotatingAnalogCCW_ = false;
__CtrlSetAnalogXY(0, 0.0f, 0.0f);
break;

default:
if (onVKeyUp_)
onVKeyUp_(vkey);
Expand Down
13 changes: 13 additions & 0 deletions Core/ControlMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@

class ControlMapper {
public:
void Update();

bool Key(const KeyInput &key, bool *pauseTrigger);
bool Axis(const AxisInput &axis);

// Required callbacks
void SetCallbacks(
std::function<void(int)> onVKeyDown,
std::function<void(int)> onVKeyUp,
std::function<void(int, float, float)> setPSPAnalog);

// Optional callback, only used in config
void SetRawCallback(std::function<void(int, float, float)> setRawAnalog);

private:
void processAxis(const AxisInput &axis, int direction);
void pspKey(int pspKeyCode, int flags);
Expand All @@ -37,8 +43,15 @@ class ControlMapper {
// De-noise mapped axis updates
int axisState_[JOYSTICK_AXIS_MAX]{};

// Mappable auto-rotation. Useful for keyboard/dpad->analog in a few games.
bool autoRotatingAnalogCW_ = false;
bool autoRotatingAnalogCCW_ = false;

// Callbacks
std::function<void(int)> onVKeyDown_;
std::function<void(int)> onVKeyUp_;
std::function<void(int, float, float)> setPSPAnalog_;
std::function<void(int, float, float)> setRawAnalog_;
};

void ConvertAnalogStick(float &x, float &y);
Loading

0 comments on commit 5bd9e93

Please sign in to comment.