ControlGroup/AnalogStick: Return state data by value

Makes it less error-prone to get state data from analog sticks (no need
to pass any locals), and also allows direct assignment, letting the
retrieved data be const.
This commit is contained in:
Lioncash 2018-07-13 11:19:08 -04:00
parent cc6526f553
commit d05f490caa
8 changed files with 63 additions and 61 deletions

View File

@ -4,6 +4,8 @@
#include "Core/HW/GCPadEmu.h" #include "Core/HW/GCPadEmu.h"
#include <array>
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -133,8 +135,6 @@ ControllerEmu::ControlGroup* GCPad::GetGroup(PadGroup group)
GCPadStatus GCPad::GetInput() const GCPadStatus GCPad::GetInput() const
{ {
const auto lock = GetStateLock(); const auto lock = GetStateLock();
ControlState x, y, triggers[2];
GCPadStatus pad = {}; GCPadStatus pad = {};
if (!(m_always_connected->GetValue() || IsDefaultDeviceConnected())) if (!(m_always_connected->GetValue() || IsDefaultDeviceConnected()))
@ -156,20 +156,21 @@ GCPadStatus GCPad::GetInput() const
m_dpad->GetState(&pad.button, dpad_bitmasks); m_dpad->GetState(&pad.button, dpad_bitmasks);
// sticks // sticks
m_main_stick->GetState(&x, &y); const ControllerEmu::AnalogStick::StateData main_stick_state = m_main_stick->GetState();
pad.stickX = pad.stickX = static_cast<u8>(GCPadStatus::MAIN_STICK_CENTER_X +
static_cast<u8>(GCPadStatus::MAIN_STICK_CENTER_X + (x * GCPadStatus::MAIN_STICK_RADIUS)); (main_stick_state.x * GCPadStatus::MAIN_STICK_RADIUS));
pad.stickY = pad.stickY = static_cast<u8>(GCPadStatus::MAIN_STICK_CENTER_Y +
static_cast<u8>(GCPadStatus::MAIN_STICK_CENTER_Y + (y * GCPadStatus::MAIN_STICK_RADIUS)); (main_stick_state.y * GCPadStatus::MAIN_STICK_RADIUS));
m_c_stick->GetState(&x, &y); const ControllerEmu::AnalogStick::StateData c_stick_state = m_c_stick->GetState();
pad.substickX = pad.substickX = static_cast<u8>(GCPadStatus::C_STICK_CENTER_X +
static_cast<u8>(GCPadStatus::C_STICK_CENTER_X + (x * GCPadStatus::C_STICK_RADIUS)); (c_stick_state.x * GCPadStatus::C_STICK_RADIUS));
pad.substickY = pad.substickY = static_cast<u8>(GCPadStatus::C_STICK_CENTER_Y +
static_cast<u8>(GCPadStatus::C_STICK_CENTER_Y + (y * GCPadStatus::C_STICK_RADIUS)); (c_stick_state.y * GCPadStatus::C_STICK_RADIUS));
// triggers // triggers
m_triggers->GetState(&pad.button, trigger_bitmasks, triggers); std::array<ControlState, 2> triggers;
m_triggers->GetState(&pad.button, trigger_bitmasks, triggers.data());
pad.triggerLeft = static_cast<u8>(triggers[0] * 0xFF); pad.triggerLeft = static_cast<u8>(triggers[0] * 0xFF);
pad.triggerRight = static_cast<u8>(triggers[1] * 0xFF); pad.triggerRight = static_cast<u8>(triggers[1] * 0xFF);

View File

@ -137,29 +137,27 @@ void Classic::GetState(u8* const data)
// left stick // left stick
{ {
ControlState x, y; const ControllerEmu::AnalogStick::StateData left_stick_state = m_left_stick->GetState();
m_left_stick->GetState(&x, &y);
classic_data.regular_data.lx = classic_data.regular_data.lx = static_cast<u8>(
static_cast<u8>(Classic::LEFT_STICK_CENTER_X + (x * Classic::LEFT_STICK_RADIUS)); Classic::LEFT_STICK_CENTER_X + (left_stick_state.x * Classic::LEFT_STICK_RADIUS));
classic_data.regular_data.ly = classic_data.regular_data.ly = static_cast<u8>(
static_cast<u8>(Classic::LEFT_STICK_CENTER_Y + (y * Classic::LEFT_STICK_RADIUS)); Classic::LEFT_STICK_CENTER_Y + (left_stick_state.y * Classic::LEFT_STICK_RADIUS));
} }
// right stick // right stick
{ {
ControlState x, y; const ControllerEmu::AnalogStick::StateData right_stick_data = m_right_stick->GetState();
m_right_stick->GetState(&x, &y);
const u8 x_ = const u8 x = static_cast<u8>(Classic::RIGHT_STICK_CENTER_X +
static_cast<u8>(Classic::RIGHT_STICK_CENTER_X + (x * Classic::RIGHT_STICK_RADIUS)); (right_stick_data.x * Classic::RIGHT_STICK_RADIUS));
const u8 y_ = const u8 y = static_cast<u8>(Classic::RIGHT_STICK_CENTER_Y +
static_cast<u8>(Classic::RIGHT_STICK_CENTER_Y + (y * Classic::RIGHT_STICK_RADIUS)); (right_stick_data.y * Classic::RIGHT_STICK_RADIUS));
classic_data.rx1 = x_; classic_data.rx1 = x;
classic_data.rx2 = x_ >> 1; classic_data.rx2 = x >> 1;
classic_data.rx3 = x_ >> 3; classic_data.rx3 = x >> 3;
classic_data.ry = y_; classic_data.ry = y;
} }
// triggers // triggers

View File

@ -74,11 +74,10 @@ void Drums::GetState(u8* const data)
// stick // stick
{ {
ControlState x, y; const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState();
m_stick->GetState(&x, &y);
drum_data.sx = static_cast<u8>((x * 0x1F) + 0x20); drum_data.sx = static_cast<u8>((stick_state.x * 0x1F) + 0x20);
drum_data.sy = static_cast<u8>((y * 0x1F) + 0x20); drum_data.sy = static_cast<u8>((stick_state.y * 0x1F) + 0x20);
} }
// TODO: softness maybe // TODO: softness maybe
@ -87,6 +86,7 @@ void Drums::GetState(u8* const data)
// buttons // buttons
m_buttons->GetState(&drum_data.bt, drum_button_bitmasks.data()); m_buttons->GetState(&drum_data.bt, drum_button_bitmasks.data());
// pads // pads
m_pads->GetState(&drum_data.bt, drum_pad_bitmasks.data()); m_pads->GetState(&drum_data.bt, drum_pad_bitmasks.data());

View File

@ -106,11 +106,10 @@ void Guitar::GetState(u8* const data)
// stick // stick
{ {
ControlState x, y; const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState();
m_stick->GetState(&x, &y);
guitar_data.sx = static_cast<u8>((x * 0x1F) + 0x20); guitar_data.sx = static_cast<u8>((stick_state.x * 0x1F) + 0x20);
guitar_data.sy = static_cast<u8>((y * 0x1F) + 0x20); guitar_data.sy = static_cast<u8>((stick_state.y * 0x1F) + 0x20);
} }
// slider bar // slider bar
@ -133,8 +132,10 @@ void Guitar::GetState(u8* const data)
// buttons // buttons
m_buttons->GetState(&guitar_data.bt, guitar_button_bitmasks.data()); m_buttons->GetState(&guitar_data.bt, guitar_button_bitmasks.data());
// frets // frets
m_frets->GetState(&guitar_data.bt, guitar_fret_bitmasks.data()); m_frets->GetState(&guitar_data.bt, guitar_fret_bitmasks.data());
// strum // strum
m_strum->GetState(&guitar_data.bt, guitar_strum_bitmasks.data()); m_strum->GetState(&guitar_data.bt, guitar_strum_bitmasks.data());

View File

@ -76,11 +76,9 @@ void Nunchuk::GetState(u8* const data)
wm_nc nc_data = {}; wm_nc nc_data = {};
// stick // stick
double jx, jy; const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState();
m_stick->GetState(&jx, &jy); nc_data.jx = u8(STICK_CENTER + stick_state.x * STICK_RADIUS);
nc_data.jy = u8(STICK_CENTER + stick_state.y * STICK_RADIUS);
nc_data.jx = u8(STICK_CENTER + jx * STICK_RADIUS);
nc_data.jy = u8(STICK_CENTER + jy * STICK_RADIUS);
// Some terribly coded games check whether to move with a check like // Some terribly coded games check whether to move with a check like
// //

View File

@ -90,11 +90,10 @@ void Turntable::GetState(u8* const data)
// stick // stick
{ {
ControlState x, y; const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState();
m_stick->GetState(&x, &y);
tt_data.sx = static_cast<u8>((x * 0x1F) + 0x20); tt_data.sx = static_cast<u8>((stick_state.x * 0x1F) + 0x20);
tt_data.sy = static_cast<u8>((y * 0x1F) + 0x20); tt_data.sy = static_cast<u8>((stick_state.y * 0x1F) + 0x20);
} }
// left table // left table

View File

@ -36,20 +36,20 @@ AnalogStick::AnalogStick(const char* const name_, const char* const ui_name_,
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50)); numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
} }
void AnalogStick::GetState(ControlState* const x, ControlState* const y) AnalogStick::StateData AnalogStick::GetState()
{ {
ControlState yy = controls[0]->control_ref->State() - controls[1]->control_ref->State(); ControlState y = controls[0]->control_ref->State() - controls[1]->control_ref->State();
ControlState xx = controls[3]->control_ref->State() - controls[2]->control_ref->State(); ControlState x = controls[3]->control_ref->State() - controls[2]->control_ref->State();
ControlState radius = numeric_settings[SETTING_RADIUS]->GetValue(); const ControlState radius = numeric_settings[SETTING_RADIUS]->GetValue();
ControlState deadzone = numeric_settings[SETTING_DEADZONE]->GetValue(); const ControlState deadzone = numeric_settings[SETTING_DEADZONE]->GetValue();
ControlState m = controls[4]->control_ref->State(); const ControlState m = controls[4]->control_ref->State();
ControlState ang = atan2(yy, xx); const ControlState ang = atan2(y, x);
ControlState ang_sin = sin(ang); const ControlState ang_sin = sin(ang);
ControlState ang_cos = cos(ang); const ControlState ang_cos = cos(ang);
ControlState dist = sqrt(xx * xx + yy * yy); ControlState dist = sqrt(x * x + y * y);
// dead zone code // dead zone code
dist = std::max(0.0, dist - deadzone); dist = std::max(0.0, dist - deadzone);
@ -63,10 +63,9 @@ void AnalogStick::GetState(ControlState* const x, ControlState* const y)
if (m) if (m)
dist *= 0.5; dist *= 0.5;
yy = std::max(-1.0, std::min(1.0, ang_sin * dist)); y = std::max(-1.0, std::min(1.0, ang_sin * dist));
xx = std::max(-1.0, std::min(1.0, ang_cos * dist)); x = std::max(-1.0, std::min(1.0, ang_cos * dist));
*y = yy; return {x, y};
*x = xx;
} }
} // namespace ControllerEmu } // namespace ControllerEmu

View File

@ -18,10 +18,16 @@ public:
SETTING_DEADZONE, SETTING_DEADZONE,
}; };
struct StateData
{
ControlState x{};
ControlState y{};
};
// The GameCube controller and Wiimote attachments have a different default radius // The GameCube controller and Wiimote attachments have a different default radius
AnalogStick(const char* name, ControlState default_radius); AnalogStick(const char* name, ControlState default_radius);
AnalogStick(const char* name, const char* ui_name, ControlState default_radius); AnalogStick(const char* name, const char* ui_name, ControlState default_radius);
void GetState(ControlState* x, ControlState* y); StateData GetState();
}; };
} // namespace ControllerEmu } // namespace ControllerEmu