box2d-rs 0.0.4

Port of Box2d to Rust
Documentation
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
use serde::de::{self, Deserializer, MapAccess, SeqAccess, Visitor};
use serde::{
    ser::{SerializeStruct},
    Deserialize, Serialize, Serializer,
};

use serde::de::DeserializeSeed;
use std::fmt;

use std::rc::{Rc};
use std::cell::RefCell;

use crate::b2_body::*;
use crate::b2rs_common::UserDataType;
use crate::b2_joint::*;
use crate::b2_world::*;

use crate::joints::serialize::serialize_b2_distance_joint::*;
use crate::joints::serialize::serialize_b2_friction_joint::*;
use crate::joints::serialize::serialize_b2_gear_joint::*;
use crate::joints::serialize::serialize_b2_motor_joint::*;
use crate::joints::serialize::serialize_b2_prismatic_joint::*;
use crate::joints::serialize::serialize_b2_pulley_joint::*;
use crate::joints::serialize::serialize_b2_revolute_joint::*;
use crate::joints::serialize::serialize_b2_weld_joint::*;
use crate::joints::serialize::serialize_b2_wheel_joint::*;

use crate::serialize::serialize_b2_fixture::*;

use strum::VariantNames;
use strum_macros::EnumVariantNames;

trait B2bodyToDef<D: UserDataType> {
    fn get_def(&self) -> B2bodyDef<D>;
}

impl<D: UserDataType> B2bodyToDef<D> for B2body<D> {
    fn get_def(&self) -> B2bodyDef<D> {
        return B2bodyDef {
            body_type: self.m_type,
            position: self.m_xf.p,
            angle: self.m_sweep.a,
            linear_velocity: self.m_linear_velocity,
            angular_velocity: self.m_angular_velocity,
            linear_damping: self.m_linear_damping,
            angular_damping: self.m_angular_damping,
            allow_sleep: self.m_flags.contains(BodyFlags::E_AUTO_SLEEP_FLAG),
            awake: self.m_flags.contains(BodyFlags::E_AWAKE_FLAG),
            fixed_rotation: self.m_flags.contains(BodyFlags::E_FIXED_ROTATION_FLAG),
            bullet: self.m_flags.contains(BodyFlags::E_BULLET_FLAG),
            enabled: self.m_flags.contains(BodyFlags::E_ENABLED_FLAG),
            gravity_scale: self.m_gravity_scale,
            user_data: self.m_user_data.clone(),
        };
    }
}

impl<D: UserDataType> Serialize for B2body<D> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("B2body", 2)?;

        let definition = self.get_def();
        state.serialize_field("m_definition", &definition)?;
        state.serialize_field("m_fixture_list", &self.m_fixture_list)?;
        state.end()
    }
}

#[derive(Clone)]
pub(crate) struct B2bodyDefinitionVisitorContext<D: UserDataType> {
    pub(crate) m_world: B2worldPtr<D>,
}

impl<'de, U: UserDataType> DeserializeSeed<'de> for B2bodyDefinitionVisitorContext<U> {
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(field_identifier, rename_all = "lowercase")]
        #[derive(EnumVariantNames)]
        #[allow(non_camel_case_types)]
        enum Field {
            m_definition,
            m_fixture_list,
        }

        struct B2bodyDefinitionVisitor<D: UserDataType>(B2bodyDefinitionVisitorContext<D>);

        impl<'de, U: UserDataType> Visitor<'de> for B2bodyDefinitionVisitor<U> {
            type Value = ();

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct B2fixture")
            }

            fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
            where
                V: SeqAccess<'de>,
            {
                let world = self.0.m_world.clone();

                let def: B2bodyDef<U> = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;
                let body = Some(B2world::create_body(world.clone(), &def));

                seq.next_element_seed(B2fixtureListVisitorContext {
                    body: Rc::downgrade(&body.clone().unwrap()),
                })?
                .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                Ok(())
            }

            fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut body = None;
                let world = self.0.m_world;
                while let Some(key) = map.next_key()? {
                    match key {
                        Field::m_definition => {
                            let def: B2bodyDef<U> = map.next_value()?;
                            body = Some(B2world::create_body(world.clone(), &def));
                        }
                        Field::m_fixture_list => {
                            map.next_value_seed(B2fixtureListVisitorContext {
                                body: Rc::downgrade(&body.clone().unwrap()),
                            })?;
                        }
                    }
                }
                Ok(())
            }
        }

        deserializer.deserialize_struct("B2body", Field::VARIANTS, B2bodyDefinitionVisitor(self))
    }
}

pub(crate) struct B2bodyListContext<D: UserDataType> {
    pub(crate) m_world: B2worldPtr<D>,
}

