gloss_renderer/plugin_manager/
runner.rs

1use std::time::Duration;
2
3use crate::{viewer::Runner, viewer_dummy::RunnerDummy, viewer_headless::RunnerHeadless};
4
5use gloss_utils::abi_stable_aliases::std_types::RDuration;
6#[cfg(not(target_arch = "wasm32"))]
7use gloss_utils::abi_stable_aliases::StableAbi;
8
9//The runner in the viewer contains event loop and other things that cannot
10// cross the ffi barrier. So we make a new one that is slimmer
11#[repr(C)]
12#[cfg_attr(not(target_arch = "wasm32"), derive(StableAbi))]
13pub struct RunnerState {
14    pub is_running: bool,
15    pub do_render: bool,
16    pub request_redraw: bool,
17    first_time: bool,
18    dt: RDuration, /* delta time since we finished last frame, we store it directly here instead of constantly querying it because different
19                    * systems might then get different dt depending on how long they take to run. This dt is set once when doing
20                    * viewer.start_frame() */
21}
22//TODO use a tryinto and tryfrom functions
23impl RunnerState {
24    pub fn from(runner: &Runner) -> Self {
25        Self {
26            is_running: runner.is_running,
27            do_render: runner.do_render,
28            request_redraw: false,
29            first_time: runner.first_time,
30            dt: RDuration::from(runner.dt()),
31        }
32    }
33    pub fn from_headless(runner: &RunnerHeadless) -> Self {
34        Self {
35            is_running: runner.is_running,
36            do_render: runner.do_render,
37            request_redraw: false,
38            first_time: runner.first_time,
39            dt: RDuration::from(runner.dt()),
40        }
41    }
42    pub fn from_dummy(runner: &RunnerDummy) -> Self {
43        Self {
44            is_running: runner.is_running,
45            do_render: false, //ViewerDummy cannot render
46            request_redraw: false,
47            first_time: runner.first_time,
48            dt: RDuration::from_secs(0),
49        }
50    }
51    //onyl some of the fields are actually going to be modified and are worth
52    // storing back into the runner, at least the pub ones shound be set back into
53    // runner
54    pub fn to(&self, runner: &mut Runner) {
55        runner.is_running = self.is_running;
56        runner.do_render = self.do_render;
57    }
58    pub fn to_headless(&self, runner: &mut RunnerHeadless) {
59        runner.is_running = self.is_running;
60        runner.do_render = self.do_render;
61    }
62    pub fn to_dummy(&self, runner: &mut RunnerDummy) {
63        runner.is_running = self.is_running;
64    }
65    pub fn request_redraw(&mut self) {
66        self.request_redraw = true;
67    }
68    pub fn dt(&self) -> Duration {
69        Duration::from(self.dt)
70    }
71    pub fn is_first_time(&self) -> bool {
72        self.first_time
73    }
74}