WiiTASInputWindow.cpp 25 KB

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