Skip to content

Commit

Permalink
Fix TAS input
Browse files Browse the repository at this point in the history
  • Loading branch information
Pokechu22 committed Jan 27, 2023
1 parent 96969cd commit 5052b10
Show file tree
Hide file tree
Showing 6 changed files with 97 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 = 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<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);

void BuildDesiredExtensionState(DesiredExtensionState* target_state) override;
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 @@ 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,
Expand Down Expand Up @@ -337,3 +356,24 @@ std::optional<ControlState> TASInputWindow::GetSpinBox(QSpinBox* spin, u16 zero,

return (spin->value() - 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();
}
7 changes: 7 additions & 0 deletions Source/Core/DolphinQt/TAS/TASInputWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -81,7 +86,9 @@ class TASInputWindow : public QDialog
ControlState controller_state);
std::optional<ControlState> GetSpinBox(QSpinBox* spin, u16 zero, ControlState controller_state,
ControlState scale);
std::optional<ControlState> GetSpinBox(QDoubleSpinBox* spin, ControlState controller_state);

std::map<TASCheckBox*, bool> m_checkbox_set_by_controller;
std::map<QSpinBox*, u16> m_spinbox_most_recent_values;
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 @@ -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(
Expand Down Expand Up @@ -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<BalanceBoardExt::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 = 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<BalanceBoardExt::DataFormat>(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());
}
1 change: 1 addition & 0 deletions Source/Core/DolphinQt/TAS/WiiTASInputWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5052b10

Please sign in to comment.