From ceb28a2302e23b2484bca63626605d3fd56a1f11 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Tue, 18 Dec 2018 20:06:16 -0600 Subject: Add ability to reshape analog sticks from square/circle to octagon and make the analog stick mapping indicator pretty. --- .../ControllerEmu/ControlGroup/AnalogStick.cpp | 143 ++++++++++++++++++--- .../ControllerEmu/ControlGroup/AnalogStick.h | 73 ++++++++++- 2 files changed, 192 insertions(+), 24 deletions(-) (limited to 'Source/Core/InputCommon/ControllerEmu') diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp index c38e5f1d3a..5368c6b7ba 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp @@ -9,6 +9,7 @@ #include #include "Common/Common.h" +#include "Common/MathUtil.h" #include "InputCommon/ControlReference/ControlReference.h" #include "InputCommon/ControllerEmu/Control/Control.h" @@ -18,54 +19,158 @@ namespace ControllerEmu { -AnalogStick::AnalogStick(const char* const name_, ControlState default_radius) - : AnalogStick(name_, name_, default_radius) +AnalogStick::AnalogStick(const char* const name_, std::unique_ptr&& stick_gate) + : AnalogStick(name_, name_, std::move(stick_gate)) { } AnalogStick::AnalogStick(const char* const name_, const char* const ui_name_, - ControlState default_radius) - : ControlGroup(name_, ui_name_, GroupType::Stick) + std::unique_ptr&& stick_gate) + : ControlGroup(name_, ui_name_, GroupType::Stick), m_stick_gate(std::move(stick_gate)) { for (auto& named_direction : named_directions) controls.emplace_back(std::make_unique(Translate, named_direction)); controls.emplace_back(std::make_unique(Translate, _trans("Modifier"))); + + numeric_settings.emplace_back( + std::make_unique(_trans("Input Radius"), 1.0, 0, 100)); numeric_settings.emplace_back( - std::make_unique(_trans("Radius"), default_radius, 0, 100)); + std::make_unique(_trans("Input Shape"), 0.0, 0, 50)); numeric_settings.emplace_back(std::make_unique(_trans("Dead Zone"), 0, 0, 50)); } -AnalogStick::StateData AnalogStick::GetState() +AnalogStick::StateData AnalogStick::GetState(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 radius = numeric_settings[SETTING_RADIUS]->GetValue(); - const ControlState deadzone = numeric_settings[SETTING_DEADZONE]->GetValue(); - const ControlState m = controls[4]->control_ref->State(); + // Return raw values. (used in UI) + if (!adjusted) + return {x, y}; - const ControlState ang = atan2(y, x); + // TODO: make the AtAngle functions work with negative angles: + const ControlState ang = atan2(y, x) + MathUtil::TAU; const ControlState ang_sin = sin(ang); const ControlState ang_cos = cos(ang); - ControlState dist = sqrt(x * x + y * y); + const ControlState gate_max_dist = GetGateRadiusAtAngle(ang); + const ControlState input_max_dist = GetInputRadiusAtAngle(ang); - // dead zone code - dist = std::max(0.0, dist - deadzone); - dist /= (1 - deadzone); + // If input radius is zero we apply no scaling. + // This is useful when mapping native controllers without knowing intimate radius details. + const ControlState max_dist = input_max_dist ? input_max_dist : gate_max_dist; - // radius - dist *= radius; + ControlState dist = std::sqrt(x * x + y * y) / max_dist; - // The modifier halves the distance by 50%, which is useful - // for keyboard controls. - if (m) + // If the modifier is pressed, scale the distance by the modifier's value. + // This is affected by the modifier's "range" setting which defaults to 50%. + const ControlState modifier = controls[4]->control_ref->State(); + if (modifier) + { + // TODO: Modifier's range setting gets reset to 100% when the clear button is clicked. + // This causes the modifier to not behave how a user might suspect. + // Retaining the old scale-by-50% behavior until range is fixed to clear to 50%. dist *= 0.5; + // dist *= modifier; + } + + // Apply deadzone as a percentage of the user-defined radius/shape: + const ControlState deadzone = GetDeadzoneRadiusAtAngle(ang); + dist = std::max(0.0, dist - deadzone) / (1.0 - deadzone); + + // Scale to the gate shape/radius: + dist = dist *= gate_max_dist; y = std::max(-1.0, std::min(1.0, ang_sin * dist)); x = std::max(-1.0, std::min(1.0, ang_cos * dist)); return {x, y}; } + +ControlState AnalogStick::GetGateRadiusAtAngle(double ang) const +{ + return m_stick_gate->GetRadiusAtAngle(ang); +} + +ControlState AnalogStick::GetDeadzoneRadiusAtAngle(double ang) const +{ + return CalculateInputShapeRadiusAtAngle(ang) * numeric_settings[SETTING_DEADZONE]->GetValue(); +} + +ControlState AnalogStick::GetInputRadiusAtAngle(double ang) const +{ + return CalculateInputShapeRadiusAtAngle(ang) * numeric_settings[SETTING_INPUT_RADIUS]->GetValue(); +} + +ControlState AnalogStick::CalculateInputShapeRadiusAtAngle(double ang) const +{ + const auto shape = numeric_settings[SETTING_INPUT_SHAPE]->GetValue() * 4.0; + + if (shape < 1.0) + { + // Between 0 and 25 return a shape between octagon and circle + const auto amt = shape; + return OctagonStickGate::ComputeRadiusAtAngle(ang) * (1 - amt) + amt; + } + else + { + // Between 25 and 50 return a shape between circle and square + const auto amt = shape - 1.0; + return (1 - amt) + SquareStickGate::ComputeRadiusAtAngle(ang) * amt; + } +} + +OctagonStickGate::OctagonStickGate(ControlState radius) : m_radius(radius) +{ +} + +ControlState OctagonStickGate::GetRadiusAtAngle(double ang) const +{ + return ComputeRadiusAtAngle(ang) * m_radius; +} + +ControlState OctagonStickGate::ComputeRadiusAtAngle(double ang) +{ + // Ratio of octagon circumcircle to incircle: + const double incircle_radius = 0.923879532511287; + const double section_ang = MathUtil::TAU / 8; + return incircle_radius / std::cos(std::fmod(ang, section_ang) - section_ang / 2); +} + +RoundStickGate::RoundStickGate(ControlState radius) : m_radius(radius) +{ +} + +ControlState RoundStickGate::GetRadiusAtAngle(double) const +{ + return m_radius; +} + +SquareStickGate::SquareStickGate(ControlState half_width) : m_half_width(half_width) +{ +} + +ControlState SquareStickGate::GetRadiusAtAngle(double ang) const +{ + return ComputeRadiusAtAngle(ang) * m_half_width; +} + +ControlState SquareStickGate::ComputeRadiusAtAngle(double ang) +{ + const double section_ang = MathUtil::TAU / 4; + return 1 / std::cos(std::fmod(ang + section_ang / 2, section_ang) - section_ang / 2); +} + +OctagonAnalogStick::OctagonAnalogStick(const char* name, ControlState gate_radius) + : OctagonAnalogStick(name, name, gate_radius) +{ +} + +OctagonAnalogStick::OctagonAnalogStick(const char* name, const char* ui_name, + ControlState gate_radius) + : AnalogStick(name, ui_name, std::make_unique(gate_radius)) +{ +} + } // namespace ControllerEmu diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h index d8d8e4ddea..336669124a 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h @@ -9,12 +9,57 @@ namespace ControllerEmu { +// An abstract class representing the plastic shell that limits an analog stick's movement. +class StickGate +{ +public: + // Angle is in radians and should be non-negative + virtual ControlState GetRadiusAtAngle(double ang) const = 0; +}; + +// An octagon-shaped stick gate is found on most Nintendo GC/Wii analog sticks. +class OctagonStickGate : public StickGate +{ +public: + explicit OctagonStickGate(ControlState radius); + ControlState GetRadiusAtAngle(double ang) const override; + + static ControlState ComputeRadiusAtAngle(double ang); + + const ControlState m_radius; +}; + +// A round-shaped stick gate. Possibly found on 3rd-party accessories. +class RoundStickGate : public StickGate +{ +public: + explicit RoundStickGate(ControlState radius); + ControlState GetRadiusAtAngle(double ang) const override; + +private: + const ControlState m_radius; +}; + +// A square-shaped stick gate. e.g. keyboard input. +class SquareStickGate : public StickGate +{ +public: + explicit SquareStickGate(ControlState half_width); + ControlState GetRadiusAtAngle(double ang) const override; + + static ControlState ComputeRadiusAtAngle(double ang); + +private: + const ControlState m_half_width; +}; + class AnalogStick : public ControlGroup { public: enum { - SETTING_RADIUS, + SETTING_INPUT_RADIUS, + SETTING_INPUT_SHAPE, SETTING_DEADZONE, }; @@ -24,10 +69,28 @@ public: ControlState y{}; }; - // The GameCube controller and Wiimote attachments have a different default radius - AnalogStick(const char* name, ControlState default_radius); - AnalogStick(const char* name, const char* ui_name, ControlState default_radius); + AnalogStick(const char* name, std::unique_ptr&& stick_gate); + AnalogStick(const char* name, const char* ui_name, std::unique_ptr&& stick_gate); + + StateData GetState(bool adjusted = true); - StateData GetState(); + // Angle is in radians and should be non-negative + ControlState GetGateRadiusAtAngle(double ang) const; + ControlState GetDeadzoneRadiusAtAngle(double ang) const; + ControlState GetInputRadiusAtAngle(double ang) const; + +private: + ControlState CalculateInputShapeRadiusAtAngle(double ang) const; + + std::unique_ptr m_stick_gate; }; + +// An AnalogStick with an OctagonStickGate +class OctagonAnalogStick : public AnalogStick +{ +public: + OctagonAnalogStick(const char* name, ControlState gate_radius); + OctagonAnalogStick(const char* name, const char* ui_name, ControlState gate_radius); +}; + } // namespace ControllerEmu -- cgit v1.2.3 From da9bcf83efeb1ede43e1c2ac08e6869ce8a2c15a Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Fri, 21 Dec 2018 16:11:00 -0600 Subject: InputCommon: Simplified StickGate interface and moved class into its own file. Changed default input radius to perform no resizing. Tweaked the indicator colors a bit to improve visibility. Cleaned up some math and code. --- .../ControllerEmu/ControlGroup/AnalogStick.cpp | 60 +++------------------- .../ControllerEmu/ControlGroup/AnalogStick.h | 45 +--------------- .../Core/InputCommon/ControllerEmu/StickGate.cpp | 47 +++++++++++++++++ Source/Core/InputCommon/ControllerEmu/StickGate.h | 55 ++++++++++++++++++++ 4 files changed, 111 insertions(+), 96 deletions(-) create mode 100644 Source/Core/InputCommon/ControllerEmu/StickGate.cpp create mode 100644 Source/Core/InputCommon/ControllerEmu/StickGate.h (limited to 'Source/Core/InputCommon/ControllerEmu') diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp index 5368c6b7ba..3f766d5e45 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp @@ -4,9 +4,7 @@ #include "InputCommon/ControllerEmu/ControlGroup/AnalogStick.h" -#include #include -#include #include "Common/Common.h" #include "Common/MathUtil.h" @@ -33,8 +31,10 @@ AnalogStick::AnalogStick(const char* const name_, const char* const ui_name_, controls.emplace_back(std::make_unique(Translate, _trans("Modifier"))); + // Set default input radius to that of the gate radius (no resizing) numeric_settings.emplace_back( - std::make_unique(_trans("Input Radius"), 1.0, 0, 100)); + std::make_unique(_trans("Input Radius"), GetGateRadiusAtAngle(0.0), 0, 100)); + // Set default input shape to an octagon (no reshaping) numeric_settings.emplace_back( std::make_unique(_trans("Input Shape"), 0.0, 0, 50)); numeric_settings.emplace_back(std::make_unique(_trans("Dead Zone"), 0, 0, 50)); @@ -50,9 +50,7 @@ AnalogStick::StateData AnalogStick::GetState(bool adjusted) return {x, y}; // TODO: make the AtAngle functions work with negative angles: - const ControlState ang = atan2(y, x) + MathUtil::TAU; - const ControlState ang_sin = sin(ang); - const ControlState ang_cos = cos(ang); + const ControlState ang = std::atan2(y, x) + MathUtil::TAU; const ControlState gate_max_dist = GetGateRadiusAtAngle(ang); const ControlState input_max_dist = GetInputRadiusAtAngle(ang); @@ -82,9 +80,8 @@ AnalogStick::StateData AnalogStick::GetState(bool adjusted) // Scale to the gate shape/radius: dist = dist *= gate_max_dist; - y = std::max(-1.0, std::min(1.0, ang_sin * dist)); - x = std::max(-1.0, std::min(1.0, ang_cos * dist)); - + x = MathUtil::Clamp(std::cos(ang) * dist, -1.0, 1.0); + y = MathUtil::Clamp(std::sin(ang) * dist, -1.0, 1.0); return {x, y}; } @@ -111,57 +108,16 @@ ControlState AnalogStick::CalculateInputShapeRadiusAtAngle(double ang) const { // Between 0 and 25 return a shape between octagon and circle const auto amt = shape; - return OctagonStickGate::ComputeRadiusAtAngle(ang) * (1 - amt) + amt; + return OctagonStickGate(1).GetRadiusAtAngle(ang) * (1 - amt) + amt; } else { // Between 25 and 50 return a shape between circle and square const auto amt = shape - 1.0; - return (1 - amt) + SquareStickGate::ComputeRadiusAtAngle(ang) * amt; + return (1 - amt) + SquareStickGate(1).GetRadiusAtAngle(ang) * amt; } } -OctagonStickGate::OctagonStickGate(ControlState radius) : m_radius(radius) -{ -} - -ControlState OctagonStickGate::GetRadiusAtAngle(double ang) const -{ - return ComputeRadiusAtAngle(ang) * m_radius; -} - -ControlState OctagonStickGate::ComputeRadiusAtAngle(double ang) -{ - // Ratio of octagon circumcircle to incircle: - const double incircle_radius = 0.923879532511287; - const double section_ang = MathUtil::TAU / 8; - return incircle_radius / std::cos(std::fmod(ang, section_ang) - section_ang / 2); -} - -RoundStickGate::RoundStickGate(ControlState radius) : m_radius(radius) -{ -} - -ControlState RoundStickGate::GetRadiusAtAngle(double) const -{ - return m_radius; -} - -SquareStickGate::SquareStickGate(ControlState half_width) : m_half_width(half_width) -{ -} - -ControlState SquareStickGate::GetRadiusAtAngle(double ang) const -{ - return ComputeRadiusAtAngle(ang) * m_half_width; -} - -ControlState SquareStickGate::ComputeRadiusAtAngle(double ang) -{ - const double section_ang = MathUtil::TAU / 4; - return 1 / std::cos(std::fmod(ang + section_ang / 2, section_ang) - section_ang / 2); -} - OctagonAnalogStick::OctagonAnalogStick(const char* name, ControlState gate_radius) : OctagonAnalogStick(name, name, gate_radius) { diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h index 336669124a..2351189f74 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.h @@ -5,54 +5,11 @@ #pragma once #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h" +#include "InputCommon/ControllerEmu/StickGate.h" #include "InputCommon/ControllerInterface/Device.h" namespace ControllerEmu { -// An abstract class representing the plastic shell that limits an analog stick's movement. -class StickGate -{ -public: - // Angle is in radians and should be non-negative - virtual ControlState GetRadiusAtAngle(double ang) const = 0; -}; - -// An octagon-shaped stick gate is found on most Nintendo GC/Wii analog sticks. -class OctagonStickGate : public StickGate -{ -public: - explicit OctagonStickGate(ControlState radius); - ControlState GetRadiusAtAngle(double ang) const override; - - static ControlState ComputeRadiusAtAngle(double ang); - - const ControlState m_radius; -}; - -// A round-shaped stick gate. Possibly found on 3rd-party accessories. -class RoundStickGate : public StickGate -{ -public: - explicit RoundStickGate(ControlState radius); - ControlState GetRadiusAtAngle(double ang) const override; - -private: - const ControlState m_radius; -}; - -// A square-shaped stick gate. e.g. keyboard input. -class SquareStickGate : public StickGate -{ -public: - explicit SquareStickGate(ControlState half_width); - ControlState GetRadiusAtAngle(double ang) const override; - - static ControlState ComputeRadiusAtAngle(double ang); - -private: - const ControlState m_half_width; -}; - class AnalogStick : public ControlGroup { public: diff --git a/Source/Core/InputCommon/ControllerEmu/StickGate.cpp b/Source/Core/InputCommon/ControllerEmu/StickGate.cpp new file mode 100644 index 0000000000..c0c2a668a3 --- /dev/null +++ b/Source/Core/InputCommon/ControllerEmu/StickGate.cpp @@ -0,0 +1,47 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "InputCommon/ControllerEmu/StickGate.h" + +#include + +#include "Common/MathUtil.h" + +namespace ControllerEmu +{ +OctagonStickGate::OctagonStickGate(ControlState radius) : m_radius(radius) +{ +} + +ControlState OctagonStickGate::GetRadiusAtAngle(double ang) const +{ + constexpr int sides = 8; + constexpr double sum_int_angles = (sides - 2) * MathUtil::PI; + constexpr double half_int_angle = sum_int_angles / sides / 2; + + ang = std::fmod(ang, MathUtil::TAU / sides); + // Solve ASA triangle using The Law of Sines: + return m_radius / std::sin(MathUtil::PI - ang - half_int_angle) * std::sin(half_int_angle); +} + +RoundStickGate::RoundStickGate(ControlState radius) : m_radius(radius) +{ +} + +ControlState RoundStickGate::GetRadiusAtAngle(double) const +{ + return m_radius; +} + +SquareStickGate::SquareStickGate(ControlState half_width) : m_half_width(half_width) +{ +} + +ControlState SquareStickGate::GetRadiusAtAngle(double ang) const +{ + constexpr double section_ang = MathUtil::TAU / 4; + return m_half_width / std::cos(std::fmod(ang + section_ang / 2, section_ang) - section_ang / 2); +} + +} // namespace ControllerEmu diff --git a/Source/Core/InputCommon/ControllerEmu/StickGate.h b/Source/Core/InputCommon/ControllerEmu/StickGate.h new file mode 100644 index 0000000000..decb15d234 --- /dev/null +++ b/Source/Core/InputCommon/ControllerEmu/StickGate.h @@ -0,0 +1,55 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include "InputCommon/ControlReference/ControlReference.h" + +namespace ControllerEmu +{ +// An abstract class representing the plastic shell that limits an analog stick's movement. +class StickGate +{ +public: + // Angle is in radians and should be non-negative + virtual ControlState GetRadiusAtAngle(double ang) const = 0; + + virtual ~StickGate() = default; +}; + +// An octagon-shaped stick gate is found on most Nintendo GC/Wii analog sticks. +class OctagonStickGate : public StickGate +{ +public: + // Radius of circumscribed circle + explicit OctagonStickGate(ControlState radius); + ControlState GetRadiusAtAngle(double ang) const override final; + +private: + const ControlState m_radius; +}; + +// A round-shaped stick gate. Possibly found on 3rd-party accessories. +class RoundStickGate : public StickGate +{ +public: + explicit RoundStickGate(ControlState radius); + ControlState GetRadiusAtAngle(double ang) const override final; + +private: + const ControlState m_radius; +}; + +// A square-shaped stick gate. e.g. keyboard input. +class SquareStickGate : public StickGate +{ +public: + explicit SquareStickGate(ControlState half_width); + ControlState GetRadiusAtAngle(double ang) const override final; + +private: + const ControlState m_half_width; +}; + +} // namespace ControllerEmu -- cgit v1.2.3 From c614f5f534cdb7a083387505aff90e8be94fed73 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Thu, 27 Dec 2018 19:16:37 -0600 Subject: ControllerEmu: Allow analog stick input radius greater than one. Useful for rounded-square inputs of xbox controllers. --- .../Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'Source/Core/InputCommon/ControllerEmu') diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp index 3f766d5e45..9db29909fd 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/AnalogStick.cpp @@ -32,8 +32,10 @@ AnalogStick::AnalogStick(const char* const name_, const char* const ui_name_, controls.emplace_back(std::make_unique(Translate, _trans("Modifier"))); // Set default input radius to that of the gate radius (no resizing) + // Allow radius greater than 1.0 for definitions of rounded squares + // This is ideal for Xbox controllers (and probably others) numeric_settings.emplace_back( - std::make_unique(_trans("Input Radius"), GetGateRadiusAtAngle(0.0), 0, 100)); + std::make_unique(_trans("Input Radius"), GetGateRadiusAtAngle(0.0), 0, 140)); // Set default input shape to an octagon (no reshaping) numeric_settings.emplace_back( std::make_unique(_trans("Input Shape"), 0.0, 0, 50)); @@ -97,7 +99,10 @@ ControlState AnalogStick::GetDeadzoneRadiusAtAngle(double ang) const ControlState AnalogStick::GetInputRadiusAtAngle(double ang) const { - return CalculateInputShapeRadiusAtAngle(ang) * numeric_settings[SETTING_INPUT_RADIUS]->GetValue(); + const ControlState radius = + CalculateInputShapeRadiusAtAngle(ang) * numeric_settings[SETTING_INPUT_RADIUS]->GetValue(); + // Clamp within the -1 to +1 square as input radius may be greater than 1.0: + return std::min(radius, SquareStickGate(1).GetRadiusAtAngle(ang)); } ControlState AnalogStick::CalculateInputShapeRadiusAtAngle(double ang) const -- cgit v1.2.3