logo
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
use crate::engine::d2::{
    animation::EaseFunction, display::Sprite, Director, Entity, EntityManager,
};

use super::TweenTransition;

/// Slides the old scene off the stage, and the new scene into its place.
pub struct SlideTransition {
    pub inner: TweenTransition,
    direction: u32, // = LEFT;

    // Where the old scene should end up
    x: f32,
    y: f32,
}

impl SlideTransition {
    pub const UP: u32 = 0;
    pub const DOWN: u32 = 1;
    pub const LEFT: u32 = 2;
    pub const RIGHT: u32 = 3;

    pub fn new(duration: f32, ease: Option<EaseFunction>) -> Self {
        Self {
            inner: TweenTransition::new(duration, ease),
            direction: Self::LEFT,
            x: 0.0,
            y: 0.0,
        }
    }

    /// Slides the transition upwards.
    /// @returns This instance, for chaining.
    pub fn up_signal(&mut self) -> &Self {
        self.direction = Self::UP;

        self
    }

    /// Slides the transition downwards.
    /// @returns This instance, for chaining.
    pub fn down(&mut self) -> &Self {
        self.direction = Self::DOWN;

        self
    }

    /// Slides the transition to the left.
    /// @returns This instance, for chaining.
    pub fn left(&mut self) -> &Self {
        self.direction = Self::LEFT;

        self
    }

    /// Slides the transition to the right.
    /// @returns This instance, for chaining.
    pub fn right(&mut self) -> &Self {
        self.direction = Self::RIGHT;

        self
    }

    // override
    pub fn init(&mut self, from: Entity, to: Entity) {
        self.inner.init(from, to);

        match self.direction {
            Self::UP => {
                self.x = 0.0;
                self.y = -Director::height();
            }
            Self::DOWN => {
                self.x = 0.0;
                self.y = Director::height();
            }
            Self::LEFT => {
                self.x = -Director::width();
                self.y = 0.0;
            }
            Self::RIGHT => {
                self.x = Director::width();
                self.y = 0.0;
            }
            _ => {}
        }

        if let Some(ref mut sprite) = EntityManager::<Sprite>::get(&self.inner.inner.from) {
            sprite.set_xy(0.0, 0.0);
        } else {
            let mut sprite = Sprite::new();
            self.inner.inner.from.add(sprite.component.clone());
            sprite.set_xy(0.0, 0.0);
        }

        if let Some(ref mut sprite) = EntityManager::<Sprite>::get(&self.inner.inner.to) {
            sprite.set_xy(-self.x, -self.y);
        } else {
            let mut sprite = Sprite::new();
            self.inner.inner.to.add(sprite.component.clone());
            sprite.set_xy(-self.x, -self.y);
        }
    }

    // override
    pub fn update(&mut self, dt: f32) -> bool {
        let done = self.inner.update(dt);
        if let Some(ref mut sprite) = EntityManager::<Sprite>::get(&self.inner.inner.from) {
            sprite.set_xy(
                self.inner.interp(0.0, self.x),
                self.inner.interp(0.0, self.y),
            );
        }

        if let Some(ref mut sprite) = EntityManager::<Sprite>::get(&self.inner.inner.to) {
            sprite.set_xy(
                self.inner.interp(-self.x, 0.0),
                self.inner.interp(-self.y, 0.0),
            );
        }

        done
    }

    // override
    pub fn complete(&self) {
        if let Some(ref mut sprite) = EntityManager::<Sprite>::get(&self.inner.inner.from) {
            sprite.set_xy(0.0, 0.0);
        }
        if let Some(ref mut sprite) = EntityManager::<Sprite>::get(&self.inner.inner.to) {
            sprite.set_xy(0.0, 0.0);
        }
    }
}

impl AsRef<TweenTransition> for SlideTransition {
    fn as_ref(&self) -> &TweenTransition {
        &self.inner
    }
}