| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/TAS/WiiTASInputWindow.h" |
| 5 | |
| 6 | #include <cmath> |
| 7 | |
| 8 | #include <QCheckBox> |
| 9 | #include <QGridLayout> |
| 10 | #include <QGroupBox> |
| 11 | #include <QHBoxLayout> |
| 12 | #include <QSpacerItem> |
| 13 | #include <QSpinBox> |
| 14 | #include <QStyle> |
| 15 | #include <QVBoxLayout> |
| 16 | |
| 17 | #include "Common/CommonTypes.h" |
| 18 | #include "Common/FileUtil.h" |
| 19 | #include "Common/MathUtil.h" |
| 20 | |
| 21 | #include "Core/Core.h" |
| 22 | #include "Core/HW/Wiimote.h" |
| 23 | #include "Core/HW/WiimoteEmu/Extension/Classic.h" |
| 24 | #include "Core/HW/WiimoteEmu/Extension/Extension.h" |
| 25 | #include "Core/HW/WiimoteEmu/Extension/Nunchuk.h" |
| 26 | #include "Core/HW/WiimoteEmu/MotionPlus.h" |
| 27 | #include "Core/HW/WiimoteEmu/WiimoteEmu.h" |
| 28 | #include "Core/System.h" |
| 29 | |
| 30 | #include "DolphinQt/QtUtils/AspectRatioWidget.h" |
| 31 | #include "DolphinQt/QtUtils/QueueOnObject.h" |
| 32 | #include "DolphinQt/TAS/IRWidget.h" |
| 33 | #include "DolphinQt/TAS/TASCheckBox.h" |
| 34 | #include "DolphinQt/TAS/TASSpinBox.h" |
| 35 | |
| 36 | #include "InputCommon/ControllerEmu/ControlGroup/Attachments.h" |
| 37 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 38 | #include "InputCommon/ControllerEmu/StickGate.h" |
| 39 | #include "InputCommon/InputConfig.h" |
| 40 | |
| 41 | using namespace WiimoteCommon; |
| 42 | |
| 43 | WiiTASInputWindow::WiiTASInputWindow(QWidget* parent, int num) : TASInputWindow(parent), m_num(num) |
| 44 | { |
| 45 | const QKeySequence ir_x_shortcut_key_sequence = QKeySequence(Qt::ALT | Qt::Key_X); |
| 46 | const QKeySequence ir_y_shortcut_key_sequence = QKeySequence(Qt::ALT | Qt::Key_C); |
| 47 | |
| 48 | auto* ir_box = new QGroupBox( |
| 49 | QStringLiteral("%1 (%2/%3)") |
| 50 | .arg(tr("IR"), ir_x_shortcut_key_sequence.toString(QKeySequence::NativeText), |
| 51 | ir_y_shortcut_key_sequence.toString(QKeySequence::NativeText))); |
| 52 | |
| 53 | const int ir_x_center = static_cast<int>(std::round(IRWidget::IR_MAX_X / 2.)); |
| 54 | const int ir_y_center = static_cast<int>(std::round(IRWidget::IR_MAX_Y / 2.)); |
| 55 | |
| 56 | auto* box_layout = new QGridLayout; |
| 57 | m_ir_x_value = CreateSliderValuePair( |
| 58 | WiimoteEmu::Wiimote::IR_GROUP, ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE, |
| 59 | &m_wiimote_overrider, box_layout, ir_x_center, ir_x_center, IRWidget::IR_MIN_X, |
| 60 | IRWidget::IR_MAX_X, ir_x_shortcut_key_sequence, Qt::Horizontal, ir_box); |
| 61 | |
| 62 | m_ir_y_value = CreateSliderValuePair( |
| 63 | WiimoteEmu::Wiimote::IR_GROUP, ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE, |
| 64 | &m_wiimote_overrider, box_layout, ir_y_center, ir_y_center, IRWidget::IR_MIN_Y, |
| 65 | IRWidget::IR_MAX_Y, ir_y_shortcut_key_sequence, Qt::Vertical, ir_box); |
| 66 | m_ir_y_value->setMaximumWidth(60); |
| 67 | |
| 68 | auto* visual = new IRWidget(this); |
| 69 | visual->SetX(ir_x_center); |
| 70 | visual->SetY(ir_y_center); |
| 71 | |
| 72 | connect(m_ir_x_value, &QSpinBox::valueChanged, visual, &IRWidget::SetX); |
| 73 | connect(m_ir_y_value, &QSpinBox::valueChanged, visual, &IRWidget::SetY); |
| 74 | connect(visual, &IRWidget::ChangedX, m_ir_x_value, &QSpinBox::setValue); |
| 75 | connect(visual, &IRWidget::ChangedY, m_ir_y_value, &QSpinBox::setValue); |
| 76 | |
| 77 | auto* visual_ar = new AspectRatioWidget(visual, IRWidget::IR_MAX_X, IRWidget::IR_MAX_Y); |
| 78 | |
| 79 | // This is done to prevent the stick widget from stretching |
| 80 | box_layout->addItem(new QSpacerItem(0, 0), 2, 1); |
| 81 | box_layout->addWidget(visual_ar, 1, 1); |
| 82 | ir_box->setLayout(box_layout); |
| 83 | |
| 84 | m_ir_box = new AspectRatioWidget(ir_box, 1, 1.02f); |
| 85 | |
| 86 | m_nunchuk_stick_box = new AspectRatioWidget( |
| 87 | CreateStickInputs(tr("Nunchuk Stick"), WiimoteEmu::Nunchuk::STICK_GROUP, &m_nunchuk_overrider, |
| 88 | 0, 0, 255, 255, Qt::Key_F, Qt::Key_G), |
| 89 | 1, 1.02f); |
| 90 | |
| 91 | m_classic_left_stick_box = new AspectRatioWidget( |
| 92 | CreateStickInputs(tr("Left Stick"), WiimoteEmu::Classic::LEFT_STICK_GROUP, |
| 93 | &m_classic_overrider, 0, 0, 63, 63, Qt::Key_F, Qt::Key_G), |
| 94 | 1, 1.02f); |
| 95 | |
| 96 | m_classic_right_stick_box = new AspectRatioWidget( |
| 97 | CreateStickInputs(tr("Right Stick"), WiimoteEmu::Classic::RIGHT_STICK_GROUP, |
| 98 | &m_classic_overrider, 0, 0, 31, 31, Qt::Key_Q, Qt::Key_W), |
| 99 | 1, 1.02f); |
| 100 | |
| 101 | auto* top_layout = new QHBoxLayout; |
| 102 | top_layout->addWidget(m_ir_box); |
| 103 | top_layout->addWidget(m_nunchuk_stick_box); |
| 104 | top_layout->addWidget(m_classic_left_stick_box); |
| 105 | top_layout->addWidget(m_classic_right_stick_box); |
| 106 | |
| 107 | m_remote_accelerometer_box = new QGroupBox(tr("Wii Remote Accelerometer")); |
| 108 | |
| 109 | constexpr u16 ACCEL_ZERO_G = WiimoteEmu::Wiimote::ACCEL_ZERO_G << 2; |
| 110 | constexpr u16 ACCEL_ONE_G = WiimoteEmu::Wiimote::ACCEL_ONE_G << 2; |
| 111 | constexpr u16 ACCEL_MIN = 0; |
| 112 | constexpr u16 ACCEL_MAX = (1 << 10) - 1; |
| 113 | constexpr double ACCEL_SCALE = (ACCEL_ONE_G - ACCEL_ZERO_G) / MathUtil::GRAVITY_ACCELERATION; |
| 114 | |
| 115 | auto* remote_accelerometer_x_layout = |
| 116 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 117 | CreateSliderValuePairLayout(tr("X"), WiimoteEmu::Wiimote::ACCELEROMETER_GROUP, |
| 118 | ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE, |
| 119 | &m_wiimote_overrider, ACCEL_ZERO_G, ACCEL_ZERO_G, ACCEL_MIN, |
| 120 | ACCEL_MAX, Qt::Key_Q, m_remote_accelerometer_box, ACCEL_SCALE); |
| 121 | auto* remote_accelerometer_y_layout = |
| 122 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 123 | CreateSliderValuePairLayout(tr("Y"), WiimoteEmu::Wiimote::ACCELEROMETER_GROUP, |
| 124 | ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE, |
| 125 | &m_wiimote_overrider, ACCEL_ZERO_G, ACCEL_ZERO_G, ACCEL_MIN, |
| 126 | ACCEL_MAX, Qt::Key_W, m_remote_accelerometer_box, ACCEL_SCALE); |
| 127 | auto* remote_accelerometer_z_layout = |
| 128 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 129 | CreateSliderValuePairLayout(tr("Z"), WiimoteEmu::Wiimote::ACCELEROMETER_GROUP, |
| 130 | ControllerEmu::ReshapableInput::Z_INPUT_OVERRIDE, |
| 131 | &m_wiimote_overrider, ACCEL_ZERO_G, ACCEL_ONE_G, ACCEL_MIN, |
| 132 | ACCEL_MAX, Qt::Key_E, m_remote_accelerometer_box, ACCEL_SCALE); |
| 133 | |
| 134 | auto* remote_accelerometer_layout = new QVBoxLayout; |
| 135 | remote_accelerometer_layout->addLayout(remote_accelerometer_x_layout); |
| 136 | remote_accelerometer_layout->addLayout(remote_accelerometer_y_layout); |
| 137 | remote_accelerometer_layout->addLayout(remote_accelerometer_z_layout); |
| 138 | m_remote_accelerometer_box->setLayout(remote_accelerometer_layout); |
| 139 | |
| 140 | m_remote_gyroscope_box = new QGroupBox(tr("Wii Remote Gyroscope")); |
| 141 | |
| 142 | // MotionPlus can report values using either a slow scale (greater precision) or a fast scale |
| 143 | // (greater range). To ensure the user can select every possible value, TAS input uses the |
| 144 | // precision of the slow scale and the range of the fast scale. This does mean TAS input has more |
| 145 | // selectable values than MotionPlus has reportable values, but that's not too big of a problem. |
| 146 | constexpr double GYRO_STRETCH = |
| 147 | static_cast<double>(WiimoteEmu::MotionPlus::CALIBRATION_FAST_SCALE_DEGREES) / |
| 148 | WiimoteEmu::MotionPlus::CALIBRATION_SLOW_SCALE_DEGREES; |
| 149 | |
| 150 | constexpr u32 GYRO_MIN = 0; |
| 151 | constexpr u32 GYRO_MAX = WiimoteEmu::MotionPlus::MAX_VALUE * GYRO_STRETCH; |
| 152 | constexpr u32 GYRO_ZERO = WiimoteEmu::MotionPlus::ZERO_VALUE * GYRO_STRETCH; |
| 153 | constexpr double GYRO_SCALE = GYRO_MAX / 2 / WiimoteEmu::MotionPlus::FAST_MAX_RAD_PER_SEC; |
| 154 | |
| 155 | auto* remote_gyroscope_x_layout = |
| 156 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 157 | CreateSliderValuePairLayout(tr("X"), WiimoteEmu::Wiimote::GYROSCOPE_GROUP, |
| 158 | ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE, |
| 159 | &m_wiimote_overrider, GYRO_ZERO, GYRO_ZERO, GYRO_MIN, GYRO_MAX, |
| 160 | Qt::Key_R, m_remote_gyroscope_box, GYRO_SCALE); |
| 161 | auto* remote_gyroscope_y_layout = |
| 162 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 163 | CreateSliderValuePairLayout(tr("Y"), WiimoteEmu::Wiimote::GYROSCOPE_GROUP, |
| 164 | ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE, |
| 165 | &m_wiimote_overrider, GYRO_ZERO, GYRO_ZERO, GYRO_MIN, GYRO_MAX, |
| 166 | Qt::Key_T, m_remote_gyroscope_box, GYRO_SCALE); |
| 167 | auto* remote_gyroscope_z_layout = |
| 168 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 169 | CreateSliderValuePairLayout(tr("Z"), WiimoteEmu::Wiimote::GYROSCOPE_GROUP, |
| 170 | ControllerEmu::ReshapableInput::Z_INPUT_OVERRIDE, |
| 171 | &m_wiimote_overrider, GYRO_ZERO, GYRO_ZERO, GYRO_MIN, GYRO_MAX, |
| 172 | Qt::Key_Y, m_remote_gyroscope_box, GYRO_SCALE); |
| 173 | |
| 174 | auto* remote_gyroscope_layout = new QVBoxLayout; |
| 175 | remote_gyroscope_layout->addLayout(remote_gyroscope_x_layout); |
| 176 | remote_gyroscope_layout->addLayout(remote_gyroscope_y_layout); |
| 177 | remote_gyroscope_layout->addLayout(remote_gyroscope_z_layout); |
| 178 | m_remote_gyroscope_box->setLayout(remote_gyroscope_layout); |
| 179 | |
| 180 | m_nunchuk_accelerometer_box = new QGroupBox(tr("Nunchuk Accelerometer")); |
| 181 | |
| 182 | auto* nunchuk_accelerometer_x_layout = |
| 183 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 184 | CreateSliderValuePairLayout(tr("X"), WiimoteEmu::Nunchuk::ACCELEROMETER_GROUP, |
| 185 | ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE, |
| 186 | &m_nunchuk_overrider, ACCEL_ZERO_G, ACCEL_ZERO_G, ACCEL_MIN, |
| 187 | ACCEL_MAX, Qt::Key_I, m_nunchuk_accelerometer_box); |
| 188 | auto* nunchuk_accelerometer_y_layout = |
| 189 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 190 | CreateSliderValuePairLayout(tr("Y"), WiimoteEmu::Nunchuk::ACCELEROMETER_GROUP, |
| 191 | ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE, |
| 192 | &m_nunchuk_overrider, ACCEL_ZERO_G, ACCEL_ZERO_G, ACCEL_MIN, |
| 193 | ACCEL_MAX, Qt::Key_O, m_nunchuk_accelerometer_box); |
| 194 | auto* nunchuk_accelerometer_z_layout = |
| 195 | // i18n: Refers to a 3D axis (used when mapping motion controls) |
| 196 | CreateSliderValuePairLayout(tr("Z"), WiimoteEmu::Nunchuk::ACCELEROMETER_GROUP, |
| 197 | ControllerEmu::ReshapableInput::Z_INPUT_OVERRIDE, |
| 198 | &m_nunchuk_overrider, ACCEL_ZERO_G, ACCEL_ONE_G, ACCEL_MIN, |
| 199 | ACCEL_MAX, Qt::Key_P, m_nunchuk_accelerometer_box); |
| 200 | |
| 201 | auto* nunchuk_accelerometer_layout = new QVBoxLayout; |
| 202 | nunchuk_accelerometer_layout->addLayout(nunchuk_accelerometer_x_layout); |
| 203 | nunchuk_accelerometer_layout->addLayout(nunchuk_accelerometer_y_layout); |
| 204 | nunchuk_accelerometer_layout->addLayout(nunchuk_accelerometer_z_layout); |
| 205 | m_nunchuk_accelerometer_box->setLayout(nunchuk_accelerometer_layout); |
| 206 | |
| 207 | m_triggers_box = new QGroupBox(tr("Triggers")); |
| 208 | auto* l_trigger_layout = CreateSliderValuePairLayout( |
| 209 | tr("Left"), WiimoteEmu::Classic::TRIGGERS_GROUP, WiimoteEmu::Classic::L_ANALOG, |
| 210 | &m_classic_overrider, 0, 0, 0, 31, Qt::Key_N, m_triggers_box); |
| 211 | auto* r_trigger_layout = CreateSliderValuePairLayout( |
| 212 | tr("Right"), WiimoteEmu::Classic::TRIGGERS_GROUP, WiimoteEmu::Classic::R_ANALOG, |
| 213 | &m_classic_overrider, 0, 0, 0, 31, Qt::Key_M, m_triggers_box); |
| 214 | |
| 215 | auto* triggers_layout = new QVBoxLayout; |
| 216 | triggers_layout->addLayout(l_trigger_layout); |
| 217 | triggers_layout->addLayout(r_trigger_layout); |
| 218 | m_triggers_box->setLayout(triggers_layout); |
| 219 | |
| 220 | m_a_button = CreateButton(QStringLiteral("&A"), WiimoteEmu::Wiimote::BUTTONS_GROUP, |
| 221 | WiimoteEmu::Wiimote::A_BUTTON, &m_wiimote_overrider); |
| 222 | m_b_button = CreateButton(QStringLiteral("&B"), WiimoteEmu::Wiimote::BUTTONS_GROUP, |
| 223 | WiimoteEmu::Wiimote::B_BUTTON, &m_wiimote_overrider); |
| 224 | m_1_button = CreateButton(QStringLiteral("&1"), WiimoteEmu::Wiimote::BUTTONS_GROUP, |
| 225 | WiimoteEmu::Wiimote::ONE_BUTTON, &m_wiimote_overrider); |
| 226 | m_2_button = CreateButton(QStringLiteral("&2"), WiimoteEmu::Wiimote::BUTTONS_GROUP, |
| 227 | WiimoteEmu::Wiimote::TWO_BUTTON, &m_wiimote_overrider); |
| 228 | m_plus_button = CreateButton(QStringLiteral("&+"), WiimoteEmu::Wiimote::BUTTONS_GROUP, |
| 229 | WiimoteEmu::Wiimote::PLUS_BUTTON, &m_wiimote_overrider); |
| 230 | m_minus_button = CreateButton(QStringLiteral("&-"), WiimoteEmu::Wiimote::BUTTONS_GROUP, |
| 231 | WiimoteEmu::Wiimote::MINUS_BUTTON, &m_wiimote_overrider); |
| 232 | m_home_button = CreateButton(QStringLiteral("&HOME"), WiimoteEmu::Wiimote::BUTTONS_GROUP, |
| 233 | WiimoteEmu::Wiimote::HOME_BUTTON, &m_wiimote_overrider); |
| 234 | |
| 235 | m_left_button = CreateButton(QStringLiteral("&Left"), WiimoteEmu::Wiimote::DPAD_GROUP, |
| 236 | DIRECTION_LEFT, &m_wiimote_overrider); |
| 237 | m_up_button = CreateButton(QStringLiteral("&Up"), WiimoteEmu::Wiimote::DPAD_GROUP, DIRECTION_UP, |
| 238 | &m_wiimote_overrider); |
| 239 | m_down_button = CreateButton(QStringLiteral("&Down"), WiimoteEmu::Wiimote::DPAD_GROUP, |
| 240 | DIRECTION_DOWN, &m_wiimote_overrider); |
| 241 | m_right_button = CreateButton(QStringLiteral("&Right"), WiimoteEmu::Wiimote::DPAD_GROUP, |
| 242 | DIRECTION_RIGHT, &m_wiimote_overrider); |
| 243 | |
| 244 | m_c_button = CreateButton(QStringLiteral("&C"), WiimoteEmu::Nunchuk::BUTTONS_GROUP, |
| 245 | WiimoteEmu::Nunchuk::C_BUTTON, &m_nunchuk_overrider); |
| 246 | m_z_button = CreateButton(QStringLiteral("&Z"), WiimoteEmu::Nunchuk::BUTTONS_GROUP, |
| 247 | WiimoteEmu::Nunchuk::Z_BUTTON, &m_nunchuk_overrider); |
| 248 | |
| 249 | auto* buttons_layout = new QGridLayout; |
| 250 | buttons_layout->addWidget(m_a_button, 0, 0); |
| 251 | buttons_layout->addWidget(m_b_button, 0, 1); |
| 252 | buttons_layout->addWidget(m_1_button, 0, 2); |
| 253 | buttons_layout->addWidget(m_2_button, 0, 3); |
| 254 | buttons_layout->addWidget(m_plus_button, 0, 4); |
| 255 | buttons_layout->addWidget(m_minus_button, 0, 5); |
| 256 | |
| 257 | buttons_layout->addWidget(m_home_button, 1, 0); |
| 258 | buttons_layout->addWidget(m_left_button, 1, 1); |
| 259 | buttons_layout->addWidget(m_up_button, 1, 2); |
| 260 | buttons_layout->addWidget(m_down_button, 1, 3); |
| 261 | buttons_layout->addWidget(m_right_button, 1, 4); |
| 262 | |
| 263 | buttons_layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding), 0, 7); |
| 264 | |
| 265 | m_remote_buttons_box = new QGroupBox(tr("Wii Remote Buttons")); |
| 266 | m_remote_buttons_box->setLayout(buttons_layout); |
| 267 | |
| 268 | auto* nunchuk_buttons_layout = new QHBoxLayout; |
| 269 | nunchuk_buttons_layout->addWidget(m_c_button); |
| 270 | nunchuk_buttons_layout->addWidget(m_z_button); |
| 271 | nunchuk_buttons_layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding)); |
| 272 | |
| 273 | m_nunchuk_buttons_box = new QGroupBox(tr("Nunchuk Buttons")); |
| 274 | m_nunchuk_buttons_box->setLayout(nunchuk_buttons_layout); |
| 275 | |
| 276 | m_classic_a_button = CreateButton(QStringLiteral("&A"), WiimoteEmu::Classic::BUTTONS_GROUP, |
| 277 | WiimoteEmu::Classic::A_BUTTON, &m_classic_overrider); |
| 278 | m_classic_b_button = CreateButton(QStringLiteral("&B"), WiimoteEmu::Classic::BUTTONS_GROUP, |
| 279 | WiimoteEmu::Classic::B_BUTTON, &m_classic_overrider); |
| 280 | m_classic_x_button = CreateButton(QStringLiteral("&X"), WiimoteEmu::Classic::BUTTONS_GROUP, |
| 281 | WiimoteEmu::Classic::X_BUTTON, &m_classic_overrider); |
| 282 | m_classic_y_button = CreateButton(QStringLiteral("&Y"), WiimoteEmu::Classic::BUTTONS_GROUP, |
| 283 | WiimoteEmu::Classic::Y_BUTTON, &m_classic_overrider); |
| 284 | m_classic_zl_button = CreateButton(QStringLiteral("&ZL"), WiimoteEmu::Classic::BUTTONS_GROUP, |
| 285 | WiimoteEmu::Classic::ZL_BUTTON, &m_classic_overrider); |
| 286 | m_classic_zr_button = CreateButton(QStringLiteral("ZR"), WiimoteEmu::Classic::BUTTONS_GROUP, |
| 287 | WiimoteEmu::Classic::ZR_BUTTON, &m_classic_overrider); |
| 288 | m_classic_plus_button = CreateButton(QStringLiteral("&+"), WiimoteEmu::Classic::BUTTONS_GROUP, |
| 289 | WiimoteEmu::Classic::PLUS_BUTTON, &m_classic_overrider); |
| 290 | m_classic_minus_button = CreateButton(QStringLiteral("&-"), WiimoteEmu::Classic::BUTTONS_GROUP, |
| 291 | WiimoteEmu::Classic::MINUS_BUTTON, &m_classic_overrider); |
| 292 | m_classic_home_button = CreateButton(QStringLiteral("&HOME"), WiimoteEmu::Classic::BUTTONS_GROUP, |
| 293 | WiimoteEmu::Classic::HOME_BUTTON, &m_classic_overrider); |
| 294 | |
| 295 | m_classic_l_button = CreateButton(QStringLiteral("&L"), WiimoteEmu::Classic::TRIGGERS_GROUP, |
| 296 | WiimoteEmu::Classic::L_DIGITAL, &m_classic_overrider); |
| 297 | m_classic_r_button = CreateButton(QStringLiteral("&R"), WiimoteEmu::Classic::TRIGGERS_GROUP, |
| 298 | WiimoteEmu::Classic::R_DIGITAL, &m_classic_overrider); |
| 299 | |
| 300 | m_classic_left_button = CreateButton(QStringLiteral("L&eft"), WiimoteEmu::Classic::DPAD_GROUP, |
| 301 | DIRECTION_LEFT, &m_classic_overrider); |
| 302 | m_classic_up_button = CreateButton(QStringLiteral("&Up"), WiimoteEmu::Classic::DPAD_GROUP, |
| 303 | DIRECTION_UP, &m_classic_overrider); |
| 304 | m_classic_down_button = CreateButton(QStringLiteral("&Down"), WiimoteEmu::Classic::DPAD_GROUP, |
| 305 | DIRECTION_DOWN, &m_classic_overrider); |
| 306 | m_classic_right_button = CreateButton(QStringLiteral("R&ight"), WiimoteEmu::Classic::DPAD_GROUP, |
| 307 | DIRECTION_RIGHT, &m_classic_overrider); |
| 308 | |
| 309 | auto* classic_buttons_layout = new QGridLayout; |
| 310 | classic_buttons_layout->addWidget(m_classic_a_button, 0, 0); |
| 311 | classic_buttons_layout->addWidget(m_classic_b_button, 0, 1); |
| 312 | classic_buttons_layout->addWidget(m_classic_x_button, 0, 2); |
| 313 | classic_buttons_layout->addWidget(m_classic_y_button, 0, 3); |
| 314 | classic_buttons_layout->addWidget(m_classic_l_button, 0, 4); |
| 315 | classic_buttons_layout->addWidget(m_classic_r_button, 0, 5); |
| 316 | classic_buttons_layout->addWidget(m_classic_zl_button, 0, 6); |
| 317 | classic_buttons_layout->addWidget(m_classic_zr_button, 0, 7); |
| 318 | |
| 319 | classic_buttons_layout->addWidget(m_classic_plus_button, 1, 0); |
| 320 | classic_buttons_layout->addWidget(m_classic_minus_button, 1, 1); |
| 321 | classic_buttons_layout->addWidget(m_classic_home_button, 1, 2); |
| 322 | classic_buttons_layout->addWidget(m_classic_left_button, 1, 3); |
| 323 | classic_buttons_layout->addWidget(m_classic_up_button, 1, 4); |
| 324 | classic_buttons_layout->addWidget(m_classic_down_button, 1, 5); |
| 325 | classic_buttons_layout->addWidget(m_classic_right_button, 1, 6); |
| 326 | |
| 327 | classic_buttons_layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding), 0, 8); |
| 328 | |
| 329 | m_classic_buttons_box = new QGroupBox(tr("Classic Buttons")); |
| 330 | m_classic_buttons_box->setLayout(classic_buttons_layout); |
| 331 | |
| 332 | auto* layout = new QVBoxLayout; |
| 333 | layout->addLayout(top_layout); |
| 334 | layout->addWidget(m_remote_accelerometer_box); |
| 335 | layout->addWidget(m_remote_gyroscope_box); |
| 336 | layout->addWidget(m_nunchuk_accelerometer_box); |
| 337 | layout->addWidget(m_triggers_box); |
| 338 | layout->addWidget(m_remote_buttons_box); |
| 339 | layout->addWidget(m_nunchuk_buttons_box); |
| 340 | layout->addWidget(m_classic_buttons_box); |
| 341 | layout->addWidget(m_settings_box); |
| 342 | |
| 343 | SetupScrollArea(layout); |
| 344 | } |
| 345 | |
| 346 | WiimoteEmu::Wiimote* WiiTASInputWindow::GetWiimote() |
| 347 | { |
| 348 | return static_cast<WiimoteEmu::Wiimote*>(Wiimote::GetConfig()->GetController(m_num)); |
| 349 | } |
| 350 | |
| 351 | ControllerEmu::Attachments* WiiTASInputWindow::GetAttachments() |
| 352 | { |
| 353 | return static_cast<ControllerEmu::Attachments*>( |
| 354 | GetWiimote()->GetWiimoteGroup(WiimoteEmu::WiimoteGroup::Attachments)); |
| 355 | } |
| 356 | |
| 357 | WiimoteEmu::Extension* WiiTASInputWindow::GetExtension() |
| 358 | { |
| 359 | return static_cast<WiimoteEmu::Extension*>( |
| 360 | GetAttachments()->GetAttachmentList()[m_active_extension].get()); |
| 361 | } |
| 362 | |
| 363 | void WiiTASInputWindow::UpdateExtension(const int extension) |
| 364 | { |
| 365 | const auto new_extension = static_cast<WiimoteEmu::ExtensionNumber>(extension); |
| 366 | if (new_extension == m_active_extension) |
| 367 | return; |
| 368 | |
| 369 | m_active_extension = new_extension; |
| 370 | |
| 371 | UpdateControlVisibility(); |
| 372 | UpdateInputOverrideFunction(); |
| 373 | } |
| 374 | |
| 375 | void WiiTASInputWindow::UpdateMotionPlus(const bool attached) |
| 376 | { |
| 377 | if (attached == m_is_motion_plus_attached) |
| 378 | return; |
| 379 | |
| 380 | m_is_motion_plus_attached = attached; |
| 381 | |
| 382 | UpdateControlVisibility(); |
| 383 | } |
| 384 | |
| 385 | void WiiTASInputWindow::LoadExtensionAndMotionPlus() |
| 386 | { |
| 387 | WiimoteEmu::Wiimote* const wiimote = GetWiimote(); |
| 388 | |
| 389 | if (Core::IsRunning(Core::System::GetInstance())) |
| 390 | { |
| 391 | m_active_extension = wiimote->GetActiveExtensionNumber(); |
| 392 | m_is_motion_plus_attached = wiimote->GetMotionPlusSetting().GetValue(); |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | Common::IniFile ini; |
| 397 | ini.Load(File::GetUserPath(D_CONFIG_IDX) + "WiimoteNew.ini"); |
| 398 | const std::string section_name = "Wiimote" + std::to_string(m_num + 1); |
| 399 | |
| 400 | std::string extension; |
| 401 | ini.GetIfExists(section_name, "Extension", &extension); |
| 402 | |
| 403 | if (extension == "Nunchuk") |
| 404 | m_active_extension = WiimoteEmu::ExtensionNumber::NUNCHUK; |
| 405 | else if (extension == "Classic") |
| 406 | m_active_extension = WiimoteEmu::ExtensionNumber::CLASSIC; |
| 407 | else |
| 408 | m_active_extension = WiimoteEmu::ExtensionNumber::NONE; |
| 409 | |
| 410 | m_is_motion_plus_attached = true; |
| 411 | ini.GetIfExists(section_name, "Extension/Attach MotionPlus", &m_is_motion_plus_attached); |
| 412 | } |
| 413 | |
| 414 | UpdateControlVisibility(); |
| 415 | UpdateInputOverrideFunction(); |
| 416 | |
| 417 | m_motion_plus_callback_id = |
| 418 | wiimote->GetMotionPlusSetting().AddCallback([this](const bool attached) { |
| 419 | QueueOnObject(this, [this, attached] { UpdateMotionPlus(attached); }); |
| 420 | }); |
| 421 | m_attachment_callback_id = |
| 422 | GetAttachments()->GetAttachmentSetting().AddCallback([this](const int extension_index) { |
| 423 | QueueOnObject(this, [this, extension_index] { UpdateExtension(extension_index); }); |
| 424 | }); |
| 425 | } |
| 426 | |
| 427 | void WiiTASInputWindow::UpdateControlVisibility() |
| 428 | { |
| 429 | if (m_active_extension == WiimoteEmu::ExtensionNumber::NUNCHUK) |
| 430 | { |
| 431 | setWindowTitle(tr("Wii TAS Input %1 - Wii Remote + Nunchuk").arg(m_num + 1)); |
| 432 | m_ir_box->show(); |
| 433 | m_nunchuk_stick_box->show(); |
| 434 | m_classic_right_stick_box->hide(); |
| 435 | m_classic_left_stick_box->hide(); |
| 436 | m_remote_accelerometer_box->show(); |
| 437 | m_remote_gyroscope_box->setVisible(m_is_motion_plus_attached); |
| 438 | m_nunchuk_accelerometer_box->show(); |
| 439 | m_triggers_box->hide(); |
| 440 | m_nunchuk_buttons_box->show(); |
| 441 | m_remote_buttons_box->show(); |
| 442 | m_classic_buttons_box->hide(); |
| 443 | } |
| 444 | else if (m_active_extension == WiimoteEmu::ExtensionNumber::CLASSIC) |
| 445 | { |
| 446 | setWindowTitle(tr("Wii TAS Input %1 - Classic Controller").arg(m_num + 1)); |
| 447 | m_ir_box->hide(); |
| 448 | m_nunchuk_stick_box->hide(); |
| 449 | m_classic_right_stick_box->show(); |
| 450 | m_classic_left_stick_box->show(); |
| 451 | m_remote_accelerometer_box->hide(); |
| 452 | m_remote_gyroscope_box->hide(); |
| 453 | m_nunchuk_accelerometer_box->hide(); |
| 454 | m_triggers_box->show(); |
| 455 | m_remote_buttons_box->hide(); |
| 456 | m_nunchuk_buttons_box->hide(); |
| 457 | m_classic_buttons_box->show(); |
| 458 | } |
| 459 | else |
| 460 | { |
| 461 | setWindowTitle(tr("Wii TAS Input %1 - Wii Remote").arg(m_num + 1)); |
| 462 | m_ir_box->show(); |
| 463 | m_nunchuk_stick_box->hide(); |
| 464 | m_classic_right_stick_box->hide(); |
| 465 | m_classic_left_stick_box->hide(); |
| 466 | m_remote_accelerometer_box->show(); |
| 467 | m_remote_gyroscope_box->setVisible(m_is_motion_plus_attached); |
| 468 | m_nunchuk_accelerometer_box->hide(); |
| 469 | m_triggers_box->hide(); |
| 470 | m_remote_buttons_box->show(); |
| 471 | m_nunchuk_buttons_box->hide(); |
| 472 | m_classic_buttons_box->hide(); |
| 473 | } |
| 474 | |
| 475 | // Without these calls, switching between attachments can result in the Stick/IRWidgets being |
| 476 | // surrounded by large amounts of empty space in one dimension. |
| 477 | m_scroll_widget->layout()->activate(); |
| 478 | const QSize hint = m_scroll_widget->sizeHint(); |
| 479 | const int scrollbar_buffer = style()->pixelMetric(QStyle::PM_ScrollBarExtent) + 10; |
| 480 | resize(hint.width() + scrollbar_buffer, hint.height() + scrollbar_buffer); |
| 481 | } |
| 482 | |
| 483 | void WiiTASInputWindow::hideEvent(QHideEvent* const event) |
| 484 | { |
| 485 | WiimoteEmu::Wiimote* const wiimote = GetWiimote(); |
| 486 | |
| 487 | wiimote->ClearInputOverrideFunction(); |
| 488 | wiimote->GetMotionPlusSetting().RemoveCallback(m_motion_plus_callback_id); |
| 489 | |
| 490 | GetExtension()->ClearInputOverrideFunction(); |
| 491 | GetAttachments()->GetAttachmentSetting().RemoveCallback(m_attachment_callback_id); |
| 492 | |
| 493 | TASInputWindow::hideEvent(event); |
| 494 | } |
| 495 | |
| 496 | void WiiTASInputWindow::showEvent(QShowEvent* const event) |
| 497 | { |
| 498 | LoadExtensionAndMotionPlus(); |
| 499 | |
| 500 | TASInputWindow::showEvent(event); |
| 501 | } |
| 502 | |
| 503 | void WiiTASInputWindow::UpdateInputOverrideFunction() |
| 504 | { |
| 505 | WiimoteEmu::Wiimote* const wiimote = GetWiimote(); |
| 506 | |
| 507 | if (m_active_extension != WiimoteEmu::ExtensionNumber::CLASSIC) |
| 508 | wiimote->SetInputOverrideFunction(m_wiimote_overrider.GetInputOverrideFunction()); |
| 509 | |
| 510 | if (m_active_extension == WiimoteEmu::ExtensionNumber::NUNCHUK) |
| 511 | GetExtension()->SetInputOverrideFunction(m_nunchuk_overrider.GetInputOverrideFunction()); |
| 512 | |
| 513 | if (m_active_extension == WiimoteEmu::ExtensionNumber::CLASSIC) |
| 514 | GetExtension()->SetInputOverrideFunction(m_classic_overrider.GetInputOverrideFunction()); |
| 515 | } |