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
use rfw_math::*;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{InstanceHandle2D, InstanceHandle3D};

pub trait HasMatrix {
    fn update(&mut self, t: Vec3, r: Quat, s: Vec3);
}

pub trait HasTranslation: HasMatrix {}
impl HasTranslation for InstanceHandle2D {}
impl HasTranslation for InstanceHandle3D {}

pub trait HasRotation: HasMatrix {}
impl HasRotation for InstanceHandle2D {}
impl HasRotation for InstanceHandle3D {}

pub trait HasScale: HasMatrix {}
impl HasScale for InstanceHandle2D {}
impl HasScale for InstanceHandle3D {}

impl HasMatrix for InstanceHandle2D {
    fn update(&mut self, t: Vec3, r: Quat, s: Vec3) {
        self.set_matrix(Mat4::from_scale_rotation_translation(s, r, t));
    }
}

impl HasMatrix for InstanceHandle3D {
    fn update(&mut self, t: Vec3, r: Quat, s: Vec3) {
        self.set_matrix(Mat4::from_scale_rotation_translation(s, r, t));
    }
}

#[derive(Debug)]
pub struct Transform<'a, T: HasMatrix> {
    pub(crate) translation: Vec3,
    pub(crate) rotation: Quat,
    pub(crate) scale: Vec3,
    pub(crate) handle: &'a mut T,
    pub(crate) changed: bool,
}

impl<T: HasMatrix> Transform<'_, T> {
    pub fn translate_x(&mut self, offset: f32) -> &mut Self
    where
        T: HasTranslation,
    {
        self.translation.x += offset;
        self.changed = true;
        self
    }

    pub fn translate_y(&mut self, offset: f32) -> &mut Self
    where
        T: HasTranslation,
    {
        self.translation.y += offset;
        self.changed = true;
        self
    }

    pub fn translate_z(&mut self, offset: f32) -> &mut Self
    where
        T: HasTranslation,
    {
        self.translation.z += offset;
        self.changed = true;
        self
    }

    pub fn translate<V: Into<[f32; 3]>>(&mut self, offset: V) -> &mut Self
    where
        T: HasTranslation,
    {
        let offset: [f32; 3] = offset.into();
        self.translation += Vec3::from(offset);
        self.changed = true;
        self
    }

    pub fn rotate_x(&mut self, radians: f32) -> &mut Self
    where
        T: HasRotation,
    {
        self.rotation *= Quat::from_rotation_x(radians);
        self.changed = true;
        self
    }

    pub fn rotate_y(&mut self, radians: f32) -> &mut Self
    where
        T: HasRotation,
    {
        self.rotation *= Quat::from_rotation_y(radians);
        self.changed = true;
        self
    }

    pub fn rotate_z(&mut self, radians: f32) -> &mut Self
    where
        T: HasRotation,
    {
        self.rotation *= Quat::from_rotation_z(radians);
        self.changed = true;
        self
    }

    pub fn rotate<V: Into<[f32; 3]>>(&mut self, degrees: V) -> &mut Self
    where
        T: HasRotation,
    {
        let degrees: [f32; 3] = degrees.into();
        self.rotation *= Quat::from_rotation_x(degrees[0].to_radians());
        self.rotation *= Quat::from_rotation_y(degrees[1].to_radians());
        self.rotation *= Quat::from_rotation_z(degrees[2].to_radians());
        self.changed = true;
        self
    }

    pub fn scale_x(&mut self, offset: f32) -> &mut Self
    where
        T: HasScale,
    {
        self.scale.x *= offset;
        self.changed = true;
        self
    }

    pub fn scale_y(&mut self, offset: f32) -> &mut Self
    where
        T: HasScale,
    {
        self.scale.y *= offset;
        self.changed = true;
        self
    }

    pub fn scale_z(&mut self, offset: f32) -> &mut Self
    where
        T: HasScale,
    {
        self.scale.z *= offset;
        self.changed = true;
        self
    }

    pub fn scale<V: Into<[f32; 3]>>(&mut self, offset: V) -> &mut Self
    where
        T: HasScale,
    {
        self.scale *= Vec3::from(offset.into());
        self.changed = true;
        self
    }

    pub fn get_translation(&self) -> Vec3
    where
        T: HasTranslation,
    {
        self.translation
    }

    pub fn get_rotation(&self) -> Quat
    where
        T: HasRotation,
    {
        self.rotation
    }

    pub fn get_scale(&self) -> Vec3
    where
        T: HasScale,
    {
        self.scale
    }

    pub fn set_translation(&mut self, translation: Vec3) -> &mut Self
    where
        T: HasTranslation,
    {
        self.translation = translation;
        self.changed = true;
        self
    }

    pub fn set_rotation(&mut self, rotation: Quat) -> &mut Self
    where
        T: HasRotation,
    {
        self.rotation = rotation;
        self.changed = true;
        self
    }

    pub fn set_scale(&mut self, scale: Vec3) -> &mut Self
    where
        T: HasScale,
    {
        self.scale = scale;
        self.changed = true;
        self
    }

    pub fn set_matrix(&mut self, matrix: Mat4) -> &mut Self
    where
        T: HasTranslation + HasRotation + HasScale,
    {
        let (s, r, t) = matrix.to_scale_rotation_translation();
        self.scale = s;
        self.rotation = r;
        self.translation = t;
        self.changed = true;
        self
    }
}

impl<T: HasMatrix> Drop for Transform<'_, T> {
    fn drop(&mut self) {
        if !self.changed {
            return;
        }

        self.handle
            .update(self.translation, self.rotation, self.scale);
    }
}