| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerEmu/ControlGroup/AnalogStick.h" |
| 5 | |
| 6 | #include <optional> |
| 7 | |
| 8 | #include "Common/Common.h" |
| 9 | #include "Common/MathUtil.h" |
| 10 | |
| 11 | #include "InputCommon/ControllerEmu/Control/Control.h" |
| 12 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 13 | #include "InputCommon/ControllerEmu/Setting/NumericSetting.h" |
| 14 | |
| 15 | namespace ControllerEmu |
| 16 | { |
| 17 | AnalogStick::AnalogStick(const char* const name_, std::unique_ptr<StickGate>&& stick_gate) |
| 18 | : AnalogStick(name_, name_, std::move(stick_gate)) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | AnalogStick::AnalogStick(const char* const name_, const char* const ui_name_, |
| 23 | std::unique_ptr<StickGate>&& stick_gate) |
| 24 | : ReshapableInput(name_, ui_name_, GroupType::Stick), m_stick_gate(std::move(stick_gate)) |
| 25 | { |
| 26 | for (auto& named_direction : named_directions) |
| 27 | AddInput(Translatability::Translate, named_direction); |
| 28 | |
| 29 | AddInput(Translatability::Translate, _trans("Modifier")); |
| 30 | } |
| 31 | |
| 32 | AnalogStick::ReshapeData AnalogStick::GetReshapableState(bool adjusted) const |
| 33 | { |
| 34 | const ControlState y = controls[0]->GetState() - controls[1]->GetState(); |
| 35 | const ControlState x = controls[3]->GetState() - controls[2]->GetState(); |
| 36 | |
| 37 | // Return raw values. (used in UI) |
| 38 | if (!adjusted) |
| 39 | return {x, y}; |
| 40 | |
| 41 | return Reshape(x, y, GetModifierInput()->GetState()); |
| 42 | } |
| 43 | |
| 44 | AnalogStick::StateData AnalogStick::GetState() const |
| 45 | { |
| 46 | return GetReshapableState(true); |
| 47 | } |
| 48 | |
| 49 | AnalogStick::StateData AnalogStick::GetState(const InputOverrideFunction& override_func) const |
| 50 | { |
| 51 | StateData state = GetState(); |
| 52 | if (!override_func) |
| 53 | return state; |
| 54 | |
| 55 | if (const std::optional<ControlState> x_override = override_func(name, X_INPUT_OVERRIDE, state.x)) |
| 56 | { |
| 57 | state.x = *x_override; |
| 58 | } |
| 59 | |
| 60 | if (const std::optional<ControlState> y_override = override_func(name, Y_INPUT_OVERRIDE, state.y)) |
| 61 | { |
| 62 | state.y = *y_override; |
| 63 | } |
| 64 | |
| 65 | return state; |
| 66 | } |
| 67 | |
| 68 | ControlState AnalogStick::GetGateRadiusAtAngle(double ang) const |
| 69 | { |
| 70 | return m_stick_gate->GetRadiusAtAngle(ang); |
| 71 | } |
| 72 | |
| 73 | Control* AnalogStick::GetModifierInput() const |
| 74 | { |
| 75 | return controls[4].get(); |
| 76 | } |
| 77 | |
| 78 | OctagonAnalogStick::OctagonAnalogStick(const char* name_, ControlState gate_radius) |
| 79 | : OctagonAnalogStick(name_, name_, gate_radius) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | OctagonAnalogStick::OctagonAnalogStick(const char* name_, const char* ui_name_, |
| 84 | ControlState gate_radius) |
| 85 | : AnalogStick(name_, ui_name_, std::make_unique<ControllerEmu::OctagonStickGate>(1.0)) |
| 86 | { |
| 87 | AddVirtualNotchSetting(&m_virtual_notch_setting, 45); |
| 88 | |
| 89 | AddSetting( |
| 90 | &m_gate_size_setting, |
| 91 | {_trans("Gate Size"), |
| 92 | // i18n: The percent symbol. |
| 93 | _trans("%"), |
| 94 | // i18n: Refers to plastic shell of game controller (stick gate) that limits stick movements. |
| 95 | _trans("Adjusts target radius of simulated stick gate."), nullptr, |
| 96 | SettingVisibility::Advanced}, |
| 97 | gate_radius * 100, 0.01, 100); |
| 98 | } |
| 99 | |
| 100 | ControlState OctagonAnalogStick::GetVirtualNotchSize() const |
| 101 | { |
| 102 | return m_virtual_notch_setting.GetValue() * MathUtil::TAU / 360; |
| 103 | } |
| 104 | |
| 105 | ControlState OctagonAnalogStick::GetGateRadiusAtAngle(double ang) const |
| 106 | { |
| 107 | return AnalogStick::GetGateRadiusAtAngle(ang) * m_gate_size_setting.GetValue() / 100; |
| 108 | } |
| 109 | |
| 110 | } // namespace ControllerEmu |