2018-12-21 22:11:00 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-12-21 22:11:00 +00:00
|
|
|
|
|
|
|
#include "InputCommon/ControllerEmu/StickGate.h"
|
|
|
|
|
2017-12-25 23:38:44 +00:00
|
|
|
#include <algorithm>
|
2018-12-21 22:11:00 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2019-11-22 19:10:28 +00:00
|
|
|
#include <fmt/format.h>
|
2024-09-22 07:24:21 +00:00
|
|
|
#include <fmt/ranges.h>
|
2019-11-22 19:10:28 +00:00
|
|
|
|
2018-12-29 22:06:03 +00:00
|
|
|
#include "Common/Common.h"
|
2018-12-21 22:11:00 +00:00
|
|
|
#include "Common/MathUtil.h"
|
2019-02-05 00:50:07 +00:00
|
|
|
#include "Common/Matrix.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "InputCommon/ControllerEmu/Control/Control.h"
|
2018-12-29 22:06:03 +00:00
|
|
|
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
|
2018-12-21 22:11:00 +00:00
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
constexpr auto CALIBRATION_CONFIG_NAME = "Calibration";
|
|
|
|
constexpr auto CALIBRATION_DEFAULT_VALUE = 1.0;
|
|
|
|
constexpr auto CALIBRATION_CONFIG_SCALE = 100;
|
|
|
|
|
2019-04-12 20:26:34 +00:00
|
|
|
constexpr auto CENTER_CONFIG_NAME = "Center";
|
|
|
|
constexpr auto CENTER_CONFIG_SCALE = 100;
|
|
|
|
|
2020-09-19 13:40:36 +00:00
|
|
|
// Calculate distance to intersection of a ray with a line segment defined by two points.
|
|
|
|
std::optional<double> GetRayLineIntersection(Common::DVec2 ray, Common::DVec2 point1,
|
|
|
|
Common::DVec2 point2)
|
2019-02-05 00:50:07 +00:00
|
|
|
{
|
|
|
|
const auto diff = point2 - point1;
|
|
|
|
|
|
|
|
const auto dot = diff.Dot({-ray.y, ray.x});
|
|
|
|
if (std::abs(dot) < 0.00001)
|
|
|
|
{
|
2020-09-19 13:40:36 +00:00
|
|
|
// Both points are on top of eachother.
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto segment_position = point1.Dot({ray.y, -ray.x}) / dot;
|
|
|
|
if (segment_position < -0.00001 || segment_position > 1.00001)
|
|
|
|
{
|
|
|
|
// Ray does not pass through segment.
|
|
|
|
return std::nullopt;
|
2019-02-05 00:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return diff.Cross(-point1) / dot;
|
|
|
|
}
|
|
|
|
|
2020-10-29 22:11:34 +00:00
|
|
|
double GetNearestNotch(double angle, double virtual_notch_angle)
|
|
|
|
{
|
|
|
|
constexpr auto sides = 8;
|
|
|
|
constexpr auto rounding = MathUtil::TAU / sides;
|
|
|
|
const auto closest_notch = std::round(angle / rounding) * rounding;
|
|
|
|
const auto angle_diff =
|
|
|
|
std::fmod(angle - closest_notch + MathUtil::PI, MathUtil::TAU) - MathUtil::PI;
|
|
|
|
return std::abs(angle_diff) < virtual_notch_angle / 2 ? closest_notch : angle;
|
|
|
|
}
|
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
Common::DVec2 GetPointFromAngleAndLength(double angle, double length)
|
|
|
|
{
|
|
|
|
return Common::DVec2{std::cos(angle), std::sin(angle)} * length;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2018-12-21 22:11:00 +00:00
|
|
|
namespace ControllerEmu
|
|
|
|
{
|
2019-02-05 00:50:07 +00:00
|
|
|
constexpr int ReshapableInput::CALIBRATION_SAMPLE_COUNT;
|
|
|
|
|
|
|
|
std::optional<u32> StickGate::GetIdealCalibrationSampleCount() const
|
|
|
|
{
|
2020-02-24 22:55:44 +00:00
|
|
|
return std::nullopt;
|
2019-02-05 00:50:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 22:11:00 +00:00
|
|
|
OctagonStickGate::OctagonStickGate(ControlState radius) : m_radius(radius)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
ControlState OctagonStickGate::GetRadiusAtAngle(double angle) const
|
2018-12-21 22:11:00 +00:00
|
|
|
{
|
|
|
|
constexpr int sides = 8;
|
|
|
|
constexpr double sum_int_angles = (sides - 2) * MathUtil::PI;
|
|
|
|
constexpr double half_int_angle = sum_int_angles / sides / 2;
|
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
angle = std::fmod(angle, MathUtil::TAU / sides);
|
2018-12-21 22:11:00 +00:00
|
|
|
// Solve ASA triangle using The Law of Sines:
|
2019-02-05 00:50:07 +00:00
|
|
|
return m_radius / std::sin(MathUtil::PI - angle - half_int_angle) * std::sin(half_int_angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<u32> OctagonStickGate::GetIdealCalibrationSampleCount() const
|
|
|
|
{
|
|
|
|
return 8;
|
2018-12-21 22:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RoundStickGate::RoundStickGate(ControlState radius) : m_radius(radius)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlState RoundStickGate::GetRadiusAtAngle(double) const
|
|
|
|
{
|
|
|
|
return m_radius;
|
|
|
|
}
|
|
|
|
|
2020-02-24 22:55:44 +00:00
|
|
|
std::optional<u32> RoundStickGate::GetIdealCalibrationSampleCount() const
|
|
|
|
{
|
|
|
|
// The "radius" is the same at every angle so a single sample is enough.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-12-21 22:11:00 +00:00
|
|
|
SquareStickGate::SquareStickGate(ControlState half_width) : m_half_width(half_width)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
ControlState SquareStickGate::GetRadiusAtAngle(double angle) const
|
2018-12-21 22:11:00 +00:00
|
|
|
{
|
2019-02-05 00:50:07 +00:00
|
|
|
constexpr double section_angle = MathUtil::TAU / 4;
|
|
|
|
return m_half_width /
|
|
|
|
std::cos(std::fmod(angle + section_angle / 2, section_angle) - section_angle / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<u32> SquareStickGate::GetIdealCalibrationSampleCount() const
|
|
|
|
{
|
|
|
|
// Because angle:0 points to the right we must use 8 samples for our square.
|
|
|
|
return 8;
|
2018-12-21 22:11:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 23:17:23 +00:00
|
|
|
ReshapableInput::ReshapableInput(std::string name_, std::string ui_name_, GroupType type_)
|
|
|
|
: ControlGroup(std::move(name_), std::move(ui_name_), type_)
|
2018-12-29 22:06:03 +00:00
|
|
|
{
|
2021-05-04 20:50:23 +00:00
|
|
|
// 50 is not always enough but users can set it to more with an expression
|
2019-03-27 00:31:03 +00:00
|
|
|
AddDeadzoneSetting(&m_deadzone_setting, 50);
|
2018-12-29 22:06:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
ControlState ReshapableInput::GetDeadzoneRadiusAtAngle(double angle) const
|
2018-12-29 22:06:03 +00:00
|
|
|
{
|
2019-02-05 00:50:07 +00:00
|
|
|
// FYI: deadzone is scaled by input radius which allows the shape to match.
|
2019-03-27 00:31:03 +00:00
|
|
|
return GetInputRadiusAtAngle(angle) * GetDeadzonePercentage();
|
2018-12-29 22:06:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
ControlState ReshapableInput::GetInputRadiusAtAngle(double angle) const
|
2018-12-29 22:06:03 +00:00
|
|
|
{
|
2019-02-05 00:50:07 +00:00
|
|
|
// Handle the "default" state.
|
|
|
|
if (m_calibration.empty())
|
|
|
|
{
|
|
|
|
return GetDefaultInputRadiusAtAngle(angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetCalibrationDataRadiusAtAngle(m_calibration, angle);
|
2018-12-29 22:06:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 00:31:03 +00:00
|
|
|
ControlState ReshapableInput::GetDeadzonePercentage() const
|
|
|
|
{
|
|
|
|
return m_deadzone_setting.GetValue() / 100;
|
|
|
|
}
|
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
ControlState ReshapableInput::GetCalibrationDataRadiusAtAngle(const CalibrationData& data,
|
|
|
|
double angle)
|
2018-12-29 22:06:03 +00:00
|
|
|
{
|
2019-02-05 00:50:07 +00:00
|
|
|
const auto sample_pos = angle / MathUtil::TAU * data.size();
|
|
|
|
// Interpolate the radius between 2 calibration samples.
|
|
|
|
const u32 sample1_index = u32(sample_pos) % data.size();
|
|
|
|
const u32 sample2_index = (sample1_index + 1) % data.size();
|
|
|
|
const double sample1_angle = sample1_index * MathUtil::TAU / data.size();
|
|
|
|
const double sample2_angle = sample2_index * MathUtil::TAU / data.size();
|
|
|
|
|
2020-09-19 13:40:36 +00:00
|
|
|
const auto intersection =
|
|
|
|
GetRayLineIntersection(GetPointFromAngleAndLength(angle, 1.0),
|
|
|
|
GetPointFromAngleAndLength(sample1_angle, data[sample1_index]),
|
|
|
|
GetPointFromAngleAndLength(sample2_angle, data[sample2_index]));
|
|
|
|
|
|
|
|
// Intersection has no value when points are on top of eachother.
|
|
|
|
return intersection.value_or(data[sample1_index]);
|
2019-02-05 00:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ControlState ReshapableInput::GetDefaultInputRadiusAtAngle(double angle) const
|
|
|
|
{
|
|
|
|
// This will normally be the same as the gate radius.
|
|
|
|
// Unless a sub-class is doing weird things with the gate radius (e.g. Tilt)
|
|
|
|
return GetGateRadiusAtAngle(angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReshapableInput::SetCalibrationToDefault()
|
|
|
|
{
|
|
|
|
m_calibration.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReshapableInput::SetCalibrationFromGate(const StickGate& gate)
|
|
|
|
{
|
|
|
|
m_calibration.resize(gate.GetIdealCalibrationSampleCount().value_or(CALIBRATION_SAMPLE_COUNT));
|
|
|
|
|
|
|
|
u32 i = 0;
|
|
|
|
for (auto& val : m_calibration)
|
|
|
|
val = gate.GetRadiusAtAngle(MathUtil::TAU * i++ / m_calibration.size());
|
|
|
|
}
|
|
|
|
|
2020-09-19 13:40:36 +00:00
|
|
|
void ReshapableInput::UpdateCalibrationData(CalibrationData& data, Common::DVec2 point1,
|
|
|
|
Common::DVec2 point2)
|
2019-02-05 00:50:07 +00:00
|
|
|
{
|
2020-09-19 13:40:36 +00:00
|
|
|
for (u32 i = 0; i != data.size(); ++i)
|
2019-02-05 00:50:07 +00:00
|
|
|
{
|
2020-09-19 13:40:36 +00:00
|
|
|
const auto angle = i * MathUtil::TAU / data.size();
|
|
|
|
const auto intersection =
|
|
|
|
GetRayLineIntersection(GetPointFromAngleAndLength(angle, 1.0), point1, point2);
|
2019-02-05 00:50:07 +00:00
|
|
|
|
2020-09-19 13:40:36 +00:00
|
|
|
data[i] = std::max(data[i], intersection.value_or(data[i]));
|
2019-02-05 00:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ReshapableInput::CalibrationData& ReshapableInput::GetCalibrationData() const
|
|
|
|
{
|
|
|
|
return m_calibration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReshapableInput::SetCalibrationData(CalibrationData data)
|
|
|
|
{
|
|
|
|
m_calibration = std::move(data);
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:26:34 +00:00
|
|
|
const ReshapableInput::ReshapeData& ReshapableInput::GetCenter() const
|
|
|
|
{
|
|
|
|
return m_center;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReshapableInput::SetCenter(ReshapableInput::ReshapeData center)
|
|
|
|
{
|
|
|
|
m_center = center;
|
|
|
|
}
|
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
void ReshapableInput::LoadConfig(Common::IniFile::Section* section,
|
|
|
|
const std::string& default_device, const std::string& base_name)
|
2019-02-05 00:50:07 +00:00
|
|
|
{
|
|
|
|
ControlGroup::LoadConfig(section, default_device, base_name);
|
|
|
|
|
|
|
|
const std::string group(base_name + name + '/');
|
2022-01-04 20:09:50 +00:00
|
|
|
|
|
|
|
// Special handling for "Modifier" button "Range" settings which default to 50% instead of 100%.
|
|
|
|
if (const auto* modifier_input = GetModifierInput())
|
|
|
|
{
|
|
|
|
section->Get(group + modifier_input->name + "/Range", &modifier_input->control_ref->range,
|
|
|
|
50.0);
|
|
|
|
modifier_input->control_ref->range /= 100;
|
|
|
|
}
|
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
std::string load_str;
|
|
|
|
section->Get(group + CALIBRATION_CONFIG_NAME, &load_str, "");
|
|
|
|
const auto load_data = SplitString(load_str, ' ');
|
|
|
|
|
|
|
|
m_calibration.assign(load_data.size(), CALIBRATION_DEFAULT_VALUE);
|
|
|
|
|
|
|
|
auto it = load_data.begin();
|
|
|
|
for (auto& sample : m_calibration)
|
|
|
|
{
|
|
|
|
if (TryParse(*(it++), &sample))
|
|
|
|
sample /= CALIBRATION_CONFIG_SCALE;
|
|
|
|
}
|
2019-04-12 20:26:34 +00:00
|
|
|
|
|
|
|
section->Get(group + CENTER_CONFIG_NAME, &load_str, "");
|
|
|
|
const auto center_data = SplitString(load_str, ' ');
|
|
|
|
|
|
|
|
m_center = Common::DVec2();
|
|
|
|
|
|
|
|
if (center_data.size() == 2)
|
|
|
|
{
|
|
|
|
if (TryParse(center_data[0], &m_center.x))
|
|
|
|
m_center.x /= CENTER_CONFIG_SCALE;
|
|
|
|
|
|
|
|
if (TryParse(center_data[1], &m_center.y))
|
|
|
|
m_center.y /= CENTER_CONFIG_SCALE;
|
|
|
|
}
|
2019-02-05 00:50:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
void ReshapableInput::SaveConfig(Common::IniFile::Section* section,
|
|
|
|
const std::string& default_device, const std::string& base_name)
|
2019-02-05 00:50:07 +00:00
|
|
|
{
|
|
|
|
ControlGroup::SaveConfig(section, default_device, base_name);
|
|
|
|
|
|
|
|
const std::string group(base_name + name + '/');
|
2024-04-15 23:32:31 +00:00
|
|
|
|
|
|
|
// Special handling for "Modifier" button "Range" settings which default to 50% instead of 100%.
|
|
|
|
if (const auto* modifier_input = GetModifierInput())
|
|
|
|
{
|
|
|
|
section->Set(group + modifier_input->name + "/Range", modifier_input->control_ref->range * 100,
|
|
|
|
50.0);
|
|
|
|
}
|
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
std::vector<std::string> save_data(m_calibration.size());
|
|
|
|
std::transform(
|
|
|
|
m_calibration.begin(), m_calibration.end(), save_data.begin(),
|
2019-11-22 19:10:28 +00:00
|
|
|
[](ControlState val) { return fmt::format("{:.2f}", val * CALIBRATION_CONFIG_SCALE); });
|
2024-09-22 07:24:21 +00:00
|
|
|
section->Set(group + CALIBRATION_CONFIG_NAME, fmt::to_string(fmt::join(save_data, " ")), "");
|
2019-04-12 20:26:34 +00:00
|
|
|
|
2020-02-24 22:59:23 +00:00
|
|
|
// Save center value.
|
|
|
|
static constexpr char center_format[] = "{:.2f} {:.2f}";
|
|
|
|
const auto center_data = fmt::format(center_format, m_center.x * CENTER_CONFIG_SCALE,
|
2019-11-22 19:10:28 +00:00
|
|
|
m_center.y * CENTER_CONFIG_SCALE);
|
2020-02-24 22:59:23 +00:00
|
|
|
section->Set(group + CENTER_CONFIG_NAME, center_data, fmt::format(center_format, 0.0, 0.0));
|
2018-12-29 22:06:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 15:10:32 +00:00
|
|
|
ReshapableInput::ReshapeData ReshapableInput::Reshape(ControlState x, ControlState y,
|
2021-05-04 21:19:45 +00:00
|
|
|
ControlState modifier,
|
|
|
|
ControlState clamp) const
|
2018-12-29 22:06:03 +00:00
|
|
|
{
|
2019-04-12 20:26:34 +00:00
|
|
|
x -= m_center.x;
|
|
|
|
y -= m_center.y;
|
|
|
|
|
2021-05-04 20:50:23 +00:00
|
|
|
// We run this even if both x and y will be zero.
|
|
|
|
// In that case, std::atan2(0, 0) returns a valid non-NaN value, but the exact value
|
|
|
|
// (which depends on the signs of x and y) does not matter here as dist is zero
|
|
|
|
|
2018-12-29 22:06:03 +00:00
|
|
|
// TODO: make the AtAngle functions work with negative angles:
|
2020-10-29 22:11:34 +00:00
|
|
|
ControlState angle = std::atan2(y, x) + MathUtil::TAU;
|
2018-12-29 22:06:03 +00:00
|
|
|
|
2019-02-05 00:50:07 +00:00
|
|
|
const ControlState input_max_dist = GetInputRadiusAtAngle(angle);
|
2020-10-29 22:11:34 +00:00
|
|
|
ControlState gate_max_dist = GetGateRadiusAtAngle(angle);
|
2018-12-29 22:06:03 +00:00
|
|
|
|
2019-03-01 23:39:23 +00:00
|
|
|
// If input radius (from calibration) is zero apply no scaling to prevent division by zero.
|
2018-12-29 22:06:03 +00:00
|
|
|
const ControlState max_dist = input_max_dist ? input_max_dist : gate_max_dist;
|
|
|
|
|
2019-03-01 23:39:23 +00:00
|
|
|
ControlState dist = Common::DVec2{x, y}.Length() / max_dist;
|
2018-12-29 22:06:03 +00:00
|
|
|
|
2020-10-29 22:11:34 +00:00
|
|
|
const double virtual_notch_size = GetVirtualNotchSize();
|
|
|
|
|
|
|
|
if (virtual_notch_size > 0.0 && dist >= MINIMUM_NOTCH_DISTANCE)
|
|
|
|
{
|
|
|
|
angle = GetNearestNotch(angle, virtual_notch_size);
|
|
|
|
gate_max_dist = GetGateRadiusAtAngle(angle);
|
|
|
|
}
|
|
|
|
|
2018-12-29 22:06:03 +00:00
|
|
|
// 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%.
|
|
|
|
if (modifier)
|
|
|
|
{
|
2022-01-04 20:09:50 +00:00
|
|
|
dist *= modifier;
|
2018-12-29 22:06:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 23:39:23 +00:00
|
|
|
// Apply deadzone as a percentage of the user-defined calibration shape/size:
|
2019-03-29 19:39:48 +00:00
|
|
|
dist = ApplyDeadzone(dist, GetDeadzonePercentage());
|
2018-12-29 22:06:03 +00:00
|
|
|
|
|
|
|
// Scale to the gate shape/radius:
|
2019-03-01 23:39:23 +00:00
|
|
|
dist *= gate_max_dist;
|
2018-12-29 22:06:03 +00:00
|
|
|
|
2021-05-04 21:19:45 +00:00
|
|
|
return {std::clamp(std::cos(angle) * dist, -clamp, clamp),
|
|
|
|
std::clamp(std::sin(angle) * dist, -clamp, clamp)};
|
2018-12-29 22:06:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 20:09:50 +00:00
|
|
|
Control* ReshapableInput::GetModifierInput() const
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-21 22:11:00 +00:00
|
|
|
} // namespace ControllerEmu
|