Skip to content

Commit b81ae9c

Browse files
committed
improve gamepad handling
1 parent 4a629a1 commit b81ae9c

File tree

4 files changed

+341
-49
lines changed

4 files changed

+341
-49
lines changed

include/gf2/core/GamepadTypes.h

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace gf {
1212

13-
enum class GamepadButton : int32_t { // NOLINT(performance-enum-size)
13+
enum class GamepadButton { // NOLINT(performance-enum-size)
1414
Invalid = -1,
1515
South,
1616
East,
@@ -29,14 +29,14 @@ namespace gf {
2929
DPadRight,
3030
};
3131

32-
constexpr GamepadButton AnyGamepadButton = static_cast<GamepadButton>(0x7FFFFFFF);
32+
constexpr GamepadButton AnyGamepadButton = GamepadButton{ std::numeric_limits<std::underlying_type_t<GamepadButton>>::max() };
3333

3434
enum class GamepadStick : uint8_t {
3535
Left,
3636
Right,
3737
};
3838

39-
enum class GamepadAxis : int32_t { // NOLINT(performance-enum-size)
39+
enum class GamepadAxis { // NOLINT(performance-enum-size)
4040
Invalid = -1,
4141
LeftX,
4242
LeftY,
@@ -46,7 +46,7 @@ namespace gf {
4646
TriggerRight,
4747
};
4848

49-
constexpr GamepadAxis AnyGamepadAxis = static_cast<GamepadAxis>(0x7FFFFFFF);
49+
constexpr GamepadAxis AnyGamepadAxis = GamepadAxis{ std::numeric_limits<std::underlying_type_t<GamepadAxis>>::max() };
5050

5151
enum class GamepadAxisDirection : uint8_t {
5252
Positive,
@@ -58,6 +58,39 @@ namespace gf {
5858

5959
constexpr GamepadId AnyGamepad = static_cast<GamepadId>(std::numeric_limits<std::underlying_type_t<GamepadId>>::max());
6060

61+
enum class GamepadType : unsigned { // NOLINT(performance-enum-size)
62+
Unknown,
63+
Standard,
64+
XBox360,
65+
XBoxOne,
66+
Ps3,
67+
Ps4,
68+
Ps5,
69+
NintendoSwitchPro,
70+
NintendoSwitchJoyconLeft,
71+
NintendoSwitchJoyconRight,
72+
NintendoSwitchJoyconPair,
73+
};
74+
75+
enum class GamepadButtonLabel : unsigned { // NOLINT(performance-enum-size)
76+
Unknown,
77+
A,
78+
B,
79+
X,
80+
Y,
81+
Cross,
82+
Circle,
83+
Square,
84+
Triangle,
85+
};
86+
87+
enum class GamepadConnectionState { // NOLINT(performance-enum-size)
88+
Invalid = -1,
89+
Unknown,
90+
Wired,
91+
Wireless,
92+
};
93+
6194
}
6295

6396
#endif // GF_CORE_GAMEPAD_TYPES_H

include/gf2/graphics/Gamepad.h

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,73 @@
33
#ifndef GF_GAMEPAD_H
44
#define GF_GAMEPAD_H
55

6+
#include <cstdint>
7+
68
#include <gf2/core/GamepadTypes.h>
9+
#include <gf2/core/Id.h>
10+
#include <gf2/core/ZString.h>
711

812
#include "GraphicsApi.h"
913

14+
using SDL_Gamepad = struct SDL_Gamepad;
15+
1016
namespace gf {
1117

18+
class GamepadDevice {
19+
public:
20+
operator bool()
21+
{
22+
return m_gamepad != nullptr;
23+
}
24+
25+
bool connected();
26+
GamepadId id();
27+
28+
GamepadType type();
29+
GamepadType real_type();
30+
31+
bool has_axis(GamepadAxis axis);
32+
bool has_button(GamepadButton button);
33+
34+
GamepadButtonLabel button_label(GamepadButton button);
35+
GamepadConnectionState connection_state();
36+
37+
uint16_t vendor();
38+
uint16_t product();
39+
uint16_t product_version();
40+
uint16_t firmware_version();
41+
42+
ZString name();
43+
ZString path();
44+
ZString serial();
45+
Id serial_id();
46+
47+
void close();
48+
49+
private:
50+
friend class Gamepad;
51+
GamepadDevice(SDL_Gamepad* gamepad);
52+
SDL_Gamepad* m_gamepad = nullptr;
53+
};
54+
1255
class GF_GRAPHICS_API Gamepad {
1356
public:
14-
static const char* axis_name(GamepadAxis axis);
15-
static const char* button_name(GamepadButton button);
16-
static const char* gamepad_name(GamepadId id);
57+
static GamepadDevice open(GamepadId id);
58+
static GamepadDevice from_id(GamepadId id);
59+
60+
static ZString name(GamepadId id);
61+
static ZString path(GamepadId id);
62+
static GamepadType type(GamepadId id);
63+
static GamepadType real_type(GamepadId id);
64+
static uint16_t vendor(GamepadId id);
65+
static uint16_t product(GamepadId id);
66+
static uint16_t product_version(GamepadId id);
67+
68+
static ZString axis_name(GamepadAxis axis);
69+
static ZString button_name(GamepadButton button);
70+
static ZString type_name(GamepadType type);
1771

18-
static void open(GamepadId hwid);
19-
static bool attached(GamepadId id);
20-
static void close(GamepadId id);
72+
static GamepadButtonLabel button_label_for_type(GamepadType type, GamepadButton button);
2173
};
2274

2375
} // namespace gf

0 commit comments

Comments
 (0)