载入中...
搜索中...
未找到
Pad.h
浏览该文件的文档.
1#pragma once
2
3#include "joystick/Pad.h"
4
5#include <SDL2/SDL.h>
6#include <SDL2/SDL_haptic.h>
7
8#include <string>
9
10namespace eve::joystick::sdl {
11
13class Pad : public eve::joystick::Pad {
14public:
16 explicit Pad(int id);
18 Pad(int id, int joyindex);
20 ~Pad() override;
21
23 bool open(int deviceindex) override;
25 void close() override;
27 bool isConnected() const override;
28
30 std::string getName() const override;
31
33 int getAxisCount() const override;
35 int getButtonCount() const override;
37 int getHatCount() const override;
38
40 float getAxis(int axisindex) const override;
42 std::vector<float> getAxes() const override;
44 std::string getHat(int hatindex) const override;
45
47 bool isDown(int button) const override;
49 bool isDown(const std::vector<int>& buttons) const override;
50
52 bool openGamepad(int deviceindex) override;
54 bool isGamepad() const override;
55
57 float getGamepadAxis(const std::string& axis) const override;
59 bool isGamepadDown(const std::string& button) const override;
61 bool isGamepadDown(const std::vector<std::string>& buttons) const override;
62
64 std::string getGamepadMappingString() const override;
65
67 void* getHandle() const override;
69 std::string getGUID() const override;
71 int getInstanceID() const override;
73 int getID() const override;
74
76 void getDeviceInfo(int& vendorID, int& productID, int& productVersion) const override;
77
79 bool isVibrationSupported() override;
81 bool setVibration(float left, float right, float duration = -1.0f) override;
83 bool setVibration() override;
85 void getVibration(float& left, float& right) override;
86
87private:
88 bool checkCreateHaptic();
89 bool runVibrationEffect();
90
91 SDL_Joystick* joyhandle_ = nullptr;
92 SDL_GameController* controller_ = nullptr;
93 SDL_Haptic* haptic_ = nullptr;
94
95 SDL_JoystickID instanceid_ = -1;
96 std::string guid_;
97 int id_ = 0;
98 std::string name_;
99
100 struct Vibration {
101 float left = 0.0f;
102 float right = 0.0f;
103 SDL_HapticEffect effect{};
104 Uint16 data[4]{};
105 int id = -1;
106 Uint32 endtime = SDL_HAPTIC_INFINITY;
107 } vibration_;
108};
109
110} // namespace eve::joystick::sdl
float duration
HexVec3 left
HexVec3 right
One connected (or previously connected) joystick / gamepad device. Gamepad axis/button names follow S...
Definition Pad.h:13
SDL joystick/gamepad backend (GameController + optional haptic rumble).
Definition Pad.h:13
void close() override
Closes joystick and gamecontroller handles.
Definition Pad.cpp:65
int getID() const override
Engine-side stable pad id assigned at construction.
Definition Pad.cpp:198
~Pad() override
Closes SDL handles and stops rumble.
Definition Pad.cpp:39
void getVibration(float &left, float &right) override
Reads the last requested left/right rumble strengths.
Definition Pad.cpp:322
bool isGamepad() const override
True when a GameController mapping is active for this pad.
Definition Pad.cpp:143
float getGamepadAxis(const std::string &axis) const override
Named gamepad axis value (SDL GameController axis string).
Definition Pad.cpp:147
bool isVibrationSupported() override
True when haptic/rumble output is available.
Definition Pad.cpp:231
bool isConnected() const override
True while the underlying device is still connected.
Definition Pad.cpp:77
bool isGamepadDown(const std::string &button) const override
True if the named gamepad button is pressed.
Definition Pad.cpp:155
std::vector< float > getAxes() const override
Snapshot of all raw axis values.
Definition Pad.cpp:102
int getAxisCount() const override
Number of raw joystick axes.
Definition Pad.cpp:85
std::string getGamepadMappingString() const override
Current SDL GameController mapping string for this device.
Definition Pad.cpp:169
bool setVibration() override
Starts rumble; duration < 0 means until stopped.
Definition Pad.cpp:314
std::string getName() const override
Human-readable device name from the OS/driver.
Definition Pad.cpp:81
int getHatCount() const override
Number of hat (D-pad) controls.
Definition Pad.cpp:93
bool open(int deviceindex) override
Opens the device by SDL joystick device index.
Definition Pad.cpp:43
int getButtonCount() const override
Number of raw joystick buttons.
Definition Pad.cpp:89
std::string getHat(int hatindex) const override
Hat direction string for a hat index (see class brief).
Definition Pad.cpp:112
int getInstanceID() const override
SDL instance id that stays unique while the device is open.
Definition Pad.cpp:194
bool openGamepad(int deviceindex) override
Opens the device as an SDL GameController.
Definition Pad.cpp:131
void getDeviceInfo(int &vendorID, int &productID, int &productVersion) const override
USB/HID vendor, product, and version ids when available.
Definition Pad.cpp:202
std::string getGUID() const override
Stable GUID string for mapping lookup.
Definition Pad.cpp:190
void * getHandle() const override
Native handle (SDL_Joystick* / implementation-defined).
Definition Pad.cpp:186
bool isDown(int button) const override
True if the raw button index is currently pressed.
Definition Pad.cpp:117
float getAxis(int axisindex) const override
Normalized axis value in [-1, 1] for a raw axis index.
Definition Pad.cpp:97