From d05f490caaacf66008bd1409e61de37b1ac6c32b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 13 Jul 2018 11:19:08 -0400 Subject: 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. --- .../ControllerEmu/ControlGroup/AnalogStick.cpp | 27 +++++++++++----------- .../ControllerEmu/ControlGroup/AnalogStick.h | 8 ++++++- 2 files changed, 20 insertions(+), 15 deletions(-) (limited to 'Source/Core/InputCommon/ControllerEmu') diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp index 3317da427c..c38e5f1d3a 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp @@ -36,20 +36,20 @@ AnalogStick::AnalogStick(const char* const name_, const char* const ui_name_, numeric_settings.emplace_back(std::make_unique(_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 diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h index a598202ba7..d8d8e4ddea 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h @@ -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 -- cgit v1.2.3 From ef1240b0c73a158c1d80fce5ab9724d0a793a491 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 13 Jul 2018 11:38:26 -0400 Subject: ControlGroup/Cursor: Return state data by value Makes it less error-prone to get state data from cursors (no need to pass any pointers to locals), and also allows direct assignment, letting the retrieved data be const. --- .../ControllerEmu/ControlGroup/Cursor.cpp | 38 ++++++++++++---------- .../ControllerEmu/ControlGroup/Cursor.h | 13 +++++--- 2 files changed, 29 insertions(+), 22 deletions(-) (limited to 'Source/Core/InputCommon/ControllerEmu') diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.cpp index 154b0ca862..b0633ec6a5 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.cpp @@ -39,18 +39,18 @@ Cursor::Cursor(const std::string& name_) : ControlGroup(name_, GroupType::Cursor boolean_settings.emplace_back(std::make_unique(_trans("Auto-Hide"), false)); } -void Cursor::GetState(ControlState* const x, ControlState* const y, ControlState* const z, - const bool adjusted) +Cursor::StateData Cursor::GetState(const bool adjusted) { const ControlState zz = controls[4]->control_ref->State() - controls[5]->control_ref->State(); // silly being here - if (zz > m_z) - m_z = std::min(m_z + 0.1, zz); - else if (zz < m_z) - m_z = std::max(m_z - 0.1, zz); + if (zz > m_state.z) + m_state.z = std::min(m_state.z + 0.1, zz); + else if (zz < m_state.z) + m_state.z = std::max(m_state.z - 0.1, zz); - *z = m_z; + StateData result; + result.z = m_state.z; if (m_autohide_timer > -1) { @@ -69,11 +69,11 @@ void Cursor::GetState(ControlState* const x, ControlState* const y, ControlState } // hide - bool autohide = boolean_settings[1]->GetValue() && m_autohide_timer < 0; + const bool autohide = boolean_settings[1]->GetValue() && m_autohide_timer < 0; if (controls[6]->control_ref->State() > 0.5 || autohide) { - *x = 10000; - *y = 0; + result.x = 10000; + result.y = 0; } else { @@ -90,28 +90,30 @@ void Cursor::GetState(ControlState* const x, ControlState* const y, ControlState { // deadzone to avoid the cursor slowly drifting if (std::abs(xx) > deadzone) - m_x = MathUtil::Clamp(m_x + xx * SPEED_MULTIPLIER, -1.0, 1.0); + m_state.x = MathUtil::Clamp(m_state.x + xx * SPEED_MULTIPLIER, -1.0, 1.0); if (std::abs(yy) > deadzone) - m_y = MathUtil::Clamp(m_y + yy * SPEED_MULTIPLIER, -1.0, 1.0); + m_state.y = MathUtil::Clamp(m_state.y + yy * SPEED_MULTIPLIER, -1.0, 1.0); // recenter if (controls[7]->control_ref->State() > 0.5) { - m_x = 0.0; - m_y = 0.0; + m_state.x = 0.0; + m_state.y = 0.0; } } else { - m_x = xx; - m_y = yy; + m_state.x = xx; + m_state.y = yy; } - *x = m_x; - *y = m_y; + result.x = m_state.x; + result.y = m_state.y; } m_prev_xx = xx; m_prev_yy = yy; + + return result; } } // namespace ControllerEmu diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.h index 5e740334cf..b24cfdcba3 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.h @@ -13,9 +13,16 @@ namespace ControllerEmu class Cursor : public ControlGroup { public: + struct StateData + { + ControlState x{}; + ControlState y{}; + ControlState z{}; + }; + explicit Cursor(const std::string& name); - void GetState(ControlState* x, ControlState* y, ControlState* z, bool adjusted = false); + StateData GetState(bool adjusted = false); private: // This is used to reduce the cursor speed for relative input @@ -25,9 +32,7 @@ private: // Sets the length for the auto-hide timer static constexpr int TIMER_VALUE = 500; - ControlState m_x = 0.0; - ControlState m_y = 0.0; - ControlState m_z = 0.0; + StateData m_state; int m_autohide_timer = TIMER_VALUE; ControlState m_prev_xx; -- cgit v1.2.3 From 4c30b9e14dfbebe6a0c1131e3a5a19410b2dcdcd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 13 Jul 2018 10:35:35 -0400 Subject: ControlGroup/Force: Return state data by value Ensures that an array of sufficient size is always used and doesn't put the responsibility on the caller. It also allows for direct assignment. --- Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.cpp | 11 ++++++++--- Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.h | 6 ++++-- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'Source/Core/InputCommon/ControllerEmu') diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.cpp index 0c27e30298..c2178ea370 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.cpp @@ -29,18 +29,23 @@ Force::Force(const std::string& name_) : ControlGroup(name_, GroupType::Force) numeric_settings.emplace_back(std::make_unique(_trans("Dead Zone"), 0, 0, 50)); } -void Force::GetState(ControlState* axis) +Force::StateData Force::GetState() { + StateData state_data; const ControlState deadzone = numeric_settings[0]->GetValue(); for (u32 i = 0; i < 6; i += 2) { - ControlState tmpf = 0; const ControlState state = controls[i + 1]->control_ref->State() - controls[i]->control_ref->State(); + + ControlState tmpf = 0; if (fabs(state) > deadzone) tmpf = ((state - (deadzone * sign(state))) / (1 - deadzone)); - *axis++ = tmpf; + + state_data[i / 2] = tmpf; } + + return state_data; } } // namespace ControllerEmu diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.h index 1630a948b4..03dc07ecbd 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.h @@ -14,11 +14,13 @@ namespace ControllerEmu class Force : public ControlGroup { public: + using StateData = std::array; + explicit Force(const std::string& name); - void GetState(ControlState* axis); + StateData GetState(); private: - std::array m_swing{}; + StateData m_swing{}; }; } // namespace ControllerEmu -- cgit v1.2.3 From 918d448b5bf1510b75e50f560f41fb9e2aae439c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 13 Jul 2018 11:49:05 -0400 Subject: ControlGroup/Slider: Return state data by value Makes it less error-prone to get state data from sliders (no need to pass any locals), and also allows direct assignment, letting the retrieved data be const. --- Source/Core/InputCommon/ControllerEmu/ControlGroup/Slider.cpp | 8 ++++---- Source/Core/InputCommon/ControllerEmu/ControlGroup/Slider.h | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'Source/Core/InputCommon/ControllerEmu') diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Slider.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Slider.cpp index 6784f6e24e..f7e20dc386 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Slider.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Slider.cpp @@ -30,14 +30,14 @@ Slider::Slider(const std::string& name_) : Slider(name_, name_) { } -void Slider::GetState(ControlState* const slider) +Slider::StateData Slider::GetState() { const ControlState deadzone = numeric_settings[0]->GetValue(); const ControlState state = controls[1]->control_ref->State() - controls[0]->control_ref->State(); if (fabs(state) > deadzone) - *slider = (state - (deadzone * sign(state))) / (1 - deadzone); - else - *slider = 0; + return {(state - (deadzone * sign(state))) / (1 - deadzone)}; + + return {0.0}; } } // namespace ControllerEmu diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Slider.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Slider.h index fb038cd931..8142415a8f 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Slider.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Slider.h @@ -13,9 +13,14 @@ namespace ControllerEmu class Slider : public ControlGroup { public: + struct StateData + { + ControlState value{}; + }; + Slider(const std::string& name_, const std::string& ui_name_); explicit Slider(const std::string& name_); - void GetState(ControlState* slider); + StateData GetState(); }; } // namespace ControllerEmu -- cgit v1.2.3 From 97ba02df2721c2b6bb8edbb36b5eda0292686378 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 13 Jul 2018 11:04:40 -0400 Subject: ControlGroup/Tilt: Return state data by value Makes it less error-prone to get state data from tilt controls (no need to pass any pointers to locals), and also allows direct assignment, letting the retrieved data be const. --- .../ControllerEmu/ControlGroup/Tilt.cpp | 23 +++++++++++----------- .../InputCommon/ControllerEmu/ControlGroup/Tilt.h | 11 ++++++++--- 2 files changed, 19 insertions(+), 15 deletions(-) (limited to 'Source/Core/InputCommon/ControllerEmu') diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Tilt.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Tilt.cpp index 2785f56ba5..9221b90243 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Tilt.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Tilt.cpp @@ -31,7 +31,7 @@ Tilt::Tilt(const std::string& name_) : ControlGroup(name_, GroupType::Tilt) numeric_settings.emplace_back(std::make_unique(_trans("Angle"), 0.9, 0, 180)); } -void Tilt::GetState(ControlState* const x, ControlState* const y, const bool step) +Tilt::StateData Tilt::GetState(const bool step) { // this is all a mess @@ -80,18 +80,17 @@ void Tilt::GetState(ControlState* const x, ControlState* const y, const bool ste // silly if (step) { - if (xx > m_tilt[0]) - m_tilt[0] = std::min(m_tilt[0] + 0.1, xx); - else if (xx < m_tilt[0]) - m_tilt[0] = std::max(m_tilt[0] - 0.1, xx); - - if (yy > m_tilt[1]) - m_tilt[1] = std::min(m_tilt[1] + 0.1, yy); - else if (yy < m_tilt[1]) - m_tilt[1] = std::max(m_tilt[1] - 0.1, yy); + if (xx > m_tilt.x) + m_tilt.x = std::min(m_tilt.x + 0.1, xx); + else if (xx < m_tilt.x) + m_tilt.x = std::max(m_tilt.x - 0.1, xx); + + if (yy > m_tilt.y) + m_tilt.y = std::min(m_tilt.y + 0.1, yy); + else if (yy < m_tilt.y) + m_tilt.y = std::max(m_tilt.y - 0.1, yy); } - *y = m_tilt[1] * angle; - *x = m_tilt[0] * angle; + return {m_tilt.x * angle, m_tilt.y * angle}; } } // namespace ControllerEmu diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Tilt.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Tilt.h index 622fba3710..b03dc5a8b1 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Tilt.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Tilt.h @@ -4,7 +4,6 @@ #pragma once -#include #include #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h" #include "InputCommon/ControllerInterface/Device.h" @@ -14,11 +13,17 @@ namespace ControllerEmu class Tilt : public ControlGroup { public: + struct StateData + { + ControlState x{}; + ControlState y{}; + }; + explicit Tilt(const std::string& name); - void GetState(ControlState* x, ControlState* y, bool step = true); + StateData GetState(bool step = true); private: - std::array m_tilt{}; + StateData m_tilt; }; } // namespace ControllerEmu -- cgit v1.2.3 From cfe75490918f50dc068542f0acec124111bb4805 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 13 Jul 2018 12:06:49 -0400 Subject: ControlGroup/Triggers: 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. --- .../Core/InputCommon/ControllerEmu/ControlGroup/Triggers.cpp | 9 ++++++--- .../Core/InputCommon/ControllerEmu/ControlGroup/Triggers.h | 12 +++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'Source/Core/InputCommon/ControllerEmu') diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Triggers.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Triggers.cpp index daf7ef98d7..f950e5b24c 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Triggers.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Triggers.cpp @@ -21,12 +21,15 @@ Triggers::Triggers(const std::string& name_) : ControlGroup(name_, GroupType::Tr numeric_settings.emplace_back(std::make_unique(_trans("Dead Zone"), 0, 0, 50)); } -void Triggers::GetState(ControlState* analog) +Triggers::StateData Triggers::GetState() { const size_t trigger_count = controls.size(); const ControlState deadzone = numeric_settings[0]->GetValue(); - for (size_t i = 0; i < trigger_count; ++i, ++analog) - *analog = std::max(controls[i]->control_ref->State() - deadzone, 0.0) / (1 - deadzone); + StateData result(trigger_count); + for (size_t i = 0; i < trigger_count; ++i) + result.data[i] = std::max(controls[i]->control_ref->State() - deadzone, 0.0) / (1 - deadzone); + + return result; } } // namespace ControllerEmu diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Triggers.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Triggers.h index 591edd6fee..561eb4f7ef 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Triggers.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Triggers.h @@ -5,6 +5,8 @@ #pragma once #include +#include + #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h" #include "InputCommon/ControllerInterface/Device.h" @@ -13,8 +15,16 @@ namespace ControllerEmu class Triggers : public ControlGroup { public: + struct StateData + { + StateData() = default; + explicit StateData(std::size_t trigger_count) : data(trigger_count) {} + + std::vector data; + }; + explicit Triggers(const std::string& name); - void GetState(ControlState* analog); + StateData GetState(); }; } // namespace ControllerEmu -- cgit v1.2.3