ControllerEmu::Cursor: Add input radius/shape settings to IR Cursor mappings to allow use of round inputs in absolute mode. Make relative input option obey the center/width/height settings. Make the mapping indicator pretty and actually show what the relative/center/w/h settings are doing.

This commit is contained in:
Jordan Woyak
2018-12-30 09:10:32 -06:00
parent 247fa8c628
commit 7a00f55cfa
10 changed files with 292 additions and 211 deletions

View File

@ -37,10 +37,10 @@ AnalogStick::AnalogStick(const char* const name_, const char* const ui_name_,
AddReshapingSettings(GetGateRadiusAtAngle(0.0), 0.0, 50);
}
AnalogStick::StateData AnalogStick::GetState(bool adjusted)
AnalogStick::StateData AnalogStick::GetReshapableState(bool adjusted)
{
ControlState y = controls[0]->control_ref->State() - controls[1]->control_ref->State();
ControlState x = controls[3]->control_ref->State() - controls[2]->control_ref->State();
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();
// Return raw values. (used in UI)
if (!adjusted)
@ -51,6 +51,11 @@ AnalogStick::StateData AnalogStick::GetState(bool adjusted)
return Reshape(x, y, modifier);
}
AnalogStick::StateData AnalogStick::GetState()
{
return GetReshapableState(true);
}
ControlState AnalogStick::GetGateRadiusAtAngle(double ang) const
{
return m_stick_gate->GetRadiusAtAngle(ang);

View File

@ -13,13 +13,16 @@ namespace ControllerEmu
class AnalogStick : public ReshapableInput
{
public:
typedef ReshapeData StateData;
AnalogStick(const char* name, std::unique_ptr<StickGate>&& stick_gate);
AnalogStick(const char* name, const char* ui_name, std::unique_ptr<StickGate>&& stick_gate);
StateData GetState(bool adjusted = true) override;
StateData GetReshapableState(bool adjusted) final override;
ControlState GetGateRadiusAtAngle(double ang) const override;
StateData GetState();
private:
std::unique_ptr<StickGate> m_stick_gate;
};

View File

@ -21,7 +21,8 @@
namespace ControllerEmu
{
Cursor::Cursor(const std::string& name_) : ControlGroup(name_, GroupType::Cursor)
Cursor::Cursor(const std::string& name_)
: ReshapableInput(name_, name_, GroupType::Cursor), m_last_update(Clock::now())
{
for (auto& named_direction : named_directions)
controls.emplace_back(std::make_unique<Input>(Translate, named_direction));
@ -31,89 +32,121 @@ Cursor::Cursor(const std::string& name_) : ControlGroup(name_, GroupType::Cursor
controls.emplace_back(std::make_unique<Input>(Translate, _trans("Hide")));
controls.emplace_back(std::make_unique<Input>(Translate, _trans("Recenter")));
// Default shape is a 1.0 square (no resizing/reshaping):
AddReshapingSettings(1.0, 0.5, 50);
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Center"), 0.5));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Width"), 0.5));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Height"), 0.5));
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 20));
boolean_settings.emplace_back(std::make_unique<BooleanSetting>(_trans("Relative Input"), false));
boolean_settings.emplace_back(std::make_unique<BooleanSetting>(_trans("Auto-Hide"), false));
}
ReshapableInput::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();
// Return raw values. (used in UI)
if (!adjusted)
return {x, y};
return Reshape(x, y, 0.0);
}
ControlState Cursor::GetGateRadiusAtAngle(double ang) const
{
// TODO: Change this to 0.5 and adjust the math,
// so pointer doesn't have to be clamped to the configured width/height?
return SquareStickGate(1.0).GetRadiusAtAngle(ang);
}
Cursor::StateData Cursor::GetState(const bool adjusted)
{
const ControlState zz = controls[4]->control_ref->State() - controls[5]->control_ref->State();
ControlState z = controls[4]->control_ref->State() - controls[5]->control_ref->State();
// silly being here
if (zz > m_state.z)
m_state.z = std::min(m_state.z + 0.1, zz);
else if (zz < m_state.z)
m_state.z = std::max(m_state.z - 0.1, zz);
StateData result;
result.z = m_state.z;
if (m_autohide_timer > -1)
if (!adjusted)
{
--m_autohide_timer;
const auto raw_input = GetReshapableState(false);
return {raw_input.x, raw_input.y, z};
}
ControlState yy = controls[0]->control_ref->State() - controls[1]->control_ref->State();
ControlState xx = controls[3]->control_ref->State() - controls[2]->control_ref->State();
const auto input = GetReshapableState(true);
const ControlState deadzone = numeric_settings[3]->GetValue();
// TODO: Using system time is ugly.
// Kill this after state is moved into wiimote rather than this class.
const auto now = Clock::now();
const auto ms_since_update =
std::chrono::duration_cast<std::chrono::milliseconds>(now - m_last_update).count();
m_last_update = now;
// reset auto-hide timer
if (std::abs(m_prev_xx - xx) > deadzone || std::abs(m_prev_yy - yy) > deadzone)
{
m_autohide_timer = TIMER_VALUE;
}
const double max_step = STEP_PER_SEC / 1000.0 * ms_since_update;
const double max_z_step = STEP_Z_PER_SEC / 1000.0 * ms_since_update;
// hide
const bool autohide = boolean_settings[1]->GetValue() && m_autohide_timer < 0;
if (controls[6]->control_ref->State() > 0.5 || autohide)
// Apply deadzone to z:
const ControlState deadzone = numeric_settings[SETTING_DEADZONE]->GetValue();
z = std::copysign(std::max(0.0, std::abs(z) - deadzone) / (1.0 - deadzone), z);
// Smooth out z movement:
// FYI: Not using relative input for Z.
m_state.z += MathUtil::Clamp(z - m_state.z, -max_z_step, max_z_step);
// Relative input:
if (boolean_settings[0]->GetValue())
{
result.x = 10000;
result.y = 0;
}
else
{
// adjust cursor according to settings
if (adjusted)
// Recenter:
if (controls[7]->control_ref->State() > BUTTON_THRESHOLD)
{
xx *= (numeric_settings[1]->GetValue() * 2);
yy *= (numeric_settings[2]->GetValue() * 2);
yy += (numeric_settings[0]->GetValue() - 0.5);
}
// relative input
if (boolean_settings[0]->GetValue())
{
// deadzone to avoid the cursor slowly drifting
if (std::abs(xx) > deadzone)
m_state.x = MathUtil::Clamp(m_state.x + xx * SPEED_MULTIPLIER, -1.0, 1.0);
if (std::abs(yy) > deadzone)
m_state.y = MathUtil::Clamp(m_state.y + yy * SPEED_MULTIPLIER, -1.0, 1.0);
// recenter
if (controls[7]->control_ref->State() > 0.5)
{
m_state.x = 0.0;
m_state.y = 0.0;
}
m_state.x = 0.0;
m_state.y = 0.0;
}
else
{
m_state.x = xx;
m_state.y = yy;
m_state.x = MathUtil::Clamp(m_state.x + input.x * max_step, -1.0, 1.0);
m_state.y = MathUtil::Clamp(m_state.y + input.y * max_step, -1.0, 1.0);
}
result.x = m_state.x;
result.y = m_state.y;
}
// Absolute input:
else
{
m_state.x = input.x;
m_state.y = input.y;
}
m_prev_xx = xx;
m_prev_yy = yy;
StateData result = m_state;
// Adjust cursor according to settings:
result.x *= (numeric_settings[SETTING_WIDTH]->GetValue() * 2);
result.y *= (numeric_settings[SETTING_HEIGHT]->GetValue() * 2);
result.y += (numeric_settings[SETTING_CENTER]->GetValue() - 0.5);
const bool autohide = boolean_settings[1]->GetValue();
// Auto-hide timer:
// TODO: should Z movement reset this?
if (!autohide || std::abs(m_prev_result.x - result.x) > AUTO_HIDE_DEADZONE ||
std::abs(m_prev_result.y - result.y) > AUTO_HIDE_DEADZONE)
{
m_auto_hide_timer = AUTO_HIDE_MS;
}
else if (m_auto_hide_timer)
{
m_auto_hide_timer -= std::min<int>(ms_since_update, m_auto_hide_timer);
}
m_prev_result = result;
// If auto-hide time is up or hide button is held:
if (!m_auto_hide_timer || controls[6]->control_ref->State() > BUTTON_THRESHOLD)
{
// TODO: Use NaN or something:
result.x = 10000;
result.y = 0;
}
return result;
}
} // namespace ControllerEmu

