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
use std::time::Duration;

use super::config::Config;
use super::context::Context;
use super::Constructor;

/// Application setup structure.
/// Describes root node, title, pause behavior, etc.
pub struct Setup<Cfg>
where
    Cfg: Config,
{
    pub(super) title: String,
    pub(super) update_delay: Duration,
    pub(super) fullscreen: bool,
    pub(super) scale: u32,
    pub(super) render_target: Cfg::RenderTarget,
    pub(super) constructor: Constructor<Cfg::Root, Cfg>,
    #[cfg(target_arch = "wasm32")]
    pub(super) element_id: Option<&'static str>,
    pub(super) pause_on_focus_lost: bool,
    pub(super) input: Cfg::Input,
}

impl<Cfg> Setup<Cfg>
where
    Cfg: Config,
{
    /// Create new setup with given the `RenderTarget`, `Input` and `Node` constructor.
    /// Defaults to 30 frames per second update.
    pub fn new<F>(render_target: Cfg::RenderTarget, input: Cfg::Input, constructor: F) -> Self
    where
        F: 'static + FnOnce(&mut Context<Cfg>) -> Cfg::Root,
    {
        let title = String::new();
        let update_delay = Duration::from_secs_f64(1.0 / 30.0);
        let fullscreen = false;
        let scale = 1;
        let constructor = Box::new(constructor);
        Self {
            title,
            update_delay,
            fullscreen,
            scale,
            render_target,
            constructor,
            #[cfg(target_arch = "wasm32")]
            element_id: None,
            pause_on_focus_lost: true,
            input,
        }
    }

    /// Set application title.
    pub fn with_title<T: Into<String>>(self, title: T) -> Self {
        Self {
            title: title.into(),
            ..self
        }
    }

    /// Set display scale.
    pub fn with_scale(self, scale: u32) -> Self {
        Self { scale, ..self }
    }

    /// Set fullscreen option.
    pub fn with_fullscreen(self, fullscreen: bool) -> Self {
        Self { fullscreen, ..self }
    }

    /// Set update delay.
    pub fn with_update_delay(self, update_delay: Duration) -> Self {
        Self {
            update_delay,
            ..self
        }
    }

    #[cfg(target_arch = "wasm32")]
    /// Set target element id for canvas holding on wasm32 target.
    pub fn with_element_id(self, element_id: &'static str) -> Self {
        let element_id = Some(element_id);
        Self { element_id, ..self }
    }

    /// Set whether to apply pause on focus being lost.
    pub fn with_pause_on_focus_lost(self, pause_on_focus_lost: bool) -> Self {
        Self {
            pause_on_focus_lost,
            ..self
        }
    }
}