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
use log::error;
use minterpolate::InterpolationPrimitive;
use serde::{Deserialize, Serialize};

use amethyst_assets::Handle;
use amethyst_rendy::sprite::{SpriteRender, SpriteSheet};

use crate::{AnimationSampling, ApplyData, BlendMethod};

/// Sampler primitive for SpriteRender animations
/// Note that sprites can only ever be animated with `Step`, or a panic will occur.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum SpriteRenderPrimitive {
    /// A spritesheet id
    #[serde(skip)]
    SpriteSheet(Handle<SpriteSheet>),
    /// An index into a spritesheet
    SpriteIndex(usize),
}

impl InterpolationPrimitive for SpriteRenderPrimitive {
    fn add(&self, _: &Self) -> Self {
        panic!("Cannot add SpriteRenderPrimitive")
    }

    fn sub(&self, _: &Self) -> Self {
        panic!("Cannot sub SpriteRenderPrimitive")
    }

    fn mul(&self, _: f32) -> Self {
        panic!("Cannot mul SpriteRenderPrimitive")
    }

    fn dot(&self, _: &Self) -> f32 {
        panic!("Cannot dot SpriteRenderPrimitive")
    }

    fn magnitude2(&self) -> f32 {
        panic!("Cannot magnitude2 SpriteRenderPrimitive")
    }

    fn magnitude(&self) -> f32 {
        panic!("Cannot magnitude SpriteRenderPrimitive")
    }

    fn normalize(&self) -> Self {
        panic!("Cannot normalize SpriteRenderPrimitive")
    }
}

/// Channels that are animatable on `SpriteRender`
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum SpriteRenderChannel {
    /// Selecting a spritesheet dynamically
    SpriteSheet,
    /// Selecting a sprite index dynamically
    SpriteIndex,
}

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

impl AnimationSampling for SpriteRender {
    type Primitive = SpriteRenderPrimitive;
    type Channel = SpriteRenderChannel;

    fn apply_sample(&mut self, channel: &Self::Channel, data: &Self::Primitive, _: &()) {
        use self::{SpriteRenderChannel as Channel, SpriteRenderPrimitive as Primitive};
        match (channel, data) {
            (Channel::SpriteSheet, Primitive::SpriteSheet(handle)) => {
                self.sprite_sheet = handle.clone();
            }
            (Channel::SpriteIndex, Primitive::SpriteIndex(index)) => {
                self.sprite_number = *index;
            }

            // Error cases
            (Channel::SpriteSheet, Primitive::SpriteIndex(_)) => {
                let message = "The `SpriteSheet` render channel must be used with \
                               `SpriteRenderPrimitive::SpriteSheet`"
                    .to_string();
                error!("{}", message);
                panic!("{}", message);
            }
            (Channel::SpriteIndex, Primitive::SpriteSheet(_)) => {
                let message = "The `SpriteIndex` render channel must be used with \
                               `SpriteRenderPrimitive::SpriteIndex`"
                    .to_string();
                error!("{}", message);
                panic!("{}", message);
            }
        }
    }

    fn current_sample(&self, channel: &Self::Channel, _: &()) -> Self::Primitive {
        use self::{SpriteRenderChannel as Channel, SpriteRenderPrimitive as Primitive};

        match channel {
            Channel::SpriteSheet => Primitive::SpriteSheet(self.sprite_sheet.clone()),
            Channel::SpriteIndex => Primitive::SpriteIndex(self.sprite_number),
        }
    }

    fn default_primitive(_: &Self::Channel) -> Self::Primitive {
        panic!("Blending is not applicable to SpriteRender animation")
    }

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