From f05659e35bff04e8bdcfdff1f30c665ba121a88e Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 7 Mar 2024 00:50:47 +0100 Subject: [PATCH] change bg color api again --- hui/src/background.rs | 38 ++++++++++--------------- hui/src/element/builtin/container.rs | 6 ++-- hui/src/element/builtin/fill_rect.rs | 6 ++-- hui/src/element/builtin/progress_bar.rs | 10 +++---- 4 files changed, 26 insertions(+), 34 deletions(-) diff --git a/hui/src/background.rs b/hui/src/background.rs index 70226a6..146667c 100644 --- a/hui/src/background.rs +++ b/hui/src/background.rs @@ -11,26 +11,26 @@ use crate::rectangle::Corners; //TODO: move this into the color module? #[derive(Clone, Copy, Default, Debug, PartialEq)] -pub enum RectBackground { +pub enum BackgroundColor { #[default] Transparent, Solid(Vec4), Gradient(Corners), } -impl From<(f32, f32, f32, f32)> for RectBackground { +impl From<(f32, f32, f32, f32)> for BackgroundColor { fn from(color: (f32, f32, f32, f32)) -> Self { Self::Solid(vec4(color.0, color.1, color.2, color.3)) } } -impl From> for RectBackground { +impl From> for BackgroundColor { fn from(corners: Corners) -> Self { Self::Gradient(corners) } } -impl From> for RectBackground { +impl From> for BackgroundColor { fn from(color: Option) -> Self { match color { Some(color) => Self::Solid(color), @@ -39,19 +39,19 @@ impl From> for RectBackground { } } -impl From for RectBackground { +impl From for BackgroundColor { fn from(color: Vec4) -> Self { Self::Solid(color) } } -impl From<(f32, f32, f32)> for RectBackground { +impl From<(f32, f32, f32)> for BackgroundColor { fn from(color: (f32, f32, f32)) -> Self { Self::Solid(vec4(color.0, color.1, color.2, 1.)) } } -impl From> for RectBackground { +impl From> for BackgroundColor { fn from(corners: Corners) -> Self { Self::Gradient(Corners { top_left: corners.top_left.extend(1.), @@ -62,7 +62,7 @@ impl From> for RectBackground { } } -impl From> for RectBackground { +impl From> for BackgroundColor { fn from(color: Option) -> Self { match color { Some(color) => Self::Solid(color.extend(1.)), @@ -71,21 +71,19 @@ impl From> for RectBackground { } } -impl From for RectBackground { +impl From for BackgroundColor { fn from(color: Vec3) -> Self { Self::Solid(color.extend(1.)) } } -impl RectBackground { - /// Currently, never returns None.\ - /// `Option` has been added in preparation for future changes.\ - /// (`Background::Texture` etc) - pub fn corners(&self) -> Option> { +impl BackgroundColor { + /// Returns the colors of individual corners + pub fn corners(&self) -> Corners { match *self { - Self::Transparent => Some(Corners::all(Vec4::ZERO)), - Self::Solid(color) => Some(Corners::all(color)), - Self::Gradient(corners) => Some(corners), + Self::Transparent => Corners::all(Vec4::ZERO), + Self::Solid(color) => Corners::all(color), + Self::Gradient(corners) => corners, } } @@ -105,9 +103,3 @@ impl RectBackground { } } } - -// impl From<(GradientDirection, Vec4, Vec4)> for Background { -// fn from(gradient: (GradientDirection, Vec4, Vec4)) -> Self { -// Self::Gradient(gradient.0, gradient.1, gradient.2) -// } -// } diff --git a/hui/src/element/builtin/container.rs b/hui/src/element/builtin/container.rs index 84b2897..b2f8e64 100644 --- a/hui/src/element/builtin/container.rs +++ b/hui/src/element/builtin/container.rs @@ -3,7 +3,7 @@ use derive_setters::Setters; use glam::{Vec2, vec2}; use crate::{ - background::RectBackground, + background::BackgroundColor, draw::{RoundedCorners, UiDrawCommand}, element::{ElementList, MeasureContext, ProcessContext, UiElement}, layout::{Alignment, Alignment2d, LayoutInfo, UiDirection, Size, Size2d}, @@ -57,7 +57,7 @@ pub struct Container { /// Background color of the container #[setters(into)] - pub background: RectBackground, + pub background: BackgroundColor, /// Corner radius of the background rectangle #[setters(into)] @@ -311,7 +311,7 @@ impl UiElement for Container { //background if !self.background.is_transparent() { - let corner_colors = self.background.corners().unwrap(); + let corner_colors = self.background.corners(); ctx.draw.add(UiDrawCommand::Rectangle { position, size: ctx.measure.size, diff --git a/hui/src/element/builtin/fill_rect.rs b/hui/src/element/builtin/fill_rect.rs index 6703736..551622a 100644 --- a/hui/src/element/builtin/fill_rect.rs +++ b/hui/src/element/builtin/fill_rect.rs @@ -3,7 +3,7 @@ use derive_setters::Setters; use glam::{vec2, Vec4}; use crate::{ - background::RectBackground, + background::BackgroundColor, draw::{UiDrawCommand, RoundedCorners}, element::{UiElement, MeasureContext, ProcessContext}, layout::{Size, Size2d}, @@ -22,7 +22,7 @@ pub struct FillRect { /// Background color of the rectangle #[setters(into)] - pub background: RectBackground, + pub background: BackgroundColor, /// Corner radius of the rectangle #[setters(into)] @@ -67,7 +67,7 @@ impl UiElement for FillRect { ctx.draw.add(UiDrawCommand::Rectangle { position: ctx.layout.position, size: ctx.measure.size, - color: self.background.corners().unwrap(), + color: self.background.corners(), texture: None, rounded_corners: (self.corner_radius.max_f32() > 0.).then_some({ RoundedCorners::from_radius(self.corner_radius) diff --git a/hui/src/element/builtin/progress_bar.rs b/hui/src/element/builtin/progress_bar.rs index eba1e49..57c9f12 100644 --- a/hui/src/element/builtin/progress_bar.rs +++ b/hui/src/element/builtin/progress_bar.rs @@ -1,7 +1,7 @@ use derive_setters::Setters; use glam::{vec2, vec4}; use crate::{ - background::RectBackground, + background::BackgroundColor, draw::{RoundedCorners, UiDrawCommand}, element::{MeasureContext, ProcessContext, UiElement}, layout::{Size, Size2d}, @@ -21,11 +21,11 @@ pub struct ProgressBar { /// Foreground (bar) color #[setters(into)] - pub foreground: RectBackground, + pub foreground: BackgroundColor, /// Background color #[setters(into)] - pub background: RectBackground, + pub background: BackgroundColor, /// Corner radius of the progress bar #[setters(into)] @@ -94,7 +94,7 @@ impl UiElement for ProgressBar { ctx.draw.add(UiDrawCommand::Rectangle { position: ctx.layout.position, size: ctx.measure.size, - color: self.background.corners().unwrap(), + color: self.background.corners(), texture: None, rounded_corners }); @@ -103,7 +103,7 @@ impl UiElement for ProgressBar { ctx.draw.add(UiDrawCommand::Rectangle { position: ctx.layout.position, size: ctx.measure.size * vec2(value, 1.0), - color: self.foreground.corners().unwrap(), + color: self.foreground.corners(), texture: None, rounded_corners, });