| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerEmu/ControlGroup/Force.h" |
| 5 | |
| 6 | #include <string> |
| 7 | |
| 8 | #include "Common/Common.h" |
| 9 | #include "Common/MathUtil.h" |
| 10 | |
| 11 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 12 | #include "InputCommon/ControllerEmu/Setting/NumericSetting.h" |
| 13 | |
| 14 | namespace ControllerEmu |
| 15 | { |
| 16 | Force::Force(const std::string& name_) : ReshapableInput(name_, name_, GroupType::Force) |
| 17 | { |
| 18 | AddInput(Translatability::Translate, _trans("Up")); |
| 19 | AddInput(Translatability::Translate, _trans("Down")); |
| 20 | AddInput(Translatability::Translate, _trans("Left")); |
| 21 | AddInput(Translatability::Translate, _trans("Right")); |
| 22 | AddInput(Translatability::Translate, _trans("Forward")); |
| 23 | AddInput(Translatability::Translate, _trans("Backward")); |
| 24 | |
| 25 | AddSetting(&m_distance_setting, |
| 26 | {_trans("Distance"), |
| 27 | // i18n: The symbol/abbreviation for centimeters. |
| 28 | _trans("cm"), |
| 29 | // i18n: Referring to emulated wii remote swing movement. |
| 30 | _trans("Distance of travel from neutral position.")}, |
| 31 | 50, 1, 100); |
| 32 | |
| 33 | // These speed settings are used to calculate a maximum jerk (change in acceleration). |
| 34 | // The calculation uses a travel distance of 1 meter. |
| 35 | // The maximum value of 40 m/s is the approximate speed of the head of a golf club. |
| 36 | // Games seem to not even properly detect motions at this speed. |
| 37 | // Values result in an exponentially increasing jerk. |
| 38 | |
| 39 | AddSetting(&m_speed_setting, |
| 40 | {_trans("Speed"), |
| 41 | // i18n: The symbol/abbreviation for meters per second. |
| 42 | _trans("m/s"), |
| 43 | // i18n: Referring to emulated wii remote swing movement. |
| 44 | _trans("Peak velocity of outward swing movements.")}, |
| 45 | 16, 1, 40); |
| 46 | |
| 47 | // "Return Speed" allows for a "slow return" that won't trigger additional actions. |
| 48 | AddSetting(&m_return_speed_setting, |
| 49 | {_trans("Return Speed"), |
| 50 | // i18n: The symbol/abbreviation for meters per second. |
| 51 | _trans("m/s"), |
| 52 | // i18n: Referring to emulated wii remote swing movement. |
| 53 | _trans("Peak velocity of movements to neutral position.")}, |
| 54 | 2, 1, 40); |
| 55 | |
| 56 | AddSetting(&m_angle_setting, |
| 57 | {_trans("Angle"), |
| 58 | // i18n: The symbol/abbreviation for degrees (unit of angular measure). |
| 59 | _trans("°"), |
| 60 | // i18n: Referring to emulated wii remote swing movement. |
| 61 | _trans("Rotation applied at extremities of swing.")}, |
| 62 | 90, 1, 180); |
| 63 | } |
| 64 | |
| 65 | Force::ReshapeData Force::GetReshapableState(bool adjusted) const |
| 66 | { |
| 67 | const ControlState y = controls[0]->GetState() - controls[1]->GetState(); |
| 68 | const ControlState x = controls[3]->GetState() - controls[2]->GetState(); |
| 69 | |
| 70 | // Return raw values. (used in UI) |
| 71 | if (!adjusted) |
| 72 | return {x, y}; |
| 73 | |
| 74 | return Reshape(x, y); |
| 75 | } |
| 76 | |
| 77 | Force::StateData Force::GetState(bool adjusted) const |
| 78 | { |
| 79 | const auto state = GetReshapableState(adjusted); |
| 80 | ControlState z = controls[4]->GetState() - controls[5]->GetState(); |
| 81 | |
| 82 | if (adjusted) |
| 83 | { |
| 84 | // Apply deadzone to z and scale. |
| 85 | z = ApplyDeadzone(z, GetDeadzonePercentage()) * GetMaxDistance(); |
| 86 | } |
| 87 | |
| 88 | return {float(state.x), float(state.y), float(z)}; |
| 89 | } |
| 90 | |
| 91 | ControlState Force::GetGateRadiusAtAngle(double) const |
| 92 | { |
| 93 | // Just a circle of the configured distance: |
| 94 | return GetMaxDistance(); |
| 95 | } |
| 96 | |
| 97 | ControlState Force::GetSpeed() const |
| 98 | { |
| 99 | return m_speed_setting.GetValue(); |
| 100 | } |
| 101 | |
| 102 | ControlState Force::GetReturnSpeed() const |
| 103 | { |
| 104 | return m_return_speed_setting.GetValue(); |
| 105 | } |
| 106 | |
| 107 | ControlState Force::GetTwistAngle() const |
| 108 | { |
| 109 | return m_angle_setting.GetValue() * MathUtil::TAU / 360; |
| 110 | } |
| 111 | |
| 112 | ControlState Force::GetMaxDistance() const |
| 113 | { |
| 114 | return m_distance_setting.GetValue() / 100; |
| 115 | } |
| 116 | |
| 117 | ControlState Force::GetDefaultInputRadiusAtAngle(double) const |
| 118 | { |
| 119 | // Just a circle of radius 1.0. |
| 120 | return 1.0; |
| 121 | } |
| 122 | |
| 123 | Shake::Shake(const std::string& name_, ControlState default_intensity_scale) |
| 124 | : ControlGroup(name_, name_, GroupType::Shake) |
| 125 | { |
| 126 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 127 | AddInput(Translatability::Translate, _trans("X")); |
| 128 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 129 | AddInput(Translatability::Translate, _trans("Y")); |
| 130 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 131 | AddInput(Translatability::Translate, _trans("Z")); |
| 132 | |
| 133 | AddDeadzoneSetting(&m_deadzone_setting, 50); |
| 134 | |
| 135 | // Total travel distance in centimeters. |
| 136 | // Negative values can be used to reverse the initial direction of movement. |
| 137 | AddSetting(&m_intensity_setting, |
| 138 | // i18n: Refers to the intensity of shaking an emulated wiimote. |
| 139 | {_trans("Intensity"), |
| 140 | // i18n: The symbol/abbreviation for centimeters. |
| 141 | _trans("cm"), |
| 142 | // i18n: Referring to emulated wii remote movement. |
| 143 | _trans("Total travel distance.")}, |
| 144 | 10 * default_intensity_scale, -50, 50); |
| 145 | |
| 146 | // Approximate number of up/down movements in one second. |
| 147 | AddSetting(&m_frequency_setting, |
| 148 | // i18n: Refers to a number of actions per second in Hz. |
| 149 | {_trans("Frequency"), |
| 150 | // i18n: The symbol/abbreviation for hertz (cycles per second). |
| 151 | _trans("Hz"), |
| 152 | // i18n: Referring to emulated wii remote movement. |
| 153 | _trans("Number of shakes per second.")}, |
| 154 | 6, 1, 20); |
| 155 | } |
| 156 | |
| 157 | Shake::StateData Shake::GetState(bool adjusted) const |
| 158 | { |
| 159 | const float x = controls[0]->GetState(); |
| 160 | const float y = controls[1]->GetState(); |
| 161 | const float z = controls[2]->GetState(); |
| 162 | |
| 163 | StateData result = {x, y, z}; |
| 164 | |
| 165 | // FYI: Unadjusted values are used in UI. |
| 166 | if (adjusted) |
| 167 | for (auto& c : result.data) |
| 168 | c = ApplyDeadzone(c, GetDeadzone()); |
| 169 | |
| 170 | return result; |
| 171 | } |
| 172 | |
| 173 | ControlState Shake::GetDeadzone() const |
| 174 | { |
| 175 | return m_deadzone_setting.GetValue() / 100; |
| 176 | } |
| 177 | |
| 178 | ControlState Shake::GetIntensity() const |
| 179 | { |
| 180 | return m_intensity_setting.GetValue() / 100; |
| 181 | } |
| 182 | |
| 183 | ControlState Shake::GetFrequency() const |
| 184 | { |
| 185 | return m_frequency_setting.GetValue(); |
| 186 | } |
| 187 | |
| 188 | } // namespace ControllerEmu |