Debian Codegraph @

Source/Core/InputCommon/ControllerEmu/StickGate.h

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <optional>
7 #include <vector>
8
9 #include "Common/Matrix.h"
10
11 #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
12 #include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
13
14 namespace ControllerEmu
15 {
16 // Minimum stick distance from the center before virtual notches are applied.
17 constexpr ControlState MINIMUM_NOTCH_DISTANCE = 0.9;
18
19 // An abstract class representing the plastic shell that limits an analog stick's movement.
20 class StickGate
21 {
22 public:
23 // Angle is in radians and should be non-negative
24 virtual ControlState GetRadiusAtAngle(double ang) const = 0;
25
26 // This is provided purely as an optimization for ReshapableInput to produce a minimal amount of
27 // calibration points that are saved in our config.
28 virtual std::optional<u32> GetIdealCalibrationSampleCount() const;
29
30 virtual ~StickGate() = default;
31 };
32
33 // An octagon-shaped stick gate is found on most Nintendo GC/Wii analog sticks.
34 class OctagonStickGate : public StickGate
35 {
36 public:
37 // Radius of circumscribed circle
38 explicit OctagonStickGate(ControlState radius);
39 ControlState GetRadiusAtAngle(double ang) const final;
40 std::optional<u32> GetIdealCalibrationSampleCount() const final;
41
42 private:
43 const ControlState m_radius;
44 };
45
46 // A round-shaped stick gate. Possibly found on 3rd-party accessories.
47 class RoundStickGate : public StickGate
48 {
49 public:
50 explicit RoundStickGate(ControlState radius);
51 ControlState GetRadiusAtAngle(double ang) const final;
52 std::optional<u32> GetIdealCalibrationSampleCount() const final;
53
54 private:
55 const ControlState m_radius;
56 };
57
58 // A square-shaped stick gate. e.g. keyboard input.
59 class SquareStickGate : public StickGate
60 {
61 public:
62 explicit SquareStickGate(ControlState half_width);
63 ControlState GetRadiusAtAngle(double ang) const final;
64 std::optional<u32> GetIdealCalibrationSampleCount() const final;
65
66 private:
67 const ControlState m_half_width;
68 };
69
70 class ReshapableInput : public ControlGroup
71 {
72 public:
73 // This is the number of samples we generate but any number could be loaded from config.
74 static constexpr int CALIBRATION_SAMPLE_COUNT = 32;
75
76 // Contains input radius maximums at evenly-spaced angles.
77 using CalibrationData = std::vector<ControlState>;
78
79 ReshapableInput(std::string name, std::string ui_name, GroupType type);
80
81 using ReshapeData = Common::DVec2;
82
83 // Angle is in radians and should be non-negative
84 ControlState GetDeadzoneRadiusAtAngle(double angle) const;
85 ControlState GetInputRadiusAtAngle(double angle) const;
86
87 ControlState GetDeadzonePercentage() const;
88
89 virtual ControlState GetVirtualNotchSize() const { return 0.0; }
90
91 virtual ControlState GetGateRadiusAtAngle(double angle) const = 0;
92 virtual ReshapeData GetReshapableState(bool adjusted) const = 0;
93 virtual ControlState GetDefaultInputRadiusAtAngle(double ang) const;
94
95 void SetCalibrationToDefault();
96 void SetCalibrationFromGate(const StickGate& gate);
97
98 static void UpdateCalibrationData(CalibrationData& data, Common::DVec2 point1,
99 Common::DVec2 point2);
100 static ControlState GetCalibrationDataRadiusAtAngle(const CalibrationData& data, double angle);
101
102 const CalibrationData& GetCalibrationData() const;
103 void SetCalibrationData(CalibrationData data);
104
105 const ReshapeData& GetCenter() const;
106 void SetCenter(ReshapeData center);
107
108 static constexpr const char* X_INPUT_OVERRIDE = "X";
109 static constexpr const char* Y_INPUT_OVERRIDE = "Y";
110 static constexpr const char* Z_INPUT_OVERRIDE = "Z";
111
112 protected:
113 ReshapeData Reshape(ControlState x, ControlState y, ControlState modifier = 0.0,
114 ControlState clamp = 1.0) const;
115
116 virtual Control* GetModifierInput() const;
117
118 private:
119 void LoadConfig(Common::IniFile::Section*, const std::string& base_name) override;
120 void SaveConfig(Common::IniFile::Section*, const std::string& base_name) override;
121
122 CalibrationData m_calibration;
123 SettingValue<double> m_deadzone_setting;
124 ReshapeData m_center;
125 };
126
127 } // namespace ControllerEmu