Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ControlMapper: Change the callbacks to be more suitable for the upcoming refactor. #17209

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 26 additions & 57 deletions Core/ControlMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ 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(int, bool)> setPSPButtonState, std::function<void(int, float, float)> setPSPAnalog) {
onVKeyDown_ = onVKeyDown;
onVKeyUp_ = onVKeyUp;
void ControlMapper::SetCallbacks(
std::function<void(int, bool)> onVKey,
std::function<void(uint32_t, uint32_t)> setAllPSPButtonStates,
std::function<void(int, bool)> setPSPButtonState,
std::function<void(int, float, float)> setPSPAnalog) {
onVKey_ = onVKey;
setAllPSPButtonStates_ = setAllPSPButtonStates;
setPSPButtonState_ = setPSPButtonState;
setPSPAnalog_ = setPSPAnalog;
}
Expand Down Expand Up @@ -200,11 +204,11 @@ void ControlMapper::SetPSPKey(int deviceId, int pspKeyCode, int flags) {
int vk = pspKeyCode - VIRTKEY_FIRST;
if (flags & KEY_DOWN) {
virtKeys_[vk] = true;
onVKeyDown(deviceId, pspKeyCode);
onVKey(deviceId, pspKeyCode, true);
}
if (flags & KEY_UP) {
virtKeys_[vk] = false;
onVKeyUp(deviceId, pspKeyCode);
onVKey(deviceId, pspKeyCode, false);
}
} else {
int rotations = 0;
Expand Down Expand Up @@ -232,7 +236,7 @@ void ControlMapper::SetPSPKey(int deviceId, int pspKeyCode, int flags) {
}
}

void ControlMapper::onVKeyDown(int deviceId, int vkey) {
void ControlMapper::onVKey(int deviceId, int vkey, bool down) {
switch (vkey) {
case VIRTKEY_AXIS_X_MIN:
case VIRTKEY_AXIS_X_MAX:
Expand Down Expand Up @@ -260,62 +264,27 @@ void ControlMapper::onVKeyDown(int deviceId, int vkey) {
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);
break;
}
}

void ControlMapper::onVKeyUp(int deviceId, int vkey) {
switch (vkey) {

case VIRTKEY_AXIS_X_MIN:
case VIRTKEY_AXIS_X_MAX:
SetVKeyAnalog(deviceId, 'X', CTRL_STICK_LEFT, VIRTKEY_AXIS_X_MIN, VIRTKEY_AXIS_X_MAX);
break;
case VIRTKEY_AXIS_Y_MIN:
case VIRTKEY_AXIS_Y_MAX:
SetVKeyAnalog(deviceId, 'Y', CTRL_STICK_LEFT, VIRTKEY_AXIS_Y_MIN, VIRTKEY_AXIS_Y_MAX);
break;

case VIRTKEY_AXIS_RIGHT_X_MIN:
case VIRTKEY_AXIS_RIGHT_X_MAX:
SetVKeyAnalog(deviceId, 'X', CTRL_STICK_RIGHT, VIRTKEY_AXIS_RIGHT_X_MIN, VIRTKEY_AXIS_RIGHT_X_MAX);
break;
case VIRTKEY_AXIS_RIGHT_Y_MIN:
case VIRTKEY_AXIS_RIGHT_Y_MAX:
SetVKeyAnalog(deviceId, 'Y', CTRL_STICK_RIGHT, VIRTKEY_AXIS_RIGHT_Y_MIN, VIRTKEY_AXIS_RIGHT_Y_MAX);
break;

case VIRTKEY_ANALOG_LIGHTLY:
SetVKeyAnalog(deviceId, 'X', CTRL_STICK_LEFT, VIRTKEY_AXIS_X_MIN, VIRTKEY_AXIS_X_MAX, false);
SetVKeyAnalog(deviceId, 'Y', CTRL_STICK_LEFT, VIRTKEY_AXIS_Y_MIN, VIRTKEY_AXIS_Y_MAX, false);
SetVKeyAnalog(deviceId, 'X', CTRL_STICK_RIGHT, VIRTKEY_AXIS_RIGHT_X_MIN, VIRTKEY_AXIS_RIGHT_X_MAX, false);
SetVKeyAnalog(deviceId, 'Y', CTRL_STICK_RIGHT, VIRTKEY_AXIS_RIGHT_Y_MIN, VIRTKEY_AXIS_RIGHT_Y_MAX, false);
break;

case VIRTKEY_ANALOG_ROTATE_CW:
autoRotatingAnalogCW_ = false;
setPSPAnalog_(0, 0.0f, 0.0f);
if (down) {
autoRotatingAnalogCW_ = true;
autoRotatingAnalogCCW_ = false;
} else {
autoRotatingAnalogCW_ = false;
setPSPAnalog_(0, 0.0f, 0.0f);
}
break;

case VIRTKEY_ANALOG_ROTATE_CCW:
autoRotatingAnalogCCW_ = false;
setPSPAnalog_(0, 0.0f, 0.0f);
if (down) {
autoRotatingAnalogCW_ = false;
autoRotatingAnalogCCW_ = true;
} else {
autoRotatingAnalogCCW_ = false;
setPSPAnalog_(0, 0.0f, 0.0f);
}
break;

default:
if (onVKeyUp_)
onVKeyUp_(vkey);
if (onVKey_)
onVKey_(vkey, down);
break;
}
}
Expand Down
11 changes: 5 additions & 6 deletions Core/ControlMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ControlMapper {

// Required callbacks
void SetCallbacks(
std::function<void(int)> onVKeyDown,
std::function<void(int)> onVKeyUp,
std::function<void(int, bool)> onVKey,
std::function<void(uint32_t, uint32_t)> setAllPSPButtonStates_,
std::function<void(int, bool)> setPSPButtonState,
std::function<void(int, float, float)> setPSPAnalog);

Expand All @@ -41,8 +41,7 @@ class ControlMapper {
void SetPSPAxis(int deviceId, char axis, float value, int stick);
void ProcessAnalogSpeed(const AxisInput &axis, bool opposite);

void onVKeyDown(int deviceId, int vkey);
void onVKeyUp(int deviceId, int vkey);
void onVKey(int deviceId, int vkey, bool down);

// To track mappable virtual keys. We can have as many as we want.
bool virtKeys_[VIRTKEY_COUNT]{};
Expand All @@ -59,9 +58,9 @@ class ControlMapper {
bool autoRotatingAnalogCCW_ = false;

// Callbacks
std::function<void(int, bool)> onVKey_;
std::function<void(uint32_t, uint32_t)> setAllPSPButtonStates_;
std::function<void(int, bool)> setPSPButtonState_;
std::function<void(int)> onVKeyDown_;
std::function<void(int)> onVKeyUp_;
std::function<void(int, float, float)> setPSPAnalog_;
std::function<void(int, float, float)> setRawAnalog_;
};
Expand Down
7 changes: 7 additions & 0 deletions Core/HLE/sceCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ void __CtrlButtonUp(u32 buttonBit)
ctrlCurrent.buttons &= ~buttonBit;
}

void __CtrlSetAllButtons(u32 bitsToSet, u32 bitsToClear)
{
std::lock_guard<std::mutex> guard(ctrlMutex);
ctrlCurrent.buttons &= ~(bitsToClear & CTRL_MASK_USER);
ctrlCurrent.buttons |= (bitsToSet & CTRL_MASK_USER);
}

void __CtrlSetAnalogXY(int stick, float x, float y)
{
u8 scaledX = clamp_u8((int)ceilf(x * 127.5f + 127.5f));
Expand Down
2 changes: 2 additions & 0 deletions Core/HLE/sceCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ void __CtrlShutdown();
void __CtrlButtonDown(u32 buttonBit);
// Call this whenever a button is released. Similar to __CtrlButtonDown().
void __CtrlButtonUp(u32 buttonBit);
// To be used by the new mapping code.
void __CtrlSetAllButtons(u32 bitsToSet, u32 bitsToClear);

// Call this to set the position of an analog stick, ideally when it changes.
// X and Y values should be from -1 to 1, inclusive, in a square (no need to force to a circle.)
Expand Down
4 changes: 2 additions & 2 deletions UI/ControlMappingScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ void KeyMappingNewMouseKeyDialog::axis(const AxisInput &axis) {

AnalogSetupScreen::AnalogSetupScreen(const Path &gamePath) : UIDialogScreenWithGameBackground(gamePath) {
mapper_.SetCallbacks(
[](int vkey) {},
[](int vkey) {},
[](int vkey, bool down) {},
[&](uint32_t bitsToSet, uint32_t bitsToClear) {},
[&](int button, bool down) {},
[&](int stick, float x, float y) {
analogX_[stick] = x;
Expand Down
Loading