change bg color api again

This commit is contained in:
griffi-gh 2024-03-07 00:50:47 +01:00
parent 5dc97ae2b0
commit 1c55b1217b
4 changed files with 26 additions and 34 deletions

View file

@ -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<Vec4>),
}
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<Corners<Vec4>> for RectBackground {
impl From<Corners<Vec4>> for BackgroundColor {
fn from(corners: Corners<Vec4>) -> Self {
Self::Gradient(corners)
}
}
impl From<Option<Vec4>> for RectBackground {
impl From<Option<Vec4>> for BackgroundColor {
fn from(color: Option<Vec4>) -> Self {
match color {
Some(color) => Self::Solid(color),
@ -39,19 +39,19 @@ impl From<Option<Vec4>> for RectBackground {
}
}
impl From<Vec4> for RectBackground {
impl From<Vec4> 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<Corners<Vec3>> for RectBackground {
impl From<Corners<Vec3>> for BackgroundColor {
fn from(corners: Corners<Vec3>) -> Self {
Self::Gradient(Corners {
top_left: corners.top_left.extend(1.),
@ -62,7 +62,7 @@ impl From<Corners<Vec3>> for RectBackground {
}
}
impl From<Option<Vec3>> for RectBackground {
impl From<Option<Vec3>> for BackgroundColor {
fn from(color: Option<Vec3>) -> Self {
match color {
Some(color) => Self::Solid(color.extend(1.)),
@ -71,21 +71,19 @@ impl From<Option<Vec3>> for RectBackground {
}
}
impl From<Vec3> for RectBackground {
impl From<Vec3> 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<Corners<Vec4>> {
impl BackgroundColor {
/// Returns the colors of individual corners
pub fn corners(&self) -> Corners<Vec4> {
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)
// }
// }

View file

@ -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,

View file

@ -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)

View file

@ -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,
});