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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use once_cell::sync::OnceCell;
use std::{fmt, sync::Arc, sync::Mutex};

use super::{
    scene::{Scene2D, Transition},
    subsystem::*,
    util::Disposable,
    Component, Entity, EntityManager, System,
};

/// Manages a stack of scenes. Only the front-most scene receives game updates.

// TODO -> Major robustness cleanup and testing needed here

#[derive(Default)]
struct DirectorProps {
    inner: Component,

    /// The front-most scene
    top_scene: Option<Entity>,

    /// The complete list of scenes managed by this director, from back to front
    scenes: Vec<Entity>,

    /// The scenes that are partially occluded by a transparent or transitioning scene,
    /// from back to front. These scenes are not updated, but they're still drawn
    occluded_scenes: Vec<Entity>,

    /// The container for the current scene
    root: Entity,

    transitor: Option<Transitor>,

    /// The ideal width of the director's scenes. Used by some transitions
    width: f32, // = -1;

    /// The ideal height of the director's scenes. Used by some transitions
    height: f32, // = -1;
}

unsafe impl Send for DirectorProps {}

#[derive(Default)] // Clone, Debug
pub struct Director {
    props: Arc<Mutex<DirectorProps>>,
}

impl Director {
    fn new() -> Self {
        Self {
            props: Arc::new(Mutex::new(Default::default())),
            // inner: Component::default(),
            // top_scene: None,
            // scenes: Vec::new(),
            // occluded_scenes: Vec::new(),
            // root: Entity::new(),
            // height: 0.0,
            // width: 0.0,
            // transitor: None,
        }
    }

