GamepadInput Arduino Lib 1.2.1
Loading...
Searching...
No Matches
gamepad_input_tracker.h
1
2#pragma once
3
4#ifndef GAMEPAD_INPUT_TRACKER_H
5#define GAMEPAD_INPUT_TRACKER_H
6
7#include "gamepad_input_state.h"
8
9namespace gamepad::input {
10
11/**
12 * @~English
13 * @brief Tracks changes in gamepad input between frames.
14 *
15 * Useful for detecting button press, release, hold,
16 * and significant axis movement.
17 */
18/**
19 * @~Chinese
20 * @brief 手柄输入帧间状态追踪器
21 *
22 * 用于检测按键按下、弹起、按住以及轴变化。
23 */
24class Tracker {
25 public:
26 /**
27 * @~English
28 * @brief Get the current input state.
29 */
30 /**
31 * @~Chinese
32 * @brief 获取当前输入状态
33 */
34 const State& raw() const noexcept { return current_; }
35
36 /**
37 * @~English
38 * @brief Reset tracker state to neutral.
39 */
40 /**
41 * @~Chinese
42 * @brief 重置追踪器状态
43 */
44 void Reset() noexcept {
45 current_.Reset();
46 previous_.Reset();
47 }
48
49 /**
50 * @~English
51 * @brief Advance to next frame (swap states).
52 */
53 /**
54 * @~Chinese
55 * @brief 推进到下一帧(交换状态)
56 */
57 void Tick() noexcept { previous_ = current_; }
58
59 /**
60 * @~English
61 * @brief Update current frame input state.
62 */
63 /**
64 * @~Chinese
65 * @brief 更新当前帧输入状态
66 */
67 void Update(State state) noexcept { current_ = state; };
68
69 /**
70 * @~English
71 * @brief Detect button press (falling edge).
72 * @return True if button is pressed
73 */
74 /**
75 * @~Chinese
76 * @brief 检测按键按下(下降沿)
77 * @return 如果按键被按下
78 */
79 bool pressed(Button button) const noexcept { return current_[button] && !previous_[button]; }
80
81 /**
82 * @~English
83 * @brief Detect button release (rising edge).
84 * @return True if button is released
85 */
86 /**
87 * @~Chinese
88 * @brief 检测按键释放(上升沿)
89 * @return 如果按键被释放
90 */
91 bool released(Button button) const noexcept { return !current_[button] && previous_[button]; }
92
93 /**
94 * @~English
95 * @brief Detect button hold.
96 * @return True if button is held
97 */
98 /**
99 * @~Chinese
100 * @brief 检测按键持续
101 * @return 如果按键持续
102 */
103 bool holding(Button button) const noexcept { return current_[button] && previous_[button]; }
104
105 /**
106 * @~English
107 * @brief Detect axis movement.
108 * @param threshold Threshold for detecting axis movement
109 * @return True if axis has moved
110 */
111 /**
112 * @~Chinese
113 * @brief 检测轴移动
114 * @param threshold 用于检测轴移动的阈值
115 * @return 如果轴移动
116 */
117 bool AxisChanged(Axis axis, uint8_t threshold) const noexcept {
118 return AxisChanged(previous_[axis], current_[axis], threshold);
119 }
120
121 /**
122 * @~English
123 * @brief Check if a button is pressed.
124 * @return True if button is pressed
125 */
126 /**
127 * @~Chinese
128 * @brief 检查按键是否被按下
129 * @return 如果按键被按下
130 */
131 bool operator[](Button button) const noexcept { return current_[button]; }
132
133 /**
134 * @~English
135 * @brief Get axis value.
136 * @return Axis value
137 */
138 /**
139 * @~Chinese
140 * @brief 获取轴值
141 * @return 轴值
142 */
143 uint8_t operator[](Axis axis) const noexcept { return current_[axis]; }
144
145 private:
146 static bool AxisChanged(uint8_t previous, uint8_t current, uint8_t threshold) noexcept {
147 if (previous == current) {
148 return false;
149 }
150
151 if (current == 0 || current == 255) {
152 return true;
153 }
154
155 const uint8_t diff = current > previous ? current - previous : previous - current;
156 return diff >= threshold;
157 }
158
159 State current_{};
160 State previous_{};
161};
162} // namespace gamepad::input
163
164#endif
Tracks changes in gamepad input between frames.
void Reset() noexcept
Reset tracker state to neutral.
bool operator[](Button button) const noexcept
Check if a button is pressed.
void Tick() noexcept
Advance to next frame (swap states).
const State & raw() const noexcept
Get the current input state.
void Update(State state) noexcept
Update current frame input state.
bool holding(Button button) const noexcept
Detect button hold.
bool released(Button button) const noexcept
Detect button release (rising edge).
bool pressed(Button button) const noexcept
Detect button press (falling edge).
bool AxisChanged(Axis axis, uint8_t threshold) const noexcept
Detect axis movement.
uint8_t operator[](Axis axis) const noexcept
Get axis value.
Button
Button identifiers for a generic gamepad.
Represents a snapshot of gamepad input state.