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
//! Handles loading the global resources and playing an intro animation
//!
//! ## Overview
//!
//! This module contains `LoadingScreen` which will perform multi-threaded resource loading while rendering a loading animation.
//!
//! ## Whats happening
//!
//! - Discord RPC is set
//! - Resources are loaded
//! - Animation is rendered

use poll_promise::Promise;
use raylib::prelude::*;

use crate::{
    discord::{DiscordChannel, DiscordRpcSignal},
    global_resource_package::GlobalResources,
    project_constants::ProjectConstants,
};

pub struct LoadingScreen {
    pub resources: Option<GlobalResources>,
    has_updated_discord_status: bool,
}

impl LoadingScreen {
    /// Construct a new `LoadingScreen`
    pub fn new() -> Self {
        Self {
            resources: None,
            has_updated_discord_status: false,
        }
    }

    pub async fn render(
        &mut self,
        raylib: &mut RaylibHandle,
        rl_thread: &RaylibThread,
        discord: &DiscordChannel,
        constants: &ProjectConstants,
    ) -> bool {
        // Handle updating the Discord status
        if !self.has_updated_discord_status {
            discord
                .send(DiscordRpcSignal::ChangeDetails {
                    details: constants
                        .discord
                        .strings
                        .get("details.loading")
                        .unwrap()
                        .to_owned(),
                    party_status: None,
                })
                .await
                .unwrap();
            self.has_updated_discord_status = true;
        }

        // Begin loading resources if we haven't already
        if let None = self.resources {
            self.resources = Some(GlobalResources::load(raylib, rl_thread).await);
        }

        // Draw some graphics
        let mut d = raylib.begin_drawing(&rl_thread);
        d.clear_background(raylib::color::Color::BLACK);


        true
    }
}