iced_graphics/
viewport.rs
1use crate::core::{Size, Transformation};
2
3#[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 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 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 pub fn physical_size(&self) -> Size<u32> {
46 self.physical_size
47 }
48
49 pub fn physical_width(&self) -> u32 {
51 self.physical_size.width
52 }
53
54 pub fn physical_height(&self) -> u32 {
56 self.physical_size.height
57 }
58
59 pub fn logical_size(&self) -> Size<f32> {
61 self.logical_size
62 }
63
64 pub fn scale_factor(&self) -> f64 {
66 self.scale_factor
67 }
68
69 pub fn projection(&self) -> Transformation {
71 self.projection
72 }
73}