fj_viewer/screen.rs
1//! Types that describe aspects of the screen
2
3use std::sync::Arc;
4
5use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
6
7/// Needs to be implemented by types that can serve as a screen to render to
8pub trait Screen {
9 /// The window
10 type Window: HasDisplayHandle + HasWindowHandle + Send + Sync + 'static;
11
12 /// Access the size of the screen
13 fn size(&self) -> ScreenSize;
14
15 /// Access the window
16 fn window(&self) -> Arc<Self::Window>;
17}
18
19/// Cursor position in normalized coordinates (-1 to +1)
20///
21/// The center of the screen is at (0, 0). The aspect ratio is taken into
22/// account.
23#[derive(Clone, Copy, Debug)]
24pub struct NormalizedScreenPosition {
25 /// The x coordinate of the position [-1, 1]
26 pub x: f64,
27
28 /// The y coordinate of the position [-1, 1]
29 pub y: f64,
30}
31
32/// The size of the screen
33#[derive(Clone, Copy, Debug)]
34pub struct ScreenSize {
35 /// The width of the screen
36 pub width: u32,
37
38 /// The height of the screen
39 pub height: u32,
40}
41
42impl ScreenSize {
43 /// Convert size to `f64`
44 pub fn as_f64(&self) -> [f64; 2] {
45 [self.width, self.height].map(Into::into)
46 }
47}