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

View File

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

View File

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

View File

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

View File

@ -90,11 +90,10 @@ void Turntable::GetState(u8* const data)
// stick
{
ControlState x, y;
m_stick->GetState(&x, &y);
const ControllerEmu::AnalogStick::StateData stick_state = m_stick->GetState();
tt_data.sx = static_cast<u8>((x * 0x1F) + 0x20);
tt_data.sy = static_cast<u8>((y * 0x1F) + 0x20);
tt_data.sx = static_cast<u8>((stick_state.x * 0x1F) + 0x20);
tt_data.sy = static_cast<u8>((stick_state.y * 0x1F) + 0x20);
}
// 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));
}
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 xx = controls[3]->control_ref->State() - controls[2]->control_ref->State();
ControlState y = controls[0]->control_ref->State() - controls[1]->control_ref->State();
ControlState x = controls[3]->control_ref->State() - controls[2]->control_ref->State();
ControlState radius = numeric_settings[SETTING_RADIUS]->GetValue();
ControlState deadzone = numeric_settings[SETTING_DEADZONE]->GetValue();
ControlState m = controls[4]->control_ref->State();
const ControlState radius = numeric_settings[SETTING_RADIUS]->GetValue();
const ControlState deadzone = numeric_settings[SETTING_DEADZONE]->GetValue();
const ControlState m = controls[4]->control_ref->State();
ControlState ang = atan2(yy, xx);
ControlState ang_sin = sin(ang);
ControlState ang_cos = cos(ang);
const ControlState ang = atan2(y, x);
const ControlState ang_sin = sin(ang);
const ControlState ang_cos = cos(ang);
ControlState dist = sqrt(xx * xx + yy * yy);
ControlState dist = sqrt(x * x + y * y);
// dead zone code
dist = std::max(0.0, dist - deadzone);
@ -63,10 +63,9 @@ void AnalogStick::GetState(ControlState* const x, ControlState* const y)
if (m)
dist *= 0.5;
yy = std::max(-1.0, std::min(1.0, ang_sin * dist));
xx = std::max(-1.0, std::min(1.0, ang_cos * dist));
y = std::max(-1.0, std::min(1.0, ang_sin * dist));
x = std::max(-1.0, std::min(1.0, ang_cos * dist));
*y = yy;
*x = xx;
return {x, y};
}
} // namespace ControllerEmu

View File

@ -18,10 +18,16 @@ public:
SETTING_DEADZONE,
};
struct StateData
{
ControlState x{};
ControlState y{};
};
// The GameCube controller and Wiimote attachments have a different default radius
AnalogStick(const char* 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