View File

@ -4,13 +4,15 @@
#pragma once
#include <chrono>
#include <string>
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
#include "InputCommon/ControllerEmu/StickGate.h"
#include "InputCommon/ControllerInterface/Device.h"
namespace ControllerEmu
{
class Cursor : public ControlGroup
class Cursor : public ReshapableInput
{
public:
struct StateData
@ -20,22 +22,42 @@ public:
ControlState z{};
};
enum
{
SETTING_CENTER = ReshapableInput::SETTING_COUNT,
SETTING_WIDTH,
SETTING_HEIGHT,
};
explicit Cursor(const std::string& name);
StateData GetState(bool adjusted = false);
ReshapeData GetReshapableState(bool adjusted) final override;
ControlState GetGateRadiusAtAngle(double ang) const override;
StateData GetState(bool adjusted);
private:
// This is used to reduce the cursor speed for relative input
// to something that makes sense with the default range.
static constexpr double SPEED_MULTIPLIER = 0.04;
static constexpr double STEP_PER_SEC = 0.04 * 200;
// Sets the length for the auto-hide timer
static constexpr int TIMER_VALUE = 500;
// Smooth out forward/backward movements:
static constexpr double STEP_Z_PER_SEC = 0.05 * 200;
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;
int m_autohide_timer = TIMER_VALUE;
ControlState m_prev_xx;
ControlState m_prev_yy;
// Adjusted:
StateData m_prev_result;
int m_auto_hide_timer = AUTO_HIDE_MS;
typedef std::chrono::steady_clock Clock;
Clock::time_point m_last_update;
};
} // namespace ControllerEmu

