fyrox_impl/scene/node/
container.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
//! A wrapper for node pool record that allows to define custom visit method to have full
//! control over instantiation process at deserialization.

use crate::{
    core::{
        pool::PayloadContainer,
        reflect::prelude::*,
        uuid::Uuid,
        visitor::{Visit, VisitError, VisitResult, Visitor},
    },
    engine::SerializationContext,
    scene::{
        self,
        camera::Camera,
        decal::Decal,
        dim2::{self, rectangle::Rectangle},
        light::{directional::DirectionalLight, point::PointLight, spot::SpotLight},
        mesh::Mesh,
        node::Node,
        particle_system::ParticleSystem,
        pivot::Pivot,
        sound::{listener::Listener, Sound},
        sprite::Sprite,
        terrain::Terrain,
    },
};

/// A wrapper for node pool record that allows to define custom visit method to have full
/// control over instantiation process at deserialization.
#[derive(Debug, Default, Reflect)]
pub struct NodeContainer(Option<Node>);

fn read_node(name: &str, visitor: &mut Visitor) -> Result<Node, VisitError> {
    let node = {
        // Handle legacy nodes.
        let mut kind_id = 0u8;
        if kind_id.visit("KindId", visitor).is_ok() {
            let mut node = match kind_id {
                0 => Node::new(Pivot::default()),
                1 => {
                    let mut region = visitor.enter_region(name)?;

                    let mut light_id = 0u32;
                    light_id.visit("KindId", &mut region)?;

                    let mut light_node = match light_id {
                        0 => Node::new(SpotLight::default()),
                        1 => Node::new(PointLight::default()),
                        2 => Node::new(DirectionalLight::default()),
                        _ => {
                            return Err(VisitError::User(format!(
                                "Invalid legacy light kind {}",
                                light_id
                            )))
                        }
                    };

                    light_node.visit("Data", &mut region)?;

                    return Ok(light_node);
                }
                2 => Node::new(Camera::default()),
                3 => Node::new(Mesh::default()),
                4 => Node::new(Sprite::default()),
                5 => Node::new(ParticleSystem::default()),
                6 => Node::new(Terrain::default()),
                7 => Node::new(Decal::default()),
                8 => Node::new(scene::rigidbody::RigidBody::default()),
                9 => Node::new(scene::collider::Collider::default()),
                10 => Node::new(scene::joint::Joint::default()),
                11 => Node::new(Rectangle::default()),
                12 => Node::new(dim2::rigidbody::RigidBody::default()),
                13 => Node::new(dim2::collider::Collider::default()),
                14 => Node::new(dim2::joint::Joint::default()),
                15 => Node::new(Sound::default()),
                16 => Node::new(Listener::default()),
                _ => {
                    return Err(VisitError::User(format!(
                        "Invalid legacy node kind {}",
                        kind_id
                    )))
                }
            };

            node.visit(name, visitor)?;

            node
        } else {
            // Latest version
            let mut region = visitor.enter_region(name)?;

            let mut id = Uuid::default();
            id.visit("TypeUuid", &mut region)?;

            let serialization_context = region
                .blackboard
                .get::<SerializationContext>()
                .expect("Visitor environment must contain serialization context!");

            let mut node = serialization_context
                .node_constructors
                .try_create(&id)
                .ok_or_else(|| VisitError::User(format!("Unknown node type uuid {}!", id)))?;

            node.visit("NodeData", &mut region)?;

            node
        }
    };

    Ok(node)
}

fn write_node(name: &str, node: &mut Node, visitor: &mut Visitor) -> VisitResult {
    let mut region = visitor.enter_region(name)?;

    let mut id = node.id();
    id.visit("TypeUuid", &mut region)?;

    node.visit("NodeData", &mut region)?;

    Ok(())
}

impl Visit for NodeContainer {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        let mut region = visitor.enter_region(name)?;

        let mut is_some = u8::from(self.is_some());
        is_some.visit("IsSome", &mut region)?;

        if is_some != 0 {
            if region.is_reading() {
                *self = NodeContainer(Some(read_node("Data", &mut region)?));
            } else {
                write_node("Data", self.0.as_mut().unwrap(), &mut region)?;
            }
        }

        Ok(())
    }
}

impl PayloadContainer for NodeContainer {
    type Element = Node;

    fn new_empty() -> Self {
        Self(None)
    }

    fn new(element: Self::Element) -> Self {
        Self(Some(element))
    }

    fn is_some(&self) -> bool {
        self.0.is_some()
    }

    fn as_ref(&self) -> Option<&Self::Element> {
        self.0.as_ref()
    }

    fn as_mut(&mut self) -> Option<&mut Self::Element> {
        self.0.as_mut()
    }

    fn replace(&mut self, element: Self::Element) -> Option<Self::Element> {
        self.0.replace(element)
    }

    fn take(&mut self) -> Option<Self::Element> {
        self.0.take()
    }
}