Merge pull request #8616 from jordan-woyak/threshold-cleanup

InputCommon: Eliminate some duplicated button threshold logic.
This commit is contained in:
Tilka 2020-02-10 00:05:10 +00:00 committed by GitHub
commit db65b9a766
17 changed files with 50 additions and 45 deletions

View File

@ -261,5 +261,5 @@ void GCPad::LoadDefaults(const ControllerInterface& ciface)
bool GCPad::GetMicButton() const
{
const auto lock = GetStateLock();
return (0.0f != m_mic->controls.back()->control_ref->State());
return m_mic->controls.back()->GetState<bool>();
}

View File

@ -330,7 +330,7 @@ void EmulateIMUCursor(IMUCursorState* state, ControllerEmu::IMUCursor* imu_ir_gr
auto target_yaw = std::clamp(yaw, -max_yaw, max_yaw);
// Handle the "Recenter" button being pressed.
if (imu_ir_group->controls[0]->control_ref->GetState<bool>())
if (imu_ir_group->controls[0]->GetState<bool>())
{
state->recentered_pitch = std::asin((inv_rotation * Common::Vec3{0, 1, 0}).z);
target_yaw = 0;

View File

@ -145,8 +145,6 @@ private:
// This is the region exposed over bluetooth:
static constexpr int EEPROM_FREE_SIZE = 0x1700;
static constexpr double BUTTON_THRESHOLD = 0.5;
void UpdateButtonsStatus();
// Returns simulated accelerometer data in m/s^2.

View File

@ -4,10 +4,10 @@
#pragma once
#include <cmath>
#include <memory>
#include "InputCommon/ControlReference/ExpressionParser.h"
#include "InputCommon/ControlReference/FunctionExpression.h"
#include "InputCommon/ControllerInterface/Device.h"
// ControlReference
@ -52,11 +52,18 @@ protected:
template <>
inline bool ControlReference::GetState<bool>()
{
return State() > ciface::ExpressionParser::CONDITION_THRESHOLD;
// Round to nearest of 0 or 1.
return std::lround(State()) > 0;
}
template <typename T>
T ControlReference::GetState()
template <>
inline int ControlReference::GetState<int>()
{
return std::lround(State());
}
template <>
inline ControlState ControlReference::GetState<ControlState>()
{
return State();
}

View File

@ -7,7 +7,7 @@
#include <memory>
#include <string>
class ControlReference;
#include "InputCommon/ControlReference/ControlReference.h"
namespace ControllerEmu
{
@ -22,6 +22,12 @@ class Control
public:
virtual ~Control();
template <typename T = ControlState>
T GetState()
{
return control_ref->GetState<T>();
}
std::unique_ptr<ControlReference> const control_ref;
const Translatability translate;
const std::string name;
@ -33,4 +39,5 @@ protected:
Control(std::unique_ptr<ControlReference> ref, Translatability translate,
const std::string& name);
};
} // namespace ControllerEmu

View File

@ -34,14 +34,14 @@ AnalogStick::AnalogStick(const char* const name_, const char* const ui_name_,
AnalogStick::ReshapeData AnalogStick::GetReshapableState(bool adjusted)
{
const ControlState y = controls[0]->control_ref->State() - controls[1]->control_ref->State();
const ControlState x = controls[3]->control_ref->State() - controls[2]->control_ref->State();
const ControlState y = controls[0]->GetState() - controls[1]->GetState();
const ControlState x = controls[3]->GetState() - controls[2]->GetState();
// Return raw values. (used in UI)
if (!adjusted)
return {x, y};
const ControlState modifier = controls[4]->control_ref->State();
const ControlState modifier = controls[4]->GetState();
return Reshape(x, y, modifier);
}

View File

@ -23,12 +23,7 @@ public:
void GetState(C* const buttons, const C* bitmasks)
{
for (auto& control : controls)
{
if (control->control_ref->GetState<bool>())
*buttons |= *bitmasks;
bitmasks++;
}
*buttons |= *(bitmasks++) * control->GetState<bool>();
}
};
} // namespace ControllerEmu

View File

