Skip to content

Commit

Permalink
ControllerEmu: Add new "input override" system
Browse files Browse the repository at this point in the history
  • Loading branch information
JosJuice committed May 8, 2022
1 parent 31232f8 commit f1d3426
Show file tree
Hide file tree
Showing 27 changed files with 354 additions and 73 deletions.
13 changes: 7 additions & 6 deletions Source/Core/Core/HW/GCPadEmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ GCPadStatus GCPad::GetInput() const
const auto lock = GetStateLock();
GCPadStatus pad = {};

if (!(m_always_connected_setting.GetValue() || IsDefaultDeviceConnected()))
if (!(m_always_connected_setting.GetValue() || IsDefaultDeviceConnected() ||
m_input_override_function))
{
pad.isConnected = false;
return pad;
}

// buttons
m_buttons->GetState(&pad.button, button_bitmasks);
m_buttons->GetState(&pad.button, button_bitmasks, m_input_override_function);

// set analog A/B analog to full or w/e, prolly not needed
if (pad.button & PAD_BUTTON_A)
Expand All @@ -154,20 +155,20 @@ GCPadStatus GCPad::GetInput() const
pad.analogB = 0xFF;

// dpad
m_dpad->GetState(&pad.button, dpad_bitmasks);
m_dpad->GetState(&pad.button, dpad_bitmasks, m_input_override_function);

// sticks
const auto main_stick_state = m_main_stick->GetState();
const auto main_stick_state = m_main_stick->GetState(m_input_override_function);
pad.stickX = MapFloat<u8>(main_stick_state.x, GCPadStatus::MAIN_STICK_CENTER_X, 1);
pad.stickY = MapFloat<u8>(main_stick_state.y, GCPadStatus::MAIN_STICK_CENTER_Y, 1);

const auto c_stick_state = m_c_stick->GetState();
const auto c_stick_state = m_c_stick->GetState(m_input_override_function);
pad.substickX = MapFloat<u8>(c_stick_state.x, GCPadStatus::C_STICK_CENTER_X, 1);
pad.substickY = MapFloat<u8>(c_stick_state.y, GCPadStatus::C_STICK_CENTER_Y, 1);

// triggers
std::array<ControlState, 2> triggers;
m_triggers->GetState(&pad.button, trigger_bitmasks, triggers.data());
m_triggers->GetState(&pad.button, trigger_bitmasks, triggers.data(), m_input_override_function);
pad.triggerLeft = MapFloat<u8>(triggers[0], 0);
pad.triggerRight = MapFloat<u8>(triggers[1], 0);

Expand Down
6 changes: 4 additions & 2 deletions Source/Core/Core/HW/WiimoteEmu/Dynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <algorithm>
#include <cmath>
#include <optional>

#include "Common/MathUtil.h"
#include "Core/Config/SYSCONFSettings.h"
Expand Down Expand Up @@ -221,9 +222,10 @@ WiimoteCommon::AccelData ConvertAccelData(const Common::Vec3& accel, u16 zero_g,
u16(std::clamp(std::lround(scaled_accel.z + zero_g), 0l, MAX_VALUE))});
}