View File

@ -10,6 +10,8 @@
#include <string>
#include "Common/Common.h"
#include "Common/MathUtil.h"
#include "InputCommon/ControlReference/ControlReference.h"
#include "InputCommon/ControllerEmu/Control/Control.h"
#include "InputCommon/ControllerEmu/Control/Input.h"
@ -35,10 +37,10 @@ Tilt::Tilt(const std::string& name_)
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Angle"), 0.9, 0, 180));
}
Tilt::StateData Tilt::GetState(bool adjusted)
Tilt::StateData Tilt::GetReshapableState(bool adjusted)
{
ControlState y = controls[0]->control_ref->State() - controls[1]->control_ref->State();
ControlState x = controls[3]->control_ref->State() - controls[2]->control_ref->State();
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();
// Return raw values. (used in UI)
if (!adjusted)
@ -49,26 +51,37 @@ Tilt::StateData Tilt::GetState(bool adjusted)
// Compute desired tilt:
StateData target = Reshape(x, y, modifier);
// Step the simulation. This is pretty ugly being here.
// Step the simulation. This is somewhat ugly being here.
// We should be able to GetState without changing state.
// State should be stored outside of this object inside the wiimote,
// and separately inside the UI.
// We're using system time rather than ticks to step this.
// I don't think that's too horrible as we can consider this part of user input.
// And at least the Mapping UI will behave sanely this way.
// TODO: when state is moved outside of this class have a separate Step()
// function that takes a ms_passed argument
const auto now = Clock::now();
const auto ms_since_update =
std::chrono::duration_cast<std::chrono::milliseconds>(now - m_last_update).count();
m_last_update = now;
constexpr int MAX_DEG_PER_SEC = 360 * 2;
const double MAX_STEP = MAX_DEG_PER_SEC / 180.0 * ms_since_update / 1000;
const double max_step = MAX_DEG_PER_SEC / 180.0 * ms_since_update / 1000;
// TODO: Allow wrap around from 1.0 to -1.0
// (take the fastest route to target)
const double diff_x = (target.x - m_tilt.x);
m_tilt.x += std::min(MAX_STEP, std::abs(diff_x)) * ((diff_x < 0) ? -1 : 1);
const double diff_y = (target.y - m_tilt.y);
m_tilt.y += std::min(MAX_STEP, std::abs(diff_y)) * ((diff_y < 0) ? -1 : 1);
m_tilt.x += MathUtil::Clamp(target.x - m_tilt.x, -max_step, max_step);
m_tilt.y += MathUtil::Clamp(target.y - m_tilt.y, -max_step, max_step);
return m_tilt;
}
Tilt::StateData Tilt::GetState()
{
return GetReshapableState(true);
}
ControlState Tilt::GetGateRadiusAtAngle(double ang) const
{
const ControlState max_tilt_angle = numeric_settings[SETTING_MAX_ANGLE]->GetValue() / 1.8;

View File

@ -15,6 +15,8 @@ namespace ControllerEmu
class Tilt : public ReshapableInput
{
public:
typedef ReshapeData StateData;
enum
{
SETTING_MAX_ANGLE = ReshapableInput::SETTING_COUNT,
@ -22,14 +24,17 @@ public:
explicit Tilt(const std::string& name);
StateData GetState(bool adjusted = true);
StateData GetReshapableState(bool adjusted) final override;
ControlState GetGateRadiusAtAngle(double ang) const override;
StateData GetState();
private:
typedef std::chrono::steady_clock Clock;
static constexpr int MAX_DEG_PER_SEC = 360 * 6;
StateData m_tilt;
typedef std::chrono::steady_clock Clock;
Clock::time_point m_last_update;
};
} // namespace ControllerEmu

View File

@ -76,8 +76,8 @@ void ReshapableInput::AddReshapingSettings(ControlState default_radius, ControlS
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
}
ReshapableInput::StateData ReshapableInput::Reshape(ControlState x, ControlState y,
ControlState modifier)
ReshapableInput::ReshapeData ReshapableInput::Reshape(ControlState x, ControlState y,
ControlState modifier)
{
// TODO: make the AtAngle functions work with negative angles:
const ControlState ang = std::atan2(y, x) + MathUtil::TAU;

View File

@ -58,7 +58,7 @@ class ReshapableInput : public ControlGroup
public:
ReshapableInput(std::string name, std::string ui_name, GroupType type);
struct StateData
struct ReshapeData
{
ControlState x{};
ControlState y{};
@ -77,13 +77,13 @@ public:
ControlState GetInputRadiusAtAngle(double ang) const;
virtual ControlState GetGateRadiusAtAngle(double ang) const = 0;
virtual StateData GetState(bool adjusted = true) = 0;
virtual ReshapeData GetReshapableState(bool adjusted) = 0;
protected:
void AddReshapingSettings(ControlState default_radius, ControlState default_shape,
int max_deadzone);
StateData Reshape(ControlState x, ControlState y, ControlState modifier = 0.0);
ReshapeData Reshape(ControlState x, ControlState y, ControlState modifier = 0.0);
private:
ControlState CalculateInputShapeRadiusAtAngle(double ang) const;