leafwing_input_manager/user_input/
trait_reflection.rs

1//! Implementations of the various [`bevy::reflect`] traits required to make our types reflectable.
2//!
3//! Note that [bevy #3392](https://github.com/bevyengine/bevy/issues/3392) would eliminate the need for this.
4
5use std::any::Any;
6
7use bevy::reflect::{
8    utility::{GenericTypePathCell, NonGenericTypeInfoCell},
9    FromReflect, FromType, GetTypeRegistration, OpaqueInfo, Reflect, ReflectDeserialize,
10    ReflectFromPtr, ReflectKind, ReflectMut, ReflectOwned, ReflectRef, ReflectSerialize, TypeInfo,
11    TypePath, TypeRegistration, Typed,
12};
13
14use dyn_eq::DynEq;
15
16mod buttonlike {
17    use bevy::reflect::PartialReflect;
18
19    use super::*;
20
21    use crate::user_input::Buttonlike;
22
23    dyn_clone::clone_trait_object!(Buttonlike);
24    dyn_eq::eq_trait_object!(Buttonlike);
25    dyn_hash::hash_trait_object!(Buttonlike);
26
27    impl PartialReflect for Box<dyn Buttonlike> {
28        fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
29            Some(Self::type_info())
30        }
31
32        fn reflect_kind(&self) -> ReflectKind {
33            ReflectKind::Opaque
34        }
35
36        fn reflect_ref(&self) -> ReflectRef {
37            ReflectRef::Opaque(self)
38        }
39
40        fn reflect_mut(&mut self) -> ReflectMut {
41            ReflectMut::Opaque(self)
42        }
43
44        fn reflect_owned(self: Box<Self>) -> ReflectOwned {
45            ReflectOwned::Opaque(self)
46        }
47
48        fn clone_value(&self) -> Box<dyn PartialReflect> {
49            Box::new(self.clone())
50        }
51
52        fn try_apply(
53            &mut self,
54            value: &dyn PartialReflect,
55        ) -> Result<(), bevy::reflect::ApplyError> {
56            if let Some(value) = value.try_downcast_ref::<Self>() {
57                *self = value.clone();
58                Ok(())
59            } else {
60                Err(bevy::reflect::ApplyError::MismatchedTypes {
61                    from_type: self
62                        .reflect_type_ident()
63                        .unwrap_or_default()
64                        .to_string()
65                        .into_boxed_str(),
66                    to_type: self
67                        .reflect_type_ident()
68                        .unwrap_or_default()
69                        .to_string()
70                        .into_boxed_str(),
71                })
72            }
73        }
74
75        fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
76            self
77        }
78
79        fn as_partial_reflect(&self) -> &dyn PartialReflect {
80            self
81        }
82
83        fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
84            self
85        }
86
87        fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
88            Ok(self)
89        }
90
91        fn try_as_reflect(&self) -> Option<&dyn Reflect> {
92            Some(self)
93        }
94
95        fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
96            Some(self)
97        }
98    }
99
100    impl Reflect for Box<dyn Buttonlike> {
101        fn into_any(self: Box<Self>) -> Box<dyn Any> {
102            self
103        }
104
105        fn as_any(&self) -> &dyn Any {
106            self
107        }
108
109        fn as_any_mut(&mut self) -> &mut dyn Any {
110            self
111        }
112
113        fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
114            self
115        }
116
117        fn as_reflect(&self) -> &dyn Reflect {
118            self
119        }
120
121        fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
122            self
123        }
124
125        fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
126            *self = value.take()?;
127            Ok(())
128        }
129    }
130
131    impl Typed for Box<dyn Buttonlike> {
132        fn type_info() -> &'static TypeInfo {
133            static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
134            CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
135        }
136    }
137
138    impl TypePath for Box<dyn Buttonlike> {
139        fn type_path() -> &'static str {
140            static CELL: GenericTypePathCell = GenericTypePathCell::new();
141            CELL.get_or_insert::<Self, _>(|| {
142                {
143                    format!("std::boxed::Box<dyn {}::Buttonlike>", module_path!())
144                }
145            })
146        }
147
148        fn short_type_path() -> &'static str {
149            static CELL: GenericTypePathCell = GenericTypePathCell::new();
150            CELL.get_or_insert::<Self, _>(|| "Box<dyn Buttonlike>".to_string())
151        }
152
153        fn type_ident() -> Option<&'static str> {
154            Some("Box<dyn Buttonlike>")
155        }
156
157        fn crate_name() -> Option<&'static str> {
158            module_path!().split(':').next()
159        }
160
161        fn module_path() -> Option<&'static str> {
162            Some(module_path!())
163        }
164    }
165
166    impl GetTypeRegistration for Box<dyn Buttonlike> {
167        fn get_type_registration() -> TypeRegistration {
168            let mut registration = TypeRegistration::of::<Self>();
169            registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());
170            registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
171            registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());
172            registration
173        }
174    }
175
176    impl FromReflect for Box<dyn Buttonlike> {
177        fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
178            Some(reflect.try_downcast_ref::<Self>()?.clone())
179        }
180    }
181}
182
183mod axislike {
184    use bevy::reflect::PartialReflect;
185
186    use super::*;
187
188    use crate::user_input::Axislike;
189
190    dyn_clone::clone_trait_object!(Axislike);
191    dyn_eq::eq_trait_object!(Axislike);
192    dyn_hash::hash_trait_object!(Axislike);
193
194    impl PartialReflect for Box<dyn Axislike> {
195        fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
196            Some(Self::type_info())
197        }
198
199        fn reflect_kind(&self) -> ReflectKind {
200            ReflectKind::Opaque
201        }
202
203        fn reflect_ref(&self) -> ReflectRef {
204            ReflectRef::Opaque(self)
205        }
206
207        fn reflect_mut(&mut self) -> ReflectMut {
208            ReflectMut::Opaque(self)
209        }
210
211        fn reflect_owned(self: Box<Self>) -> ReflectOwned {
212            ReflectOwned::Opaque(self)
213        }
214
215        fn clone_value(&self) -> Box<dyn PartialReflect> {
216            Box::new(self.clone())
217        }
218
219        fn try_apply(
220            &mut self,
221            value: &dyn PartialReflect,
222        ) -> Result<(), bevy::reflect::ApplyError> {
223            if let Some(value) = value.try_downcast_ref::<Self>() {
224                *self = value.clone();
225                Ok(())
226            } else {
227                Err(bevy::reflect::ApplyError::MismatchedTypes {
228                    from_type: self
229                        .reflect_type_ident()
230                        .unwrap_or_default()
231                        .to_string()
232                        .into_boxed_str(),
233                    to_type: self
234                        .reflect_type_ident()
235                        .unwrap_or_default()
236                        .to_string()
237                        .into_boxed_str(),
238                })
239            }
240        }
241
242        fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
243            self
244        }
245
246        fn as_partial_reflect(&self) -> &dyn PartialReflect {
247            self
248        }
249
250        fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
251            self
252        }
253
254        fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
255            Ok(self)
256        }
257
258        fn try_as_reflect(&self) -> Option<&dyn Reflect> {
259            Some(self)
260        }
261
262        fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
263            Some(self)
264        }
265    }
266
267    impl Reflect for Box<dyn Axislike> {
268        fn into_any(self: Box<Self>) -> Box<dyn Any> {
269            self
270        }
271
272        fn as_any(&self) -> &dyn Any {
273            self
274        }
275
276        fn as_any_mut(&mut self) -> &mut dyn Any {
277            self
278        }
279
280        fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
281            self
282        }
283
284        fn as_reflect(&self) -> &dyn Reflect {
285            self
286        }
287
288        fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
289            self
290        }
291
292        fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
293            *self = value.take()?;
294            Ok(())
295        }
296    }
297
298    impl Typed for Box<dyn Axislike> {
299        fn type_info() -> &'static TypeInfo {
300            static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
301            CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
302        }
303    }
304
305    impl TypePath for Box<dyn Axislike> {
306        fn type_path() -> &'static str {
307            static CELL: GenericTypePathCell = GenericTypePathCell::new();
308            CELL.get_or_insert::<Self, _>(|| {
309                {
310                    format!("std::boxed::Box<dyn {}::Axislike>", module_path!())
311                }
312            })
313        }
314
315        fn short_type_path() -> &'static str {
316            static CELL: GenericTypePathCell = GenericTypePathCell::new();
317            CELL.get_or_insert::<Self, _>(|| "Box<dyn Axislike>".to_string())
318        }
319
320        fn type_ident() -> Option<&'static str> {
321            Some("Box<dyn Axislike>")
322        }
323
324        fn crate_name() -> Option<&'static str> {
325            module_path!().split(':').next()
326        }
327
328        fn module_path() -> Option<&'static str> {
329            Some(module_path!())
330        }
331    }
332
333    impl GetTypeRegistration for Box<dyn Axislike> {
334        fn get_type_registration() -> TypeRegistration {
335            let mut registration = TypeRegistration::of::<Self>();
336            registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());
337            registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
338            registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());
339            registration
340        }
341    }
342
343    impl FromReflect for Box<dyn Axislike> {
344        fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
345            Some(reflect.try_downcast_ref::<Self>()?.clone())
346        }
347    }
348}
349
350mod dualaxislike {
351    use bevy::reflect::{OpaqueInfo, PartialReflect};
352
353    use super::*;
354
355    use crate::user_input::DualAxislike;
356
357    dyn_clone::clone_trait_object!(DualAxislike);
358    dyn_eq::eq_trait_object!(DualAxislike);
359    dyn_hash::hash_trait_object!(DualAxislike);
360
361    impl PartialReflect for Box<dyn DualAxislike> {
362        fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
363            Some(Self::type_info())
364        }
365
366        fn try_apply(
367            &mut self,
368            value: &dyn PartialReflect,
369        ) -> Result<(), bevy::reflect::ApplyError> {
370            if let Some(value) = value.try_downcast_ref::<Self>() {
371                *self = value.clone();
372                Ok(())
373            } else {
374                Err(bevy::reflect::ApplyError::MismatchedTypes {
375                    from_type: self
376                        .reflect_type_ident()
377                        .unwrap_or_default()
378                        .to_string()
379                        .into_boxed_str(),
380                    to_type: self
381                        .reflect_type_ident()
382                        .unwrap_or_default()
383                        .to_string()
384                        .into_boxed_str(),
385                })
386            }
387        }
388
389        fn reflect_kind(&self) -> ReflectKind {
390            ReflectKind::Opaque
391        }
392
393        fn reflect_ref(&self) -> ReflectRef {
394            ReflectRef::Opaque(self)
395        }
396
397        fn reflect_mut(&mut self) -> ReflectMut {
398            ReflectMut::Opaque(self)
399        }
400
401        fn reflect_owned(self: Box<Self>) -> ReflectOwned {
402            ReflectOwned::Opaque(self)
403        }
404
405        fn clone_value(&self) -> Box<dyn PartialReflect> {
406            Box::new(self.clone())
407        }
408
409        fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
410            self
411        }
412
413        fn as_partial_reflect(&self) -> &dyn PartialReflect {
414            self
415        }
416
417        fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
418            self
419        }
420
421        fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
422            Ok(self)
423        }
424
425        fn try_as_reflect(&self) -> Option<&dyn Reflect> {
426            Some(self)
427        }
428
429        fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
430            Some(self)
431        }
432    }
433
434    impl Reflect for Box<dyn DualAxislike> {
435        fn into_any(self: Box<Self>) -> Box<dyn Any> {
436            self
437        }
438
439        fn as_any(&self) -> &dyn Any {
440            self
441        }
442
443        fn as_any_mut(&mut self) -> &mut dyn Any {
444            self
445        }
446
447        fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
448            self
449        }
450
451        fn as_reflect(&self) -> &dyn Reflect {
452            self
453        }
454
455        fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
456            self
457        }
458
459        fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
460            *self = value.take()?;
461            Ok(())
462        }
463    }
464
465    impl Typed for Box<dyn DualAxislike> {
466        fn type_info() -> &'static TypeInfo {
467            static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
468            CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
469        }
470    }
471
472    impl TypePath for Box<dyn DualAxislike> {
473        fn type_path() -> &'static str {
474            static CELL: GenericTypePathCell = GenericTypePathCell::new();
475            CELL.get_or_insert::<Self, _>(|| {
476                {
477                    format!("std::boxed::Box<dyn {}::DualAxislike>", module_path!())
478                }
479            })
480        }
481
482        fn short_type_path() -> &'static str {
483            static CELL: GenericTypePathCell = GenericTypePathCell::new();
484            CELL.get_or_insert::<Self, _>(|| "Box<dyn DualAxislike>".to_string())
485        }
486
487        fn type_ident() -> Option<&'static str> {
488            Some("Box<dyn DualAxislike>")
489        }
490
491        fn crate_name() -> Option<&'static str> {
492            module_path!().split(':').next()
493        }
494
495        fn module_path() -> Option<&'static str> {
496            Some(module_path!())
497        }
498    }
499
500    impl GetTypeRegistration for Box<dyn DualAxislike> {
501        fn get_type_registration() -> TypeRegistration {
502            let mut registration = TypeRegistration::of::<Self>();
503            registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());
504            registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
505            registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());
506            registration
507        }
508    }
509
510    impl FromReflect for Box<dyn DualAxislike> {
511        fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
512            Some(reflect.try_downcast_ref::<Self>()?.clone())
513        }
514    }
515}
516
517mod tripleaxislike {
518    use bevy::reflect::{OpaqueInfo, PartialReflect};
519
520    use super::*;
521
522    use crate::user_input::TripleAxislike;
523
524    dyn_clone::clone_trait_object!(TripleAxislike);
525    dyn_eq::eq_trait_object!(TripleAxislike);
526    dyn_hash::hash_trait_object!(TripleAxislike);
527
528    impl PartialReflect for Box<dyn TripleAxislike> {
529        fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
530            Some(Self::type_info())
531        }
532
533        fn try_apply(
534            &mut self,
535            value: &dyn PartialReflect,
536        ) -> Result<(), bevy::reflect::ApplyError> {
537            if let Some(value) = value.try_downcast_ref::<Self>() {
538                *self = value.clone();
539                Ok(())
540            } else {
541                Err(bevy::reflect::ApplyError::MismatchedTypes {
542                    from_type: self
543                        .reflect_type_ident()
544                        .unwrap_or_default()
545                        .to_string()
546                        .into_boxed_str(),
547                    to_type: self
548                        .reflect_type_ident()
549                        .unwrap_or_default()
550                        .to_string()
551                        .into_boxed_str(),
552                })
553            }
554        }
555
556        fn reflect_kind(&self) -> ReflectKind {
557            ReflectKind::Opaque
558        }
559
560        fn reflect_ref(&self) -> ReflectRef {
561            ReflectRef::Opaque(self)
562        }
563
564        fn reflect_mut(&mut self) -> ReflectMut {
565            ReflectMut::Opaque(self)
566        }
567
568        fn reflect_owned(self: Box<Self>) -> ReflectOwned {
569            ReflectOwned::Opaque(self)
570        }
571
572        fn clone_value(&self) -> Box<dyn PartialReflect> {
573            Box::new(self.clone())
574        }
575
576        fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
577            self
578        }
579
580        fn as_partial_reflect(&self) -> &dyn PartialReflect {
581            self
582        }
583
584        fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
585            self
586        }
587
588        fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
589            Ok(self)
590        }
591
592        fn try_as_reflect(&self) -> Option<&dyn Reflect> {
593            Some(self)
594        }
595
596        fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
597            Some(self)
598        }
599    }
600
601    impl Reflect for Box<dyn TripleAxislike> {
602        fn into_any(self: Box<Self>) -> Box<dyn Any> {
603            self
604        }
605
606        fn as_any(&self) -> &dyn Any {
607            self
608        }
609
610        fn as_any_mut(&mut self) -> &mut dyn Any {
611            self
612        }
613
614        fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
615            self
616        }
617
618        fn as_reflect(&self) -> &dyn Reflect {
619            self
620        }
621
622        fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
623            self
624        }
625
626        fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
627            *self = value.take()?;
628            Ok(())
629        }
630    }
631
632    impl Typed for Box<dyn TripleAxislike> {
633        fn type_info() -> &'static TypeInfo {
634            static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
635            CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
636        }
637    }
638
639    impl TypePath for Box<dyn TripleAxislike> {
640        fn type_path() -> &'static str {
641            static CELL: GenericTypePathCell = GenericTypePathCell::new();
642            CELL.get_or_insert::<Self, _>(|| {
643                {
644                    format!("std::boxed::Box<dyn {}::TripleAxislike>", module_path!())
645                }
646            })
647        }
648
649        fn short_type_path() -> &'static str {
650            static CELL: GenericTypePathCell = GenericTypePathCell::new();
651            CELL.get_or_insert::<Self, _>(|| "Box<dyn TripleAxislike>".to_string())
652        }
653
654        fn type_ident() -> Option<&'static str> {
655            Some("Box<dyn TripleAxislike>")
656        }
657
658        fn crate_name() -> Option<&'static str> {
659            module_path!().split(':').next()
660        }
661
662        fn module_path() -> Option<&'static str> {
663            Some(module_path!())
664        }
665    }
666
667    impl GetTypeRegistration for Box<dyn TripleAxislike> {
668        fn get_type_registration() -> TypeRegistration {
669            let mut registration = TypeRegistration::of::<Self>();
670            registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());
671            registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
672            registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());
673            registration
674        }
675    }
676
677    impl FromReflect for Box<dyn TripleAxislike> {
678        fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
679            Some(reflect.try_downcast_ref::<Self>()?.clone())
680        }
681    }
682}