rename stuff

This commit is contained in:
griffi-gh 2024-02-18 19:27:45 +01:00
parent 246b80c0fa
commit b04694ce83
10 changed files with 46 additions and 40 deletions

View file

@ -49,13 +49,13 @@ fn main() {
hui.add(Container { hui.add(Container {
align: (Alignment::Center, Alignment::Center), align: (Alignment::Center, Alignment::Center),
size: (UiSize::Percentage(1.), UiSize::Percentage(1.)), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
background: Some(vec4(0.1, 0.1, 0.1, 1.)), background: Some(vec4(0.1, 0.1, 0.1, 1.)),
elements: vec![Box::new(Container { elements: vec![Box::new(Container {
gap: 5., gap: 5.,
padding: Sides::all(10.), padding: Sides::all(10.),
align: (Alignment::Begin, Alignment::Begin), align: (Alignment::Begin, Alignment::Begin),
size: (UiSize::Pixels(450.), UiSize::Auto), size: (UiSize::Static(450.), UiSize::Auto),
background: Some(vec4(0.2, 0.2, 0.5, 1.)), background: Some(vec4(0.2, 0.2, 0.5, 1.)),
elements: elements(|el| { elements: elements(|el| {
if instant.elapsed().as_secs_f32() < 5. { if instant.elapsed().as_secs_f32() < 5. {

View file

@ -65,7 +65,7 @@ fn main() {
for i in 0..cnt { for i in 0..cnt {
elements.push(Box::new(ProgressBar { elements.push(Box::new(ProgressBar {
value: (instant.elapsed().as_secs_f32() + (i as f32 / 10.)).sin().powi(2), value: (instant.elapsed().as_secs_f32() + (i as f32 / 10.)).sin().powi(2),
size: (UiSize::Auto, UiSize::Pixels(5.)), size: (UiSize::Auto, UiSize::Static(5.)),
..Default::default() ..Default::default()
})); }));
} }

View file

@ -48,7 +48,7 @@ fn main() {
gap: 5., gap: 5.,
padding: Sides::all(5.), padding: Sides::all(5.),
align: (Alignment::Begin, Alignment::Center), align: (Alignment::Begin, Alignment::Center),
size: (UiSize::Percentage(1.), UiSize::Percentage(1.)), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
elements: vec![ elements: vec![
Box::new(ProgressBar { Box::new(ProgressBar {
value: 0.5, value: 0.5,
@ -62,31 +62,31 @@ fn main() {
gap: 5., gap: 5.,
padding: Sides::all(5.), padding: Sides::all(5.),
align: (Alignment::End, Alignment::Center), align: (Alignment::End, Alignment::Center),
size: (UiSize::Percentage(1.), UiSize::Percentage(1.)), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
elements: vec![ elements: vec![
Box::new(ProgressBar { Box::new(ProgressBar {
value: z, value: z,
..Default::default() ..Default::default()
}), }),
Box::new(Container { Box::new(Container {
size: (UiSize::Percentage(1.), UiSize::Auto), size: (UiSize::Fraction(1.), UiSize::Auto),
align: (Alignment::Center, Alignment::End), align: (Alignment::Center, Alignment::End),
padding: Sides::all(5.), padding: Sides::all(5.),
gap: 10., gap: 10.,
elements: vec![ elements: vec![
Box::new(Rect { Box::new(Rect {
size: (UiSize::Percentage(0.5), UiSize::Pixels(30.)), size: (UiSize::Fraction(0.5), UiSize::Static(30.)),
color: Some(vec4(0.75, 0., 0., 1.)) color: Some(vec4(0.75, 0., 0., 1.))
}), }),
Box::new(Rect { Box::new(Rect {
size: (UiSize::Percentage(z / 2. + 0.5), UiSize::Pixels(30.)), size: (UiSize::Fraction(z / 2. + 0.5), UiSize::Static(30.)),
color: Some(vec4(0., 0.75, 0., 1.)) color: Some(vec4(0., 0.75, 0., 1.))
}), }),
], ],
..Default::default() ..Default::default()
}), }),
Box::new(Rect { Box::new(Rect {
size: (UiSize::Percentage(z / 2. + 0.5), UiSize::Pixels(30.)), size: (UiSize::Fraction(z / 2. + 0.5), UiSize::Static(30.)),
color: Some(vec4(0., 0.75, 0., 1.)) color: Some(vec4(0., 0.75, 0., 1.))
}), }),
Box::new(Container { Box::new(Container {
@ -98,7 +98,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(Rect { x.push(Box::new(Rect {
size: (UiSize::Pixels(50.), UiSize::Pixels(50.)), size: (UiSize::Static(50.), UiSize::Static(50.)),
color: if i == 1 { color: if i == 1 {
Some(vec4(0.75, 0.75, 0.75, 0.75)) Some(vec4(0.75, 0.75, 0.75, 0.75))
} else { } else {
@ -120,7 +120,7 @@ fn main() {
}, },
elements: vec![ elements: vec![
Box::new(Rect { Box::new(Rect {
size: (UiSize::Pixels(50.), UiSize::Pixels(50.)), size: (UiSize::Static(50.), UiSize::Static(50.)),
color: Some(vec4(1., 1., 1., 0.75)) color: Some(vec4(1., 1., 1., 0.75))
}.into_interactable().on_click(|| { }.into_interactable().on_click(|| {
println!("clicked"); println!("clicked");

View file

@ -39,7 +39,7 @@ fn main() {
gap: 5., gap: 5.,
padding: Sides::all(5.), padding: Sides::all(5.),
align: (Alignment::Center, Alignment::Center), align: (Alignment::Center, Alignment::Center),
size: (UiSize::Percentage(1.), UiSize::Percentage(1.)), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
elements: vec![ elements: vec![
Box::new(Text { Box::new(Text {
text: "Hello, world!\nGoodbye, world!\nowo\nuwu".into(), text: "Hello, world!\nGoodbye, world!\nowo\nuwu".into(),

View file

@ -44,7 +44,7 @@ fn main() {
hui.begin(); hui.begin();
hui.add(Container { hui.add(Container {
size: (UiSize::Percentage(1.), UiSize::Percentage(1.)), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
background: Some(vec4(0.1, 0.1, 0.1, 1.)), background: Some(vec4(0.1, 0.1, 0.1, 1.)),
elements: elements(|elem| { elements: elements(|elem| {
elem.add(Text { elem.add(Text {
@ -68,11 +68,11 @@ fn main() {
}); });
} }
elem.add(Rect { elem.add(Rect {
size: (UiSize::Percentage(1.), UiSize::Pixels(10.)), size: (UiSize::Fraction(1.), UiSize::Static(10.)),
color: Some(vec4(0., 0., 1., 1.)), color: Some(vec4(0., 0., 1., 1.)),
}); });
elem.add(Rect { elem.add(Rect {
size: (UiSize::Percentage(1.), UiSize::Pixels(10.)), size: (UiSize::Fraction(1.), UiSize::Static(10.)),
color: Some(vec4(1., 1., 0., 1.)), color: Some(vec4(1., 1., 0., 1.)),
}); });
elem.add(Text { elem.add(Text {
@ -83,11 +83,11 @@ fn main() {
}); });
if instant.elapsed().as_secs() & 1 != 0 { if instant.elapsed().as_secs() & 1 != 0 {
elem.add(Rect { elem.add(Rect {
size: (UiSize::Percentage(1.), UiSize::Pixels(10.)), size: (UiSize::Fraction(1.), UiSize::Static(10.)),
color: Some(vec4(1., 0., 0., 1.)), color: Some(vec4(1., 0., 0., 1.)),
}); });
elem.add(Rect { elem.add(Rect {
size: (UiSize::Percentage(1.), UiSize::Pixels(10.)), size: (UiSize::Fraction(1.), UiSize::Static(10.)),
color: Some(vec4(0., 0., 0., 1.)), color: Some(vec4(0., 0., 0., 1.)),
}); });
elem.add(Spacer(100.)); elem.add(Spacer(100.));

View file

@ -87,13 +87,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.0 { let outer_size_x = match self.size.0 {
UiSize::Auto => layout.max_size.x, UiSize::Auto => layout.max_size.x,
UiSize::Percentage(p) => layout.max_size.x * p, UiSize::Fraction(p) => layout.max_size.x * p,
UiSize::Pixels(p) => p, UiSize::Static(p) => p,
}; };
let outer_size_y = match self.size.1 { let outer_size_y = match self.size.1 {
UiSize::Auto => layout.max_size.y, UiSize::Auto => layout.max_size.y,
UiSize::Percentage(p) => layout.max_size.y * p, UiSize::Fraction(p) => layout.max_size.y * p,
UiSize::Pixels(p) => p, UiSize::Static(p) => p,
}; };
vec2( vec2(
outer_size_x - (self.padding.left + self.padding.right), outer_size_x - (self.padding.left + self.padding.right),
@ -141,13 +141,13 @@ impl UiElement for Container {
match self.size.0 { match self.size.0 {
UiSize::Auto => (), UiSize::Auto => (),
UiSize::Percentage(percentage) => size.x = ctx.layout.max_size.x * percentage, UiSize::Fraction(percentage) => size.x = ctx.layout.max_size.x * percentage,
UiSize::Pixels(pixels) => size.x = pixels, UiSize::Static(pixels) => size.x = pixels,
} }
match self.size.1 { match self.size.1 {
UiSize::Auto => (), UiSize::Auto => (),
UiSize::Percentage(percentage) => size.y = ctx.layout.max_size.y * percentage, UiSize::Fraction(percentage) => size.y = ctx.layout.max_size.y * percentage,
UiSize::Pixels(pixels) => size.y = pixels, UiSize::Static(pixels) => size.y = pixels,
} }
Response { Response {

View file

@ -35,13 +35,13 @@ impl UiElement for ProgressBar {
size: vec2( size: vec2(
match self.size.0 { match self.size.0 {
UiSize::Auto => ctx.layout.max_size.x.max(300.), UiSize::Auto => ctx.layout.max_size.x.max(300.),
UiSize::Percentage(p) => ctx.layout.max_size.x * p, UiSize::Fraction(p) => ctx.layout.max_size.x * p,
UiSize::Pixels(p) => p, UiSize::Static(p) => p,
}, },
match self.size.1 { match self.size.1 {
UiSize::Auto => BAR_HEIGHT, UiSize::Auto => BAR_HEIGHT,
UiSize::Percentage(p) => ctx.layout.max_size.y * p, UiSize::Fraction(p) => ctx.layout.max_size.y * p,
UiSize::Pixels(p) => p, UiSize::Static(p) => p,
} }
), ),
hints: Default::default(), hints: Default::default(),

View file

@ -14,7 +14,7 @@ pub struct Rect {
impl Default for Rect { impl Default for Rect {
fn default() -> Self { fn default() -> Self {
Self { Self {
size: (UiSize::Pixels(10.), UiSize::Pixels(10.)), size: (UiSize::Static(10.), UiSize::Static(10.)),
color: Some(Vec4::new(0., 0., 0., 0.5)), color: Some(Vec4::new(0., 0., 0., 0.5)),
} }
} }
@ -26,13 +26,13 @@ impl UiElement for Rect {
size: vec2( size: vec2(
match self.size.0 { match self.size.0 {
UiSize::Auto => ctx.layout.max_size.x, UiSize::Auto => ctx.layout.max_size.x,
UiSize::Percentage(percentage) => ctx.layout.max_size.x * percentage, UiSize::Fraction(percentage) => ctx.layout.max_size.x * percentage,
UiSize::Pixels(pixels) => pixels, UiSize::Static(pixels) => pixels,
}, },
match self.size.1 { match self.size.1 {
UiSize::Auto => ctx.layout.max_size.y, UiSize::Auto => ctx.layout.max_size.y,
UiSize::Percentage(percentage) => ctx.layout.max_size.y * percentage, UiSize::Fraction(percentage) => ctx.layout.max_size.y * percentage,
UiSize::Pixels(pixels) => pixels, UiSize::Static(pixels) => pixels,
}, },
), ),
hints: Default::default(), hints: Default::default(),

View file

@ -8,6 +8,12 @@ use crate::{
UiSize UiSize
}; };
pub enum TextSize {
FitToWidthRatio(f32),
FitToHeightRatio(f32),
Constant(u8),
}
pub struct Text { pub struct Text {
pub text: Cow<'static, str>, pub text: Cow<'static, str>,
pub size: (UiSize, UiSize), pub size: (UiSize, UiSize),
@ -40,13 +46,13 @@ impl UiElement for Text {
size: vec2( size: vec2(
match self.size.0 { match self.size.0 {
UiSize::Auto => size.0, UiSize::Auto => size.0,
UiSize::Percentage(percentage) => ctx.layout.max_size.x * percentage, UiSize::Fraction(percentage) => ctx.layout.max_size.x * percentage,
UiSize::Pixels(pixels) => pixels, UiSize::Static(pixels) => pixels,
}, },
match self.size.1 { match self.size.1 {
UiSize::Auto => size.1, UiSize::Auto => size.1,
UiSize::Percentage(percentage) => ctx.layout.max_size.y * percentage, UiSize::Fraction(percentage) => ctx.layout.max_size.y * percentage,
UiSize::Pixels(pixels) => pixels, UiSize::Static(pixels) => pixels,
}, },
), ),
hints: Default::default(), hints: Default::default(),

View file

@ -113,8 +113,8 @@ impl Default for UiInstance {
pub enum UiSize { pub enum UiSize {
#[default] #[default]
Auto, Auto,
Percentage(f32), Fraction(f32),
Pixels(f32), Static(f32),
} }
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]