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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use bevy::input::{
gamepad::{Gamepad, GamepadAxis, GamepadButton, GamepadEventRaw, Gamepads},
keyboard::{KeyCode, KeyboardInput},
mouse::{MouseButton, MouseButtonInput, MouseMotion, MouseWheel},
Axis, Input,
};
use petitset::PetitSet;
use bevy::ecs::prelude::{Events, ResMut, World};
use bevy::ecs::system::SystemState;
use crate::axislike::{
AxisType, DualAxisData, MouseMotionAxisType, MouseWheelAxisType, SingleAxis, VirtualDPad,
};
use crate::buttonlike::{MouseMotionDirection, MouseWheelDirection};
use crate::user_input::{InputKind, UserInput};
#[derive(Debug, Clone)]
pub struct InputStreams<'a> {
pub gamepad_buttons: &'a Input<GamepadButton>,
pub gamepad_button_axes: &'a Axis<GamepadButton>,
pub gamepad_axes: &'a Axis<GamepadAxis>,
pub gamepads: &'a Gamepads,
pub keycode: &'a Input<KeyCode>,
pub mouse_button: &'a Input<MouseButton>,
pub mouse_wheel: &'a Events<MouseWheel>,
pub mouse_motion: &'a Events<MouseMotion>,
pub associated_gamepad: Option<Gamepad>,
}
impl<'a> InputStreams<'a> {
pub fn from_world(world: &'a World, gamepad: Option<Gamepad>) -> Self {
let gamepad_buttons = world.resource::<Input<GamepadButton>>();
let gamepad_button_axes = world.resource::<Axis<GamepadButton>>();
let gamepad_axes = world.resource::<Axis<GamepadAxis>>();
let gamepads = world.resource::<Gamepads>();
let keyboard = world.resource::<Input<KeyCode>>();
let mouse = world.resource::<Input<MouseButton>>();
let mouse_wheel = world.resource::<Events<MouseWheel>>();
let mouse_motion = world.resource::<Events<MouseMotion>>();
InputStreams {
gamepad_buttons,
gamepad_button_axes,
gamepad_axes,
gamepads,
keycode: keyboard,
mouse_button: mouse,
mouse_wheel,
mouse_motion,
associated_gamepad: gamepad,
}
}
}
impl<'a> InputStreams<'a> {
pub fn guess_gamepad(&self) -> Option<Gamepad> {
match self.associated_gamepad {
Some(gamepad) => Some(gamepad),
None => self.gamepads.iter().next().copied(),
}
}
pub fn input_pressed(&self, input: &UserInput) -> bool {
match input {
UserInput::Single(button) => self.button_pressed(*button),
UserInput::Chord(buttons) => self.all_buttons_pressed(buttons),
UserInput::VirtualDPad(VirtualDPad {
up,
down,
left,
right,
}) => {
for button in [up, down, left, right] {
if self.button_pressed(*button) {
return true;
}
}
false
}
}
}
#[must_use]
pub fn any_pressed(&self, inputs: &PetitSet<UserInput, 16>) -> bool {
for input in inputs.iter() {
if self.input_pressed(input) {
return true;
}
}
false
}
#[must_use]
pub fn button_pressed(&self, button: InputKind) -> bool {
match button {
InputKind::DualAxis(_) => {
let axis_pair = self.input_axis_pair(&UserInput::Single(button)).unwrap();
axis_pair.length() != 0.0
}
InputKind::SingleAxis(_) => {
let value = self.input_value(&UserInput::Single(button));
value != 0.0
}
InputKind::GamepadButton(gamepad_button) => {
if let Some(gamepad) = self.guess_gamepad() {
self.gamepad_buttons.pressed(GamepadButton {
gamepad,
button_type: gamepad_button,
})
} else {
false
}
}
InputKind::Keyboard(keycode) => self.keycode.pressed(keycode),
InputKind::Mouse(mouse_button) => self.mouse_button.pressed(mouse_button),
InputKind::MouseWheel(mouse_wheel_direction) => {
let mut total_mouse_wheel_movement = 0.0;
let mut event_reader = self.mouse_wheel.get_reader();
for mouse_wheel_event in event_reader.iter(self.mouse_wheel) {
total_mouse_wheel_movement += match mouse_wheel_direction {
MouseWheelDirection::Up | MouseWheelDirection::Down => mouse_wheel_event.y,
MouseWheelDirection::Left | MouseWheelDirection::Right => {
mouse_wheel_event.x
}
}
}
match mouse_wheel_direction {
MouseWheelDirection::Up | MouseWheelDirection::Right => {
total_mouse_wheel_movement > 0.0
}
MouseWheelDirection::Down | MouseWheelDirection::Left => {
total_mouse_wheel_movement < 0.0
}
}
}
InputKind::MouseMotion(mouse_motion_direction) => {
let mut total_mouse_movement = 0.0;
let mut event_reader = self.mouse_motion.get_reader();
for mouse_motion_event in event_reader.iter(self.mouse_motion) {
total_mouse_movement += match mouse_motion_direction {
MouseMotionDirection::Up | MouseMotionDirection::Down => {
mouse_motion_event.delta.y
}
MouseMotionDirection::Left | MouseMotionDirection::Right => {
mouse_motion_event.delta.x
}
}
}
match mouse_motion_direction {
MouseMotionDirection::Up | MouseMotionDirection::Right => {
total_mouse_movement > 0.0
}
MouseMotionDirection::Down | MouseMotionDirection::Left => {
total_mouse_movement < 0.0
}
}
}
}
}
#[must_use]
pub fn all_buttons_pressed(&self, buttons: &PetitSet<InputKind, 8>) -> bool {
for &button in buttons.iter() {
if !self.button_pressed(button) {
return false;
}
}
true
}
pub fn input_value(&self, input: &UserInput) -> f32 {
let use_button_value = || -> f32 {
if self.input_pressed(input) {
1.0
} else {
0.0
}
};
let value_in_axis_range = |axis: &SingleAxis, value: f32| -> f32 {
if value >= axis.negative_low && value <= axis.positive_low {
0.0
} else {
value
}
};
match input {
UserInput::Single(InputKind::SingleAxis(single_axis)) => {
match single_axis.axis_type {
AxisType::Gamepad(axis_type) => {
if let Some(gamepad) = self.guess_gamepad() {
self.gamepad_axes
.get(GamepadAxis { gamepad, axis_type })
.unwrap_or_default()
} else {
0.0
}
}
AxisType::MouseWheel(axis_type) => {
let mut total_mouse_wheel_movement = 0.0;
let mut event_reader = self.mouse_wheel.get_reader();
for mouse_wheel_event in event_reader.iter(self.mouse_wheel) {
total_mouse_wheel_movement += match axis_type {
MouseWheelAxisType::X => mouse_wheel_event.x,
MouseWheelAxisType::Y => mouse_wheel_event.y,
}
}
value_in_axis_range(single_axis, total_mouse_wheel_movement)
}
AxisType::MouseMotion(axis_type) => {
let mut total_mouse_motion_movement = 0.0;
let mut event_reader = self.mouse_motion.get_reader();
for mouse_wheel_event in event_reader.iter(self.mouse_motion) {
total_mouse_motion_movement += match axis_type {
MouseMotionAxisType::X => mouse_wheel_event.delta.x,
MouseMotionAxisType::Y => mouse_wheel_event.delta.y,
}
}
value_in_axis_range(single_axis, total_mouse_motion_movement)
}
}
}
UserInput::Single(InputKind::DualAxis(_)) => {
self.input_axis_pair(input).unwrap_or_default().length()
}
UserInput::VirtualDPad { .. } => {
self.input_axis_pair(input).unwrap_or_default().length()
}
UserInput::Single(InputKind::GamepadButton(button_type)) => {
if let Some(gamepad) = self.guess_gamepad() {
self.gamepad_button_axes
.get(GamepadButton {
gamepad,
button_type: *button_type,
})
.unwrap_or_else(use_button_value)
} else {
0.0
}
}
_ => use_button_value(),
}
}
pub fn input_axis_pair(&self, input: &UserInput) -> Option<DualAxisData> {
match input {
UserInput::Single(InputKind::DualAxis(dual_axis)) => {
let x = self.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.x)));
let y = self.input_value(&UserInput::Single(InputKind::SingleAxis(dual_axis.y)));
if x > dual_axis.x.positive_low
|| x < dual_axis.x.negative_low
|| y > dual_axis.y.positive_low
|| y < dual_axis.y.negative_low
{
Some(DualAxisData::new(x, y))
} else {
Some(DualAxisData::new(0.0, 0.0))
}
}
UserInput::VirtualDPad(VirtualDPad {
up,
down,
left,
right,
}) => {
let x = self.input_value(&UserInput::Single(*right)).abs()
- self.input_value(&UserInput::Single(*left)).abs();
let y = self.input_value(&UserInput::Single(*up)).abs()
- self.input_value(&UserInput::Single(*down)).abs();
Some(DualAxisData::new(x, y))
}
_ => None,
}
}
}
#[derive(Debug)]
pub struct MutableInputStreams<'a> {
pub gamepad_buttons: &'a mut Input<GamepadButton>,
pub gamepad_button_axes: &'a mut Axis<GamepadButton>,
pub gamepad_axes: &'a mut Axis<GamepadAxis>,
pub gamepads: &'a mut Gamepads,
pub gamepad_events: &'a mut Events<GamepadEventRaw>,
pub keycode: &'a mut Input<KeyCode>,
pub keyboard_events: &'a mut Events<KeyboardInput>,
pub mouse_button: &'a mut Input<MouseButton>,
pub mouse_button_events: &'a mut Events<MouseButtonInput>,
pub mouse_wheel: &'a mut Events<MouseWheel>,
pub mouse_motion: &'a mut Events<MouseMotion>,
pub associated_gamepad: Option<Gamepad>,
}
impl<'a> MutableInputStreams<'a> {
pub fn from_world(world: &'a mut World, gamepad: Option<Gamepad>) -> Self {
let mut input_system_state: SystemState<(
ResMut<Input<GamepadButton>>,
ResMut<Axis<GamepadButton>>,
ResMut<Axis<GamepadAxis>>,
ResMut<Gamepads>,
ResMut<Events<GamepadEventRaw>>,
ResMut<Input<KeyCode>>,
ResMut<Events<KeyboardInput>>,
ResMut<Input<MouseButton>>,
ResMut<Events<MouseButtonInput>>,
ResMut<Events<MouseWheel>>,
ResMut<Events<MouseMotion>>,
)> = SystemState::new(world);
let (
gamepad_buttons,
gamepad_button_axes,
gamepad_axes,
gamepads,
gamepad_events,
keyboard,
keyboard_events,
mouse,
mouse_button_events,
mouse_wheel,
mouse_motion,
) = input_system_state.get_mut(world);
MutableInputStreams {
gamepad_buttons: gamepad_buttons.into_inner(),
gamepad_button_axes: gamepad_button_axes.into_inner(),
gamepad_axes: gamepad_axes.into_inner(),
gamepads: gamepads.into_inner(),
gamepad_events: gamepad_events.into_inner(),
keycode: keyboard.into_inner(),
keyboard_events: keyboard_events.into_inner(),
mouse_button: mouse.into_inner(),
mouse_button_events: mouse_button_events.into_inner(),
mouse_wheel: mouse_wheel.into_inner(),
mouse_motion: mouse_motion.into_inner(),
associated_gamepad: gamepad,
}
}
pub fn guess_gamepad(&self) -> Option<Gamepad> {
match self.associated_gamepad {
Some(gamepad) => Some(gamepad),
None => self.gamepads.iter().next().copied(),
}
}
}
impl<'a> From<MutableInputStreams<'a>> for InputStreams<'a> {
fn from(mutable_streams: MutableInputStreams<'a>) -> Self {
InputStreams {
gamepad_buttons: &*(mutable_streams.gamepad_buttons),
gamepad_button_axes: &*(mutable_streams.gamepad_button_axes),
gamepad_axes: &*(mutable_streams.gamepad_axes),
gamepads: &*(mutable_streams.gamepads),
keycode: &*(mutable_streams.keycode),
mouse_button: &*(mutable_streams.mouse_button),
mouse_wheel: &*(mutable_streams.mouse_wheel),
mouse_motion: &*(mutable_streams.mouse_motion),
associated_gamepad: mutable_streams.associated_gamepad,
}
}
}
impl<'a> From<&'a MutableInputStreams<'a>> for InputStreams<'a> {
fn from(mutable_streams: &'a MutableInputStreams<'a>) -> Self {
InputStreams {
gamepad_buttons: &*(mutable_streams.gamepad_buttons),
gamepad_button_axes: &*(mutable_streams.gamepad_button_axes),
gamepad_axes: &*(mutable_streams.gamepad_axes),
gamepads: &*(mutable_streams.gamepads),
keycode: &*(mutable_streams.keycode),
mouse_button: &*(mutable_streams.mouse_button),
mouse_wheel: &*(mutable_streams.mouse_wheel),
mouse_motion: &*(mutable_streams.mouse_motion),
associated_gamepad: mutable_streams.associated_gamepad,
}
}
}