static -> absolute, fraction -> relative

This commit is contained in:
griffi-gh 2024-03-22 18:39:18 +01:00
parent cf7919d041
commit a0dc882799
9 changed files with 59 additions and 59 deletions

View file

@ -44,7 +44,7 @@ fn main() {
gap: 5., gap: 5.,
padding: Sides::all(5.), padding: Sides::all(5.),
align: (Alignment::Center, Alignment::Begin).into(), align: (Alignment::Center, Alignment::Begin).into(),
size: (Size::Fraction(1.), Size::Fraction(1.)).into(), size: (Size::Relative(1.), Size::Relative(1.)).into(),
children: ElementList(vec![ children: ElementList(vec![
Box::new(ProgressBar { Box::new(ProgressBar {
value: 0.5, value: 0.5,
@ -58,7 +58,7 @@ fn main() {
gap: 5., gap: 5.,
padding: Sides::all(5.), padding: Sides::all(5.),
align: (Alignment::Center, Alignment::End).into(), align: (Alignment::Center, Alignment::End).into(),
size: (Size::Fraction(1.), Size::Fraction(1.)).into(), size: (Size::Relative(1.), Size::Relative(1.)).into(),
children: ElementList(vec![ children: ElementList(vec![
Box::new(ProgressBar { Box::new(ProgressBar {
value: z, value: z,
@ -66,18 +66,18 @@ fn main() {
..Default::default() ..Default::default()
}), }),
Box::new(Container { Box::new(Container {
size: (Size::Fraction(1.), Size::Auto).into(), size: (Size::Relative(1.), Size::Auto).into(),
align: (Alignment::End, Alignment::Center).into(), align: (Alignment::End, Alignment::Center).into(),
padding: Sides::all(5.), padding: Sides::all(5.),
gap: 10., gap: 10.,
children: ElementList(vec![ children: ElementList(vec![
Box::new(FillRect { Box::new(FillRect {
size: (Size::Fraction(0.5), Size::Static(30.)).into(), size: (Size::Relative(0.5), Size::Absolute(30.)).into(),
background: vec4(0.75, 0., 0., 1.).into(), background: vec4(0.75, 0., 0., 1.).into(),
..Default::default() ..Default::default()
}), }),
Box::new(FillRect { Box::new(FillRect {
size: (Size::Fraction(z / 2. + 0.5), Size::Static(30.)).into(), size: (Size::Relative(z / 2. + 0.5), Size::Absolute(30.)).into(),
background: Corners::left_right( background: Corners::left_right(
vec4(1., 0., 0., 1.), vec4(1., 0., 0., 1.),
vec4(0., 1., 0., 1.) vec4(0., 1., 0., 1.)
@ -88,7 +88,7 @@ fn main() {
..Default::default() ..Default::default()
}), }),
Box::new(FillRect { Box::new(FillRect {
size: (Size::Fraction(z / 2. + 0.5), Size::Static(30.)).into(), size: (Size::Relative(z / 2. + 0.5), Size::Absolute(30.)).into(),
background: vec4(0., 0.75, 0., 1.).into(), background: vec4(0., 0.75, 0., 1.).into(),
..Default::default() ..Default::default()
}), }),
@ -101,7 +101,7 @@ fn main() {
let mut x: Vec<Box<dyn UiElement>> = vec![]; let mut x: Vec<Box<dyn UiElement>> = vec![];
for i in 0..10 { for i in 0..10 {
x.push(Box::new(FillRect { x.push(Box::new(FillRect {
size: (Size::Static(50.), Size::Static(50.)).into(), size: (Size::Absolute(50.), Size::Absolute(50.)).into(),
background: if i == 1 { background: if i == 1 {
vec4(0.75, 0.75, 0.75, 0.75).into() vec4(0.75, 0.75, 0.75, 0.75).into()
} else { } else {
@ -130,7 +130,7 @@ fn main() {
}, },
children: ElementList(vec![ children: ElementList(vec![
Box::new(FillRect { Box::new(FillRect {
size: (Size::Static(50.), Size::Static(50.)).into(), size: (Size::Absolute(50.), Size::Absolute(50.)).into(),
background: vec4(1., 1., 1., 0.75).into(), background: vec4(1., 1., 1., 0.75).into(),
..Default::default() ..Default::default()
}), }),

View file

@ -42,11 +42,11 @@ fn main() {
hui.add(Container { hui.add(Container {
gap: 10., gap: 10.,
align: Alignment::Center.into(), align: Alignment::Center.into(),
size: (Size::Fraction(1.), Size::Fraction(1.)).into(), size: (Size::Relative(1.), Size::Relative(1.)).into(),
children: ElementList(vec![ children: ElementList(vec![
Box::new(Container { Box::new(Container {
align: Alignment::Center.into(), align: Alignment::Center.into(),
size: (Size::Fraction(0.5), Size::Fraction(0.5)).into(), size: (Size::Relative(0.5), Size::Relative(0.5)).into(),
background: vec4(1., 0., 0., 1.).into(), background: vec4(1., 0., 0., 1.).into(),
corner_radius: Corners { corner_radius: Corners {
top_left: 10., top_left: 10.,
@ -86,7 +86,7 @@ fn main() {
direction: Direction::Horizontal, direction: Direction::Horizontal,
children: ElementList(vec![ children: ElementList(vec![
Box::new(Container { Box::new(Container {
size: (Size::Static(100.), Size::Static(100.)).into(), size: (Size::Absolute(100.), Size::Absolute(100.)).into(),
background: Corners::left_right( background: Corners::left_right(
vec4(1., 0., 0., 1.), vec4(1., 0., 0., 1.),
vec4(0., 1., 0., 1.) vec4(0., 1., 0., 1.)
@ -95,7 +95,7 @@ fn main() {
..Default::default() ..Default::default()
}), }),
Box::new(Container { Box::new(Container {
size: (Size::Static(100.), Size::Static(100.)).into(), size: (Size::Absolute(100.), Size::Absolute(100.)).into(),
background: Corners::left_right( background: Corners::left_right(
vec4(1., 0., 0., 1.), vec4(1., 0., 0., 1.),
vec4(0., 1., 0., 1.) vec4(0., 1., 0., 1.)
@ -104,7 +104,7 @@ fn main() {
..Default::default() ..Default::default()
}), }),
Box::new(Container { Box::new(Container {
size: (Size::Static(100.), Size::Static(100.)).into(), size: (Size::Absolute(100.), Size::Absolute(100.)).into(),
background: Corners::left_right( background: Corners::left_right(
vec4(1., 0., 0., 1.), vec4(1., 0., 0., 1.),
vec4(0., 1., 0., 1.) vec4(0., 1., 0., 1.)
@ -113,7 +113,7 @@ fn main() {
..Default::default() ..Default::default()
}), }),
Box::new(Container { Box::new(Container {
size: (Size::Static(100.), Size::Static(100.)).into(), size: (Size::Absolute(100.), Size::Absolute(100.)).into(),
background: Corners::left_right( background: Corners::left_right(
vec4(1., 0., 0., 1.), vec4(1., 0., 0., 1.),
vec4(0., 1., 0., 1.) vec4(0., 1., 0., 1.)

View file

@ -48,7 +48,7 @@ fn main() {
hui.begin(); hui.begin();
hui.add(Container { hui.add(Container {
size: (Size::Fraction(1.), Size::Fraction(1.)).into(), size: (Size::Relative(1.), Size::Relative(1.)).into(),
background: vec4(0.1, 0.1, 0.1, 1.).into(), background: vec4(0.1, 0.1, 0.1, 1.).into(),
children: elements(|elem| { children: elements(|elem| {
elem.push(Box::new(Text { elem.push(Box::new(Text {
@ -72,12 +72,12 @@ fn main() {
})); }));
} }
elem.push(Box::new(FillRect { elem.push(Box::new(FillRect {
size: (Size::Fraction(1.), Size::Static(10.)).into(), size: (Size::Relative(1.), Size::Absolute(10.)).into(),
background: vec4(0., 0., 1., 1.).into(), background: vec4(0., 0., 1., 1.).into(),
..Default::default() ..Default::default()
})); }));
elem.push(Box::new(FillRect { elem.push(Box::new(FillRect {
size: (Size::Fraction(1.), Size::Static(10.)).into(), size: (Size::Relative(1.), Size::Absolute(10.)).into(),
background: vec4(1., 1., 0., 1.).into(), background: vec4(1., 1., 0., 1.).into(),
..Default::default() ..Default::default()
})); }));
@ -89,12 +89,12 @@ fn main() {
})); }));
if instant.elapsed().as_secs() & 1 != 0 { if instant.elapsed().as_secs() & 1 != 0 {
elem.push(Box::new(FillRect { elem.push(Box::new(FillRect {
size: (Size::Fraction(1.), Size::Static(10.)).into(), size: (Size::Relative(1.), Size::Absolute(10.)).into(),
background: vec4(1., 0., 0., 1.).into(), background: vec4(1., 0., 0., 1.).into(),
..Default::default() ..Default::default()
})); }));
elem.push(Box::new(FillRect { elem.push(Box::new(FillRect {
size: (Size::Fraction(1.), Size::Static(10.)).into(), size: (Size::Relative(1.), Size::Absolute(10.)).into(),
background: vec4(0., 0., 0., 1.).into(), background: vec4(0., 0., 0., 1.).into(),
..Default::default() ..Default::default()
})); }));

View file

@ -116,13 +116,13 @@ impl Container {
pub fn measure_max_inner_size(&self, layout: &LayoutInfo) -> Vec2 { pub fn measure_max_inner_size(&self, layout: &LayoutInfo) -> Vec2 {
let outer_size_x = match self.size.width { let outer_size_x = match self.size.width {
Size::Auto => layout.max_size.x, Size::Auto => layout.max_size.x,
Size::Fraction(p) => layout.max_size.x * p, Size::Relative(p) => layout.max_size.x * p,
Size::Static(p) => p, Size::Absolute(p) => p,
}; };
let outer_size_y = match self.size.height { let outer_size_y = match self.size.height {
Size::Auto => layout.max_size.y, Size::Auto => layout.max_size.y,
Size::Fraction(p) => layout.max_size.y * p, Size::Relative(p) => layout.max_size.y * p,
Size::Static(p) => p, Size::Absolute(p) => p,
}; };
vec2( vec2(
outer_size_x - (self.padding.left + self.padding.right), outer_size_x - (self.padding.left + self.padding.right),
@ -148,13 +148,13 @@ impl UiElement for Container {
let max_line_pri = match self.direction { let max_line_pri = match self.direction {
Direction::Horizontal => match self.size.width { Direction::Horizontal => match self.size.width {
Size::Auto => ctx.layout.max_size.x, Size::Auto => ctx.layout.max_size.x,
Size::Fraction(p) => ctx.layout.max_size.x * p, Size::Relative(p) => ctx.layout.max_size.x * p,
Size::Static(p) => p, Size::Absolute(p) => p,
}, },
Direction::Vertical => match self.size.height { Direction::Vertical => match self.size.height {
Size::Auto => ctx.layout.max_size.y, Size::Auto => ctx.layout.max_size.y,
Size::Fraction(p) => ctx.layout.max_size.y * p, Size::Relative(p) => ctx.layout.max_size.y * p,
Size::Static(p) => p, Size::Absolute(p) => p,
} }
}; };
@ -300,13 +300,13 @@ impl UiElement for Container {
match self.size.width { match self.size.width {
Size::Auto => (), Size::Auto => (),
Size::Fraction(percentage) => total_size.x = ctx.layout.max_size.x * percentage, Size::Relative(percentage) => total_size.x = ctx.layout.max_size.x * percentage,
Size::Static(pixels) => total_size.x = pixels, Size::Absolute(pixels) => total_size.x = pixels,
} }
match self.size.height { match self.size.height {
Size::Auto => (), Size::Auto => (),
Size::Fraction(percentage) => total_size.y = ctx.layout.max_size.y * percentage, Size::Relative(percentage) => total_size.y = ctx.layout.max_size.y * percentage,
Size::Static(pixels) => total_size.y = pixels, Size::Absolute(pixels) => total_size.y = pixels,
} }
Response { Response {

View file

@ -48,13 +48,13 @@ impl UiElement for FillRect {
size: vec2( size: vec2(
match self.size.width { match self.size.width {
Size::Auto => ctx.layout.max_size.x, Size::Auto => ctx.layout.max_size.x,
Size::Fraction(percentage) => ctx.layout.max_size.x * percentage, Size::Relative(percentage) => ctx.layout.max_size.x * percentage,
Size::Static(pixels) => pixels, Size::Absolute(pixels) => pixels,
}, },
match self.size.height { match self.size.height {
Size::Auto => ctx.layout.max_size.y, Size::Auto => ctx.layout.max_size.y,
Size::Fraction(percentage) => ctx.layout.max_size.y * percentage, Size::Relative(percentage) => ctx.layout.max_size.y * percentage,
Size::Static(pixels) => pixels, Size::Absolute(pixels) => pixels,
}, },
), ),
..Default::default() ..Default::default()

View file

@ -86,13 +86,13 @@ impl UiElement for Text {
size: vec2( size: vec2(
match self.size.width { match self.size.width {
Size::Auto => size.0, Size::Auto => size.0,
Size::Fraction(percentage) => ctx.layout.max_size.x * percentage, Size::Relative(percentage) => ctx.layout.max_size.x * percentage,
Size::Static(pixels) => pixels, Size::Absolute(pixels) => pixels,
}, },
match self.size.height { match self.size.height {
Size::Auto => size.1, Size::Auto => size.1,
Size::Fraction(percentage) => ctx.layout.max_size.y * percentage, Size::Relative(percentage) => ctx.layout.max_size.y * percentage,
Size::Static(pixels) => pixels, Size::Absolute(pixels) => pixels,
}, },
), ),
..Default::default() ..Default::default()

View file

@ -28,8 +28,8 @@ impl From<Size> for FramePoint {
fn from(size: Size) -> Self { fn from(size: Size) -> Self {
match size { match size {
Size::Auto => Self::BEGIN, Size::Auto => Self::BEGIN,
Size::Fraction(value) => Self::relative(value), Size::Relative(value) => Self::relative(value),
Size::Static(value) => Self::absolute(value), Size::Absolute(value) => Self::absolute(value),
} }
} }
} }

View file

@ -91,18 +91,18 @@ pub enum Size {
/// ///
/// Out of range values are allowed, but are not guaranteed to work as expected\ /// Out of range values are allowed, but are not guaranteed to work as expected\
/// (especially with negative values) /// (especially with negative values)
Fraction(f32), Relative(f32),
//TODO FractionRemaining(f32), //TODO FractionRemaining(f32),
/// Static size in pixels /// Static size in pixels
Static(f32), Absolute(f32),
} }
impl From<f32> for Size { impl From<f32> for Size {
#[inline] #[inline]
fn from(value: f32) -> Self { fn from(value: f32) -> Self {
Self::Static(value) Self::Absolute(value)
} }
} }
@ -166,13 +166,13 @@ pub struct LayoutInfo {
pub fn compute_size(layout: &LayoutInfo, size: Size2d, comfy_size: Vec2) -> Vec2 { pub fn compute_size(layout: &LayoutInfo, size: Size2d, comfy_size: Vec2) -> Vec2 {
let width = match size.width { let width = match size.width {
Size::Auto => comfy_size.x, Size::Auto => comfy_size.x,
Size::Fraction(fraction) => layout.max_size.x * fraction, Size::Relative(fraction) => layout.max_size.x * fraction,
Size::Static(size) => size, Size::Absolute(size) => size,
}; };
let height = match size.height { let height = match size.height {
Size::Auto => comfy_size.y, Size::Auto => comfy_size.y,
Size::Fraction(fraction) => layout.max_size.y * fraction, Size::Relative(fraction) => layout.max_size.y * fraction,
Size::Static(size) => size, Size::Absolute(size) => size,
}; };
vec2(width, height) vec2(width, height)
} }

View file

@ -3,16 +3,16 @@
/// ///
/// # Syntax: /// # Syntax:
/// - `auto` - `Size::Auto` /// - `auto` - `Size::Auto`
/// - `x` - `Size::Static(x)` /// - `x` - `Size::Absolute(x)`
/// - `x%` - `Size::Fraction(x / 100.)` *(literal only)* /// - `x%` - `Size::Relative(x / 100.)` *(literal only)*
/// - `x/` - `Size::Fraction(x)` /// - `x/` - `Size::Relative(x)`
/// ///
/// ...where `x` is a literal, identifier or an expression wrapped in parentheses /// ...where `x` is a literal, identifier or an expression wrapped in parentheses
/// ///
/// # Note: /// # Note:
/// - If a single argument is provided, it creates a `Size` using the rules specified above\ /// - If a single argument is provided, it creates a `Size` using the rules specified above\
/// - If two arguments are provided, it creates a `Size2d` with the first value as width and the second as height\ /// - If two arguments are provided, it creates a `Size2d` with the first value as width and the second as height\
/// Example: `size!(100, 50%)` creates a `Size2d` with width `100` (`Size::Static(100.)`) and height `50%` (`Size::Fraction(0.5)`) /// Example: `size!(100, 50%)` creates a `Size2d` with width `100` (`Size::Absolute(100.)`) and height `50%` (`Size::Relative(0.5)`)
/// - `%` syntax is only valid for literals (`50%`), not expressions or identidiers.\ /// - `%` syntax is only valid for literals (`50%`), not expressions or identidiers.\
/// Use `/` instead (`(0.5 * x)/`, `x/`), but be aware of the different range (0.0-1.0) \ /// Use `/` instead (`(0.5 * x)/`, `x/`), but be aware of the different range (0.0-1.0) \
/// - Expressions must be wrapped in parentheses (for example: `(x + 5)`).\ /// - Expressions must be wrapped in parentheses (for example: `(x + 5)`).\
@ -24,27 +24,27 @@ macro_rules! size {
}; };
($x:literal) => { ($x:literal) => {
$crate::layout::Size::Static($x as f32) $crate::layout::Size::Absolute($x as f32)
}; };
($x:literal %) => { ($x:literal %) => {
$crate::layout::Size::Fraction($x as f32 / 100.) $crate::layout::Size::Relative($x as f32 / 100.)
}; };
($x:literal /) => { ($x:literal /) => {
$crate::layout::Size::Fraction($x as f32) $crate::layout::Size::Relative($x as f32)
}; };
($x:ident) => { ($x:ident) => {
$crate::layout::Size::Static($x as f32) $crate::layout::Size::Absolute($x as f32)
}; };
($x:ident /) => { ($x:ident /) => {
$crate::layout::Size::Fraction($x as f32) $crate::layout::Size::Relative($x as f32)
}; };
(($x:expr)) => { (($x:expr)) => {
$crate::layout::Size::Static(($x) as f32) $crate::layout::Size::Absolute(($x) as f32)
}; };
(($x:expr) /) => { (($x:expr) /) => {
$crate::layout::Size::Fraction(($x) as f32) $crate::layout::Size::Relative(($x) as f32)
}; };
($x:tt , $y:tt $($ys:tt)?) => { ($x:tt , $y:tt $($ys:tt)?) => {