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.
This commit is contained in:
Lioncash 2018-07-13 11:04:40 -04:00
parent 918d448b5b
commit 97ba02df27
3 changed files with 28 additions and 28 deletions

View file

@ -177,28 +177,24 @@ void EmulateDynamicShake(AccelData* const accel, DynamicData& dynamic_data,
void EmulateTilt(AccelData* const accel, ControllerEmu::Tilt* const tilt_group, const bool sideways, void EmulateTilt(AccelData* const accel, ControllerEmu::Tilt* const tilt_group, const bool sideways,
const bool upright) const bool upright)
{ {
ControlState roll, pitch;
// 180 degrees // 180 degrees
tilt_group->GetState(&roll, &pitch); const ControllerEmu::Tilt::StateData state = tilt_group->GetState();
const ControlState roll = state.x * PI;
const ControlState pitch = state.y * PI;
roll *= PI; // Some notes that no one will understand but me :p
pitch *= PI;
unsigned int ud = 0, lr = 0, fb = 0;
// some notes that no one will understand but me :p
// left, forward, up // left, forward, up
// lr/ left == negative for all orientations // lr/ left == negative for all orientations
// ud/ up == negative for upright longways // ud/ up == negative for upright longways
// fb/ forward == positive for (sideways flat) // fb/ forward == positive for (sideways flat)
// determine which axis is which direction // Determine which axis is which direction
ud = upright ? (sideways ? 0 : 1) : 2; const u32 ud = upright ? (sideways ? 0 : 1) : 2;
lr = sideways; const u32 lr = sideways;
fb = upright ? 2 : (sideways ? 0 : 1); const u32 fb = upright ? 2 : (sideways ? 0 : 1);
int sgn[3] = {-1, 1, 1}; // sign fix
// Sign fix
std::array<int, 3> sgn{{-1, 1, 1}};
if (sideways && !upright) if (sideways && !upright)
sgn[fb] *= -1; sgn[fb] *= -1;
if (!sideways && upright) if (!sideways && upright)

View file

@ -31,7 +31,7 @@ Tilt::Tilt(const std::string& name_) : ControlGroup(name_, GroupType::Tilt)
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Angle"), 0.9, 0, 180)); numeric_settings.emplace_back(std::make_unique<NumericSetting>(_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 // this is all a mess
@ -80,18 +80,17 @@ void Tilt::GetState(ControlState* const x, ControlState* const y, const bool ste
// silly // silly
if (step) if (step)
{ {
if (xx > m_tilt[0]) if (xx > m_tilt.x)
m_tilt[0] = std::min(m_tilt[0] + 0.1, xx); m_tilt.x = std::min(m_tilt.x + 0.1, xx);
else if (xx < m_tilt[0]) else if (xx < m_tilt.x)
m_tilt[0] = std::max(m_tilt[0] - 0.1, xx); m_tilt.x = std::max(m_tilt.x - 0.1, xx);
if (yy > m_tilt[1]) if (yy > m_tilt.y)
m_tilt[1] = std::min(m_tilt[1] + 0.1, yy); m_tilt.y = std::min(m_tilt.y + 0.1, yy);
else if (yy < m_tilt[1]) else if (yy < m_tilt.y)
m_tilt[1] = std::max(m_tilt[1] - 0.1, yy); m_tilt.y = std::max(m_tilt.y - 0.1, yy);
} }
*y = m_tilt[1] * angle; return {m_tilt.x * angle, m_tilt.y * angle};
*x = m_tilt[0] * angle;
} }
} // namespace ControllerEmu } // namespace ControllerEmu

View file

@ -4,7 +4,6 @@
#pragma once #pragma once
#include <array>
#include <string> #include <string>
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h" #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
#include "InputCommon/ControllerInterface/Device.h" #include "InputCommon/ControllerInterface/Device.h"
@ -14,11 +13,17 @@ namespace ControllerEmu
class Tilt : public ControlGroup class Tilt : public ControlGroup
{ {
public: public:
struct StateData
{
ControlState x{};
ControlState y{};
};
explicit Tilt(const std::string& name); explicit Tilt(const std::string& name);
void GetState(ControlState* x, ControlState* y, bool step = true); StateData GetState(bool step = true);
private: private:
std::array<ControlState, 2> m_tilt{}; StateData m_tilt;
}; };
} // namespace ControllerEmu } // namespace ControllerEmu