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