leafwing_input_manager/user_input/
trait_serde.rs

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
//! Serialization and deserialization of user input.

use std::sync::RwLock;

use bevy::app::App;
use bevy::prelude::TypePath;
use bevy::reflect::GetTypeRegistration;
use once_cell::sync::Lazy;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_flexitos::ser::require_erased_serialize_impl;
use serde_flexitos::{serialize_trait_object, Registry};

use super::{Axislike, Buttonlike, DualAxislike, TripleAxislike};
use crate::typetag::{InfallibleMapRegistry, RegisterTypeTag};

/// Registry of deserializers for [`Buttonlike`]s.
static mut BUTTONLIKE_REGISTRY: Lazy<RwLock<InfallibleMapRegistry<dyn Buttonlike>>> =
    Lazy::new(|| RwLock::new(InfallibleMapRegistry::new("Buttonlike")));

/// Registry of deserializers for [`Axislike`]s.
static mut AXISLIKE_REGISTRY: Lazy<RwLock<InfallibleMapRegistry<dyn Axislike>>> =
    Lazy::new(|| RwLock::new(InfallibleMapRegistry::new("Axislike")));

/// Registry of deserializers for [`DualAxislike`]s.
static mut DUAL_AXISLIKE_REGISTRY: Lazy<RwLock<InfallibleMapRegistry<dyn DualAxislike>>> =
    Lazy::new(|| RwLock::new(InfallibleMapRegistry::new("DualAxislike")));

/// Registry of deserializers for [`TripleAxislike`]s.
static mut TRIPLE_AXISLIKE_REGISTRY: Lazy<RwLock<InfallibleMapRegistry<dyn TripleAxislike>>> =
    Lazy::new(|| RwLock::new(InfallibleMapRegistry::new("TripleAxislike")));

/// A trait for registering inputs.
pub trait RegisterUserInput {
    /// Registers the specified [`Buttonlike`].
    fn register_buttonlike_input<'de, T>(&mut self) -> &mut Self
    where
        T: RegisterTypeTag<'de, dyn Buttonlike> + GetTypeRegistration + TypePath;

    /// Registers the specified [`Axislike`].
    fn register_axislike_input<'de, T>(&mut self) -> &mut Self
    where
        T: RegisterTypeTag<'de, dyn Axislike> + GetTypeRegistration + TypePath;

    /// Registers the specified [`DualAxislike`].
    fn register_dual_axislike_input<'de, T>(&mut self) -> &mut Self
    where
        T: RegisterTypeTag<'de, dyn DualAxislike> + GetTypeRegistration;

    /// Registers the specified [`TripleAxislike`].
    fn register_triple_axislike_input<'de, T>(&mut self) -> &mut Self
    where
        T: RegisterTypeTag<'de, dyn TripleAxislike> + GetTypeRegistration;
}

impl RegisterUserInput for App {
    fn register_buttonlike_input<'de, T>(&mut self) -> &mut Self
    where
        T: RegisterTypeTag<'de, dyn Buttonlike> + GetTypeRegistration,
    {
        let mut registry = unsafe { BUTTONLIKE_REGISTRY.write().unwrap() };
        T::register_typetag(&mut registry);
        self.register_type::<T>();
        self
    }

    fn register_axislike_input<'de, T>(&mut self) -> &mut Self
    where
        T: RegisterTypeTag<'de, dyn Axislike> + GetTypeRegistration,
    {
        let mut registry = unsafe { AXISLIKE_REGISTRY.write().unwrap() };
        T::register_typetag(&mut registry);
        self.register_type::<T>();
        self
    }

    fn register_dual_axislike_input<'de, T>(&mut self) -> &mut Self
    where
        T: RegisterTypeTag<'de, dyn DualAxislike> + GetTypeRegistration,
    {
        let mut registry = unsafe { DUAL_AXISLIKE_REGISTRY.write().unwrap() };
        T::register_typetag(&mut registry);
        self.register_type::<T>();
        self
    }

