GamepadInput Arduino 库 1.3.0
载入中...
搜索中...
未找到
gamepad_input_state.h
1#pragma once
2
3#ifndef GAMEPAD_INPUT_STATE_H
4#define GAMEPAD_INPUT_STATE_H
5
6#include <stddef.h>
7#include <stdint.h>
8#include <string.h>
9
10#include "gamepad_input_axis.h"
11#include "gamepad_input_button.h"
12
13namespace gamepad::input {
14#if __cplusplus >= 201703L
15constexpr inline size_t kAxisCount = 4;
16constexpr inline uint8_t kAxisCenter = 0x80;
17#else
18static constexpr size_t kAxisCount = 4;
19static constexpr uint8_t kAxisCenter = 0x80;
20#endif
21
22/**
23 * @~English
24 * @brief Represents a snapshot of gamepad input state.
25 *
26 * This structure is designed to be compact and stable for serialization.
27 */
28/**
29 * @~Chinese
30 * @brief 游戏手柄输入状态快照
31 *
32 * 该结构体布局稳定,可用于序列化与跨模块传输。
33 */
34struct State {
35 uint32_t buttons{0};
36 uint8_t axes[kAxisCount]{kAxisCenter, kAxisCenter, kAxisCenter, kAxisCenter};
37
38 /**
39 * @~English
40 * @brief Construct a State from raw memory.
41 * @param data Pointer to input data
42 */
43 /**
44 * @~Chinese
45 * @brief 从原始内存构造状态
46 * @param data 输入数据指针
47 */
48 static State FromBytes(const void* data) noexcept {
49 State state;
50 memcpy(&state, data, sizeof(state));
51 return state;
52 }
53
54 /**
55 * @~English
56 * @brief Reset all inputs to neutral state.
57 */
58 /**
59 * @~Chinese
60 * @brief 重置所有输入到中立状态
61 */
62 void Reset() noexcept {
63 buttons = 0;
64 memset(axes, kAxisCenter, sizeof(axes));
65 }
66
67 /**
68 * @~English
69 * @brief Check if a button is pressed.
70 */
71 /**
72 * @~Chinese
73 * @brief 检查按键是否被按下
74 */
75 inline bool operator[](Button button) const noexcept { return (buttons & button) != 0; }
76
77 /**
78 * @~English
79 * @brief Press a button.
80 */
81 /**
82 * @~Chinese
83 * @brief 按下指定按键
84 */
85 inline void Set(Button button) noexcept { buttons |= static_cast<uint32_t>(button); }
86
87 /**
88 * @~English
89 * @brief Release a button.
90 */
91 /**
92 * @~Chinese
93 * @brief 释放指定按键
94 */
95 inline void Clear(Button button) noexcept { buttons &= ~static_cast<uint32_t>(button); }
96
97 /**
98 * @~English
99 * @brief Access analog axis value.
100 */
101 /**
102 * @~Chinese
103 * @brief 访问模拟轴数值
104 */
105 inline uint8_t& operator[](Axis axis) noexcept { return axes[static_cast<size_t>(axis)]; }
106
107 /**
108 * @~English
109 * @brief Access analog axis value (const).
110 */
111 /**
112 * @~Chinese
113 * @brief 访问模拟轴数值(只读)
114 */
115 inline const uint8_t& operator[](Axis axis) const noexcept { return axes[static_cast<size_t>(axis)]; }
116};
117
118static_assert(sizeof(State) == 8, "State layout must be stable");
119} // namespace gamepad::input
120#endif
Button
通用游戏手柄按键枚举
游戏手柄输入状态快照
bool operator[](Button button) const noexcept
检查按键是否被按下
const uint8_t & operator[](Axis axis) const noexcept
访问模拟轴数值(只读)
uint8_t & operator[](Axis axis) noexcept
访问模拟轴数值
void Clear(Button button) noexcept
释放指定按键
static State FromBytes(const void *data) noexcept
从原始内存构造状态
void Set(Button button) noexcept
按下指定按键
void Reset() noexcept
重置所有输入到中立状态