diff --git a/hui/src/background.rs b/hui/src/background.rs deleted file mode 100644 index ab1f5f9..0000000 --- a/hui/src/background.rs +++ /dev/null @@ -1,115 +0,0 @@ -//! (deprecated) background color, gradient and texturing -#![allow(deprecated)] - -use glam::{vec4, Vec3, Vec4}; -use crate::rect::Corners; - -//TODO: use this -// pub struct Background { -// pub color: BackgroundColor, -// pub texture: Option -// } - -//TODO: move this into the color module? -/// Represents the background color of an element -/// -/// Can be either a solid color, a gradient or transparent -#[deprecated(note = "Use `CornersColors` instead")] -#[derive(Clone, Copy, Default, Debug, PartialEq)] -pub enum BackgroundColor { - /// Transparent background (alpha = 0) - #[default] - Transparent, - - /// Solid, RGBA color - Solid(Vec4), - - /// Simple gradient color, with different colors for each corner - Gradient(Corners), -} - -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 BackgroundColor { - fn from(corners: Corners) -> Self { - Self::Gradient(corners) - } -} - -impl From> for BackgroundColor { - fn from(color: Option) -> Self { - match color { - Some(color) => Self::Solid(color), - None => Self::Transparent, - } - } -} - -impl From for BackgroundColor { - fn from(color: Vec4) -> Self { - Self::Solid(color) - } -} - -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 BackgroundColor { - 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 BackgroundColor { - fn from(color: Option) -> Self { - match color { - Some(color) => Self::Solid(color.extend(1.)), - None => Self::Transparent, - } - } -} - -impl From for BackgroundColor { - fn from(color: Vec3) -> Self { - Self::Solid(color.extend(1.)) - } -} - -impl BackgroundColor { - /// Returns the colors of individual corners - pub fn corners(&self) -> Corners { - match *self { - Self::Transparent => Corners::all(Vec4::ZERO), - Self::Solid(color) => Corners::all(color), - Self::Gradient(corners) => corners, - } - } - - /// Returns `true` if the background is `Transparent` or all corners have an alpha value of `0`. - pub fn is_transparent(&self) -> bool { - match *self { - Self::Transparent => true, - Self::Solid(color) => color.w == 0., - Self::Gradient(corners) => { - let max_alpha = - corners.top_left.w - .max(corners.top_right.w) - .max(corners.bottom_left.w) - .max(corners.bottom_right.w); - max_alpha == 0. - }, - } - } -} diff --git a/hui/src/element/builtin/container.rs b/hui/src/element/builtin/container.rs index 7e584c8..1104a12 100644 --- a/hui/src/element/builtin/container.rs +++ b/hui/src/element/builtin/container.rs @@ -3,12 +3,11 @@ use derive_setters::Setters; use glam::{Vec2, vec2}; use crate::{ - background::BackgroundColor, draw::{ImageHandle, RoundedCorners, UiDrawCommand}, element::{ElementList, MeasureContext, ProcessContext, UiElement}, layout::{Alignment, Alignment2d, Direction, LayoutInfo, Size, Size2d}, measure::{Hints, Response}, - rect::{Corners, Sides}, + rect::{Corners, FillColor, Sides}, }; // pub struct Border { @@ -59,7 +58,7 @@ pub struct Container { /// /// If the container has a background texture, it will be multiplied by this color #[setters(into)] - pub background: BackgroundColor, + pub background: FillColor, /// Background texture of the container /// diff --git a/hui/src/element/builtin/fill_rect.rs b/hui/src/element/builtin/fill_rect.rs index 5b330c1..077b1dc 100644 --- a/hui/src/element/builtin/fill_rect.rs +++ b/hui/src/element/builtin/fill_rect.rs @@ -3,12 +3,11 @@ use derive_setters::Setters; use glam::{vec2, Vec4}; use crate::{ - background::BackgroundColor, - draw::{UiDrawCommand, RoundedCorners}, - element::{UiElement, MeasureContext, ProcessContext}, + draw::{RoundedCorners, UiDrawCommand}, + element::{MeasureContext, ProcessContext, UiElement}, layout::{Size, Size2d}, measure::Response, - rect::Corners, + rect::{Corners, FillColor}, size, }; @@ -22,7 +21,7 @@ pub struct FillRect { /// Background color of the rectangle #[setters(into)] - pub background: BackgroundColor, + pub background: FillColor, /// Corner radius of the rectangle #[setters(into)] diff --git a/hui/src/element/builtin/image.rs b/hui/src/element/builtin/image.rs index 831384e..12c6744 100644 --- a/hui/src/element/builtin/image.rs +++ b/hui/src/element/builtin/image.rs @@ -1,12 +1,11 @@ use derive_setters::Setters; use glam::vec2; use crate::{ - background::BackgroundColor, draw::{ImageHandle, RoundedCorners, UiDrawCommand}, element::{MeasureContext, ProcessContext, UiElement}, layout::{compute_size, Size, Size2d}, measure::Response, - rect::Corners, + rect::{Corners, FillColor}, }; #[derive(Setters)] @@ -29,7 +28,7 @@ pub struct Image { /// /// Image will get multiplied/tinted by this color or gradient #[setters(into)] - pub color: BackgroundColor, + pub color: FillColor, /// Corner radius of the image #[setters(into)] @@ -44,7 +43,7 @@ impl Image { width: Size::Auto, height: Size::Auto, }, - color: BackgroundColor::from((1., 1., 1., 1.)), + color: (1., 1., 1.).into(), corner_radius: Corners::all(0.), } } diff --git a/hui/src/element/builtin/progress_bar.rs b/hui/src/element/builtin/progress_bar.rs index 1c73cb0..6c56a52 100644 --- a/hui/src/element/builtin/progress_bar.rs +++ b/hui/src/element/builtin/progress_bar.rs @@ -1,12 +1,11 @@ use derive_setters::Setters; use glam::{vec2, vec4}; use crate::{ - background::BackgroundColor, draw::{RoundedCorners, UiDrawCommand}, element::{MeasureContext, ProcessContext, UiElement}, layout::{compute_size, Size, Size2d}, measure::Response, - rect::Corners + rect::{Corners, FillColor} }; #[derive(Debug, Clone, Copy, Setters)] @@ -21,11 +20,11 @@ pub struct ProgressBar { /// Foreground (bar) color #[setters(into)] - pub foreground: BackgroundColor, + pub foreground: FillColor, /// Background color #[setters(into)] - pub background: BackgroundColor, + pub background: FillColor, /// Corner radius of the progress bar #[setters(into)] diff --git a/hui/src/lib.rs b/hui/src/lib.rs index db862d6..a23165f 100644 --- a/hui/src/lib.rs +++ b/hui/src/lib.rs @@ -13,7 +13,6 @@ mod instance; mod macros; pub mod layout; pub mod rect; -pub mod background; pub mod element; pub mod event; pub mod input; diff --git a/hui/src/rect.rs b/hui/src/rect.rs index d26f7a6..4278449 100644 --- a/hui/src/rect.rs +++ b/hui/src/rect.rs @@ -1,5 +1,7 @@ //! contains types which represent the sides and corners of a rectangular shape. +//XXX: this is kinda a mess, either move the rect struct here or come up with a better name for this module +#[allow(clippy::module_inception)] mod rect; pub use rect::Rect; diff --git a/hui/src/rect/color.rs b/hui/src/rect/color.rs index 35eeffe..1933b0e 100644 --- a/hui/src/rect/color.rs +++ b/hui/src/rect/color.rs @@ -50,9 +50,14 @@ impl FillColor { }) } - /// Construct a simple gradient fill from four colors representing the corners of the rectangle - pub const fn corners(top_left: Vec4, top_right: Vec4, bottom_left: Vec4, bottom_right: Vec4) -> Self { - Self(Corners { top_left, top_right, bottom_left, bottom_right }) + /// Construct a solid color fill from colors for each corner + pub const fn from_corners(corners: Corners) -> Self { + Self(corners) + } + + /// Get a list of the colors for each corner + pub const fn corners(&self) -> Corners { + self.0 } }