Skip to content

Commit

Permalink
Merge pull request #18597 from hrydgard/combo-key-behavior
Browse files Browse the repository at this point in the history
Combo key: Only trigger when keys are pressed in the mapped order
  • Loading branch information
hrydgard committed Dec 22, 2023
2 parents cf81ae1 + d21b185 commit 33c0052
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
49 changes: 42 additions & 7 deletions Core/ControlMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,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;
std::vector<MultiInputMapping> inputMappings;
Expand Down Expand Up @@ -297,9 +297,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;
}
Expand Down Expand Up @@ -338,12 +350,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;
}
Expand Down Expand Up @@ -484,6 +507,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();

Expand All @@ -499,15 +534,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);
}
Expand Down
2 changes: 2 additions & 0 deletions Core/ControlMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class ControlMapper {
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.
Expand Down

0 comments on commit 33c0052

Please sign in to comment.