From cae3f39299deb120de01e5eee457171d790dfd2e Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 4 Oct 2022 16:36:12 -0700 Subject: [PATCH] Fix TAS input --- .../HW/WiimoteEmu/Extension/BalanceBoard.cpp | 32 ++++++++++-- .../HW/WiimoteEmu/Extension/BalanceBoard.h | 7 +++ Source/Core/DolphinQt/TAS/TASInputWindow.cpp | 40 +++++++++++++++ Source/Core/DolphinQt/TAS/TASInputWindow.h | 7 +++ .../Core/DolphinQt/TAS/WiiTASInputWindow.cpp | 49 ++++++------------- Source/Core/DolphinQt/TAS/WiiTASInputWindow.h | 1 + 6 files changed, 97 insertions(+), 39 deletions(-) diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/BalanceBoard.cpp b/Source/Core/Core/HW/WiimoteEmu/Extension/BalanceBoard.cpp index a96a9ebea2d7..bd5a052b5c97 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/BalanceBoard.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/BalanceBoard.cpp @@ -39,10 +39,34 @@ void BalanceBoardExt::BuildDesiredExtensionState(DesiredExtensionState* target_s const double total_weight = 63.5 * weight; // kilograms - const auto top_right = total_weight * (1 + balance_state.x + balance_state.y) / 4; - const auto bottom_right = total_weight * (1 + balance_state.x - balance_state.y) / 4; - const auto top_left = total_weight * (1 - balance_state.x + balance_state.y) / 4; - const auto bottom_left = total_weight * (1 - balance_state.x - balance_state.y) / 4; + auto top_right = total_weight * (1 + balance_state.x + balance_state.y) / 4; + auto bottom_right = total_weight * (1 + balance_state.x - balance_state.y) / 4; + auto top_left = total_weight * (1 - balance_state.x + balance_state.y) / 4; + auto bottom_left = total_weight * (1 - balance_state.x - balance_state.y) / 4; + + if (m_input_override_function) + { + if (const std::optional top_right_override = + m_input_override_function(BALANCE_GROUP, TOP_RIGHT_SENSOR, top_right)) + { + top_right = *top_right_override; + } + if (const std::optional bottom_right_override = + m_input_override_function(BALANCE_GROUP, BOTTOM_RIGHT_SENSOR, bottom_right)) + { + bottom_right = *bottom_right_override; + } + if (const std::optional top_left_override = + m_input_override_function(BALANCE_GROUP, TOP_LEFT_SENSOR, top_left)) + { + top_left = *top_left_override; + } + if (const std::optional bottom_left_override = + m_input_override_function(BALANCE_GROUP, BOTTOM_LEFT_SENSOR, bottom_left)) + { + bottom_left = *bottom_left_override; + } + } DataFormat bb_data = {}; diff --git a/Source/Core/Core/HW/WiimoteEmu/Extension/BalanceBoard.h b/Source/Core/Core/HW/WiimoteEmu/Extension/BalanceBoard.h index 24345282cd7e..b1ec8ffcb058 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Extension/BalanceBoard.h +++ b/Source/Core/Core/HW/WiimoteEmu/Extension/BalanceBoard.h @@ -33,6 +33,13 @@ class BalanceBoardExt : public Extension1stParty }; static_assert(sizeof(DataFormat) == 12, "Wrong size"); + static constexpr const char* BALANCE_GROUP = "Balance"; + + static constexpr const char* TOP_RIGHT_SENSOR = "TR"; + static constexpr const char* BOTTOM_RIGHT_SENSOR = "BR"; + static constexpr const char* TOP_LEFT_SENSOR = "TL"; + static constexpr const char* BOTTOM_LEFT_SENSOR = "BL"; + BalanceBoardExt(BalanceBoard* owner); void BuildDesiredExtensionState(DesiredExtensionState* target_state) override; diff --git a/Source/Core/DolphinQt/TAS/TASInputWindow.cpp b/Source/Core/DolphinQt/TAS/TASInputWindow.cpp index 815f222d4c35..e15f1cbdc00d 100644 --- a/Source/Core/DolphinQt/TAS/TASInputWindow.cpp +++ b/Source/Core/DolphinQt/TAS/TASInputWindow.cpp @@ -235,6 +235,25 @@ QSpinBox* TASInputWindow::CreateSliderValuePair(QBoxLayout* layout, int default_ return value; } +QDoubleSpinBox* TASInputWindow::CreateWeightSliderValuePair(std::string_view group_name, + std::string_view control_name, + InputOverrider* overrider, + QBoxLayout* layout, int min, int max, + QKeySequence shortcut_key_sequence, + QWidget* shortcut_widget) +{ + QDoubleSpinBox* value = + CreateWeightSliderValuePair(layout, min, max, shortcut_key_sequence, shortcut_widget); + + InputOverrider::OverrideFunction func = [this, value](ControlState controller_state) { + return GetSpinBox(value, controller_state); + }; + + overrider->AddFunction(group_name, control_name, std::move(func)); + + return value; +} + // The shortcut_widget argument needs to specify the container widget that will be hidden/shown. // This is done to avoid ambigous shortcuts QDoubleSpinBox* TASInputWindow::CreateWeightSliderValuePair(QBoxLayout* layout, int min, int max, @@ -337,3 +356,24 @@ std::optional TASInputWindow::GetSpinBox(QSpinBox* spin, u16 zero, return (spin->value() - zero) / scale; } + +std::optional TASInputWindow::GetSpinBox(QDoubleSpinBox* spin, + ControlState controller_state) +{ + if (m_use_controller->isChecked()) + { + if (!m_spinbox_most_recent_values_double.count(spin) || + m_spinbox_most_recent_values_double[spin] != controller_state) + { + QueueOnObjectBlocking(spin, [spin, controller_state] { spin->setValue(controller_state); }); + } + + m_spinbox_most_recent_values_double[spin] = controller_state; + } + else + { + m_spinbox_most_recent_values_double.clear(); + } + + return spin->value(); +} diff --git a/Source/Core/DolphinQt/TAS/TASInputWindow.h b/Source/Core/DolphinQt/TAS/TASInputWindow.h index 44668f08d54d..914a6b238fd5 100644 --- a/Source/Core/DolphinQt/TAS/TASInputWindow.h +++ b/Source/Core/DolphinQt/TAS/TASInputWindow.h @@ -66,6 +66,11 @@ class TASInputWindow : public QDialog QSpinBox* CreateSliderValuePair(QBoxLayout* layout, int default_, u16 max, QKeySequence shortcut_key_sequence, Qt::Orientation orientation, QWidget* shortcut_widget); + QDoubleSpinBox* CreateWeightSliderValuePair(std::string_view group_name, + std::string_view control_name, + InputOverrider* overrider, QBoxLayout* layout, + int min, int max, QKeySequence shortcut_key_sequence, + QWidget* shortcut_widget); QDoubleSpinBox* CreateWeightSliderValuePair(QBoxLayout* layout, int min, int max, QKeySequence shortcut_key_sequence, QWidget* shortcut_widget); @@ -81,7 +86,9 @@ class TASInputWindow : public QDialog ControlState controller_state); std::optional GetSpinBox(QSpinBox* spin, u16 zero, ControlState controller_state, ControlState scale); + std::optional GetSpinBox(QDoubleSpinBox* spin, ControlState controller_state); std::map m_checkbox_set_by_controller; std::map m_spinbox_most_recent_values; + std::map m_spinbox_most_recent_values_double; }; diff --git a/Source/Core/DolphinQt/TAS/WiiTASInputWindow.cpp b/Source/Core/DolphinQt/TAS/WiiTASInputWindow.cpp index 7c8b1ef3a2a0..b773c6ef6229 100644 --- a/Source/Core/DolphinQt/TAS/WiiTASInputWindow.cpp +++ b/Source/Core/DolphinQt/TAS/WiiTASInputWindow.cpp @@ -119,15 +119,23 @@ WiiTASInputWindow::WiiTASInputWindow(QWidget* parent, int num) : TASInputWindow( auto* bal_top_layout = new QHBoxLayout; m_top_left_balance_value = CreateWeightSliderValuePair( - bal_top_layout, -34, 68, balance_tl_shortcut_key_sequence, m_balance_board_box); + WiimoteEmu::BalanceBoardExt::BALANCE_GROUP, WiimoteEmu::BalanceBoardExt::TOP_LEFT_SENSOR, + &m_balance_board_overrider, bal_top_layout, -34, 68, balance_tl_shortcut_key_sequence, + m_balance_board_box); m_top_right_balance_value = CreateWeightSliderValuePair( - bal_top_layout, -34, 68, balance_tr_shortcut_key_sequence, m_balance_board_box); + WiimoteEmu::BalanceBoardExt::BALANCE_GROUP, WiimoteEmu::BalanceBoardExt::TOP_RIGHT_SENSOR, + &m_balance_board_overrider, bal_top_layout, -34, 68, balance_tr_shortcut_key_sequence, + m_balance_board_box); auto* bal_bottom_layout = new QHBoxLayout; m_bottom_left_balance_value = CreateWeightSliderValuePair( - bal_bottom_layout, -34, 68, balance_bl_shortcut_key_sequence, m_balance_board_box); + WiimoteEmu::BalanceBoardExt::BALANCE_GROUP, WiimoteEmu::BalanceBoardExt::BOTTOM_LEFT_SENSOR, + &m_balance_board_overrider, bal_bottom_layout, -34, 68, balance_bl_shortcut_key_sequence, + m_balance_board_box); m_bottom_right_balance_value = CreateWeightSliderValuePair( - bal_bottom_layout, -34, 68, balance_br_shortcut_key_sequence, m_balance_board_box); + WiimoteEmu::BalanceBoardExt::BALANCE_GROUP, WiimoteEmu::BalanceBoardExt::BOTTOM_RIGHT_SENSOR, + &m_balance_board_overrider, bal_bottom_layout, -34, 68, balance_br_shortcut_key_sequence, + m_balance_board_box); auto* bal_weight_layout = new QHBoxLayout; m_total_weight_value = CreateWeightSliderValuePair( @@ -515,35 +523,6 @@ void WiiTASInputWindow::showEvent(QShowEvent* event) if (m_active_extension == WiimoteEmu::ExtensionNumber::CLASSIC) GetExtension()->SetInputOverrideFunction(m_classic_overrider.GetInputOverrideFunction()); - /* - if (rpt.HasExt() && m_balance_board_box->isVisible()) - { - using WiimoteEmu::BalanceBoardExt; - - u8* const ext_data = rpt.GetExtDataPtr(); - BalanceBoardExt::DataFormat bb_data = Common::BitCastPtr(ext_data); - - // TODO: Reading the existing values, but then just clobbering them instead of using them if - // controller input is enabled - double top_right = BalanceBoardExt::ConvertToKilograms(Common::swap16(bb_data.top_right)); - double bottom_right = BalanceBoardExt::ConvertToKilograms(Common::swap16(bb_data.bottom_right)); - double top_left = BalanceBoardExt::ConvertToKilograms(Common::swap16(bb_data.top_left)); - double bottom_left = BalanceBoardExt::ConvertToKilograms(Common::swap16(bb_data.bottom_left)); - - top_right = m_top_right_balance_value->value(); - bottom_right = m_bottom_right_balance_value->value(); - top_left = m_top_left_balance_value->value(); - bottom_left = m_bottom_left_balance_value->value(); - - bb_data.top_right = Common::swap16(BalanceBoardExt::ConvertToSensorWeight(top_right)); - bb_data.bottom_right = Common::swap16(BalanceBoardExt::ConvertToSensorWeight(bottom_right)); - bb_data.top_left = Common::swap16(BalanceBoardExt::ConvertToSensorWeight(top_left)); - bb_data.bottom_left = Common::swap16(BalanceBoardExt::ConvertToSensorWeight(bottom_left)); - bb_data.temperature = BalanceBoardExt::TEMPERATURE; - bb_data.battery = 0x83; - - Common::BitCastPtr(ext_data) = bb_data; - key.Encrypt(ext_data, 0, sizeof(BalanceBoardExt::DataFormat)); - } -*/ + if (m_active_extension == WiimoteEmu::ExtensionNumber::BALANCE_BOARD) + GetExtension()->SetInputOverrideFunction(m_balance_board_overrider.GetInputOverrideFunction()); } diff --git a/Source/Core/DolphinQt/TAS/WiiTASInputWindow.h b/Source/Core/DolphinQt/TAS/WiiTASInputWindow.h index ad2384c846d2..2a279c38dfc5 100644 --- a/Source/Core/DolphinQt/TAS/WiiTASInputWindow.h +++ b/Source/Core/DolphinQt/TAS/WiiTASInputWindow.h @@ -40,6 +40,7 @@ class WiiTASInputWindow : public TASInputWindow InputOverrider m_wiimote_overrider; InputOverrider m_nunchuk_overrider; InputOverrider m_classic_overrider; + InputOverrider m_balance_board_overrider; TASCheckBox* m_a_button; TASCheckBox* m_b_button;