aboutsummaryrefslogtreecommitdiff
path: root/src/classes/transform.rs
blob: 7a24a5c414b8f86e55820ce9be03e66441566884 (plain)
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
use super::{gameobject::GameObject, pptr::PPtr};
use crate::object::{Value, parser::FromValue};
use anyhow::{Context, Result};
use glam::{Quat, Vec3};
use serde::Serialize;

#[derive(Debug, Serialize)]
pub struct Transform {
    pub father: PPtr<Transform>,
    pub children: Vec<PPtr<Transform>>,
    pub gameobject: PPtr<GameObject>,
    pub local_position: Vec3,
    pub local_rotation: Quat,
    pub local_scale: Vec3,
}

impl FromValue for Transform {
    fn from_value(v: Value) -> Result<Self> {
        let mut fields = v
            .clone()
            .as_class("RectTransform")
            .or(v.as_class("Transform"))?;
        Ok(Self {
            father: fields.field("m_Father")?,
            gameobject: fields
                .field("m_GameObject")
                .context("gameobject of transform")?,
            local_position: fields.field("m_LocalPosition")?,
            local_rotation: fields.field("m_LocalRotation")?,
            local_scale: fields.field("m_LocalScale")?,
            children: fields
                .remove("m_Children")
                .unwrap()
                .as_vector()
                .unwrap()
                .into_iter()
                .map(|e| PPtr::from_value(e).unwrap())
                .collect(),
        })
    }
}