From 73fde36c8a6df497a0daa0abd5445a7437de6fc2 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Tue, 20 Feb 2024 21:13:21 +0100 Subject: [PATCH] rename `border_radius` to `corner_radius` add it to mom downloader example --- hui-examples/examples/align_test.rs | 7 +- hui-examples/examples/mom_downloader.rs | 1 + hui-examples/examples/rounded_rect.rs | 120 ++++++++++++++++-------- hui/src/background.rs | 52 +++++++++- hui/src/element/builtin/progress_bar.rs | 8 +- 5 files changed, 141 insertions(+), 47 deletions(-) diff --git a/hui-examples/examples/align_test.rs b/hui-examples/examples/align_test.rs index e43418f..2be1543 100644 --- a/hui-examples/examples/align_test.rs +++ b/hui-examples/examples/align_test.rs @@ -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() diff --git a/hui-examples/examples/mom_downloader.rs b/hui-examples/examples/mom_downloader.rs index 61b3de8..ec80518 100644 --- a/hui-examples/examples/mom_downloader.rs +++ b/hui-examples/examples/mom_downloader.rs @@ -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 { diff --git a/hui-examples/examples/rounded_rect.rs b/hui-examples/examples/rounded_rect.rs index 0242c73..cdd0fbc 100644 --- a/hui-examples/examples/rounded_rect.rs +++ b/hui-examples/examples/rounded_rect.rs @@ -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); diff --git a/hui/src/background.rs b/hui/src/background.rs index 09fc821..764f055 100644 --- a/hui/src/background.rs +++ b/hui/src/background.rs @@ -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), } -impl From 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> for Background { + fn from(corners: Corners) -> Self { + Self::Gradient(corners) } } @@ -32,6 +38,44 @@ impl From> for Background { } } +impl From 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> for Background { + fn from(corners: Corners) -> 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> for Background { + fn from(color: Option) -> Self { + match color { + Some(color) => Self::Solid(color.extend(1.)), + None => Self::Transparent, + } + } +} + +impl From 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.\ diff --git a/hui/src/element/builtin/progress_bar.rs b/hui/src/element/builtin/progress_bar.rs index 96b6ea5..0cb4c8e 100644 --- a/hui/src/element/builtin/progress_bar.rs +++ b/hui/src/element/builtin/progress_bar.rs @@ -9,7 +9,7 @@ pub struct ProgressBar { pub value: f32, pub color_foreground: Vec4, pub color_background: Vec4, - pub border_radius: Corners, + pub corner_radius: Corners, } 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.;