| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerEmu/ControlGroup/MixedTriggers.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cmath> |
| 8 | #include <cstddef> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "Common/Common.h" |
| 12 | #include "Common/CommonTypes.h" |
| 13 | |
| 14 | #include "InputCommon/ControllerEmu/Control/Control.h" |
| 15 | |
| 16 | namespace ControllerEmu |
| 17 | { |
| 18 | MixedTriggers::MixedTriggers(const std::string& name_) |
| 19 | : ControlGroup(name_, GroupType::MixedTriggers) |
| 20 | { |
| 21 | AddDeadzoneSetting(&m_deadzone_setting, 25); |
| 22 | |
| 23 | AddSetting(&m_threshold_setting, |
| 24 | {_trans("Threshold"), |
| 25 | // i18n: The percent symbol. |
| 26 | _trans("%"), |
| 27 | // i18n: Refers to the "threshold" setting for pressure sensitive gamepad inputs. |
| 28 | _trans("Input strength required for activation.")}, |
| 29 | 90, 1, 100); |
| 30 | } |
| 31 | |
| 32 | void MixedTriggers::GetState(u16* const digital, const u16* bitmasks, ControlState* analog, |
| 33 | bool adjusted) const |
| 34 | { |
| 35 | const ControlState threshold = GetThreshold(); |
| 36 | ControlState deadzone = GetDeadzone(); |
| 37 | |
| 38 | // Return raw values. (used in UI) |
| 39 | if (!adjusted) |
| 40 | { |
| 41 | deadzone = 0.0; |
| 42 | } |
| 43 | |
| 44 | const int trigger_count = int(controls.size() / 2); |
| 45 | for (int i = 0; i != trigger_count; ++i) |
| 46 | { |
| 47 | const ControlState button_value = ApplyDeadzone(controls[i]->GetState(), deadzone); |
| 48 | ControlState analog_value = |
| 49 | std::min(ApplyDeadzone(controls[trigger_count + i]->GetState(), deadzone), 1.0); |
| 50 | |
| 51 | // Apply threshold: |
| 52 | if (button_value >= threshold) |
| 53 | { |
| 54 | // Fully activate analog: |
| 55 | analog_value = 1.0; |
| 56 | |
| 57 | // Activate button: |
| 58 | *digital |= bitmasks[i]; |
| 59 | } |
| 60 | |
| 61 | analog[i] = analog_value; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void MixedTriggers::GetState(u16* digital, const u16* bitmasks, ControlState* analog, |
| 66 | const InputOverrideFunction& override_func, bool adjusted) const |
| 67 | { |
| 68 | if (!override_func) |
| 69 | return GetState(digital, bitmasks, analog, adjusted); |
| 70 | |
| 71 | const ControlState threshold = GetThreshold(); |
| 72 | ControlState deadzone = GetDeadzone(); |
| 73 | |
| 74 | // Return raw values. (used in UI) |
| 75 | if (!adjusted) |
| 76 | { |
| 77 | deadzone = 0.0; |
| 78 | } |
| 79 | |
| 80 | const int trigger_count = int(controls.size() / 2); |
| 81 | for (int i = 0; i != trigger_count; ++i) |
| 82 | { |
| 83 | bool button_bool = false; |
| 84 | const ControlState button_value = ApplyDeadzone(controls[i]->GetState(), deadzone); |
| 85 | ControlState analog_value = ApplyDeadzone(controls[trigger_count + i]->GetState(), deadzone); |
| 86 | |
| 87 | // Apply threshold: |
| 88 | if (button_value >= threshold) |
| 89 | { |
| 90 | analog_value = 1.0; |
| 91 | button_bool = true; |
| 92 | } |
| 93 | |
| 94 | if (const std::optional<ControlState> button_override = |
| 95 | override_func(name, controls[i]->name, static_cast<ControlState>(button_bool))) |
| 96 | { |
| 97 | button_bool = std::lround(*button_override) > 0; |
| 98 | } |
| 99 | |
| 100 | if (const std::optional<ControlState> analog_override = |
| 101 | override_func(name, controls[trigger_count + i]->name, analog_value)) |
| 102 | { |
| 103 | analog_value = *analog_override; |
| 104 | } |
| 105 | |
| 106 | if (button_bool) |
| 107 | *digital |= bitmasks[i]; |
| 108 | analog[i] = std::min(analog_value, 1.0); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | ControlState MixedTriggers::GetDeadzone() const |
| 113 | { |
| 114 | return m_deadzone_setting.GetValue() / 100; |
| 115 | } |
| 116 | |
| 117 | ControlState MixedTriggers::GetThreshold() const |
| 118 | { |
| 119 | return m_threshold_setting.GetValue() / 100; |
| 120 | } |
| 121 | |
| 122 | size_t MixedTriggers::GetTriggerCount() const |
| 123 | { |
| 124 | return controls.size() / 2; |
| 125 | } |
| 126 | |
| 127 | } // namespace ControllerEmu |