Skip to content
Snippets Groups Projects
bevy_tweenables.rs 2.29 KiB
Newer Older
Louis's avatar
Louis committed
use crate::Tweenable;
use bevy_color::{Color, ColorToComponents};
use bevy_derive::{Deref, DerefMut};
use bevy_math::curve::CurveExt;
use bevy_math::curve::Ease;
use bevy_math::{Curve, Vec3, Vec4};
use bevy_sprite::Sprite;
use bevy_text::TextColor;
use bevy_transform::components::Transform;
use bevy_ui::widget::ImageNode;

#[derive(Deref, DerefMut, Clone)]
#[repr(transparent)]
pub struct TweenableColour(pub Color);
impl From<Color> for TweenableColour {
	fn from(value: Color) -> Self {
		Self(value)
	}
}
impl Ease for TweenableColour {
	fn interpolating_curve_unbounded(start: Self, end: Self) -> impl Curve<Self> {
		let start = start.to_srgba().to_vec4();
		let end = end.to_srgba().to_vec4();

		<Vec4 as Ease>::interpolating_curve_unbounded(start, end).map(|components| {
			TweenableColour::from(Color::srgba(
				components.x,
				components.y,
				components.z,
				components.w,
			))
		})
	}
}

pub struct TweenSpriteColour;
impl Tweenable for TweenSpriteColour {
	type Comp = Sprite;
	type Data = TweenableColour;

	fn current_value(cmp: &Self::Comp) -> Self::Data {
		cmp.color.into()
	}

	fn update_component(cmp: &mut Self::Comp, value: Self::Data) {
		cmp.color = *value;
	}
}

pub struct TweenImageNodeColour;
impl Tweenable for TweenImageNodeColour {
	type Comp = ImageNode;
	type Data = TweenableColour;
	fn current_value(cmp: &Self::Comp) -> Self::Data {
		cmp.color.into()
	}
	fn update_component(cmp: &mut Self::Comp, value: Self::Data) {
		cmp.color = *value;
	}
}

pub struct TweenTextColour;
impl Tweenable for TweenTextColour {
	type Comp = TextColor;
	type Data = TweenableColour;
	fn current_value(cmp: &Self::Comp) -> Self::Data {
		cmp.0.into()
	}
	fn update_component(cmp: &mut Self::Comp, value: Self::Data) {
		cmp.0 = *value;
	}
}

pub struct TweenTransformTranslation;
impl Tweenable for TweenTransformTranslation {
	type Comp = Transform;
	type Data = Vec3;
	fn current_value(cmp: &Self::Comp) -> Self::Data {
		cmp.translation
	}
	fn update_component(cmp: &mut Self::Comp, value: Self::Data) {
		cmp.translation = value;
	}
}

pub struct TweenTransformScale;
impl Tweenable for TweenTransformScale {
	type Comp = Transform;
	type Data = Vec3;
	fn current_value(cmp: &Self::Comp) -> Self::Data {
		cmp.scale
	}
	fn update_component(cmp: &mut Self::Comp, value: Self::Data) {
		cmp.scale = value;
	}
}