| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerEmu/ControlGroup/Cursor.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cmath> |
| 8 | #include <limits> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "Common/Common.h" |
| 13 | #include "Common/MathUtil.h" |
| 14 | |
| 15 | #include "InputCommon/ControlReference/ControlReference.h" |
| 16 | #include "InputCommon/ControllerEmu/Control/Control.h" |
| 17 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 18 | #include "InputCommon/ControllerEmu/Setting/NumericSetting.h" |
| 19 | |
| 20 | namespace ControllerEmu |
| 21 | { |
| 22 | Cursor::Cursor(std::string name_, std::string ui_name_) |
| 23 | : ReshapableInput(std::move(name_), std::move(ui_name_), GroupType::Cursor), |
| 24 | m_last_update(Clock::now()) |
| 25 | { |
| 26 | for (auto& named_direction : named_directions) |
| 27 | AddInput(Translatability::Translate, named_direction); |
| 28 | |
| 29 | AddInput(Translatability::Translate, _trans("Hide")); |
| 30 | AddInput(Translatability::Translate, _trans("Recenter")); |
| 31 | |
| 32 | AddInput(Translatability::Translate, _trans("Relative Input Hold")); |
| 33 | |
| 34 | // Default values chosen to reach screen edges in most games including the Wii Menu. |
| 35 | |
| 36 | AddSetting(&m_vertical_offset_setting, |
| 37 | // i18n: Refers to a positional offset applied to an emulated wiimote. |
| 38 | {_trans("Vertical Offset"), |
| 39 | // i18n: The symbol/abbreviation for centimeters. |
| 40 | _trans("cm")}, |
| 41 | 10, -100, 100); |
| 42 | |
| 43 | AddSetting(&m_yaw_setting, |
| 44 | // i18n: Refers to an amount of rotational movement about the "yaw" axis. |
| 45 | {_trans("Total Yaw"), |
| 46 | // i18n: The symbol/abbreviation for degrees (unit of angular measure). |
| 47 | _trans("°"), |
| 48 | // i18n: Refers to emulated wii remote movements. |
| 49 | _trans("Total rotation about the yaw axis.")}, |
| 50 | 25, 0, 360); |
| 51 | |
| 52 | AddSetting(&m_pitch_setting, |
| 53 | // i18n: Refers to an amount of rotational movement about the "pitch" axis. |
| 54 | {_trans("Total Pitch"), |
| 55 | // i18n: The symbol/abbreviation for degrees (unit of angular measure). |
| 56 | _trans("°"), |
| 57 | // i18n: Refers to emulated wii remote movements. |
| 58 | _trans("Total rotation about the pitch axis.")}, |
| 59 | 20, 0, 360); |
| 60 | |
| 61 | AddSetting(&m_relative_setting, {_trans("Relative Input")}, false); |
| 62 | AddSetting(&m_autohide_setting, {_trans("Auto-Hide")}, false); |
| 63 | } |
| 64 | |
| 65 | Cursor::ReshapeData Cursor::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, 0.0); |
| 75 | } |
| 76 | |
| 77 | ControlState Cursor::GetGateRadiusAtAngle(double ang) const |
| 78 | { |
| 79 | return SquareStickGate(1.0).GetRadiusAtAngle(ang); |
| 80 | } |
| 81 | |
| 82 | Cursor::StateData Cursor::GetState(const bool adjusted) |
| 83 | { |
| 84 | const ReshapeData input = GetReshapableState(adjusted); |
| 85 | const StateData state = adjusted ? UpdateState(input) : StateData{input.x, input.y}; |
| 86 | return state; |
| 87 | } |
| 88 | |
| 89 | Cursor::StateData Cursor::GetState(const bool adjusted, |
| 90 | const ControllerEmu::InputOverrideFunction& override_func) |
| 91 | { |
| 92 | StateData state = GetState(adjusted); |
| 93 | if (!override_func) |
| 94 | return state; |
| 95 | |
| 96 | if (const std::optional<ControlState> x_override = override_func(name, X_INPUT_OVERRIDE, state.x)) |
| 97 | state.x = *x_override; |
| 98 | if (const std::optional<ControlState> y_override = override_func(name, Y_INPUT_OVERRIDE, state.y)) |
| 99 | state.y = *y_override; |
| 100 | |
| 101 | return state; |
| 102 | } |
| 103 | |
| 104 | Cursor::StateData Cursor::UpdateState(Cursor::ReshapeData input) |
| 105 | { |
| 106 | // TODO: Using system time is ugly. |
| 107 | // Kill this after state is moved into wiimote rather than this class. |
| 108 | const auto now = Clock::now(); |
| 109 | const auto ms_since_update = |
| 110 | std::chrono::duration_cast<std::chrono::milliseconds>(now - m_last_update).count(); |
| 111 | m_last_update = now; |
| 112 | |
| 113 | const double max_step = STEP_PER_SEC / 1000.0 * ms_since_update; |
| 114 | |
| 115 | // Relative input: |
| 116 | if (m_relative_setting.GetValue() ^ (controls[6]->GetState<bool>())) |
| 117 | { |
| 118 | // Recenter: |
| 119 | if (controls[5]->GetState<bool>()) |
| 120 | { |
| 121 | m_state.x = 0.0; |
| 122 | m_state.y = 0.0; |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | m_state.x = std::clamp(m_state.x + input.x * max_step, -1.0, 1.0); |
| 127 | m_state.y = std::clamp(m_state.y + input.y * max_step, -1.0, 1.0); |
| 128 | } |
| 129 | } |
| 130 | // Absolute input: |
| 131 | else |
| 132 | { |
| 133 | m_state.x = input.x; |
| 134 | m_state.y = input.y; |
| 135 | } |
| 136 | |
| 137 | StateData result = m_state; |
| 138 | |
| 139 | const bool autohide = m_autohide_setting.GetValue(); |
| 140 | |
| 141 | // Auto-hide timer: |
| 142 | // TODO: should Z movement reset this? |
| 143 | if (!autohide || std::abs(m_prev_result.x - result.x) > AUTO_HIDE_DEADZONE || |
| 144 | std::abs(m_prev_result.y - result.y) > AUTO_HIDE_DEADZONE) |
| 145 | { |
| 146 | m_auto_hide_timer = AUTO_HIDE_MS; |
| 147 | } |
| 148 | else if (m_auto_hide_timer) |
| 149 | { |
| 150 | m_auto_hide_timer -= std::min<int>(ms_since_update, m_auto_hide_timer); |
| 151 | } |
| 152 | |
| 153 | m_prev_result = result; |
| 154 | |
| 155 | // If auto-hide time is up, the hide button is held, or the input gate is disabled, hide the |
| 156 | // cursor. We need to check the input gate explicitly as the hide button check always returns |
| 157 | // false if the input gate is disabled (e.g. the window is not focused with background input |
| 158 | // disabled) |
| 159 | if (!m_auto_hide_timer || !ControlReference::GetInputGate() || controls[4]->GetState<bool>()) |
| 160 | { |
| 161 | result.x = std::numeric_limits<ControlState>::quiet_NaN(); |
| 162 | result.y = 0; |
| 163 | } |
| 164 | |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | ControlState Cursor::GetTotalYaw() const |
| 169 | { |
| 170 | return m_yaw_setting.GetValue() * MathUtil::TAU / 360; |
| 171 | } |
| 172 | |
| 173 | ControlState Cursor::GetTotalPitch() const |
| 174 | { |
| 175 | return m_pitch_setting.GetValue() * MathUtil::TAU / 360; |
| 176 | } |
| 177 | |
| 178 | ControlState Cursor::GetVerticalOffset() const |
| 179 | { |
| 180 | return m_vertical_offset_setting.GetValue() / 100; |
| 181 | } |
| 182 | |
| 183 | void Cursor::SetRelativeInput(bool value) |
| 184 | { |
| 185 | m_relative_setting.SetValue(value); |
| 186 | } |
| 187 | |
| 188 | bool Cursor::StateData::IsVisible() const |
| 189 | { |
| 190 | return !std::isnan(x); |
| 191 | } |
| 192 | |
| 193 | } // namespace ControllerEmu |