remove deprecated BackgroundColor struct

This commit is contained in:
griffi-gh 2024-03-21 23:31:49 +01:00
parent 02558e0695
commit d36a78d2ee
8 changed files with 22 additions and 135 deletions

View file

@ -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<TextureH>
// }
//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<Vec4>),
}
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<Corners<Vec4>> for BackgroundColor {
fn from(corners: Corners<Vec4>) -> Self {
Self::Gradient(corners)
}
}
impl From<Option<Vec4>> for BackgroundColor {
fn from(color: Option<Vec4>) -> Self {
match color {
Some(color) => Self::Solid(color),
None => Self::Transparent,
}
}
}
impl From<Vec4> 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<Corners<Vec3>> for BackgroundColor {
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 BackgroundColor {
fn from(color: Option<Vec3>) -> Self {
match color {
Some(color) => Self::Solid(color.extend(1.)),
None => Self::Transparent,
}
}
}
impl From<Vec3> 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<Vec4> {
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.
},
}
}
}

View file

@ -3,12 +3,11 @@
use derive_setters::Setters; use derive_setters::Setters;
use glam::{Vec2, vec2}; use glam::{Vec2, vec2};
use crate::{ use crate::{
background::BackgroundColor,
draw::{ImageHandle, RoundedCorners, UiDrawCommand}, draw::{ImageHandle, RoundedCorners, UiDrawCommand},
element::{ElementList, MeasureContext, ProcessContext, UiElement}, element::{ElementList, MeasureContext, ProcessContext, UiElement},
layout::{Alignment, Alignment2d, Direction, LayoutInfo, Size, Size2d}, layout::{Alignment, Alignment2d, Direction, LayoutInfo, Size, Size2d},
measure::{Hints, Response}, measure::{Hints, Response},
rect::{Corners, Sides}, rect::{Corners, FillColor, Sides},
}; };
// pub struct Border { // pub struct Border {
@ -59,7 +58,7 @@ pub struct Container {
/// ///
/// If the container has a background texture, it will be multiplied by this color /// If the container has a background texture, it will be multiplied by this color
#[setters(into)] #[setters(into)]
pub background: BackgroundColor, pub background: FillColor,
/// Background texture of the container /// Background texture of the container
/// ///

View file

@ -3,12 +3,11 @@
use derive_setters::Setters; use derive_setters::Setters;
use glam::{vec2, Vec4}; use glam::{vec2, Vec4};
use crate::{ use crate::{
background::BackgroundColor, draw::{RoundedCorners, UiDrawCommand},
draw::{UiDrawCommand, RoundedCorners}, element::{MeasureContext, ProcessContext, UiElement},
element::{UiElement, MeasureContext, ProcessContext},
layout::{Size, Size2d}, layout::{Size, Size2d},
measure::Response, measure::Response,
rect::Corners, rect::{Corners, FillColor},
size, size,
}; };
@ -22,7 +21,7 @@ pub struct FillRect {
/// Background color of the rectangle /// Background color of the rectangle
#[setters(into)] #[setters(into)]
pub background: BackgroundColor, pub background: FillColor,
/// Corner radius of the rectangle /// Corner radius of the rectangle
#[setters(into)] #[setters(into)]

View file

@ -1,12 +1,11 @@
use derive_setters::Setters; use derive_setters::Setters;
use glam::vec2; use glam::vec2;
use crate::{ use crate::{
background::BackgroundColor,
draw::{ImageHandle, RoundedCorners, UiDrawCommand}, draw::{ImageHandle, RoundedCorners, UiDrawCommand},
element::{MeasureContext, ProcessContext, UiElement}, element::{MeasureContext, ProcessContext, UiElement},
layout::{compute_size, Size, Size2d}, layout::{compute_size, Size, Size2d},
measure::Response, measure::Response,
rect::Corners, rect::{Corners, FillColor},
}; };
#[derive(Setters)] #[derive(Setters)]
@ -29,7 +28,7 @@ pub struct Image {
/// ///
/// Image will get multiplied/tinted by this color or gradient /// Image will get multiplied/tinted by this color or gradient
#[setters(into)] #[setters(into)]
pub color: BackgroundColor, pub color: FillColor,
/// Corner radius of the image /// Corner radius of the image
#[setters(into)] #[setters(into)]
@ -44,7 +43,7 @@ impl Image {
width: Size::Auto, width: Size::Auto,
height: Size::Auto, height: Size::Auto,
}, },
color: BackgroundColor::from((1., 1., 1., 1.)), color: (1., 1., 1.).into(),
corner_radius: Corners::all(0.), corner_radius: Corners::all(0.),
} }
} }

View file

@ -1,12 +1,11 @@
use derive_setters::Setters; use derive_setters::Setters;
use glam::{vec2, vec4}; use glam::{vec2, vec4};
use crate::{ use crate::{
background::BackgroundColor,
draw::{RoundedCorners, UiDrawCommand}, draw::{RoundedCorners, UiDrawCommand},
element::{MeasureContext, ProcessContext, UiElement}, element::{MeasureContext, ProcessContext, UiElement},
layout::{compute_size, Size, Size2d}, layout::{compute_size, Size, Size2d},
measure::Response, measure::Response,
rect::Corners rect::{Corners, FillColor}
}; };
#[derive(Debug, Clone, Copy, Setters)] #[derive(Debug, Clone, Copy, Setters)]
@ -21,11 +20,11 @@ pub struct ProgressBar {
/// Foreground (bar) color /// Foreground (bar) color
#[setters(into)] #[setters(into)]
pub foreground: BackgroundColor, pub foreground: FillColor,
/// Background color /// Background color
#[setters(into)] #[setters(into)]
pub background: BackgroundColor, pub background: FillColor,
/// Corner radius of the progress bar /// Corner radius of the progress bar
#[setters(into)] #[setters(into)]

View file

@ -13,7 +13,6 @@ mod instance;
mod macros; mod macros;
pub mod layout; pub mod layout;
pub mod rect; pub mod rect;
pub mod background;
pub mod element; pub mod element;
pub mod event; pub mod event;
pub mod input; pub mod input;

View file

@ -1,5 +1,7 @@
//! contains types which represent the sides and corners of a rectangular shape. //! 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; mod rect;
pub use rect::Rect; pub use rect::Rect;

View file

@ -50,9 +50,14 @@ impl FillColor {
}) })
} }
/// Construct a simple gradient fill from four colors representing the corners of the rectangle /// Construct a solid color fill from colors for each corner
pub const fn corners(top_left: Vec4, top_right: Vec4, bottom_left: Vec4, bottom_right: Vec4) -> Self { pub const fn from_corners(corners: Corners<Vec4>) -> Self {
Self(Corners { top_left, top_right, bottom_left, bottom_right }) Self(corners)
}
/// Get a list of the colors for each corner
pub const fn corners(&self) -> Corners<Vec4> {
self.0
} }
} }