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