iced_graphics/
viewport.rs

1use crate::core::{Size, Transformation};
2
3/// A viewing region for displaying computer graphics.
4#[derive(Debug, Clone)]
5pub struct Viewport {
6    physical_size: Size<u32>,
7    logical_size: Size<f32>,
8    scale_factor: f64,
9    projection: Transformation,
10}
11
12impl Viewport {
13    /// Creates a new [`Viewport`] with the given logical dimensions and scale factor
14    pub fn with_logical_size(size: Size<f32>, scale_factor: f64) -> Viewport {
15        let physical_size = Size::new(
16            (size.width as f64 * scale_factor).ceil() as u32,
17            (size.height as f64 * scale_factor).ceil() as u32,
18        );
19        Viewport {
20            physical_size,
21            logical_size: size,
22            scale_factor,
23            projection: Transformation::orthographic(
24                physical_size.width,
25                physical_size.height,
26            ),
27        }
28    }
29
30    /// Creates a new [`Viewport`] with the given physical dimensions and scale
31    /// factor.
32    pub fn with_physical_size(size: Size<u32>, scale_factor: f64) -> Viewport {
33        Viewport {
34            physical_size: size,
35            logical_size: Size::new(
36                (size.width as f64 / scale_factor) as f32,
37                (size.height as f64 / scale_factor) as f32,
38            ),
39            scale_factor,
40            projection: Transformation::orthographic(size.width, size.height),
41        }
42    }
43
44    /// Returns the physical size of the [`Viewport`].
45    pub fn physical_size(&self) -> Size<u32> {
46        self.physical_size
47    }
48
49    /// Returns the physical width of the [`Viewport`].
50    pub fn physical_width(&self) -> u32 {
51        self.physical_size.width
52    }
53
54    /// Returns the physical height of the [`Viewport`].
55    pub fn physical_height(&self) -> u32 {
56        self.physical_size.height
57    }
58
59    /// Returns the logical size of the [`Viewport`].
60    pub fn logical_size(&self) -> Size<f32> {
61        self.logical_size
62    }
63
64    /// Returns the scale factor of the [`Viewport`].
65    pub fn scale_factor(&self) -> f64 {
66        self.scale_factor
67    }
68
69    /// Returns the projection transformation of the [`Viewport`].
70    pub fn projection(&self) -> Transformation {
71        self.projection
72    }
73}