Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/core/analog_joystick.h
4802 views
1
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <[email protected]> and contributors.
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
6
#include "controller.h"
7
8
#include <array>
9
#include <memory>
10
#include <optional>
11
12
class AnalogJoystick final : public Controller
13
{
14
public:
15
enum class Axis : u8
16
{
17
LeftX,
18
LeftY,
19
RightX,
20
RightY,
21
Count
22
};
23
24
enum class Button : u8
25
{
26
Select = 0,
27
L3 = 1,
28
R3 = 2,
29
Start = 3,
30
Up = 4,
31
Right = 5,
32
Down = 6,
33
Left = 7,
34
L2 = 8,
35
R2 = 9,
36
L1 = 10,
37
R1 = 11,
38
Triangle = 12,
39
Circle = 13,
40
Cross = 14,
41
Square = 15,
42
Mode = 16,
43
Count
44
};
45
46
enum class HalfAxis : u8
47
{
48
LLeft,
49
LRight,
50
LDown,
51
LUp,
52
RLeft,
53
RRight,
54
RDown,
55
RUp,
56
Count
57
};
58
59
static const Controller::ControllerInfo INFO;
60
61
AnalogJoystick(u32 index);
62
~AnalogJoystick() override;
63
64
static std::unique_ptr<AnalogJoystick> Create(u32 index);
65
66
ControllerType GetType() const override;
67
68
void Reset() override;
69
bool DoState(StateWrapper& sw, bool apply_input_state) override;
70
71
float GetBindState(u32 index) const override;
72
void SetBindState(u32 index, float value) override;
73
u32 GetButtonStateBits() const override;
74
std::optional<u32> GetAnalogInputBytes() const override;
75
76
void ResetTransferState() override;
77
bool Transfer(const u8 data_in, u8* data_out) override;
78
79
void LoadSettings(const SettingsInterface& si, const char* section, bool initial) override;
80
81
private:
82
enum class TransferState : u8
83
{
84
Idle,
85
Ready,
86
IDMSB,
87
ButtonsLSB,
88
ButtonsMSB,
89
RightAxisX,
90
RightAxisY,
91
LeftAxisX,
92
LeftAxisY
93
};
94
95
static constexpr u32 HALFAXIS_BIND_START_INDEX = static_cast<u32>(Button::Count);
96
static constexpr u32 LED_BIND_START_INDEX = HALFAXIS_BIND_START_INDEX + static_cast<u32>(HalfAxis::Count);
97
98
static const Controller::ControllerBindingInfo s_binding_info[];
99
100
u16 GetID() const;
101
void ToggleAnalogMode();
102
103
float m_analog_deadzone = 0.0f;
104
float m_analog_sensitivity = 1.33f;
105
u8 m_invert_left_stick = 0;
106
u8 m_invert_right_stick = 0;
107
108
// On original hardware, the mode toggle is a switch rather than a button, so we'll enable Analog Mode by default
109
bool m_analog_mode = true;
110
111
// buttons are active low
112
u16 m_button_state = UINT16_C(0xFFFF);
113
114
std::array<u8, static_cast<u8>(Axis::Count)> m_axis_state{};
115
116
// both directions of axis state, merged to m_axis_state
117
std::array<u8, static_cast<u32>(HalfAxis::Count)> m_half_axis_state{};
118
119
TransferState m_transfer_state = TransferState::Idle;
120
};
121
122