sketch_2d/
screen.rs

1/*
2 * Created on Mon Jul 03 2023
3 *
4 * Copyright (c) storycraft. Licensed under the MIT Licence.
5 */
6
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub struct ScreenRect {
9    pub x: u32,
10    pub y: u32,
11
12    pub width: u32,
13    pub height: u32,
14
15    pub scale_factor: f32,
16}
17
18impl ScreenRect {
19    pub const ZERO: ScreenRect = ScreenRect::new(0, 0, 0, 0, 1.0);
20
21    pub const fn new(x: u32, y: u32, width: u32, height: u32, scale_factor: f32) -> Self {
22        Self {
23            x,
24            y,
25            width,
26            height,
27            scale_factor,
28        }
29    }
30
31    pub const fn pos(&self) -> (u32, u32) {
32        (self.x, self.y)
33    }
34
35    pub const fn size(&self) -> (u32, u32) {
36        (self.width, self.width)
37    }
38
39    pub const fn is_none(&self) -> bool {
40        self.width == 0 && self.height == 0
41    }
42
43    pub fn logical_width(&self) -> f32 {
44        self.width as f32 / self.scale_factor
45    }
46
47    pub fn logical_height(&self) -> f32 {
48        self.height as f32 / self.scale_factor
49    }
50
51    pub fn logical_size(&self) -> (f32, f32) {
52        (self.logical_width(), self.logical_height())
53    }
54
55    pub fn to_render_pos(&self, logical_x: f32, logical_y: f32) -> (f32, f32) {
56        (
57            ((logical_x - self.x as f32) / self.logical_width()) * 2.0 - 1.0,
58            ((logical_y - self.y as f32) / self.logical_height()) * 2.0 - 1.0,
59        )
60    }
61}