Skip to content

Commit

Permalink
Fix TAS input
Browse files Browse the repository at this point in the history
  • Loading branch information
Pokechu22 committed Mar 31, 2024
1 parent 3f4ec76 commit 1b384f6
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 39 deletions.
32 changes: 28 additions & 4 deletions Source/Core/Core/HW/WiimoteEmu/Extension/BalanceBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,34 @@ void BalanceBoardExt::BuildDesiredExtensionState(DesiredExtensionState* target_s

const double total_weight = DEFAULT_WEIGHT * 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<ControlState> top_right_override =
m_input_override_function(BALANCE_GROUP, TOP_RIGHT_SENSOR, top_right))
{
top_right = *top_right_override;
}
if (const std::optional<ControlState> bottom_right_override =
m_input_override_function(BALANCE_GROUP, BOTTOM_RIGHT_SENSOR, bottom_right))
{
bottom_right = *bottom_right_override;
}
if (const std::optional<ControlState> top_left_override =
m_input_override_function(BALANCE_GROUP, TOP_LEFT_SENSOR, top_left))
{
top_left = *top_left_override;
}
if (const std::optional<ControlState> bottom_left_override =
m_input_override_function(BALANCE_GROUP, BOTTOM_LEFT_SENSOR, bottom_left))
{
bottom_left = *bottom_left_override;
}
}

DataFormat bb_data = {};

Expand Down
7 changes: 7 additions & 0 deletions Source/Core/Core/HW/WiimoteEmu/Extension/BalanceBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

static constexpr float DEFAULT_WEIGHT = 63.5; // kilograms; no specific meaning to this value
Expand Down
40 changes: 40 additions & 0 deletions Source/Core/DolphinQt/TAS/TASInputWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,25 @@ TASSpinBox* TASInputWindow::CreateSliderValuePair(QBoxLayout* layout, int defaul
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,
Expand Down Expand Up @@ -302,3 +321,24 @@ std::optional<ControlState> TASInputWindow::GetSpinBox(TASSpinBox* spin, int zer

return (spin->GetValue() - zero) / scale;
}

std::optional<ControlState> 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();
}
8 changes: 8 additions & 0 deletions Source/Core/DolphinQt/TAS/TASInputWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class TASInputWindow : public QDialog
TASSpinBox* CreateSliderValuePair(QBoxLayout* layout, int default_, int 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);
Expand All @@ -82,4 +87,7 @@ class TASInputWindow : public QDialog
ControlState controller_state);
std::optional<ControlState> GetSpinBox(TASSpinBox* spin, int zero, ControlState controller_state,
ControlState scale);
std::optional<ControlState> GetSpinBox(QDoubleSpinBox* spin, ControlState controller_state);

std::map<QDoubleSpinBox*, u16> m_spinbox_most_recent_values_double;
};
49 changes: 14 additions & 35 deletions Source/Core/DolphinQt/TAS/WiiTASInputWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,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(
Expand Down Expand Up @@ -572,35 +580,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::BalanceBoard;
u8* const ext_data = rpt.GetExtDataPtr();
BalanceBoard::DataFormat bb_data = Common::BitCastPtr<BalanceBoard::DataFormat>(ext_data);
// TODO: Reading the existing values, but then just clobbering them instead of using them if
// controller input is enabled
double top_right = BalanceBoard::ConvertToKilograms(Common::swap16(bb_data.top_right));
double bottom_right = BalanceBoard::ConvertToKilograms(Common::swap16(bb_data.bottom_right));
double top_left = BalanceBoard::ConvertToKilograms(Common::swap16(bb_data.top_left));
double bottom_left = BalanceBoard::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(BalanceBoard::ConvertToSensorWeight(top_right));
bb_data.bottom_right = Common::swap16(BalanceBoard::ConvertToSensorWeight(bottom_right));
bb_data.top_left = Common::swap16(BalanceBoard::ConvertToSensorWeight(top_left));
bb_data.bottom_left = Common::swap16(BalanceBoard::ConvertToSensorWeight(bottom_left));
bb_data.temperature = BalanceBoard::TEMPERATURE;
bb_data.battery = 0x83;
Common::BitCastPtr<BalanceBoard::DataFormat>(ext_data) = bb_data;
key.Encrypt(ext_data, 0, sizeof(BalanceBoard::DataFormat));
}
*/
if (m_active_extension == WiimoteEmu::ExtensionNumber::BALANCE_BOARD)
GetExtension()->SetInputOverrideFunction(m_balance_board_overrider.GetInputOverrideFunction());
}
1 change: 1 addition & 0 deletions Source/Core/DolphinQt/TAS/WiiTASInputWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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;
Expand Down

0 comments on commit 1b384f6

Please sign in to comment.