1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*!
Preset bindings for common input binding patterns.
Consider a common pattern of moving a character using keyboard keys and a gamepad stick.
With the help of [modifiers](crate::modifier) you can achieve this as follows:
```
use bevy::prelude::*;
use bevy_enhanced_input::prelude::*;
#[derive(Component, Default, Clone)]
struct Player;
#[derive(InputAction)]
#[action_output(Vec2)]
struct Movement;
fn spawn(mut commands: Commands) {
commands.spawn_scene(bsn! {
Player
Actions<Player> [
Action::<Movement>::new()
// Modifier components at the action level.
DeadZone // Apply non-uniform normalization that works for both digital and analog inputs, otherwise diagonal movement will be faster.
SmoothNudge // Apply smoothing.
DeltaScale // Multiply by delta time to make it framerate-independent.
~Scale::splat(30.0) // Additionally multiply by a constant to achieve the desired speed.
Bindings [
// Keyboard keys captured as `bool`, but the output of `Movement` is defined as `Vec2`,
// so you need to assign keys to axes using swizzle to reorder them and negation.
Binding(KeyCode::KeyW)
~SwizzleAxis::YXZ
--
Binding(KeyCode::KeyA)
~Negate::all()
--
Binding(KeyCode::KeyS)
~Negate::all()
~SwizzleAxis::YXZ
--
Binding(KeyCode::KeyD)
--
// In Bevy sticks split by axes and captured as 1-dimensional inputs,
// so Y stick needs to be sweezled into Y axis.
Binding(GamepadAxis::LeftStickX)
--
Binding(GamepadAxis::LeftStickY)
~SwizzleAxis::YXZ
]
]
});
}
```
However, this is quite onerous! It would be inconvenient to bind WASD keys and analog sticks manually, like in the example above,
every time. You can use [`cardinal::wasd_keys`] and [`axial::left_stick`] presets to simplify the example above.
```
use bevy::prelude::*;
use bevy_enhanced_input::prelude::*;
#[derive(Component, Default, Clone)]
struct Player;
#[derive(InputAction)]
#[action_output(Vec2)]
struct Movement;
fn spawn(mut commands: Commands) {
commands.spawn_scene(bsn! {
Player
Actions<Player> [
Action::<Movement>::new()
DeadZone
SmoothNudge
DeltaScale
~Scale::splat(30.0)
Bindings [
{preset::cardinal::wasd_keys()}
--
{preset::axial::left_stick()}
]
]
});
}
```
# Implementation details
Each preset function defined in this module generates a list of binding entities with the appropriate components
attached to them.
To achieve this, each function returns a [`SceneList`], which you can use inside [`Bindings`](crate::prelude::Bindings).
The [`prelude`](crate::prelude) re-exports this `preset` module.
Each preset module also have a `new` function that accepts [`IntoBindingSceneBundle`]: either an input like
[`KeyCode::KeyW`], or a tuple with additional components or bundles. They must be cloneable and have no
[`bundle effects`](bevy::ecs::bundle::NoBundleEffect). Duplicate components within a bundle will panic.
# Examples
Adding additional bindings:
## BSN
Mix preset scene lists with individual binding scenes, separated by `--`:
```
# use bevy::prelude::*;
# use bevy_enhanced_input::prelude::*;
let scene = bsn! {
Bindings [
{preset::cardinal::wasd_keys()}
--
{preset::axial::left_stick()}
--
Binding(KeyCode::ArrowUp)
~SwizzleAxis::YXZ
]
};
```
Initializing fields:
## BSN
Pass inputs directly, or use tuples to attach modifiers to individual bindings.
To attach the same modifiers to both axes, pass them to each argument of [`axial::new`]:
```
# use bevy::prelude::*;
# use bevy_enhanced_input::prelude::*;
let scene = bsn! {
Bindings [
{preset::bidirectional::new(
(KeyCode::NumpadAdd, Scale::splat(2.0)),
KeyCode::NumpadSubtract,
)}
--
{preset::axial::new(
(GamepadAxis::LeftStickX, Scale::splat(1.0), Negate::all()),
(GamepadAxis::LeftStickY, Scale::splat(1.0), Negate::all()),
)}
]
};
```
Avoid duplicating modifiers already supplied by a preset, as duplicate components within a bundle will panic.
Alternatively, attach shared modifiers at the action level, as in the movement example above.
Loading from settings:
## BSN
The BSN constructors accept stored [`Binding`] values, including [`Binding::None`]:
```
# use bevy::prelude::*;
# use bevy_enhanced_input::prelude::*;
// Could be loaded from a file.
// `Binding::None` represents unbound inputs.
let settings = InputSettings {
forward: [Binding::from(KeyCode::KeyW), Binding::None],
right: [Binding::from(KeyCode::KeyD), Binding::None],
backward: [Binding::from(KeyCode::KeyS), Binding::None],
left: [Binding::from(KeyCode::KeyA), Binding::None],
};
let scene = bsn! {
Bindings [
{preset::cardinal::new(
settings.forward[0],
settings.right[0],
settings.backward[0],
settings.left[0],
)}
--
{preset::cardinal::new(
settings.forward[1],
settings.right[1],
settings.backward[1],
settings.left[1],
)}
]
};
/// Bindings for actions.
///
/// Represented as arrays because in games you usually
/// have 2 or 3 bindings for a single action.
///
/// Usually stored as a resource.
#[derive(Resource)]
struct InputSettings {
forward: [Binding; 2],
right: [Binding; 2],
backward: [Binding; 2],
left: [Binding; 2],
}
```
*/
use ;
use crateBinding;
/// A binding value, optionally followed by additional bundles in a tuple, for a BSN preset.
///
/// These bundles must be cloneable and cannot have bundle effects.
all_tuples!;
/// Helper trait for attaching a bundle to a preset.
///
/// See the examples on [`WithBundle::with`].