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.
This commit is contained in:
Lioncash 2018-07-13 12:06:49 -04:00
parent 97ba02df27
commit cfe7549091
4 changed files with 23 additions and 13 deletions

View File

@ -126,9 +126,8 @@ void Guitar::GetState(u8* const data)
} }
// whammy bar // whammy bar
ControlState whammy; const ControllerEmu::Triggers::StateData whammy_state = m_whammy->GetState();
m_whammy->GetState(&whammy); guitar_data.whammy = static_cast<u8>(whammy_state.data[0] * 0x1F);
guitar_data.whammy = static_cast<u8>(whammy * 0x1F);
// buttons // buttons
m_buttons->GetState(&guitar_data.bt, guitar_button_bitmasks.data()); m_buttons->GetState(&guitar_data.bt, guitar_button_bitmasks.data());

View File

@ -118,13 +118,11 @@ void Turntable::GetState(u8* const data)
// effect dial // effect dial
{ {
ControlState dial; const ControllerEmu::Triggers::StateData state = m_effect_dial->GetState();
m_effect_dial->GetState(&dial); const u8 dial = static_cast<u8>(state.data[0] * 0x0F);
const u8 dial_ = static_cast<u8>(dial * 0x0F); tt_data.dial1 = dial;
tt_data.dial2 = dial >> 3;
tt_data.dial1 = dial_;
tt_data.dial2 = dial_ >> 3;
} }
// crossfade slider // crossfade slider

View File

@ -21,12 +21,15 @@ Triggers::Triggers(const std::string& name_) : ControlGroup(name_, GroupType::Tr
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 Triggers::GetState(ControlState* analog) Triggers::StateData Triggers::GetState()
{ {
const size_t trigger_count = controls.size(); const size_t trigger_count = controls.size();
const ControlState deadzone = numeric_settings[0]->GetValue(); const ControlState deadzone = numeric_settings[0]->GetValue();
for (size_t i = 0; i < trigger_count; ++i, ++analog) StateData result(trigger_count);
*analog = std::max(controls[i]->control_ref->State() - deadzone, 0.0) / (1 - deadzone); 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 } // namespace ControllerEmu

View File

@ -5,6 +5,8 @@
#pragma once #pragma once
#include <string> #include <string>
#include <vector>
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h" #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
#include "InputCommon/ControllerInterface/Device.h" #include "InputCommon/ControllerInterface/Device.h"
@ -13,8 +15,16 @@ namespace ControllerEmu
class Triggers : public ControlGroup class Triggers : public ControlGroup
{ {
public: public:
struct StateData
{
StateData() = default;
explicit StateData(std::size_t trigger_count) : data(trigger_count) {}
std::vector<ControlState> data;
};
explicit Triggers(const std::string& name); explicit Triggers(const std::string& name);
void GetState(ControlState* analog); StateData GetState();
}; };
} // namespace ControllerEmu } // namespace ControllerEmu