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
use amethyst_core::cgmath::{InnerSpace, Quaternion, Vector3};
use amethyst_core::Transform;

use resources::{AnimationSampling, ApplyData, BlendMethod};
use util::SamplerPrimitive;

/// Channels that can be animated on `Transform`
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum TransformChannel {
    Translation,
    Rotation,
    Scale,
}

impl<'a> ApplyData<'a> for Transform {
    type ApplyData = ();
}

impl AnimationSampling for Transform {
    type Primitive = SamplerPrimitive<f32>;
    type Channel = TransformChannel;

    fn apply_sample(&mut self, channel: &Self::Channel, data: &SamplerPrimitive<f32>, _: &()) {
        use self::TransformChannel::*;
        use util::SamplerPrimitive::*;
        match (channel, *data) {
            (&Translation, Vec3(ref d)) => self.translation = Vector3::from(*d),
            (&Rotation, Vec4(ref d)) => self.rotation = Quaternion::from(*d).normalize(),
            (&Scale, Vec3(ref d)) => self.scale = Vector3::from(*d),
            _ => panic!("Attempt to apply invalid sample to Transform"),
        }
    }

    fn current_sample(&self, channel: &Self::Channel, _: &()) -> SamplerPrimitive<f32> {
        use self::TransformChannel::*;
        match channel {
            &Translation => SamplerPrimitive::Vec3(self.translation.into()),
            &Rotation => SamplerPrimitive::Vec4(self.rotation.into()),
            &Scale => SamplerPrimitive::Vec3(self.scale.into()),
        }
    }
    fn default_primitive(channel: &Self::Channel) -> Self::Primitive {
        use self::TransformChannel::*;
        match channel {
            &Translation => SamplerPrimitive::Vec3([0.; 3]),
            &Rotation => SamplerPrimitive::Vec4([0.; 4]),
            &Scale => SamplerPrimitive::Vec3([0.; 3]),
        }
    }

    fn blend_method(&self, _: &Self::Channel) -> Option<BlendMethod> {
        Some(BlendMethod::Linear)
    }
}