Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InputCommon: Eliminate some duplicated button threshold logic. #8616

Merged
merged 1 commit into from Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/GCPadEmu.cpp
Expand Up @@ -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>();
}
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/WiimoteEmu/Dynamics.cpp
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions Source/Core/Core/HW/WiimoteEmu/WiimoteEmu.h
Expand Up @@ -145,8 +145,6 @@ class Wiimote : public ControllerEmu::EmulatedController
// 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.
Expand Down
15 changes: 11 additions & 4 deletions Source/Core/InputCommon/ControlReference/ControlReference.h
Expand Up @@ -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
Expand Down Expand Up @@ -52,11 +52,18 @@ class ControlReference
template <>
inline bool ControlReference::GetState<bool>()
{
return State() > ciface::ExpressionParser::CONDITION_THRESHOLD;
// Round to nearest of 0 or 1.
return std::lround(State()) > 0;
jordan-woyak marked this conversation as resolved.
Show resolved Hide resolved
}

template <typename T>
T ControlReference::GetState()
template <>
inline int ControlReference::GetState<int>()
{
return std::lround(State());
}

template <>
inline ControlState ControlReference::GetState<ControlState>()
{
return State();
}
Expand Down
9 changes: 8 additions & 1 deletion Source/Core/InputCommon/ControllerEmu/Control/Control.h
Expand Up @@ -7,7 +7,7 @@
#include <memory>
#include <string>

class ControlReference;
#include "InputCommon/ControlReference/ControlReference.h"

namespace ControllerEmu
{
Expand All @@ -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;
Expand All @@ -33,4 +39,5 @@ class Control
Control(std::unique_ptr<ControlReference> ref, Translatability translate,
const std::string& name);
};

} // namespace ControllerEmu
Expand Up @@ -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);
}
Expand Down
7 changes: 1 addition & 6 deletions Source/Core/InputCommon/ControllerEmu/ControlGroup/Buttons.h
Expand Up @@ -23,12 +23,7 @@ class Buttons : public ControlGroup
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
10 changes: 5 additions & 5 deletions Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions Source/Core/InputCommon/ControllerEmu/ControlGroup/Cursor.h
Expand Up @@ -47,8 +47,6 @@ class Cursor : public ReshapableInput
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;

Expand Down
12 changes: 6 additions & 6 deletions Source/Core/InputCommon/ControllerEmu/ControlGroup/Force.cpp
Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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};

Expand Down
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -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)
Expand Down
Expand Up @@ -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])
{
Expand Down
Expand Up @@ -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)};
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/InputCommon/ControllerEmu/ControlGroup/Tilt.cpp
Expand Up @@ -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);
}
Expand Down
Expand Up @@ -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;
}
Expand Down