rename border_radius to corner_radius add it to mom downloader example

This commit is contained in:
griffi-gh 2024-02-20 21:13:21 +01:00
parent ff448ed393
commit 73fde36c8a
5 changed files with 141 additions and 47 deletions

View file

@ -66,7 +66,7 @@ fn main() {
elements: vec![
Box::new(ProgressBar {
value: z,
border_radius: Corners::all(0.25 * ProgressBar::DEFAULT_HEIGHT),
corner_radius: Corners::all(0.25 * ProgressBar::DEFAULT_HEIGHT),
..Default::default()
}),
Box::new(Container {
@ -81,7 +81,10 @@ fn main() {
}),
Box::new(Rect {
size: (UiSize::Fraction(z / 2. + 0.5), UiSize::Static(30.)),
color: vec4(0., 0.75, 0., 1.).into()
color: Corners::left_right(
vec4(1., 0., 0., 1.),
vec4(0., 1., 0., 1.)
).into()
}),
],
..Default::default()

View file

@ -67,6 +67,7 @@ fn main() {
});
el.add(ProgressBar {
value: mom_ratio,
corner_radius: Corners::all(0.125 * ProgressBar::DEFAULT_HEIGHT),
..Default::default()
});
el.add(Container {

View file

@ -38,45 +38,91 @@ fn main() {
hui.begin();
hui.add(Container {
gap: 10.,
align: Alignment::Center.into(),
size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
elements: vec![Box::new(Container {
align: Alignment::Center.into(),
size: (UiSize::Fraction(0.5), UiSize::Fraction(0.5)),
background: vec4(1., 0., 0., 1.).into(),
corner_radius: Corners {
top_left: 10.,
top_right: 20.,
bottom_left: 50.,
bottom_right: 80.
},
elements: vec![
Box::new(Container {
padding: Sides::all(20.),
direction: UiDirection::Horizontal,
align: Alignment::Center.into(),
size: (UiSize::Auto, UiSize::Auto),
background: vec4(0.1, 0.1, 0.1, 0.5).into(),
corner_radius: Corners::all(8.),
elements: vec![
Box::new(Text {
text: "Corners".into(),
text_size: 50,
color: vec4(1., 1., 1., 1.),
..Default::default()
}),
Box::new(Text {
text: "!".into(),
text_size: 50,
color: vec4(1., 1., 0., 1.),
..Default::default()
}),
],
..Default::default()
}),
],
..Default::default()
})],
elements: vec![
Box::new(Container {
align: Alignment::Center.into(),
size: (UiSize::Fraction(0.5), UiSize::Fraction(0.5)),
background: vec4(1., 0., 0., 1.).into(),
corner_radius: Corners {
top_left: 10.,
top_right: 20.,
bottom_left: 50.,
bottom_right: 80.
},
elements: vec![
Box::new(Container {
padding: Sides::all(20.),
direction: UiDirection::Horizontal,
align: Alignment::Center.into(),
size: (UiSize::Auto, UiSize::Auto),
background: vec4(0.1, 0.1, 0.1, 0.5).into(),
corner_radius: Corners::all(8.),
elements: vec![
Box::new(Text {
text: "Corners".into(),
text_size: 50,
color: vec4(1., 1., 1., 1.),
..Default::default()
}),
Box::new(Text {
text: "!".into(),
text_size: 50,
color: vec4(1., 1., 0., 1.),
..Default::default()
}),
],
..Default::default()
}),
],
..Default::default()
}),
Box::new(Container {
gap: 10.,
direction: UiDirection::Horizontal,
elements: vec![
Box::new(Container {
size: (UiSize::Static(100.), UiSize::Static(100.)),
background: Corners::left_right(
vec4(1., 0., 0., 1.),
vec4(0., 1., 0., 1.)
).into(),
corner_radius: Corners::all(0.),
..Default::default()
}),
Box::new(Container {
size: (UiSize::Static(100.), UiSize::Static(100.)),
background: Corners::left_right(
vec4(1., 0., 0., 1.),
vec4(0., 1., 0., 1.)
).into(),
corner_radius: Corners::all(10.),
..Default::default()
}),
Box::new(Container {
size: (UiSize::Static(100.), UiSize::Static(100.)),
background: Corners::left_right(
vec4(1., 0., 0., 1.),
vec4(0., 1., 0., 1.)
).into(),
corner_radius: Corners::all(20.),
..Default::default()
}),
Box::new(Container {
size: (UiSize::Static(100.), UiSize::Static(100.)),
background: Corners::left_right(
vec4(1., 0., 0., 1.),
vec4(0., 1., 0., 1.)
).into(),
corner_radius: Corners::all(30.),
..Default::default()
}),
],
..Default::default()
}),
],
..Default::default()
}, resolution);

View file

@ -1,4 +1,4 @@
use glam::Vec4;
use glam::{vec4, Vec3, Vec4};
use crate::rectangle::Corners;
// #[derive(Clone, Copy, PartialEq, Eq, Debug)]
@ -17,9 +17,15 @@ pub enum Background {
Gradient(Corners<Vec4>),
}
impl From<Vec4> for Background {
fn from(color: Vec4) -> Self {
Self::Solid(color)
impl From<(f32, f32, f32, f32)> for Background {
fn from(color: (f32, f32, f32, f32)) -> Self {
Self::Solid(vec4(color.0, color.1, color.2, color.3))
}
}
impl From<Corners<Vec4>> for Background {
fn from(corners: Corners<Vec4>) -> Self {
Self::Gradient(corners)
}
}
@ -32,6 +38,44 @@ impl From<Option<Vec4>> for Background {
}
}
impl From<Vec4> for Background {
fn from(color: Vec4) -> Self {
Self::Solid(color)
}
}
impl From<(f32, f32, f32)> for Background {
fn from(color: (f32, f32, f32)) -> Self {
Self::Solid(vec4(color.0, color.1, color.2, 1.))
}
}
impl From<Corners<Vec3>> for Background {
fn from(corners: Corners<Vec3>) -> Self {
Self::Gradient(Corners {
top_left: corners.top_left.extend(1.),
top_right: corners.top_right.extend(1.),
bottom_left: corners.bottom_left.extend(1.),
bottom_right: corners.bottom_right.extend(1.),
})
}
}
impl From<Option<Vec3>> for Background {
fn from(color: Option<Vec3>) -> Self {
match color {
Some(color) => Self::Solid(color.extend(1.)),
None => Self::Transparent,
}
}
}
impl From<Vec3> for Background {
fn from(color: Vec3) -> Self {
Self::Solid(color.extend(1.))
}
}
impl Background {
/// Currently, never returns None.\
/// `Option` has been added in preparation for future changes.\

View file

@ -9,7 +9,7 @@ pub struct ProgressBar {
pub value: f32,
pub color_foreground: Vec4,
pub color_background: Vec4,
pub border_radius: Corners<f32>,
pub corner_radius: Corners<f32>,
}
impl ProgressBar {
@ -23,7 +23,7 @@ impl Default for ProgressBar {
value: 0.,
color_foreground: vec4(0.0, 0.0, 1.0, 1.0),
color_background: vec4(0.0, 0.0, 0.0, 1.0),
border_radius: Corners::all(0.),
corner_radius: Corners::all(0.),
}
}
}
@ -53,9 +53,9 @@ impl UiElement for ProgressBar {
fn process(&self, ctx: ProcessContext) {
let value = self.value.clamp(0., 1.);
let rounded_corners =
(self.border_radius.max_f32() > 0.).then_some({
(self.corner_radius.max_f32() > 0.).then_some({
//HACK: fix clipping issues; //todo: get rid of this
let mut radii = self.border_radius;
let mut radii = self.corner_radius;
let width = ctx.measure.size.x * value;
if width <= radii.max_f32() * 2. {
radii.bottom_right = 0.;