Graphics v0.0.0
A simple abstraction layer for the modern graphics APIs.
载入中...
搜索中...
未找到
Gamepad.h
1// Copyright 2022 ShenMian
2// License(Apache-2.0)
3
4#pragma once
5
6#include <filesystem>
7#include <functional>
8#include <math/math.hpp>
9#include <string_view>
10
19{
20public:
21 using handle_type = unsigned short;
22
23 enum class Thumb;
24 enum class Trigger;
25 enum class Button : uint8_t;
26
30 Gamepad(handle_type handle = 0);
31
35 ~Gamepad();
36
40 void update();
41
49 Vector2f get(Thumb thumb) const noexcept;
50
60 Vector2f get_raw(Thumb thumb) const noexcept;
61
69 float get(Trigger trigger) const noexcept;
70
80 float get_raw(Trigger trigger) const noexcept;
81
89 bool get(Button button) const noexcept;
90
99 void set_vibration(float leftSpeed, float rightSpeed);
100
104 std::string_view get_name() const;
105
111 bool is_connected() const;
112
120 bool load_mappings_db(const std::filesystem::path& path);
121
122 bool operator==(const Gamepad&) const;
123
124 static void init();
125 static void deinit();
126
127private:
128 handle_type handle_;
129
130 float left_thumb_deadzone_ = 0.1f;
131 float right_thumb_deadzone_ = 0.1f;
132 float trigger_threshold_ = 0.01f;
133
134 unsigned char buttons_[15] = {};
135 float axes_[6] = {};
136};
137
142{
143 left,
144 right
145};
146
151{
152 left,
153 right
154};
155
159enum class Gamepad::Button : uint8_t
160{
161 A = 0,
162 B = 1,
163 X = 2,
164 Y = 3,
165
166 LeftBumper = 4,
167 RightBumper = 5,
168
169 Back = 6,
170 Start = 7,
171 Guide = 8,
172
173 LeftThumb = 9,
174 RightThumb = 10,
175
176 DPAD_Up = 11,
177 DPAD_Right = 12,
178 DPAD_Down = 13,
179 DPAD_Left = 14,
180
181 Cross = A,
182 Circle = B,
183 Square = X,
184 Triangle = Y
185};
186
游戏手柄.
Definition: Gamepad.h:19
Vector2f get_raw(Thumb thumb) const noexcept
获取原始摇杆数据.
Definition: Gamepad.cpp:73
bool load_mappings_db(const std::filesystem::path &path)
加载 SDL 按键映射数据库.
Definition: Gamepad.cpp:129
void set_vibration(float leftSpeed, float rightSpeed)
设置震动反馈.
Definition: Gamepad.cpp:105
~Gamepad()
默认析构函数.
Definition: Gamepad.cpp:28
std::string_view get_name() const
获取设备名称. 人类可读的, UTF-8 编码.
Definition: Gamepad.cpp:119
bool is_connected() const
检查手柄是否处于连接状态.
Definition: Gamepad.cpp:124
Vector2f get(Thumb thumb) const noexcept
获取摇杆数据.
Definition: Gamepad.cpp:52
void update()
更新数据.
Definition: Gamepad.cpp:33
Button
手柄按键.
Definition: Gamepad.h:160
Thumb
手柄摇杆.
Definition: Gamepad.h:142
Trigger
手柄线性按键.
Definition: Gamepad.h:151