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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
use crate::buttonlike::{MouseMotionDirection, MouseWheelDirection};
use crate::orientation::{Direction, Rotation};
use crate::prelude::QwertyScanCode;
use crate::user_input::InputKind;
use bevy::input::{
gamepad::{GamepadAxisType, GamepadButtonType},
keyboard::KeyCode,
};
use bevy::math::Vec2;
use bevy::reflect::{FromReflect, Reflect};
use bevy::utils::FloatOrd;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct SingleAxis {
pub axis_type: AxisType,
pub positive_low: f32,
pub negative_low: f32,
pub value: Option<f32>,
}
impl SingleAxis {
#[must_use]
pub fn symmetric(axis_type: impl Into<AxisType>, threshold: f32) -> SingleAxis {
SingleAxis {
axis_type: axis_type.into(),
positive_low: threshold,
negative_low: -threshold,
value: None,
}
}
#[must_use]
pub fn from_value(axis_type: impl Into<AxisType>, value: f32) -> SingleAxis {
SingleAxis {
axis_type: axis_type.into(),
positive_low: 0.0,
negative_low: 0.0,
value: Some(value),
}
}
#[must_use]
pub const fn mouse_wheel_x() -> SingleAxis {
SingleAxis {
axis_type: AxisType::MouseWheel(MouseWheelAxisType::X),
positive_low: 0.,
negative_low: 0.,
value: None,
}
}
#[must_use]
pub const fn mouse_wheel_y() -> SingleAxis {
SingleAxis {
axis_type: AxisType::MouseWheel(MouseWheelAxisType::Y),
positive_low: 0.,
negative_low: 0.,
value: None,
}
}
#[must_use]
pub const fn mouse_motion_x() -> SingleAxis {
SingleAxis {
axis_type: AxisType::MouseMotion(MouseMotionAxisType::X),
positive_low: 0.,
negative_low: 0.,
value: None,
}
}
#[must_use]
pub const fn mouse_motion_y() -> SingleAxis {
SingleAxis {
axis_type: AxisType::MouseMotion(MouseMotionAxisType::Y),
positive_low: 0.,
negative_low: 0.,
value: None,
}
}
pub fn negative_only(axis_type: impl Into<AxisType>, threshold: f32) -> SingleAxis {
SingleAxis {
axis_type: axis_type.into(),
negative_low: threshold,
positive_low: f32::MAX,
value: None,
}
}
pub fn positive_only(axis_type: impl Into<AxisType>, threshold: f32) -> SingleAxis {
SingleAxis {
axis_type: axis_type.into(),
negative_low: f32::MIN,
positive_low: threshold,
value: None,
}
}
#[must_use]
pub fn with_deadzone(mut self, deadzone: f32) -> SingleAxis {
self.negative_low = deadzone;
self.positive_low = deadzone;
self
}
}
impl PartialEq for SingleAxis {
fn eq(&self, other: &Self) -> bool {
self.axis_type == other.axis_type
&& FloatOrd(self.positive_low) == FloatOrd(other.positive_low)
&& FloatOrd(self.negative_low) == FloatOrd(other.negative_low)
}
}
impl Eq for SingleAxis {}
impl std::hash::Hash for SingleAxis {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.axis_type.hash(state);
FloatOrd(self.positive_low).hash(state);
FloatOrd(self.negative_low).hash(state);
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct DualAxis {
pub x: SingleAxis,
pub y: SingleAxis,
}
impl DualAxis {
pub const DEFAULT_DEADZONE: f32 = 0.1;
#[must_use]
pub fn symmetric(
x_axis_type: impl Into<AxisType>,
y_axis_type: impl Into<AxisType>,
threshold: f32,
) -> DualAxis {
DualAxis {
x: SingleAxis::symmetric(x_axis_type, threshold),
y: SingleAxis::symmetric(y_axis_type, threshold),
}
}
#[must_use]
pub fn from_value(
x_axis_type: impl Into<AxisType>,
y_axis_type: impl Into<AxisType>,
x_value: f32,
y_value: f32,
) -> DualAxis {
DualAxis {
x: SingleAxis::from_value(x_axis_type, x_value),
y: SingleAxis::from_value(y_axis_type, y_value),
}
}
#[must_use]
pub fn left_stick() -> DualAxis {
DualAxis::symmetric(
GamepadAxisType::LeftStickX,
GamepadAxisType::LeftStickY,
Self::DEFAULT_DEADZONE,
)
}
#[must_use]
pub fn right_stick() -> DualAxis {
DualAxis::symmetric(
GamepadAxisType::RightStickX,
GamepadAxisType::RightStickY,
Self::DEFAULT_DEADZONE,
)
}
pub const fn mouse_wheel() -> DualAxis {
DualAxis {
x: SingleAxis::mouse_wheel_x(),
y: SingleAxis::mouse_wheel_y(),
}
}
pub const fn mouse_motion() -> DualAxis {
DualAxis {
x: SingleAxis::mouse_motion_x(),
y: SingleAxis::mouse_motion_y(),
}
}
#[must_use]
pub fn with_deadzone(mut self, deadzone: f32) -> DualAxis {
self.x = self.x.with_deadzone(deadzone);
self.y = self.y.with_deadzone(deadzone);
self
}
}
#[allow(clippy::doc_markdown)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct VirtualDPad {
pub up: InputKind,
pub down: InputKind,
pub left: InputKind,
pub right: InputKind,
}
impl VirtualDPad {
pub fn arrow_keys() -> VirtualDPad {
VirtualDPad {
up: InputKind::Keyboard(KeyCode::Up),
down: InputKind::Keyboard(KeyCode::Down),
left: InputKind::Keyboard(KeyCode::Left),
right: InputKind::Keyboard(KeyCode::Right),
}
}
pub fn wasd() -> VirtualDPad {
VirtualDPad {
up: InputKind::KeyLocation(QwertyScanCode::W.into()),
down: InputKind::KeyLocation(QwertyScanCode::S.into()),
left: InputKind::KeyLocation(QwertyScanCode::A.into()),
right: InputKind::KeyLocation(QwertyScanCode::D.into()),
}
}
#[allow(clippy::doc_markdown)] pub fn dpad() -> VirtualDPad {
VirtualDPad {
up: InputKind::GamepadButton(GamepadButtonType::DPadUp),
down: InputKind::GamepadButton(GamepadButtonType::DPadDown),
left: InputKind::GamepadButton(GamepadButtonType::DPadLeft),
right: InputKind::GamepadButton(GamepadButtonType::DPadRight),
}
}
pub fn gamepad_face_buttons() -> VirtualDPad {
VirtualDPad {
up: InputKind::GamepadButton(GamepadButtonType::North),
down: InputKind::GamepadButton(GamepadButtonType::South),
left: InputKind::GamepadButton(GamepadButtonType::West),
right: InputKind::GamepadButton(GamepadButtonType::East),
}
}
pub fn mouse_wheel() -> VirtualDPad {
VirtualDPad {
up: InputKind::MouseWheel(MouseWheelDirection::Up),
down: InputKind::MouseWheel(MouseWheelDirection::Down),
left: InputKind::MouseWheel(MouseWheelDirection::Left),
right: InputKind::MouseWheel(MouseWheelDirection::Right),
}
}
pub fn mouse_motion() -> VirtualDPad {
VirtualDPad {
up: InputKind::MouseMotion(MouseMotionDirection::Up),
down: InputKind::MouseMotion(MouseMotionDirection::Down),
left: InputKind::MouseMotion(MouseMotionDirection::Left),
right: InputKind::MouseMotion(MouseMotionDirection::Right),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct VirtualAxis {
pub negative: InputKind,
pub positive: InputKind,
}
impl VirtualAxis {
pub fn horizontal_arrow_keys() -> VirtualAxis {
VirtualAxis {
negative: InputKind::Keyboard(KeyCode::Left),
positive: InputKind::Keyboard(KeyCode::Right),
}
}
pub fn vertical_arrow_keys() -> VirtualAxis {
VirtualAxis {
negative: InputKind::Keyboard(KeyCode::Down),
positive: InputKind::Keyboard(KeyCode::Up),
}
}
pub fn ad() -> VirtualAxis {
VirtualAxis {
negative: InputKind::Keyboard(KeyCode::A),
positive: InputKind::Keyboard(KeyCode::D),
}
}
pub fn ws() -> VirtualAxis {
VirtualAxis {
negative: InputKind::Keyboard(KeyCode::S),
positive: InputKind::Keyboard(KeyCode::W),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AxisType {
Gamepad(GamepadAxisType),
MouseWheel(MouseWheelAxisType),
MouseMotion(MouseMotionAxisType),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MouseWheelAxisType {
X,
Y,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MouseMotionAxisType {
X,
Y,
}
impl From<GamepadAxisType> for AxisType {
fn from(axis_type: GamepadAxisType) -> Self {
AxisType::Gamepad(axis_type)
}
}
impl From<MouseWheelAxisType> for AxisType {
fn from(axis_type: MouseWheelAxisType) -> Self {
AxisType::MouseWheel(axis_type)
}
}
impl From<MouseMotionAxisType> for AxisType {
fn from(axis_type: MouseMotionAxisType) -> Self {
AxisType::MouseMotion(axis_type)
}
}
impl TryFrom<AxisType> for GamepadAxisType {
type Error = AxisConversionError;
fn try_from(axis_type: AxisType) -> Result<Self, AxisConversionError> {
match axis_type {
AxisType::Gamepad(inner) => Ok(inner),
_ => Err(AxisConversionError),
}
}
}
impl TryFrom<AxisType> for MouseWheelAxisType {
type Error = AxisConversionError;
fn try_from(axis_type: AxisType) -> Result<Self, AxisConversionError> {
match axis_type {
AxisType::MouseWheel(inner) => Ok(inner),
_ => Err(AxisConversionError),
}
}
}
impl TryFrom<AxisType> for MouseMotionAxisType {
type Error = AxisConversionError;
fn try_from(axis_type: AxisType) -> Result<Self, AxisConversionError> {
match axis_type {
AxisType::MouseMotion(inner) => Ok(inner),
_ => Err(AxisConversionError),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AxisConversionError;
#[derive(Debug, Copy, Clone, PartialEq, Default, Deserialize, Serialize, Reflect, FromReflect)]
pub struct DualAxisData {
xy: Vec2,
}
impl DualAxisData {
pub fn new(x: f32, y: f32) -> DualAxisData {
DualAxisData {
xy: Vec2::new(x, y),
}
}
pub fn from_xy(xy: Vec2) -> DualAxisData {
DualAxisData { xy }
}
pub fn merged_with(&self, other: DualAxisData) -> DualAxisData {
DualAxisData::from_xy(self.xy() + other.xy())
}
}
impl DualAxisData {
#[must_use]
#[inline]
pub fn x(&self) -> f32 {
self.xy.x
}
#[must_use]
#[inline]
pub fn y(&self) -> f32 {
self.xy.y
}
#[must_use]
#[inline]
pub fn xy(&self) -> Vec2 {
self.xy
}
#[must_use]
#[inline]
pub fn direction(&self) -> Option<Direction> {
if self.xy.length() > 0.00001 {
return Some(Direction::new(self.xy));
}
None
}
#[must_use]
#[inline]
pub fn rotation(&self) -> Option<Rotation> {
match Rotation::from_xy(self.xy) {
Ok(rotation) => Some(rotation),
Err(_) => None,
}
}
#[must_use]
#[inline]
pub fn length(&self) -> f32 {
self.xy.length()
}
#[must_use]
#[inline]
pub fn length_squared(&self) -> f32 {
self.xy.length_squared()
}
pub fn clamp_length(&mut self, max: f32) {
self.xy = self.xy.clamp_length_max(max);
}
}
impl From<DualAxisData> for Vec2 {
fn from(data: DualAxisData) -> Vec2 {
data.xy
}
}