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
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
use std::{collections::HashMap, fmt, rc::Rc};

use crate::engine::d2::{
    swf::{Library, MovieSprite},
    util::{Disposable, Value},
    Component, Entity,
};

use super::Symbol;

/// A convenient controller to play though multiple different movies. Designed for characters and
/// objects that have a separate Flump symbol for each of their animations, and need to switch
/// between them. The played movies will be added to a new child entity of the owner.

// TODO: should implement value for movie.
// Aka watch the movie sprite
#[derive(Default, Clone)]
pub struct MoviePlayer {
    pub inner: Component,
    /// The movie currently being shown.
    pub movie: Option<MovieSprite>, // Option<Value<MovieSprite>>,

    /// Whether the current movie is being looped.
    pub looping: bool,

    library: Library,
    root: Entity,

    oneshot_sprite: Option<MovieSprite>,
    looping_sprite: Option<MovieSprite>,

    decorator: Option<Rc<dyn Fn(MovieSprite)>>,
    cache: Option<HashMap<String, MovieSprite>>,
}

impl MoviePlayer {
    pub fn new(lib: Library) -> Self {
        Self {
            inner: Default::default(),
            movie: None,
            looping: false,
            library: lib,
            root: Entity::new(),
            looping_sprite: None,
            oneshot_sprite: None,
            decorator: None,
            cache: Some(HashMap::new()),
        }
    }

    /// Configures whether this MoviePlayer will keep a cache of all its MovieSprites, rather than
    /// creating a new instance for each play. This makes switching movies faster, at the expense of
    /// memory. By default, the cache is enabled. If this MoviePlayer plays lots of different movies,
    /// but doesn't match through them too often, consider disabling the cache.
    /// @returns This instance, for chaining.
    pub fn set_cache(&mut self, cache: bool) -> &Self {
        self.cache = if cache { Some(HashMap::new()) } else { None };

        self
    }

    /// Configures the callback used to decorate newly created MovieSprites, if any. This can be used
    /// to dress up avatars or other custom initialization.
    /// @returns This instance, for chaining.
    pub fn set_decorator(&mut self, decorator: Option<Rc<dyn Fn(MovieSprite)>>) -> &Self {
        self.decorator = decorator;

        self
    }

    /// Shows a movie that plays once. When it completes, the last looping movie is returned to. It
    /// is an error to call this without starting a loop() first.
    /// @param name The symbol name of the movie to play.
    /// @param restart If this movie is already being played, whether it will restart it from the
    ///   beginning.
    /// @returns This instance, for chaining.
    // restart :bool = true
    pub fn play(&mut self, name: String, restart: bool) -> &Self {
        assert!(
            self.looping_sprite.is_some(),
            "A loop must be started before the first call to play()"
        );

        let should_update = self
            .oneshot_sprite
            .as_ref()
            .map(|sprite| sprite.symbol.name().map(|x| x != name).unwrap_or_default())
            .unwrap_or_default();

        if restart || should_update {
            self.oneshot_sprite = self.play_from_cache(name);
        }

        self
    }

    /// Shows a movie that loops forever.
    /// @param name The symbol name of the movie to loop.
    /// @param restart If this movie is already being looped, whether it will restart it from the
    ///   beginning.
    /// @returns This instance, for chaining.
    // restart :bool = true
    pub fn play_with_loop(&mut self, name: String, restart: bool) -> &Self {
        let should_update = self
            .looping_sprite
            .as_ref()
            .map(|sprite| sprite.symbol.name().map(|x| x != name).unwrap_or_default())
            .unwrap_or_default();

        if restart || should_update {
            self.oneshot_sprite = None;
            self.looping_sprite = self.play_from_cache(name);
        }

        self
    }

    // override
    pub fn on_added(&self) {
        if let Some(owner) = self.inner.owner {
            owner.entity().add_child(self.root.clone(), true, None);
        }
    }

    // override
    pub fn on_removed(&mut self) {
        self.root.dispose();
        self.oneshot_sprite = None;
        self.looping_sprite = None;
        self.movie = None;
    }

    // override
    pub fn on_update(&mut self, dt: f32) {
        // If this update would end the oneshot movie, replace it with the looping movie
        if let Some(ref oneshot_sprite) = self.oneshot_sprite {
            if oneshot_sprite.position() + dt > oneshot_sprite.symbol.duration {
                self.oneshot_sprite = None;
                self.set_current(self.looping_sprite.clone());
            }
        }
    }

    fn play_from_cache(&mut self, name: String) -> Option<MovieSprite> {
        match self.cache {
            Some(ref mut cache) => {
                match cache.get_mut(&name) {
                    Some(sprite) => {
                        // Rewind it
                        sprite.set_position(0.0);

                        // set_current
                        self.root.add(sprite.inner.component.clone());
                        self.movie = Some(sprite.clone());

                        return Some(sprite.clone());
                    }
                    None => {
                        // Not in the cache, create the new entry

                        if let Some(ref sprite) = self.library.create_movie(name.clone(), true) {
                            if let Some(ref decorator) = self.decorator {
                                (decorator)(sprite.clone());
                            }

                            cache.insert(name, sprite.clone());
                            self.set_current(Some(sprite.clone()));
                            Some(sprite.clone())
                        } else {
                            None
                        }
                    }
                }
            }
            None => {
                // Caching disabled, create a new movie each time
                self.create_movie(name).map(|ref sprite| {
                    self.set_current(Some(sprite.clone()));
                    sprite.clone()
                })
            }
        }
    }

    fn create_movie(&self, name: String) -> Option<MovieSprite> {
        // let sprite = self.library.create_movie(name, true);
        // if let Some(ref decorator) = self.decorator {
        //     (decorator)(sprite);
        // }

        // sprite
        unimplemented!()
    }

    fn looping(&self) -> bool {
        self.oneshot_sprite.is_none() && self.looping_sprite.is_some()
    }

    fn set_current(&mut self, current: Option<MovieSprite>) {
        if let Some(ref sprite) = current {
            self.root.add(sprite.inner.component.clone());
        }

        self.movie = current;
    }
}

impl PartialEq for MoviePlayer {
    fn eq(&self, other: &Self) -> bool {
        self.root == other.root
    }
}

impl fmt::Debug for MoviePlayer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MoviePlayer")
            //  .field("x", &self.x)
            //  .field("y", &self.y)
            .finish()
    }
}

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