rust_droid/common/
relative_rect.rs1use super::rect::Rect;
4
5#[derive(Debug, Clone, Copy)]
7pub struct RelativeRect {
8 pub x: f32,
9 pub y: f32,
10 pub width: f32,
11 pub height: f32,
12}
13
14impl RelativeRect {
15 pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
20 debug_assert!((0.0..=1.0).contains(&x));
21 debug_assert!((0.0..=1.0).contains(&y));
22 debug_assert!((0.0..=1.0).contains(&width));
23 debug_assert!((0.0..=1.0).contains(&height));
24 Self {
25 x,
26 y,
27 width,
28 height,
29 }
30 }
31
32 pub fn to_absolute(&self, screen_width: u32, screen_height: u32) -> Rect {
34 Rect {
35 x: (self.x * screen_width as f32) as u32,
36 y: (self.y * screen_height as f32) as u32,
37 width: (self.width * screen_width as f32) as u32,
38 height: (self.height * screen_height as f32) as u32,
39 }
40 }
41}