use crate::Val;
use crate::ValArithmeticError;
use crate::ValNum;
use bevy_derive::Deref;
use bevy_ecs::component::Component;
use bevy_ecs::prelude::ReflectComponent;
use bevy_math::Affine2;
use bevy_math::Mat2;
use bevy_math::Rot2;
use bevy_math::Vec2;
use bevy_reflect::prelude::*;
use core::ops::Mul;
#[derive(Debug, PartialEq, Clone, Copy, Reflect)]
#[reflect(Default, PartialEq, Debug, Clone)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct Val2 {
pub x: Val,
pub y: Val,
}
impl Val2 {
pub const ZERO: Self = Self {
x: Val::ZERO,
y: Val::ZERO,
};
pub const fn new(x: Val, y: Val) -> Self {
Self { x, y }
}
pub const fn all(val: Val) -> Self {
Self::new(val, val)
}
pub fn px<X: ValNum, Y: ValNum>(x: X, y: Y) -> Self {
Self::new(Val::Px(x.val_num_f32()), Val::Px(y.val_num_f32()))
}
pub fn percent<X: ValNum, Y: ValNum>(x: X, y: Y) -> Self {
Self::new(Val::Percent(x.val_num_f32()), Val::Percent(y.val_num_f32()))
}
pub fn resolve(&self, scale_factor: f32, base_size: Vec2, viewport_size: Vec2) -> Vec2 {
Vec2::new(
self.x
.resolve(scale_factor, base_size.x, viewport_size)
.unwrap_or(0.),
self.y
.resolve(scale_factor, base_size.y, viewport_size)
.unwrap_or(0.),
)
}
pub fn try_add(self, other: Val2) -> Result<Self, ValArithmeticError> {
let (Ok(x), Ok(y)) = (self.x.try_add(other.x), self.y.try_add(other.y)) else {
return Err(ValArithmeticError::IncompatibleUnits);
};
Ok(Self { x, y })
}
pub fn try_sub(self, other: Val2) -> Result<Self, ValArithmeticError> {
let (Ok(x), Ok(y)) = (self.x.try_sub(other.x), self.y.try_sub(other.y)) else {
return Err(ValArithmeticError::IncompatibleUnits);
};
Ok(Self { x, y })
}
}
impl Default for Val2 {
fn default() -> Self {
Self::ZERO
}
}
#[derive(Component, Debug, PartialEq, Clone, Copy, Reflect)]
#[reflect(Component, Default, PartialEq, Debug, Clone)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
#[require(UiGlobalTransform)]
pub struct UiTransform {
pub translation: Val2,
pub scale: Vec2,
pub rotation: Rot2,
}
impl UiTransform {
pub const IDENTITY: Self = Self {
translation: Val2::ZERO,
scale: Vec2::ONE,
rotation: Rot2::IDENTITY,
};
pub const fn from_rotation(rotation: Rot2) -> Self {
Self {
rotation,
..Self::IDENTITY
}
}
pub const fn from_translation(translation: Val2) -> Self {
Self {
translation,
..Self::IDENTITY
}
}
pub const fn from_scale(scale: Vec2) -> Self {
Self {
scale,
..Self::IDENTITY
}
}
pub const fn from_xy(x: Val, y: Val) -> Self {
Self {
translation: Val2::new(x, y),
..Self::IDENTITY
}
}
pub fn compute_affine(&self, scale_factor: f32, base_size: Vec2, target_size: Vec2) -> Affine2 {
Affine2::from_mat2_translation(
Mat2::from(self.rotation) * Mat2::from_diagonal(self.scale),
self.translation
.resolve(scale_factor, base_size, target_size),
)
}
}
impl Default for UiTransform {
fn default() -> Self {
Self::IDENTITY
}
}
#[derive(Component, Debug, PartialEq, Clone, Copy, Reflect, Deref)]
#[reflect(Component, Default, PartialEq, Debug, Clone)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct UiGlobalTransform(Affine2);
impl Default for UiGlobalTransform {
fn default() -> Self {
Self(Affine2::IDENTITY)
}
}
impl UiGlobalTransform {
#[inline]
pub fn try_inverse(&self) -> Option<Affine2> {
(self.matrix2.determinant() != 0.).then_some(self.inverse())
}
#[inline]
pub fn from_translation(translation: Vec2) -> Self {
Self(Affine2::from_translation(translation))
}
#[inline]
pub fn from_xy(x: f32, y: f32) -> Self {
Self::from_translation(Vec2::new(x, y))
}
#[inline]
pub fn from_rotation(rotation: Rot2) -> Self {
Self(Affine2::from_mat2(rotation.into()))
}
#[inline]
pub fn from_scale(scale: Vec2) -> Self {
Self(Affine2::from_scale(scale))
}
#[inline]
pub fn to_scale_angle_translation(&self) -> (Vec2, f32, Vec2) {
self.0.to_scale_angle_translation()
}
#[inline]
pub fn affine(&self) -> Affine2 {
self.0
}
}
impl From<Affine2> for UiGlobalTransform {
fn from(value: Affine2) -> Self {
Self(value)
}
}
impl From<UiGlobalTransform> for Affine2 {
fn from(value: UiGlobalTransform) -> Self {
value.0
}
}
impl From<&UiGlobalTransform> for Affine2 {
fn from(value: &UiGlobalTransform) -> Self {
value.0
}
}
impl Mul for UiGlobalTransform {
type Output = Self;
#[inline]
fn mul(self, value: Self) -> Self::Output {
Self(self.0 * value.0)
}
}
impl Mul<Affine2> for UiGlobalTransform {
type Output = Affine2;
#[inline]
fn mul(self, affine2: Affine2) -> Self::Output {
self.0 * affine2
}
}
impl Mul<UiGlobalTransform> for Affine2 {
type Output = Affine2;
#[inline]
fn mul(self, transform: UiGlobalTransform) -> Self::Output {
self * transform.0
}
}
impl Mul<Vec2> for UiGlobalTransform {
type Output = Vec2;
#[inline]
fn mul(self, value: Vec2) -> Vec2 {
self.transform_point2(value)
}
}