void EmulatePoint(MotionState* state, ControllerEmu::Cursor* ir_group, float time_elapsed)
void EmulatePoint(MotionState* state, ControllerEmu::Cursor* ir_group,
const ControllerEmu::InputOverrideFunction& override_func, float time_elapsed)
{
const auto cursor = ir_group->GetState(true);
const auto cursor = ir_group->GetState(true, override_func);

if (!cursor.IsVisible())
{
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/Core/HW/WiimoteEmu/Dynamics.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "InputCommon/ControllerEmu/ControlGroup/IMUCursor.h"
#include "InputCommon/ControllerEmu/ControlGroup/IMUGyroscope.h"
#include "InputCommon/ControllerEmu/ControlGroup/Tilt.h"
#include "InputCommon/ControllerEmu/ControllerEmu.h"

namespace WiimoteEmu
{
Expand Down Expand Up @@ -81,7 +82,8 @@ void ApproachAngleWithAccel(RotationalState* state, const Common::Vec3& target,
void EmulateShake(PositionalState* state, ControllerEmu::Shake* shake_group, float time_elapsed);
void EmulateTilt(RotationalState* state, ControllerEmu::Tilt* tilt_group, float time_elapsed);
void EmulateSwing(MotionState* state, ControllerEmu::Force* swing_group, float time_elapsed);
void EmulatePoint(MotionState* state, ControllerEmu::Cursor* ir_group, float time_elapsed);
void EmulatePoint(MotionState* state, ControllerEmu::Cursor* ir_group,
const ControllerEmu::InputOverrideFunction& override_func, float time_elapsed);
void EmulateIMUCursor(IMUCursorState* state, ControllerEmu::IMUCursor* imu_ir_group,
ControllerEmu::IMUAccelerometer* imu_accelerometer_group,
ControllerEmu::IMUGyroscope* imu_gyroscope_group, float time_elapsed);
Expand Down
19 changes: 11 additions & 8 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Classic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ void Classic::Update()

// left stick
{
const ControllerEmu::AnalogStick::StateData left_stick_state = m_left_stick->GetState();
const ControllerEmu::AnalogStick::StateData left_stick_state =
m_left_stick->GetState(m_input_override_function);

const u8 x = static_cast<u8>(LEFT_STICK_CENTER + (left_stick_state.x * LEFT_STICK_RADIUS));
const u8 y = static_cast<u8>(LEFT_STICK_CENTER + (left_stick_state.y * LEFT_STICK_RADIUS));
Expand All @@ -121,7 +122,8 @@ void Classic::Update()

// right stick
{
const ControllerEmu::AnalogStick::StateData right_stick_data = m_right_stick->GetState();
const ControllerEmu::AnalogStick::StateData right_stick_data =
m_right_stick->GetState(m_input_override_function);

const u8 x = static_cast<u8>(RIGHT_STICK_CENTER + (right_stick_data.x * RIGHT_STICK_RADIUS));
const u8 y = static_cast<u8>(RIGHT_STICK_CENTER + (right_stick_data.y * RIGHT_STICK_RADIUS));
Expand All @@ -133,19 +135,20 @@ void Classic::Update()

// triggers
{
ControlState trigs[2] = {0, 0};
m_triggers->GetState(&buttons, classic_trigger_bitmasks.data(), trigs);
ControlState triggers[2] = {0, 0};
m_triggers->GetState(&buttons, classic_trigger_bitmasks.data(), triggers,
m_input_override_function);

const u8 lt = static_cast<u8>(trigs[0] * TRIGGER_RANGE);
const u8 rt = static_cast<u8>(trigs[1] * TRIGGER_RANGE);
const u8 lt = static_cast<u8>(triggers[0] * TRIGGER_RANGE);
const u8 rt = static_cast<u8>(triggers[1] * TRIGGER_RANGE);

classic_data.SetLeftTrigger(lt);
classic_data.SetRightTrigger(rt);
}

// buttons and dpad
m_buttons->GetState(&buttons, classic_button_bitmasks.data());
m_dpad->GetState(&buttons, classic_dpad_bitmasks.data());
m_buttons->GetState(&buttons, classic_button_bitmasks.data(), m_input_override_function);
m_dpad->GetState(&buttons, classic_dpad_bitmasks.data(), m_input_override_function);

classic_data.SetButtons(buttons);

Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/HW/WiimoteEmu/Extension/DrawsomeTablet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void DrawsomeTablet::Update()
constexpr double CENTER_X = (MAX_X + MIN_X) / 2.0;
constexpr double CENTER_Y = (MAX_Y + MIN_Y) / 2.0;

const auto stylus_state = m_stylus->GetState();
const auto stylus_state = m_stylus->GetState(m_input_override_function);
const auto stylus_x = u16(std::lround(CENTER_X + stylus_state.x * (MAX_X - CENTER_X)));
const auto stylus_y = u16(std::lround(CENTER_Y + stylus_state.y * (MAX_Y - CENTER_Y)));

Expand All @@ -72,7 +72,7 @@ void DrawsomeTablet::Update()
// Pressure (0 - 0x7ff):
constexpr u16 MAX_PRESSURE = 0x7ff;

const auto touch_state = m_touch->GetState();
const auto touch_state = m_touch->GetState(m_input_override_function);
const auto pressure = u16(std::lround(touch_state.data[0] * MAX_PRESSURE));

tablet_data.pressure1 = u8(pressure);
Expand Down
7 changes: 4 additions & 3 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Drums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,19 @@ void Drums::Update()

// Stick.
{
const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState();
const ControllerEmu::AnalogStick::StateData stick_state =
m_stick->GetState(m_input_override_function);

drum_data.stick_x = MapFloat(stick_state.x, STICK_CENTER, STICK_MIN, STICK_MAX);
drum_data.stick_y = MapFloat(stick_state.y, STICK_CENTER, STICK_MIN, STICK_MAX);
}

// Buttons.
m_buttons->GetState(&drum_data.buttons, drum_button_bitmasks.data());
m_buttons->GetState(&drum_data.buttons, drum_button_bitmasks.data(), m_input_override_function);

// Drum pads.
u8 current_pad_input = 0;
m_pads->GetState(&current_pad_input, drum_pad_bitmasks.data());
m_pads->GetState(&current_pad_input, drum_pad_bitmasks.data(), m_input_override_function);
m_new_pad_hits |= ~m_prev_pad_input & current_pad_input;
m_prev_pad_input = current_pad_input;

Expand Down
15 changes: 9 additions & 6 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ void Guitar::Update()

// stick
{
const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState();
const ControllerEmu::AnalogStick::StateData stick_state =
m_stick->GetState(m_input_override_function);

guitar_data.sx = static_cast<u8>((stick_state.x * STICK_RADIUS) + STICK_CENTER);
guitar_data.sy = static_cast<u8>((stick_state.y * STICK_RADIUS) + STICK_CENTER);
Expand All @@ -109,7 +110,8 @@ void Guitar::Update()
if (m_slider_bar->controls[0]->control_ref->BoundCount() &&
m_slider_bar->controls[1]->control_ref->BoundCount())
{
const ControllerEmu::Slider::StateData slider_data = m_slider_bar->GetState();
const ControllerEmu::Slider::StateData slider_data =
m_slider_bar->GetState(m_input_override_function);

guitar_data.sb = s_slider_bar_control_codes.lower_bound(slider_data.value)->second;
}
Expand All @@ -120,17 +122,18 @@ void Guitar::Update()
}

// whammy bar
const ControllerEmu::Triggers::StateData whammy_state = m_whammy->GetState();
const ControllerEmu::Triggers::StateData whammy_state =
m_whammy->GetState(m_input_override_function);
guitar_data.whammy = static_cast<u8>(whammy_state.data[0] * 0x1F);

// buttons
m_buttons->GetState(&guitar_data.bt, guitar_button_bitmasks.data());
m_buttons->GetState(&guitar_data.bt, guitar_button_bitmasks.data(), m_input_override_function);

// frets
m_frets->GetState(&guitar_data.bt, guitar_fret_bitmasks.data());
m_frets->GetState(&guitar_data.bt, guitar_fret_bitmasks.data(), m_input_override_function);

// strum
m_strum->GetState(&guitar_data.bt, guitar_strum_bitmasks.data());
m_strum->GetState(&guitar_data.bt, guitar_strum_bitmasks.data(), m_input_override_function);

// flip button bits
guitar_data.bt ^= 0xFFFF;
Expand Down
37 changes: 22 additions & 15 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,34 @@ void Nunchuk::Update()
DataFormat nc_data = {};

// stick
const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState();
bool override_occurred = false;
const ControllerEmu::AnalogStick::StateData stick_state =
m_stick->GetState(m_input_override_function, &override_occurred);
nc_data.jx = u8(STICK_CENTER + stick_state.x * STICK_RADIUS);
nc_data.jy = u8(STICK_CENTER + stick_state.y * STICK_RADIUS);

// Some terribly coded games check whether to move with a check like
//
// if (x != 0 && y != 0)
// do_movement(x, y);
//
// With keyboard controls, these games break if you simply hit
// of the axes. Adjust this if you're hitting one of the axes so that
// we slightly tweak the other axis.
if (nc_data.jx != STICK_CENTER || nc_data.jy != STICK_CENTER)
if (!override_occurred)
{
if (nc_data.jx == STICK_CENTER)
++nc_data.jx;
if (nc_data.jy == STICK_CENTER)
++nc_data.jy;
// Some terribly coded games check whether to move with a check like
//
// if (x != 0 && y != 0)
// do_movement(x, y);
//
// With keyboard controls, these games break if you simply hit one
// of the axes. Adjust this if you're hitting one of the axes so that
// we slightly tweak the other axis.
if (nc_data.jx != STICK_CENTER || nc_data.jy != STICK_CENTER)
{
if (nc_data.jx == STICK_CENTER)
++nc_data.jx;
if (nc_data.jy == STICK_CENTER)
++nc_data.jy;
}
}

// buttons
u8 buttons = 0;
m_buttons->GetState(&buttons, nunchuk_button_bitmasks.data());
m_buttons->GetState(&buttons, nunchuk_button_bitmasks.data(), m_input_override_function);
nc_data.SetButtons(buttons);

// Acceleration data:
Expand All @@ -106,6 +111,8 @@ void Nunchuk::Update()
// shake
accel += m_shake_state.acceleration;

accel = Wiimote::OverrideVec3(m_imu_accelerometer, accel, m_input_override_function);

// Calibration values are 8-bit but we want 10-bit precision, so << 2.
const auto acc = ConvertAccelData(accel, ACCEL_ZERO_G << 2, ACCEL_ONE_G << 2);
nc_data.SetAccel(acc.value);
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/HW/WiimoteEmu/Extension/TaTaCon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ void TaTaCon::Update()
{
DataFormat tatacon_data = {};

m_center->GetState(&tatacon_data.state, center_bitmasks.data());
m_rim->GetState(&tatacon_data.state, rim_bitmasks.data());
m_center->GetState(&tatacon_data.state, center_bitmasks.data(), m_input_override_function);
m_rim->GetState(&tatacon_data.state, rim_bitmasks.data(), m_input_override_function);

// Flip button bits.
tatacon_data.state ^= 0xff;
Expand Down
13 changes: 7 additions & 6 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ void Turntable::Update()

// stick
{
const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState();
const ControllerEmu::AnalogStick::StateData stick_state =
m_stick->GetState(m_input_override_function);

tt_data.sx = static_cast<u8>((stick_state.x * STICK_RADIUS) + STICK_CENTER);
tt_data.sy = static_cast<u8>((stick_state.y * STICK_RADIUS) + STICK_CENTER);
}

// left table
{
const ControllerEmu::Slider::StateData lt = m_left_table->GetState();
const ControllerEmu::Slider::StateData lt = m_left_table->GetState(m_input_override_function);
const s8 tt = static_cast<s8>(lt.value * TABLE_RANGE);

tt_data.ltable1 = tt;
Expand All @@ -101,7 +102,7 @@ void Turntable::Update()

// right table
{
const ControllerEmu::Slider::StateData rt = m_right_table->GetState();
const ControllerEmu::Slider::StateData rt = m_right_table->GetState(m_input_override_function);
const s8 tt = static_cast<s8>(rt.value * TABLE_RANGE);

tt_data.rtable1 = tt;
Expand All @@ -112,7 +113,7 @@ void Turntable::Update()

// effect dial
{
const auto dial_state = m_effect_dial->GetState();
const auto dial_state = m_effect_dial->GetState(m_input_override_function);
const u8 dial = static_cast<u8>(dial_state.value * EFFECT_DIAL_RANGE) + EFFECT_DIAL_CENTER;

tt_data.dial1 = dial;
Expand All @@ -121,13 +122,13 @@ void Turntable::Update()

// crossfade slider
{
const ControllerEmu::Slider::StateData cfs = m_crossfade->GetState();
const ControllerEmu::Slider::StateData cfs = m_crossfade->GetState(m_input_override_function);

tt_data.slider = static_cast<u8>((cfs.value * CROSSFADE_RANGE) + CROSSFADE_CENTER);
}

// buttons
m_buttons->GetState(&tt_data.bt, turntable_button_bitmasks.data());
m_buttons->GetState(&tt_data.bt, turntable_button_bitmasks.data(), m_input_override_function);

// flip button bits :/
tt_data.bt ^= (BUTTON_L_GREEN | BUTTON_L_RED | BUTTON_L_BLUE | BUTTON_R_GREEN | BUTTON_R_RED |
Expand Down
Loading

0 comments on commit f1d3426

Please sign in to comment.