summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControllerEmu/StickGate.h
blob: 919cc08815a7605f1b42e6bdf10582776be95624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <optional>
#include <vector>

#include "Common/Matrix.h"

#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"

namespace ControllerEmu
{
// Minimum stick distance from the center before virtual notches are applied.
constexpr ControlState MINIMUM_NOTCH_DISTANCE = 0.9;

// 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;

  // This is provided purely as an optimization for ReshapableInput to produce a minimal amount of
  // calibration points that are saved in our config.
  virtual std::optional<u32> GetIdealCalibrationSampleCount() const;

  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 final;
  std::optional<u32> GetIdealCalibrationSampleCount() const 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 final;
  std::optional<u32> GetIdealCalibrationSampleCount() const 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 final;
  std::optional<u32> GetIdealCalibrationSampleCount() const final;

private:
  const ControlState m_half_width;
};

class ReshapableInput : public ControlGroup
{
public:
  // This is the number of samples we generate but any number could be loaded from config.
  static constexpr int CALIBRATION_SAMPLE_COUNT = 32;

  // Contains input radius maximums at evenly-spaced angles.
  using CalibrationData = std::vector<ControlState>;

  ReshapableInput(std::string name, std::string ui_name, GroupType type);

  using ReshapeData = Common::DVec2;

  // Angle is in radians and should be non-negative
  ControlState GetDeadzoneRadiusAtAngle(double angle) const;
  ControlState GetInputRadiusAtAngle(double angle) const;

  ControlState GetDeadzonePercentage() const;

  virtual ControlState GetVirtualNotchSize() const { return 0.0; }

  virtual ControlState GetGateRadiusAtAngle(double angle) const = 0;
  virtual ReshapeData GetReshapableState(bool adjusted) const = 0;
  virtual ControlState GetDefaultInputRadiusAtAngle(double ang) const;

  void SetCalibrationToDefault();
  void SetCalibrationFromGate(const StickGate& gate);

  static void UpdateCalibrationData(CalibrationData& data, Common::DVec2 point1,
                                    Common::DVec2 point2);
  static ControlState GetCalibrationDataRadiusAtAngle(const CalibrationData& data, double angle);

  const CalibrationData& GetCalibrationData() const;
  void SetCalibrationData(CalibrationData data);

  const ReshapeData& GetCenter() const;
  void SetCenter(ReshapeData center);

  static constexpr const char* X_INPUT_OVERRIDE = "X";
  static constexpr const char* Y_INPUT_OVERRIDE = "Y";
  static constexpr const char* Z_INPUT_OVERRIDE = "Z";

protected:
  ReshapeData Reshape(ControlState x, ControlState y, ControlState modifier = 0.0,
                      ControlState clamp = 1.0) const;

  virtual Control* GetModifierInput() const;

private:
  void LoadConfig(Common::IniFile::Section*, const std::string& base_name) override;
  void SaveConfig(Common::IniFile::Section*, const std::string& base_name) override;

  CalibrationData m_calibration;
  SettingValue<double> m_deadzone_setting;
  ReshapeData m_center;
};

}  // namespace ControllerEmu