leafwing_input_manager/action_state/
action_data.rs1use bevy::{
4 math::{Vec2, Vec3},
5 platform::time::Instant,
6 reflect::Reflect,
7};
8use serde::{Deserialize, Serialize};
9
10use crate::buttonlike::ButtonValue;
11#[cfg(feature = "timing")]
12use crate::timing::Timing;
13use crate::{buttonlike::ButtonState, InputControlKind};
14
15#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Reflect)]
20pub struct ActionData {
21 pub disabled: bool,
25 pub kind_data: ActionKindData,
27}
28
29impl ActionData {
30 pub fn from_kind(input_control_kind: InputControlKind) -> Self {
32 Self {
33 disabled: false,
34 kind_data: match input_control_kind {
35 InputControlKind::Button => ActionKindData::Button(ButtonData::default()),
36 InputControlKind::Axis => ActionKindData::Axis(AxisData::default()),
37 InputControlKind::DualAxis => ActionKindData::DualAxis(DualAxisData::default()),
38 InputControlKind::TripleAxis => {
39 ActionKindData::TripleAxis(TripleAxisData::default())
40 }
41 },
42 }
43 }
44
45 pub fn tick(&mut self, _current_instant: Instant, _previous_instant: Instant) {
47 match self.kind_data {
48 ActionKindData::Button(ref mut data) => {
49 data.state.tick();
50
51 #[cfg(feature = "timing")]
52 data.timing.tick(_current_instant, _previous_instant);
53 }
54 ActionKindData::Axis(ref mut _data) => {}
55 ActionKindData::DualAxis(ref mut _data) => {}
56 ActionKindData::TripleAxis(ref mut _data) => {}
57 }
58 }
59}
60
61#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Reflect)]
63pub enum ActionKindData {
64 Button(ButtonData),
66 Axis(AxisData),
68 DualAxis(DualAxisData),
70 TripleAxis(TripleAxisData),
72}
73
74impl ActionKindData {
75 pub(super) fn swap_to_update_state(&mut self) {
76 match self {
79 Self::Button(data) => {
80 data.fixed_update_state = data.state;
81 data.fixed_update_value = data.value;
82 data.state = data.update_state;
83 data.value = data.update_value;
84 }
85 Self::Axis(data) => {
86 data.fixed_update_value = data.value;
87 data.value = data.update_value;
88 }
89 Self::DualAxis(data) => {
90 data.fixed_update_pair = data.pair;
91 data.pair = data.update_pair;
92 }
93 Self::TripleAxis(data) => {
94 data.fixed_update_triple = data.triple;
95 data.triple = data.update_triple;
96 }
97 }
98 }
99
100 pub(super) fn swap_to_fixed_update_state(&mut self) {
101 match self {
104 Self::Button(data) => {
105 data.update_state = data.state;
106 data.update_value = data.value;
107 data.state = data.fixed_update_state;
108 data.value = data.fixed_update_value;
109 }
110 Self::Axis(data) => {
111 data.update_value = data.value;
112 data.value = data.fixed_update_value;
113 }
114 Self::DualAxis(data) => {
115 data.update_pair = data.pair;
116 data.pair = data.fixed_update_pair;
117 }
118 Self::TripleAxis(data) => {
119 data.update_triple = data.triple;
120 data.triple = data.fixed_update_triple;
121 }
122 }
123 }
124
125 pub(super) fn set_update_state_from_state(&mut self) {
127 match self {
128 Self::Button(data) => {
129 data.update_state = data.state;
130 data.update_value = data.value;
131 }
132 Self::Axis(data) => {
133 data.update_value = data.value;
134 }
135 Self::DualAxis(data) => {
136 data.update_pair = data.pair;
137 }
138 Self::TripleAxis(data) => {
139 data.update_triple = data.triple;
140 }
141 }
142 }
143
144 pub(super) fn set_fixed_update_state_from_state(&mut self) {
146 match self {
147 Self::Button(data) => {
148 data.fixed_update_state = data.state;
149 data.fixed_update_value = data.value;
150 }
151 Self::Axis(data) => {
152 data.fixed_update_value = data.value;
153 }
154 Self::DualAxis(data) => {
155 data.fixed_update_pair = data.pair;
156 }
157 Self::TripleAxis(data) => {
158 data.fixed_update_triple = data.triple;
159 }
160 }
161 }
162}
163
164#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, Reflect)]
166pub struct ButtonData {
167 pub state: ButtonState,
169 pub update_state: ButtonState,
171 pub fixed_update_state: ButtonState,
173 pub value: f32,
175 pub update_value: f32,
177 pub fixed_update_value: f32,
179 #[cfg(feature = "timing")]
181 pub timing: Timing,
182}
183
184impl ButtonData {
185 pub const JUST_PRESSED: Self = Self {
187 state: ButtonState::JustPressed,
188 update_state: ButtonState::JustPressed,
189 fixed_update_state: ButtonState::JustPressed,
190 value: 1.0,
191 update_value: 1.0,
192 fixed_update_value: 1.0,
193 #[cfg(feature = "timing")]
194 timing: Timing::NEW,
195 };
196
197 pub const JUST_RELEASED: Self = Self {
199 state: ButtonState::JustReleased,
200 update_state: ButtonState::JustReleased,
201 fixed_update_state: ButtonState::JustReleased,
202 value: 0.0,
203 update_value: 0.0,
204 fixed_update_value: 0.0,
205 #[cfg(feature = "timing")]
206 timing: Timing::NEW,
207 };
208
209 pub const RELEASED: Self = Self {
215 state: ButtonState::Released,
216 update_state: ButtonState::Released,
217 fixed_update_state: ButtonState::Released,
218 value: 0.0,
219 update_value: 0.0,
220 fixed_update_value: 0.0,
221 #[cfg(feature = "timing")]
222 timing: Timing::NEW,
223 };
224
225 #[inline]
227 #[must_use]
228 pub fn pressed(&self) -> bool {
229 self.state.pressed()
230 }
231
232 #[inline]
234 #[must_use]
235 pub fn just_pressed(&self) -> bool {
236 self.state.just_pressed()
237 }
238
239 #[inline]
241 #[must_use]
242 pub fn released(&self) -> bool {
243 self.state.released()
244 }
245
246 #[inline]
248 #[must_use]
249 pub fn just_released(&self) -> bool {
250 self.state.just_released()
251 }
252
253 #[inline]
255 #[must_use]
256 pub fn to_button_value(&self) -> ButtonValue {
257 ButtonValue::new(self.state.pressed(), self.value)
258 }
259}
260
261#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize, Reflect)]
263pub struct AxisData {
264 pub value: f32,
266 pub update_value: f32,
268 pub fixed_update_value: f32,
270}
271
272#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize, Reflect)]
274pub struct DualAxisData {
275 pub pair: Vec2,
277 pub update_pair: Vec2,
279 pub fixed_update_pair: Vec2,
281}
282
283#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize, Reflect)]
285pub struct TripleAxisData {
286 pub triple: Vec3,
288 pub update_triple: Vec3,
290 pub fixed_update_triple: Vec3,
292}