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
use bevy_math::Mat4;
use bevy_property::Properties;
use std::fmt;

#[derive(Debug, PartialEq, Clone, Copy, Properties)]
pub struct Transform {
    pub value: Mat4,
    pub sync: bool, // NOTE: this is hopefully a temporary measure to allow setting the transform directly.
                    // ideally setting the transform automatically propagates back to position / translation / rotation,
                    // but right now they are always considered the source of truth
}

impl Transform {
    #[inline(always)]
    pub fn identity() -> Self {
        Transform {
            value: Mat4::identity(),
            sync: true,
        }
    }

    #[inline(always)]
    pub fn new(value: Mat4) -> Self {
        Transform { value, sync: true }
    }

    /// This creates a new `LocalToWorld` transform with the `sync` field set to `false`.
    /// While `sync` is false, position, rotation, and scale components will not be synced to the transform.
    /// This is helpful if you want to manually set the transform to a value (ex: Mat4::face_toward)  
    #[inline(always)]
    pub fn new_sync_disabled(value: Mat4) -> Self {
        Transform { value, sync: false }
    }
}

impl Default for Transform {
    fn default() -> Self {
        Self::identity()
    }
}

impl fmt::Display for Transform {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}