raui_app/app/
immediate.rs

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
use crate::{app::SharedApp, interactions::AppInteractionsEngine, Vertex};
use glutin::{event::Event, window::Window};
use raui_core::{
    application::Application,
    interactive::default_interactions_engine::DefaultInteractionsEngine,
    make_widget,
    tester::{AppCycleFrameRunner, AppCycleTester},
    widget::{component::containers::content_box::content_box, utils::Color},
};
use raui_immediate::{make_widgets, ImmediateContext};
use spitfire_fontdue::TextRenderer;
use spitfire_glow::{
    app::{App, AppConfig, AppState},
    graphics::Graphics,
};

#[derive(Default)]
pub struct ImmediateApp {
    shared: SharedApp,
}

impl ImmediateApp {
    pub fn simple(title: impl ToString, callback: impl FnMut() + 'static) {
        App::<Vertex>::new(AppConfig::default().title(title)).run(Self::default().update(callback));
    }

    pub fn simple_fullscreen(title: impl ToString, callback: impl FnMut() + 'static) {
        App::<Vertex>::new(AppConfig::default().title(title).fullscreen(true))
            .run(Self::default().update(callback));
    }

    pub fn test_frame<F: FnMut()>(f: F) -> ImmediateAppCycleFrameRunner<F> {
        ImmediateAppCycleFrameRunner(f)
    }

    pub fn update(mut self, callback: impl FnMut() + 'static) -> Self {
        let mut callback = Box::new(callback);
        let context = ImmediateContext::default();
        self.shared.on_update = Some(Box::new(move |application| {
            raui_immediate::reset();
            let widgets = make_widgets(&context, || {
                callback();
            });
            application.apply(make_widget!(content_box).listed_slots(widgets));
        }));
        self
    }

    pub fn redraw(
        mut self,
        f: impl FnMut(f32, &mut Graphics<Vertex>, &mut TextRenderer<Color>) + 'static,
    ) -> Self {
        self.shared.on_redraw = Some(Box::new(f));
        self
    }

    pub fn event(
        mut self,
        f: impl FnMut(&mut Application, Event<()>, &mut Window, &mut DefaultInteractionsEngine) -> bool
            + 'static,
    ) -> Self {
        self.shared.on_event = Some(Box::new(f));
        self
    }

    pub fn setup(mut self, mut f: impl FnMut(&mut Application)) -> Self {
        f(&mut self.shared.application);
        self
    }

    pub fn setup_interactions(mut self, mut f: impl FnMut(&mut AppInteractionsEngine)) -> Self {
        f(&mut self.shared.interactions);
        self
    }
}

impl AppState<Vertex> for ImmediateApp {
    fn on_init(&mut self, graphics: &mut Graphics<Vertex>) {
        self.shared.init(graphics);
    }

    fn on_redraw(&mut self, graphics: &mut Graphics<Vertex>) {
        self.shared.redraw(graphics);
    }

    fn on_event(&mut self, event: Event<()>, window: &mut Window) -> bool {
        self.shared.event(event, window)
    }
}

pub struct ImmediateAppCycleFrameRunner<F: FnMut()>(F);

impl<F: FnMut()> AppCycleFrameRunner<ImmediateContext> for ImmediateAppCycleFrameRunner<F> {
    fn run_frame(mut self, tester: &mut AppCycleTester<ImmediateContext>) {
        raui_immediate::reset();
        let widgets = make_widgets(&tester.user_data, || {
            (self.0)();
        });
        tester
            .application
            .apply(make_widget!(content_box).listed_slots(widgets));
    }
}