WiiTASInputWindow.cpp 24 KB

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