gooey/interface/controller/
size.rs

1use derive_more::{From, TryInto};
2
3use crate::math::Normalized;
4use crate::math::num_traits::One;
5
6/// Absolute or relative width and height
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub struct Size {
9  pub width  : Unsigned,
10  pub height : Unsigned
11}
12
13#[derive(Clone, Copy, Debug, Eq, PartialEq, From, TryInto)]
14pub enum Unsigned {
15  Absolute (u32),
16  Relative (Normalized <f32>)
17}
18
19impl Size {
20  pub fn default_absolute() -> Self {
21    Size {
22      width:  0.into(),
23      height: 0.into()
24    }
25  }
26  pub fn default_relative() -> Self {
27    Size {
28      width:  Normalized::<f32>::zero().into(),
29      height: Normalized::<f32>::zero().into()
30    }
31  }
32  /// Relative size with 100% width and height
33  pub fn fill() -> Self {
34    Size {
35      width:  Unsigned::fill(),
36      height: Unsigned::fill()
37    }
38  }
39}
40
41impl Unsigned {
42  pub fn fill() -> Self {
43    Normalized::<f32>::one().into()
44  }
45  pub fn half (self) -> Self {
46    match self {
47      Unsigned::Absolute (x) => Unsigned::Absolute (x / 2),
48      Unsigned::Relative (x) => Unsigned::Relative (x * Normalized::clamp (0.5))
49    }
50  }
51}