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.
This commit is contained in:
Lioncash 2018-07-13 11:49:05 -04:00
parent 4c30b9e14d
commit 918d448b5b
4 changed files with 25 additions and 25 deletions

View File

@ -115,9 +115,9 @@ void Guitar::GetState(u8* const data)
// slider bar
if (m_slider_bar->controls[0]->control_ref->BoundCount())
{
ControlState slider_bar;
m_slider_bar->GetState(&slider_bar);
guitar_data.sb = s_slider_bar_control_codes.lower_bound(slider_bar)->second;
const ControllerEmu::Slider::StateData slider_data = m_slider_bar->GetState();
guitar_data.sb = s_slider_bar_control_codes.lower_bound(slider_data.value)->second;
}
else
{

View File

@ -98,26 +98,22 @@ void Turntable::GetState(u8* const data)
// left table
{
ControlState tt;
m_left_table->GetState(&tt);
const ControllerEmu::Slider::StateData lt = m_left_table->GetState();
const s8 tt = static_cast<s8>(lt.value * 0x1F);
const s8 tt_ = static_cast<s8>(tt * 0x1F);
tt_data.ltable1 = tt_;
tt_data.ltable2 = tt_ >> 5;
tt_data.ltable1 = tt;
tt_data.ltable2 = tt >> 5;
}
// right table
{
ControlState tt;
m_right_table->GetState(&tt);
const ControllerEmu::Slider::StateData rt = m_right_table->GetState();
const s8 tt = static_cast<s8>(rt.value * 0x1F);
const s8 tt_ = static_cast<s8>(tt * 0x1F);
tt_data.rtable1 = tt_;
tt_data.rtable2 = tt_ >> 1;
tt_data.rtable3 = tt_ >> 3;
tt_data.rtable4 = tt_ >> 5;
tt_data.rtable1 = tt;
tt_data.rtable2 = tt >> 1;
tt_data.rtable3 = tt >> 3;
tt_data.rtable4 = tt >> 5;
}
// effect dial
@ -133,10 +129,9 @@ void Turntable::GetState(u8* const data)
// crossfade slider
{
ControlState cfs;
m_crossfade->GetState(&cfs);
const ControllerEmu::Slider::StateData cfs = m_crossfade->GetState();
tt_data.slider = static_cast<u8>((cfs * 0x07) + 0x08);
tt_data.slider = static_cast<u8>((cfs.value * 0x07) + 0x08);
}
// buttons

View File

@ -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

View File

@ -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