@ -67,8 +67,8 @@ Cursor::Cursor(std::string name, std::string ui_name)
Cursor::ReshapeData Cursor::GetReshapableState(bool adjusted)
{
const ControlState y = controls[0]->control_ref->State() - controls[1]->control_ref->State();
const ControlState x = controls[3]->control_ref->State() - controls[2]->control_ref->State();
const ControlState y = controls[0]->GetState() - controls[1]->GetState();
const ControlState x = controls[3]->GetState() - controls[2]->GetState();
// Return raw values. (used in UI)
if (!adjusted)
@ -103,10 +103,10 @@ Cursor::StateData Cursor::GetState(const bool adjusted)
const double max_step = STEP_PER_SEC / 1000.0 * ms_since_update;
// Relative input:
if (m_relative_setting.GetValue() ^ (controls[6]->control_ref->State() > BUTTON_THRESHOLD))
if (m_relative_setting.GetValue() ^ (controls[6]->GetState<bool>()))
{
// Recenter:
if (controls[5]->control_ref->State() > BUTTON_THRESHOLD)
if (controls[5]->GetState<bool>())
{
m_state.x = 0.0;
m_state.y = 0.0;
@ -143,7 +143,7 @@ Cursor::StateData Cursor::GetState(const bool adjusted)
m_prev_result = result;
// If auto-hide time is up or hide button is held:
if (!m_auto_hide_timer || controls[4]->control_ref->State() > BUTTON_THRESHOLD)
if (!m_auto_hide_timer || controls[4]->GetState<bool>())
{
result.x = std::numeric_limits<ControlState>::quiet_NaN();
result.y = 0;

View File

@ -47,8 +47,6 @@ private:
static constexpr int AUTO_HIDE_MS = 2500;
static constexpr double AUTO_HIDE_DEADZONE = 0.001;
static constexpr double BUTTON_THRESHOLD = 0.5;
// Not adjusted by width/height/center:
StateData m_state;

View File

@ -67,8 +67,8 @@ Force::Force(const std::string& name_) : ReshapableInput(name_, name_, GroupType
Force::ReshapeData Force::GetReshapableState(bool adjusted)
{
const ControlState y = controls[0]->control_ref->State() - controls[1]->control_ref->State();
const ControlState x = controls[3]->control_ref->State() - controls[2]->control_ref->State();
const ControlState y = controls[0]->GetState() - controls[1]->GetState();
const ControlState x = controls[3]->GetState() - controls[2]->GetState();
// Return raw values. (used in UI)
if (!adjusted)
@ -80,7 +80,7 @@ Force::ReshapeData Force::GetReshapableState(bool adjusted)
Force::StateData Force::GetState(bool adjusted)
{
const auto state = GetReshapableState(adjusted);
ControlState z = controls[4]->control_ref->State() - controls[5]->control_ref->State();
ControlState z = controls[4]->GetState() - controls[5]->GetState();
if (adjusted)
{
@ -159,9 +159,9 @@ Shake::Shake(const std::string& name_, ControlState default_intensity_scale)
Shake::StateData Shake::GetState(bool adjusted) const
{
const float x = controls[0]->control_ref->State();
const float y = controls[1]->control_ref->State();
const float z = controls[2]->control_ref->State();
const float x = controls[0]->GetState();
const float y = controls[1]->GetState();
const float z = controls[2]->GetState();
StateData result = {x, y, z};

View File

@ -31,9 +31,9 @@ std::optional<IMUAccelerometer::StateData> IMUAccelerometer::GetState() const
return std::nullopt;
StateData state;
state.x = (controls[2]->control_ref->State() - controls[3]->control_ref->State());
state.y = (controls[5]->control_ref->State() - controls[4]->control_ref->State());
state.z = (controls[0]->control_ref->State() - controls[1]->control_ref->State());
state.x = (controls[2]->GetState() - controls[3]->GetState());
state.y = (controls[5]->GetState() - controls[4]->GetState());
state.z = (controls[0]->GetState() - controls[1]->GetState());
return state;
}

View File

@ -31,9 +31,9 @@ std::optional<IMUGyroscope::StateData> IMUGyroscope::GetState() const
return std::nullopt;
StateData state;
state.x = (controls[1]->control_ref->State() - controls[0]->control_ref->State());
state.y = (controls[2]->control_ref->State() - controls[3]->control_ref->State());
state.z = (controls[4]->control_ref->State() - controls[5]->control_ref->State());
state.x = (controls[1]->GetState() - controls[0]->GetState());
state.y = (controls[2]->GetState() - controls[3]->GetState());
state.z = (controls[4]->GetState() - controls[5]->GetState());
return state;
}

View File

@ -46,9 +46,9 @@ void MixedTriggers::GetState(u16* const digital, const u16* bitmasks, ControlSta
const int trigger_count = int(controls.size() / 2);
for (int i = 0; i != trigger_count; ++i)
{
const ControlState button_value = ApplyDeadzone(controls[i]->control_ref->State(), deadzone);
const ControlState button_value = ApplyDeadzone(controls[i]->GetState(), deadzone);
ControlState analog_value =
std::min(ApplyDeadzone(controls[trigger_count + i]->control_ref->State(), deadzone), 1.0);
std::min(ApplyDeadzone(controls[trigger_count + i]->GetState(), deadzone), 1.0);
// Apply threshold:
if (button_value > threshold)

View File

@ -35,7 +35,7 @@ void ModifySettingsButton::GetState()
{
for (size_t i = 0; i < controls.size(); ++i)
{
const bool state = controls[i]->control_ref->GetState<bool>();
const bool state = controls[i]->GetState<bool>();
if (!associated_settings_toggle[i])
{

View File

@ -32,7 +32,7 @@ Slider::Slider(const std::string& name_) : Slider(name_, name_)
Slider::StateData Slider::GetState()
{
const ControlState deadzone = m_deadzone_setting.GetValue() / 100;
const ControlState state = controls[1]->control_ref->State() - controls[0]->control_ref->State();
const ControlState state = controls[1]->GetState() - controls[0]->GetState();
return {std::clamp(ApplyDeadzone(state, deadzone), -1.0, 1.0)};
}

View File

@ -44,14 +44,14 @@ Tilt::Tilt(const std::string& name_) : ReshapableInput(name_, name_, GroupType::
Tilt::ReshapeData Tilt::GetReshapableState(bool adjusted)
{
const ControlState y = controls[0]->control_ref->State() - controls[1]->control_ref->State();
const ControlState x = controls[3]->control_ref->State() - controls[2]->control_ref->State();
const ControlState y = controls[0]->GetState() - controls[1]->GetState();
const ControlState x = controls[3]->GetState() - controls[2]->GetState();
// Return raw values. (used in UI)
if (!adjusted)
return {x, y};
const ControlState modifier = controls[4]->control_ref->State();
const ControlState modifier = controls[4]->GetState();
return Reshape(x, y, modifier);
}

View File

@ -28,7 +28,7 @@ Triggers::StateData Triggers::GetState()
StateData result(trigger_count);
for (size_t i = 0; i < trigger_count; ++i)
result.data[i] = std::min(ApplyDeadzone(controls[i]->control_ref->State(), deadzone), 1.0);
result.data[i] = std::min(ApplyDeadzone(controls[i]->GetState(), deadzone), 1.0);
return result;
}