From cfe75490918f50dc068542f0acec124111bb4805 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 13 Jul 2018 12:06:49 -0400 Subject: [PATCH] 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. --- Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp | 5 ++--- .../Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp | 10 ++++------ .../ControllerEmu/ControlGroup/Triggers.cpp | 9 ++++++--- .../ControllerEmu/ControlGroup/Triggers.h | 12 +++++++++++- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp index fed0fb380a..cb2d59a885 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Guitar.cpp @@ -126,9 +126,8 @@ void Guitar::GetState(u8* const data) } // whammy bar - ControlState whammy; - m_whammy->GetState(&whammy); - guitar_data.whammy = static_cast(whammy * 0x1F); + const ControllerEmu::Triggers::StateData whammy_state = m_whammy->GetState(); + guitar_data.whammy = static_cast(whammy_state.data[0] * 0x1F); // buttons m_buttons->GetState(&guitar_data.bt, guitar_button_bitmasks.data()); diff --git a/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp b/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp index 554ef91c02..eb73093e2f 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Attachment/Turntable.cpp @@ -118,13 +118,11 @@ void Turntable::GetState(u8* const data) // effect dial { - ControlState dial; - m_effect_dial->GetState(&dial); + const ControllerEmu::Triggers::StateData state = m_effect_dial->GetState(); + const u8 dial = static_cast(state.data[0] * 0x0F); - const u8 dial_ = static_cast(dial * 0x0F); - - tt_data.dial1 = dial_; - tt_data.dial2 = dial_ >> 3; + tt_data.dial1 = dial; + tt_data.dial2 = dial >> 3; } // crossfade slider 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