Skip to main content

iced_graphics/
viewport.rs

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