    fn register_triple_axislike_input<'de, T>(&mut self) -> &mut Self
    where
        T: RegisterTypeTag<'de, dyn TripleAxislike> + GetTypeRegistration,
    {
        let mut registry = unsafe { TRIPLE_AXISLIKE_REGISTRY.write().unwrap() };
        T::register_typetag(&mut registry);
        self.register_type::<T>();
        self
    }
}

mod buttonlike {
    use crate::user_input::Buttonlike;

    use super::*;

    impl<'a> Serialize for dyn Buttonlike + 'a {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            // Check that `Buttonlike` has `erased_serde::Serialize` as a super trait,
            // preventing infinite recursion at runtime.
            const fn __check_erased_serialize_super_trait<T: ?Sized + Buttonlike>() {
                require_erased_serialize_impl::<T>();
            }
            serialize_trait_object(serializer, self.reflect_short_type_path(), self)
        }
    }

    impl<'de> Deserialize<'de> for Box<dyn Buttonlike> {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            let registry = unsafe { BUTTONLIKE_REGISTRY.read().unwrap() };
            registry.deserialize_trait_object(deserializer)
        }
    }
}

mod axislike {
    use crate::user_input::Axislike;

    use super::*;

    impl<'a> Serialize for dyn Axislike + 'a {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            // Check that `Axislike` has `erased_serde::Serialize` as a super trait,
            // preventing infinite recursion at runtime.
            const fn __check_erased_serialize_super_trait<T: ?Sized + Axislike>() {
                require_erased_serialize_impl::<T>();
            }
            serialize_trait_object(serializer, self.reflect_short_type_path(), self)
        }
    }

    impl<'de> Deserialize<'de> for Box<dyn Axislike> {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            let registry = unsafe { AXISLIKE_REGISTRY.read().unwrap() };
            registry.deserialize_trait_object(deserializer)
        }
    }
}

mod dualaxislike {
    use crate::user_input::DualAxislike;

    use super::*;

    impl<'a> Serialize for dyn DualAxislike + 'a {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            // Check that `DualAxislike` has `erased_serde::Serialize` as a super trait,
            // preventing infinite recursion at runtime.
            const fn __check_erased_serialize_super_trait<T: ?Sized + DualAxislike>() {
                require_erased_serialize_impl::<T>();
            }
            serialize_trait_object(serializer, self.reflect_short_type_path(), self)
        }
    }

    impl<'de> Deserialize<'de> for Box<dyn DualAxislike> {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            let registry = unsafe { DUAL_AXISLIKE_REGISTRY.read().unwrap() };
            registry.deserialize_trait_object(deserializer)
        }
    }
}

mod tripleaxislike {
    use crate::user_input::TripleAxislike;

    use super::*;

    impl<'a> Serialize for dyn TripleAxislike + 'a {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            // Check that `TripleAxislike` has `erased_serde::Serialize` as a super trait,
            // preventing infinite recursion at runtime.
            const fn __check_erased_serialize_super_trait<T: ?Sized + TripleAxislike>() {
                require_erased_serialize_impl::<T>();
            }
            serialize_trait_object(serializer, self.reflect_short_type_path(), self)
        }
    }

    impl<'de> Deserialize<'de> for Box<dyn TripleAxislike> {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            let registry = unsafe { TRIPLE_AXISLIKE_REGISTRY.read().unwrap() };
            registry.deserialize_trait_object(deserializer)
        }
    }
}

#[cfg(any(feature = "keyboard", feature = "mouse"))]
#[cfg(test)]
mod tests {
    use crate as leafwing_input_manager;
    use bevy::prelude::{App, Reflect};
    use leafwing_input_manager_macros::Actionlike;

    #[derive(Actionlike, Debug, Clone, PartialEq, Eq, Hash, Reflect)]
    pub enum Action {
        Foo,
    }

    fn register_input_deserializers() {
        let mut app = App::new();

        // Add the plugin to register input deserializers
        app.add_plugins(crate::prelude::InputManagerPlugin::<Action>::default());
    }