impl<'de, U: UserDataType> DeserializeSeed<'de> for B2bodyListContext<U> {
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct B2bodyVisitor<D: UserDataType>(B2bodyListContext<D>);

        impl<'de, U: UserDataType> Visitor<'de> for B2bodyVisitor<U> {
            type Value = ();

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct B2bodyListContext")
            }

            fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
            where
                V: SeqAccess<'de>,
            {
                let context = B2bodyDefinitionVisitorContext {
                    m_world: self.0.m_world.clone(),
                };
                while let Some(_elem) = seq.next_element_seed(context.clone())? {}
                Ok(())
            }
        }

        deserializer.deserialize_seq(B2bodyVisitor(self))
    }
}


#[derive(Clone)]
pub(crate) struct B2jointDefinitionVisitorContext<D: UserDataType> {
    pub(crate) m_world: B2worldPtr<D>,
    pub(crate) m_body_array: Rc<RefCell<Vec<BodyPtr<D>>>>,
    pub(crate) m_all_joints: Rc<RefCell<Vec<B2jointPtr<D>>>>,
}

impl<'de, U: UserDataType> DeserializeSeed<'de> for B2jointDefinitionVisitorContext<U> {
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(field_identifier, rename_all = "lowercase")]
        #[derive(EnumVariantNames)]
        #[allow(non_camel_case_types)]
        enum Field {
            jtype,
            joint_def,
        }

        struct B2jointDefinitionVisitor<D: UserDataType>(B2jointDefinitionVisitorContext<D>);

        impl<'de, U: UserDataType> Visitor<'de> for B2jointDefinitionVisitor<U> {
            type Value = ();

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct B2fixture")
            }

            fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
            where
                V: SeqAccess<'de>,
            {
                let world = self.0.m_world.clone();

                let jtype: B2jointType = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                match jtype {
                    B2jointType::EUnknownJoint =>{
                        panic!();
                    }
                    B2jointType::EDistanceJoint => {
                        let def = seq.next_element_seed(B2distanceJointDefContext {
                            m_body_array: self.0.m_body_array.clone(),
                        })?
                        .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                        world.borrow_mut().create_joint(&B2JointDefEnum::DistanceJoint(def));
                    }
                    B2jointType::EFrictionJoint => {
                        let def = seq.next_element_seed(B2frictionJointDefContext {
                            m_body_array: self.0.m_body_array.clone(),
                        })?
                        .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                        world.borrow_mut().create_joint(&B2JointDefEnum::FrictionJoint(def));
                    }
                    B2jointType::EGearJoint => {
                        let all_joints = self.0.m_all_joints.clone();
                        let def = seq.next_element_seed(B2gearJointDefContext {
                            m_body_array: self.0.m_body_array.clone(),
                            m_all_joints: all_joints.clone(),
                        })?
                        .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                        world.borrow_mut().create_joint(&B2JointDefEnum::GearJoint(def));
                    }
                    B2jointType::EMouseJoint => {
                        panic!();
                    }
                    B2jointType::EMotorJoint => {
                        let def = seq.next_element_seed(B2motorJointDefContext {
                            m_body_array: self.0.m_body_array.clone(),
                        })?
                        .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                        world.borrow_mut().create_joint(&B2JointDefEnum::MotorJoint(def));
                    }
                    B2jointType::EPulleyJoint => {
                        let def = seq.next_element_seed(B2pulleyJointDefContext {
                            m_body_array: self.0.m_body_array.clone(),
                        })?
                        .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                        world.borrow_mut().create_joint(&B2JointDefEnum::PulleyJoint(def));
                    }
                    B2jointType::ERevoluteJoint => {
                        let def = seq.next_element_seed(B2revoluteJointDefContext {
                            m_body_array: self.0.m_body_array.clone(),
                        })?
                        .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                        world.borrow_mut().create_joint(&B2JointDefEnum::RevoluteJoint(def));
                    }
                    B2jointType::EPrismaticJoint => {
                        let def = seq.next_element_seed(B2prismaticJointDefContext {
                            m_body_array: self.0.m_body_array.clone(),
                        })?
                        .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                        world.borrow_mut().create_joint(&B2JointDefEnum::PrismaticJoint(def));
                    }
                    B2jointType::EWeldJoint => {
                        let def = seq.next_element_seed(B2weldJointDefContext {
                            m_body_array: self.0.m_body_array.clone(),
                        })?
                        .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                        world.borrow_mut().create_joint(&B2JointDefEnum::WeldJoint(def));
                    }
                    B2jointType::EWheelJoint => {
                        let def = seq.next_element_seed(B2wheelJointDefContext {
                            m_body_array: self.0.m_body_array.clone(),
                        })?
                        .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                        world.borrow_mut().create_joint(&B2JointDefEnum::WheelJoint(def));
                    }
                }

                Ok(())
            }

            fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
            where
                V: MapAccess<'de>,
            {
                let world = self.0.m_world;
                let mut jtype: Option<B2jointType> = None;
                while let Some(key) = map.next_key()? {
                    match key {
                        Field::jtype => {
                            jtype = Some(map.next_value()?);
                        }
                        Field::joint_def => {
                            match jtype.unwrap() {
                                B2jointType::EUnknownJoint =>{
                                    panic!();
                                }
                                B2jointType::EDistanceJoint => {
                                    let def = map.next_value_seed(B2distanceJointDefContext {
                                        m_body_array: self.0.m_body_array.clone(),
                                    })?;
                                    world.borrow_mut().create_joint(&B2JointDefEnum::DistanceJoint(def));
                                }
                                B2jointType::EFrictionJoint => {
                                    let def = map.next_value_seed(B2frictionJointDefContext {
                                        m_body_array: self.0.m_body_array.clone(),
                                    })?;
                                    world.borrow_mut().create_joint(&B2JointDefEnum::FrictionJoint(def));
                                }
                                B2jointType::EGearJoint => {
                                    let all_joints = self.0.m_all_joints.clone();
                                    let def = map.next_value_seed(B2gearJointDefContext {
                                        m_body_array: self.0.m_body_array.clone(),
                                        m_all_joints: all_joints.clone(),
                                    })?;

                                    let gear_joint = world.borrow_mut().create_joint(&B2JointDefEnum::GearJoint(def));
                                    all_joints.borrow_mut().push(gear_joint);
                                }
                                B2jointType::EMouseJoint => {}
                                B2jointType::EMotorJoint => {
                                    let def = map.next_value_seed(B2motorJointDefContext {
                                        m_body_array: self.0.m_body_array.clone(),
                                    })?;
                                    world.borrow_mut().create_joint(&B2JointDefEnum::MotorJoint(def));
                                }
                                B2jointType::EPulleyJoint => {
                                    let def = map.next_value_seed(B2pulleyJointDefContext {
                                        m_body_array: self.0.m_body_array.clone(),
                                    })?;
                                    world.borrow_mut().create_joint(&B2JointDefEnum::PulleyJoint(def));
                                }
                                B2jointType::ERevoluteJoint => {
                                    let def = map.next_value_seed(B2revoluteJointDefContext {
                                        m_body_array: self.0.m_body_array.clone(),
                                    })?;
                                    world.borrow_mut().create_joint(&B2JointDefEnum::RevoluteJoint(def));
                                }
                                B2jointType::EPrismaticJoint => {
                                    let def = map.next_value_seed(B2prismaticJointDefContext {
                                        m_body_array: self.0.m_body_array.clone(),
                                    })?;
                                    world.borrow_mut().create_joint(&B2JointDefEnum::PrismaticJoint(def));
                                }
                                B2jointType::EWeldJoint => {
                                    let def = map.next_value_seed(B2weldJointDefContext {
                                        m_body_array: self.0.m_body_array.clone(),
                                    })?;
                                    world.borrow_mut().create_joint(&B2JointDefEnum::WeldJoint(def));
                                }
                                B2jointType::EWheelJoint => {
                                    let def = map.next_value_seed(B2wheelJointDefContext {
                                        m_body_array: self.0.m_body_array.clone(),
                                    })?;
                                    world.borrow_mut().create_joint(&B2JointDefEnum::WheelJoint(def));
                                }
                            }
                            
                        }
                    }
                }
                
                Ok(())
            }
        }

        deserializer.deserialize_struct("B2body", Field::VARIANTS, B2jointDefinitionVisitor(self))
    }
}


pub(crate) struct B2jointListContext<D: UserDataType> {
    pub(crate) m_world: B2worldPtr<D>,
    pub(crate) m_body_array: Rc<RefCell<Vec<BodyPtr<D>>>>,
    pub(crate) m_all_joints: Rc<RefCell<Vec<B2jointPtr<D>>>>,
}

impl<'de, U: UserDataType> DeserializeSeed<'de> for B2jointListContext<U> {
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct B2jointListVisitor<D: UserDataType>(B2jointListContext<D>);

        impl<'de, U: UserDataType> Visitor<'de> for B2jointListVisitor<U> {
            type Value = ();

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct B2jointListContext")
            }

            fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
            where
                V: SeqAccess<'de>,
            {
                let context = B2jointDefinitionVisitorContext {
                    m_world: self.0.m_world.clone(),
                    m_body_array: self.0.m_body_array.clone(),
                    m_all_joints: self.0.m_all_joints.clone(),
                };
                while let Some(_elem) = seq.next_element_seed(context.clone())? {}
                Ok(())
            }
        }

        deserializer.deserialize_seq(B2jointListVisitor(self))
    }
}