UI: Dry out code and check bounds to be safe.

This commit is contained in:
Unknown W. Brackets committed 2022-11-27 15:40:55 -08:00
1 parent 89aeb2484e
commit 247848891e
1 file changed
+21 -37
+21 -37
View File
@@ -588,11 +588,21 @@ void PSPCustomStick::Touch(const TouchInput &input) {
}
void PSPCustomStick::ProcessTouch(float x, float y, bool down) {
static const int buttons[16] = {0, CTRL_LTRIGGER, CTRL_RTRIGGER, CTRL_SQUARE, CTRL_TRIANGLE, CTRL_CIRCLE, CTRL_CROSS, CTRL_UP, CTRL_DOWN, CTRL_LEFT, CTRL_RIGHT, CTRL_START, CTRL_SELECT};
static const int buttons[] = {0, CTRL_LTRIGGER, CTRL_RTRIGGER, CTRL_SQUARE, CTRL_TRIANGLE, CTRL_CIRCLE, CTRL_CROSS, CTRL_UP, CTRL_DOWN, CTRL_LEFT, CTRL_RIGHT, CTRL_START, CTRL_SELECT};
u32 press = 0;
u32 release = 0;
auto toggle = [&](int config, bool simpleCheck, bool diagCheck = true) {
if (config <= 0 || (size_t)config >= ARRAY_SIZE(buttons))
return;
if (simpleCheck && (!g_Config.bRightAnalogDisableDiagonal || diagCheck))
press |= buttons[config];
else
release |= buttons[config];
};
if (down && centerX_ >= 0.0f) {
float inv_stick_size = 1.0f / (stick_size_ * scale_);
@@ -602,47 +612,21 @@ void PSPCustomStick::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));
if (g_Config.iRightAnalogRight != 0) {
if (dx > 0.5f && (!g_Config.bRightAnalogDisableDiagonal || fabs(dx) > fabs(dy)))
press |= buttons[g_Config.iRightAnalogRight];
else
release |= buttons[g_Config.iRightAnalogRight];
}
if (g_Config.iRightAnalogLeft != 0) {
if (dx < -0.5f && (!g_Config.bRightAnalogDisableDiagonal || fabs(dx) > fabs(dy)))
press |= buttons[g_Config.iRightAnalogLeft];
else
release |= buttons[g_Config.iRightAnalogLeft];
}
if (g_Config.iRightAnalogUp != 0) {
if (dy < -0.5f && (!g_Config.bRightAnalogDisableDiagonal || fabs(dx) <= fabs(dy)))
press |= buttons[g_Config.iRightAnalogUp];
else
release |= buttons[g_Config.iRightAnalogUp];
}
if (g_Config.iRightAnalogDown != 0) {
if (dy > 0.5f && (!g_Config.bRightAnalogDisableDiagonal || fabs(dx) <= fabs(dy)))
press |= buttons[g_Config.iRightAnalogDown];
else
release |= buttons[g_Config.iRightAnalogDown];
}
if (g_Config.iRightAnalogPress != 0)
press |= buttons[g_Config.iRightAnalogPress];
toggle(g_Config.iRightAnalogRight, dx > 0.5f, fabs(dx) > fabs(dy));
toggle(g_Config.iRightAnalogLeft, dx < -0.5f, fabs(dx) > fabs(dy));
toggle(g_Config.iRightAnalogUp, dy < -0.5f, fabs(dx) <= fabs(dy));
toggle(g_Config.iRightAnalogDown, dy > 0.5f, fabs(dx) <= fabs(dy));
toggle(g_Config.iRightAnalogPress, true);
posX_ = dx;
posY_ = dy;
} else {
if (g_Config.iRightAnalogUp != 0)
release |= buttons[g_Config.iRightAnalogUp];
if (g_Config.iRightAnalogDown != 0)
release |= buttons[g_Config.iRightAnalogDown];
if (g_Config.iRightAnalogLeft != 0)
release |= buttons[g_Config.iRightAnalogLeft];
if (g_Config.iRightAnalogRight != 0)
release |= buttons[g_Config.iRightAnalogRight];
if (g_Config.iRightAnalogPress != 0)
release |= buttons[g_Config.iRightAnalogPress];
toggle(g_Config.iRightAnalogRight, false);
toggle(g_Config.iRightAnalogLeft, false);
toggle(g_Config.iRightAnalogUp, false);
toggle(g_Config.iRightAnalogDown, false);
toggle(g_Config.iRightAnalogPress, false);
posX_ = 0.0f;
posY_ = 0.0f;