    #[cfg(feature = "keyboard")]
    #[test]
    fn test_button_serde() {
        use crate::prelude::Buttonlike;
        use bevy::prelude::KeyCode;
        use serde_test::{assert_tokens, Token};

        register_input_deserializers();

        let boxed_input: Box<dyn Buttonlike> = Box::new(KeyCode::KeyB);
        assert_tokens(
            &boxed_input,
            &[
                Token::Map { len: Some(1) },
                Token::BorrowedStr("KeyCode"),
                Token::UnitVariant {
                    name: "KeyCode",
                    variant: "KeyB",
                },
                Token::MapEnd,
            ],
        );
    }

    #[cfg(feature = "mouse")]
    #[test]
    fn test_axis_serde() {
        use crate::prelude::{Axislike, MouseScrollAxis};
        use serde_test::{assert_tokens, Token};

        register_input_deserializers();

        let boxed_input: Box<dyn Axislike> = Box::new(MouseScrollAxis::Y);
        assert_tokens(
            &boxed_input,
            &[
                Token::Map { len: Some(1) },
                Token::BorrowedStr("MouseScrollAxis"),
                Token::Struct {
                    name: "MouseScrollAxis",
                    len: 2,
                },
                Token::BorrowedStr("axis"),
                Token::Enum {
                    name: "DualAxisType",
                },
                Token::Str("Y"),
                Token::Unit,
                Token::BorrowedStr("processors"),
                Token::Seq { len: Some(0) },
                Token::SeqEnd,
                Token::StructEnd,
                Token::MapEnd,
            ],
        );
    }

    #[cfg(feature = "mouse")]
    #[test]
    fn test_dual_axis_serde() {
        use crate::prelude::{DualAxislike, MouseMove};
        use serde_test::{assert_tokens, Token};

        register_input_deserializers();

        let boxed_input: Box<dyn DualAxislike> = Box::new(MouseMove::default());
        assert_tokens(
            &boxed_input,
            &[
                Token::Map { len: Some(1) },
                Token::BorrowedStr("MouseMove"),
                Token::Struct {
                    name: "MouseMove",
                    len: 1,
                },
                Token::Str("processors"),
                Token::Seq { len: Some(0) },
                Token::SeqEnd,
                Token::StructEnd,
                Token::MapEnd,
            ],
        );
    }

    #[cfg(feature = "keyboard")]
    #[test]
    fn test_triple_axis_serde() {
        use crate::prelude::{KeyboardVirtualDPad3D, TripleAxislike};
        use bevy::prelude::KeyCode;
        use serde_test::{assert_tokens, Token};

        register_input_deserializers();

        let boxed_input: Box<dyn TripleAxislike> = Box::new(KeyboardVirtualDPad3D::new(
            KeyCode::KeyW,
            KeyCode::KeyS,
            KeyCode::KeyA,
            KeyCode::KeyD,
            KeyCode::KeyF,
            KeyCode::KeyB,
        ));
        assert_tokens(
            &boxed_input,
            &[
                Token::Map { len: Some(1) },
                Token::BorrowedStr("KeyboardVirtualDPad3D"),
                Token::Struct {
                    name: "KeyboardVirtualDPad3D",
                    len: 6,
                },
                Token::Str("up"),
                Token::UnitVariant {
                    name: "KeyCode",
                    variant: "KeyW",
                },
                Token::Str("down"),
                Token::UnitVariant {
                    name: "KeyCode",
                    variant: "KeyS",
                },
                Token::Str("left"),
                Token::UnitVariant {
                    name: "KeyCode",
                    variant: "KeyA",
                },
                Token::Str("right"),
                Token::UnitVariant {
                    name: "KeyCode",
                    variant: "KeyD",
                },
                Token::Str("forward"),
                Token::UnitVariant {
                    name: "KeyCode",
                    variant: "KeyF",
                },
                Token::Str("backward"),
                Token::UnitVariant {
                    name: "KeyCode",
                    variant: "KeyB",
                },
                Token::StructEnd,
                Token::MapEnd,
            ],
        );
    }
}