use crate::prelude::Interpolator;
use bevy::prelude::*;
#[derive(Debug, Default, Clone, PartialEq, Reflect)]
pub struct BackgroundColor {
#[allow(missing_docs)]
pub start: Color,
#[allow(missing_docs)]
pub end: Color,
pub delta: bool
}
impl Interpolator for BackgroundColor {
type Item = bevy::prelude::BackgroundColor;
fn interpolate(&self, item: &mut Self::Item, value: f32, previous_value: f32) {
if self.delta{
let previous_color_as_vec = self.start.mix(&self.end, previous_value).to_linear();
let next_color_as_vec = self.start.mix(&self.end, value).to_linear();
let updated_color = item.0.to_linear() + (next_color_as_vec - previous_color_as_vec);
item.0 = updated_color.into();
}else{
item.0 = self.start.mix(&self.end, value)
}
}
}
pub fn background_color(start: Color, end: Color) -> BackgroundColor {
BackgroundColor { start, end, delta: false }
}
pub fn background_color_to(
to: Color,
) -> impl Fn(&mut Color) -> BackgroundColor {
move |state| {
let start = *state;
let end = to;
*state = to;
background_color(start, end)
}
}
pub fn background_color_delta_to(
to: Color,
) -> impl Fn(&mut Color) -> BackgroundColor {
move |state| {
let start = *state;
let end = to;
*state = to;
BackgroundColor {start, end, delta: true}
}
}
#[derive(Debug, Default, Clone, PartialEq, Reflect)]
pub struct BorderColor {
#[allow(missing_docs)]
pub start: Color,
#[allow(missing_docs)]
pub end: Color,
pub delta: bool
}
impl Interpolator for BorderColor {
type Item = bevy::prelude::BorderColor;
fn interpolate(&self, item: &mut Self::Item, value: f32, previous_value: f32) {
for color in [&mut item.top, &mut item.right, &mut item.bottom, &mut item.left]{
if self.delta {
let previous_color_as_vec = self.start.mix(&self.end, previous_value).to_linear();
let next_color_as_vec = self.start.mix(&self.end, value).to_linear();
let updated_color = color.to_linear() + (next_color_as_vec - previous_color_as_vec);
*color = updated_color.into();
}else{
*color = self.start.mix(&self.end, value)
}
}
}
}
pub fn border_color(start: Color, end: Color) -> BorderColor {
BorderColor { start, end, delta: false }
}
pub fn border_color_to(to: Color) -> impl Fn(&mut Color) -> BorderColor {
move |state| {
let start = *state;
let end = to;
*state = to;
border_color(start, end)
}
}
pub fn border_color_delta_to(to: Color) -> impl Fn(&mut Color) -> BorderColor {
move |state| {
let start = *state;
let end = to;
*state = to;
BorderColor {start, end, delta: true}
}
}