    /// Retrieves the singleton instance of `Settings`
    ///
    /// # Returns
    ///
    /// the instance of `Settings`. The
    ///  returned object is owned by internals and it should not be unreferenced
    ///  directly
    fn global() -> &'static Self {
        static DIRECTOR_INSTANCE: OnceCell<Director> = OnceCell::new();
        DIRECTOR_INSTANCE.get_or_init(Self::new)
    }

    /// Sets the ideal size of the scenes in this director. By default, the size is the full stage
    /// width and height. This size is used by some transitions, such as SlideTransition.
    pub fn set_size(width: f32, height: f32) {
        match Self::global().props.lock() {
            Ok(mut props) => {
                props.width = width;
                props.height = height;
            }
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }

    pub fn push_scene(scene: Entity, transition: Option<Transition>) {
        let director = Self::global();
        director.complete_transition();
        if let Some(old_top) = director.top_scene() {
            let old_top = old_top.clone();
            director.play_transition(
                old_top.clone(),
                scene,
                transition,
                Some(Arc::new(move || {
                    Self::hide(old_top);
                })),
            );
        } else {
            director.add(scene);
            director.invalidate_visibility();
        }
    }

    pub fn pop_scene(transition: Option<Transition>) {
        let director = Self::global();

        director.complete_transition();

        if let Some(old_top) = director.top_scene() {
            match director.props.lock() {
                Ok(mut props) => {
                    props.scenes.pop(); // Pop old_top
                }
                Err(_) => {
                    panic!("Context poisoned")
                }
            }

            if let Some(new_top) = director.top_scene() {
                director.play_transition(
                    old_top,
                    new_top,
                    transition,
                    Some(Arc::new(move || {
                        Self::hide_and_dispose(old_top);
                    })),
                );
            } else {
                Self::hide_and_dispose(old_top);
                director.invalidate_visibility();
            }
        }
    }

    /// Pops the scene stack until the given entity is the top scene, or adds the scene if the stack
    /// becomes empty while popping.
    pub fn unwind_to_scene(&self, scene: Entity, transition: Option<Transition>) {
        let director = Self::global();

        director.complete_transition();

        if let Some(old_top) = director.top_scene() {
            if old_top == scene {
                return; // We're already there
            }

            match director.props.lock() {
                Ok(mut props) => {
                    props.scenes.pop(); // Pop old_top
                    while props.scenes.len() > 0 && props.scenes[props.scenes.len() - 1] != scene {
                        if let Some(entity) = props.scenes.pop() {
                            entity.dispose(); // Don't emit a hide, just dispose them
                        }
                    }
                }
                Err(_) => {
                    panic!("Context poisoned")
                }
            }

            director.play_transition(
                old_top,
                scene,
                transition,
                Some(Arc::new(move || {
                    Self::hide_and_dispose(old_top);
                })),
            );
        } else {
            Self::push_scene(scene, transition);
        }
    }

    // override
    pub fn on_added(&self) {
        let director = Self::global();

        match director.props.lock() {
            Ok(props) => {
                if let Some(owner) = props.inner.owner {
                    owner.entity().add_child(props.root.clone(), true, None);
                }
            }
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }

    // override
    pub fn on_removed(&self) {
        let director = Self::global();

        director.complete_transition();

        match director.props.lock() {
            Ok(mut props) => {
                for scene in props.scenes.iter_mut() {
                    scene.dispose();
                }
                props.scenes = Vec::new();
                props.occluded_scenes = Vec::new();

                props.root.dispose();
            }
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }

    // override
    pub fn on_update(&self, dt: f32) {
        let director = Self::global();
        match director.props.lock() {
            Ok(props) => {
                if let Some(ref transitor) = props.transitor {
                    if transitor.update(dt) {
                        director.complete_transition();
                    }
                }
            }
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }

    fn top_scene(&self) -> Option<Entity> {
        match self.props.lock() {
            Ok(props) => props.scenes.last().map(|scene| scene.clone()),
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }

    /// Whether the director is currently transitioning between scenes.
    #[inline]
    fn transitioning(&self) -> bool {
        match self.props.lock() {
            Ok(props) => props.transitor.is_some(),
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }

    fn add(&self, scene: Entity) {
        match self.props.lock() {
            Ok(mut props) => {
                if let Some(ref old_top) = self.top_scene() {
                    props.root.remove_child(old_top);
                }

                if let Some(idx) = props.scenes.iter().position(|item| item == &scene) {
                    props.scenes.remove(idx);
                }
                props.scenes.push(scene.clone());
                props.root.add_child(scene, true, None);
            }
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }

    fn hide(scene: Entity) {
        if let Some(ref events) = EntityManager::<Scene2D>::get(&scene) {
            events.hidden.emit();
        }
    }

    fn hide_and_dispose(scene: Entity) {
        Self::hide(scene);
        if EntityManager::<Scene2D>::get(&scene).is_some() {
            scene.dispose();
        }
    }

    fn show(scene: &Entity) {
        if let Some(ref events) = EntityManager::<Scene2D>::get(scene) {
            events.shown.emit();
        }
    }

    fn invalidate_visibility(&self) {
        // Find the last index of an opaque scene, or 0
        match self.props.lock() {
            Ok(mut props) => {
                let mut idx = props.scenes.len();
                while idx > 0 {
                    idx -= 1;
                    if let Some(scene) = props.scenes.get(idx) {
                        if let Some(ref comp) = EntityManager::<Scene2D>::get(scene) {
                            if comp.opaque {
                                break;
                            }
                        }
                    } else {
                        break; // DV
                    }
                }

                // All visible scenes, excluding the top scene
                props.occluded_scenes = if props.scenes.len() > 0 {
                    let mut out = props.scenes.to_owned();
                    out.truncate(props.scenes.len() - 1);
                    out
                } else {
                    Vec::new()
                };

                // Notify the new top scene that it's being shown
                if let Some(scene) = self.top_scene() {
                    Self::show(&scene);
                }
            }
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }

    // Notes:
    // - Any method that modifies the scene stack should immediately call completeTransition.
    fn complete_transition(&self) {
        let should_invalidate = match self.props.lock() {
            Ok(mut props) => {
                if let Some(ref transitor) = props.transitor {
                    transitor.complete();
                    props.transitor = None;
                    true
                } else {
                    false
                }
            }
            Err(_) => {
                panic!("Context poisoned")
            }
        };

        if should_invalidate {
            self.invalidate_visibility();
        }
    }

    fn play_transition(
        &self,
        from: Entity,
        to: Entity,
        transition: Option<Transition>,
        on_complete: Option<Arc<dyn Fn()>>,
    ) {
        match self.props.lock() {
            Ok(mut props) => {
                self.complete_transition();
                self.add(to);

                if let Some(transition) = transition {
                    props.occluded_scenes.push(from);

                    let transitor = Transitor::new(from, to, transition, on_complete);
                    transitor.init();

                    props.transitor = Some(transitor);
                } else {
                    if let Some(func) = on_complete {
                        func();
                    }
                    self.invalidate_visibility();
                }
            }
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }

    pub fn width() -> f32 {
        match Self::global().props.lock() {
            Ok(props) => {
                if props.width < 0.0 {
                    System::stage().width() as f32
                } else {
                    props.width
                }
            }
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }

    pub fn height() -> f32 {
        match Self::global().props.lock() {
            Ok(props) => {
                if props.height < 0.0 {
                    System::stage().height() as f32
                } else {
                    props.height
                }
            }
            Err(_) => {
                panic!("Context poisoned")
            }
        }
    }
}

// impl AsRef<Component> for Director {
//     fn as_ref(&self) -> &Component {
//         match self.props.read() {
//             Ok(props) => &props.inner,
//             Err(_) => {
//                 panic!("Context poisoned")
//             }
//         }
//     }
// }

#[derive(Default)] // Clone,
pub struct Transitor {
    from: Entity,
    to: Entity,
    transition: Transition,

    on_complete: Option<Arc<dyn Fn()>>,
}

impl Transitor {
    pub fn new(
        from: Entity,
        to: Entity,
        transition: Transition,
        on_complete: Option<Arc<dyn Fn()>>,
    ) -> Self {
        Self {
            from,
            to,
            transition,
            on_complete,
        }
    }

    pub fn init(&self) {
        // self.transition.init(self.from.clone(), self.to.clone());
    }

    pub fn update(&self, dt: f32) -> bool {
        self.transition.update(dt)
    }

    pub fn complete(&self) {
        self.transition.complete();
        if let Some(ref on_complete) = self.on_complete {
            (on_complete)();
        }
    }
}

unsafe impl Send for Transitor {}

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