From 81923034f63b3dfb7b8f609310177671f39c0d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 6 May 2023 16:38:46 +0200 Subject: [PATCH 1/2] ControlMapper: Keep track of when inputs were triggered --- Core/ControlMapper.cpp | 22 +++++++++++++++++----- Core/ControlMapper.h | 2 ++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Core/ControlMapper.cpp b/Core/ControlMapper.cpp index 629b1eaca3..97a088fbc7 100644 --- a/Core/ControlMapper.cpp +++ b/Core/ControlMapper.cpp @@ -265,7 +265,7 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping, double no case ROTATION_LOCKED_VERTICAL180: rotations = 3; break; } - // For the PSP's button inputs, we just go through and put the flags together. + // For the PSP's digital button inputs, we just go through and put the flags together. uint32_t buttonMask = 0; uint32_t changedButtonMask = 0; for (int i = 0; i < 32; i++) { @@ -476,6 +476,18 @@ void ControlMapper::ToggleSwapAxes() { UpdateAnalogOutput(1); } +void ControlMapper::UpdateCurInputAxis(const InputMapping &mapping, float value, double timestamp) { + InputSample &input = curInput_[mapping]; + input.value = value; + if (value > GetDeviceAxisThreshold(mapping.deviceId)) { + if (input.timestamp == 0.0) { + input.timestamp = time_now_d(); + } + } else { + input.timestamp = 0.0; + } +} + void ControlMapper::Axis(const AxisInput *axes, size_t count) { double now = time_now_d(); @@ -489,15 +501,15 @@ void ControlMapper::Axis(const AxisInput *axes, size_t count) { if (axis.value >= 0.0f) { InputMapping mapping(axis.deviceId, axis.axisId, 1); InputMapping opposite(axis.deviceId, axis.axisId, -1); - curInput_[mapping] = { axis.value, now }; - curInput_[opposite] = { 0.0f, now }; + UpdateCurInputAxis(mapping, axis.value, now); + UpdateCurInputAxis(opposite, 0.0f, now); UpdatePSPState(mapping, now); UpdatePSPState(opposite, now); } else if (axis.value < 0.0f) { InputMapping mapping(axis.deviceId, axis.axisId, -1); InputMapping opposite(axis.deviceId, axis.axisId, 1); - curInput_[mapping] = { -axis.value, now }; - curInput_[opposite] = { 0.0f, now }; + UpdateCurInputAxis(mapping, -axis.value, now); + UpdateCurInputAxis(opposite, 0.0f, now); UpdatePSPState(mapping, now); UpdatePSPState(opposite, now); } diff --git a/Core/ControlMapper.h b/Core/ControlMapper.h index 24c70a0527..60d975070e 100644 --- a/Core/ControlMapper.h +++ b/Core/ControlMapper.h @@ -58,6 +58,8 @@ private: void onVKey(int vkey, bool down); void onVKeyAnalog(int deviceId, int vkey, float value); + void UpdateCurInputAxis(const InputMapping &mapping, float value, double timestamp); + // To track mappable virtual keys. We can have as many as we want. float virtKeys_[VIRTKEY_COUNT]{}; bool virtKeyOn_[VIRTKEY_COUNT]{}; // Track boolean output separaately since thresholds may differ. From d21b185b5c842d78bb821238384f8f1607e40f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 6 May 2023 16:46:32 +0200 Subject: [PATCH 2/2] Combo keys: Only trigger if keys are pressed in the same order --- Core/ControlMapper.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Core/ControlMapper.cpp b/Core/ControlMapper.cpp index 97a088fbc7..7c34bc09a5 100644 --- a/Core/ControlMapper.cpp +++ b/Core/ControlMapper.cpp @@ -294,9 +294,21 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping, double no } // Check if all inputs are "on". bool all = true; + double curTime = 0.0; for (auto mapping : multiMapping.mappings) { auto iter = curInput_.find(mapping); - bool down = iter != curInput_.end() && iter->second.value > GetDeviceAxisThreshold(iter->first.deviceId); + if (iter == curInput_.end()) { + all = false; + continue; + } + // Stop reverse ordering from triggering. + if (iter->second.timestamp < curTime) { + all = false; + break; + } else { + curTime = iter->second.timestamp; + } + bool down = iter->second.value > GetDeviceAxisThreshold(iter->first.deviceId); if (!down) all = false; } @@ -336,12 +348,23 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping, double no } float product = 1.0f; // We multiply the various inputs in a combo mapping with each other. + double curTime = 0.0; for (auto mapping : multiMapping.mappings) { auto iter = curInput_.find(mapping); + if (iter != curInput_.end()) { + // Stop reverse ordering from triggering. + if (iter->second.timestamp < curTime) { + product = 0.0f; + break; + } else { + curTime = iter->second.timestamp; + } + if (mapping.IsAxis()) { threshold = GetDeviceAxisThreshold(iter->first.deviceId); - product *= MapAxisValue(iter->second.value, idForMapping, mapping, changedMapping, &touchedByMapping); + float value = MapAxisValue(iter->second.value, idForMapping, mapping, changedMapping, &touchedByMapping); + product *= value; } else { product *= iter->second.value; }