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? //TODO: move this into the color module?
#[derive(Clone, Copy, Default, Debug, PartialEq)] #[derive(Clone, Copy, Default, Debug, PartialEq)]
pub enum RectBackground { pub enum BackgroundColor {
#[default] #[default]
Transparent, Transparent,
Solid(Vec4), Solid(Vec4),
Gradient(Corners<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 { fn from(color: (f32, f32, f32, f32)) -> Self {
Self::Solid(vec4(color.0, color.1, color.2, color.3)) 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 { fn from(corners: Corners<Vec4>) -> Self {
Self::Gradient(corners) Self::Gradient(corners)
} }
} }
impl From<Option<Vec4>> for RectBackground { impl From<Option<Vec4>> for BackgroundColor {
fn from(color: Option<Vec4>) -> Self { fn from(color: Option<Vec4>) -> Self {
match color { match color {
Some(color) => Self::Solid(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 { fn from(color: Vec4) -> Self {
Self::Solid(color) Self::Solid(color)
} }
} }
impl From<(f32, f32, f32)> for RectBackground { impl From<(f32, f32, f32)> for BackgroundColor {
fn from(color: (f32, f32, f32)) -> Self { fn from(color: (f32, f32, f32)) -> Self {
Self::Solid(vec4(color.0, color.1, color.2, 1.)) 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 { fn from(corners: Corners<Vec3>) -> Self {
Self::Gradient(Corners { Self::Gradient(Corners {
top_left: corners.top_left.extend(1.), 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 { fn from(color: Option<Vec3>) -> Self {
match color { match color {
Some(color) => Self::Solid(color.extend(1.)), 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 { fn from(color: Vec3) -> Self {
Self::Solid(color.extend(1.)) Self::Solid(color.extend(1.))
} }
} }
impl RectBackground { impl BackgroundColor {
/// Currently, never returns None.\ /// Returns the colors of individual corners
/// `Option` has been added in preparation for future changes.\ pub fn corners(&self) -> Corners<Vec4> {
/// (`Background::Texture` etc)
pub fn corners(&self) -> Option<Corners<Vec4>> {
match *self { match *self {
Self::Transparent => Some(Corners::all(Vec4::ZERO)), Self::Transparent => Corners::all(Vec4::ZERO),
Self::Solid(color) => Some(Corners::all(color)), Self::Solid(color) => Corners::all(color),
Self::Gradient(corners) => Some(corners), 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 derive_setters::Setters;
use glam::{Vec2, vec2}; use glam::{Vec2, vec2};
use crate::{ use crate::{
background::RectBackground, background::BackgroundColor,
draw::{RoundedCorners, UiDrawCommand}, draw::{RoundedCorners, UiDrawCommand},
element::{ElementList, MeasureContext, ProcessContext, UiElement}, element::{ElementList, MeasureContext, ProcessContext, UiElement},
layout::{Alignment, Alignment2d, LayoutInfo, UiDirection, Size, Size2d}, layout::{Alignment, Alignment2d, LayoutInfo, UiDirection, Size, Size2d},
@ -57,7 +57,7 @@ pub struct Container {
/// Background color of the container /// Background color of the container
#[setters(into)] #[setters(into)]
pub background: RectBackground, pub background: BackgroundColor,
/// Corner radius of the background rectangle /// Corner radius of the background rectangle
#[setters(into)] #[setters(into)]
@ -311,7 +311,7 @@ impl UiElement for Container {
//background //background
if !self.background.is_transparent() { if !self.background.is_transparent() {
let corner_colors = self.background.corners().unwrap(); let corner_colors = self.background.corners();
ctx.draw.add(UiDrawCommand::Rectangle { ctx.draw.add(UiDrawCommand::Rectangle {
position, position,
size: ctx.measure.size, size: ctx.measure.size,

View file

@ -3,7 +3,7 @@
use derive_setters::Setters; use derive_setters::Setters;
use glam::{vec2, Vec4}; use glam::{vec2, Vec4};
use crate::{ use crate::{
background::RectBackground, background::BackgroundColor,
draw::{UiDrawCommand, RoundedCorners}, draw::{UiDrawCommand, RoundedCorners},
element::{UiElement, MeasureContext, ProcessContext}, element::{UiElement, MeasureContext, ProcessContext},
layout::{Size, Size2d}, layout::{Size, Size2d},
@ -22,7 +22,7 @@ pub struct FillRect {
/// Background color of the rectangle /// Background color of the rectangle
#[setters(into)] #[setters(into)]
pub background: RectBackground, pub background: BackgroundColor,
/// Corner radius of the rectangle /// Corner radius of the rectangle
#[setters(into)] #[setters(into)]
@ -67,7 +67,7 @@ impl UiElement for FillRect {
ctx.draw.add(UiDrawCommand::Rectangle { ctx.draw.add(UiDrawCommand::Rectangle {
position: ctx.layout.position, position: ctx.layout.position,
size: ctx.measure.size, size: ctx.measure.size,
color: self.background.corners().unwrap(), color: self.background.corners(),
texture: None, texture: None,
rounded_corners: (self.corner_radius.max_f32() > 0.).then_some({ rounded_corners: (self.corner_radius.max_f32() > 0.).then_some({
RoundedCorners::from_radius(self.corner_radius) RoundedCorners::from_radius(self.corner_radius)

View file

@ -1,7 +1,7 @@
use derive_setters::Setters; use derive_setters::Setters;
use glam::{vec2, vec4}; use glam::{vec2, vec4};
use crate::{ use crate::{
background::RectBackground, background::BackgroundColor,
draw::{RoundedCorners, UiDrawCommand}, draw::{RoundedCorners, UiDrawCommand},
element::{MeasureContext, ProcessContext, UiElement}, element::{MeasureContext, ProcessContext, UiElement},
layout::{Size, Size2d}, layout::{Size, Size2d},
@ -21,11 +21,11 @@ pub struct ProgressBar {
/// Foreground (bar) color /// Foreground (bar) color
#[setters(into)] #[setters(into)]
pub foreground: RectBackground, pub foreground: BackgroundColor,
/// Background color /// Background color
#[setters(into)] #[setters(into)]
pub background: RectBackground, pub background: BackgroundColor,
/// Corner radius of the progress bar /// Corner radius of the progress bar
#[setters(into)] #[setters(into)]
@ -94,7 +94,7 @@ impl UiElement for ProgressBar {
ctx.draw.add(UiDrawCommand::Rectangle { ctx.draw.add(UiDrawCommand::Rectangle {
position: ctx.layout.position, position: ctx.layout.position,
size: ctx.measure.size, size: ctx.measure.size,
color: self.background.corners().unwrap(), color: self.background.corners(),
texture: None, texture: None,
rounded_corners rounded_corners
}); });
@ -103,7 +103,7 @@ impl UiElement for ProgressBar {
ctx.draw.add(UiDrawCommand::Rectangle { ctx.draw.add(UiDrawCommand::Rectangle {
position: ctx.layout.position, position: ctx.layout.position,
size: ctx.measure.size * vec2(value, 1.0), size: ctx.measure.size * vec2(value, 1.0),
color: self.foreground.corners().unwrap(), color: self.foreground.corners(),
texture: None, texture: None,
rounded_corners, rounded_corners,
}); });