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 physical dimensions and scale
14    /// factor.
15    pub fn with_physical_size(size: Size<u32>, scale_factor: f64) -> Viewport {
16        Viewport {
17            physical_size: size,
18            logical_size: Size::new(
19                (size.width as f64 / scale_factor) as f32,
20                (size.height as f64 / scale_factor) as f32,
21            ),
22            scale_factor,
23            projection: Transformation::orthographic(size.width, size.height),
24        }
25    }
26
27    /// Returns the physical size of the [`Viewport`].
28    pub fn physical_size(&self) -> Size<u32> {
29        self.physical_size
30    }
31
32    /// Returns the physical width of the [`Viewport`].
33    pub fn physical_width(&self) -> u32 {
34        self.physical_size.width
35    }
36
37    /// Returns the physical height of the [`Viewport`].
38    pub fn physical_height(&self) -> u32 {
39        self.physical_size.height
40    }
41
42    /// Returns the logical size of the [`Viewport`].
43    pub fn logical_size(&self) -> Size<f32> {
44        self.logical_size
45    }
46
47    /// Returns the scale factor of the [`Viewport`].
48    pub fn scale_factor(&self) -> f64 {
49        self.scale_factor
50    }
51
52    /// Returns the projection transformation of the [`Viewport`].
53    pub fn projection(&self) -> Transformation {
54        self.projection
55    }
56}