use std::marker::PhantomData;
use bevy::{
ecs::{component::Component, event::Event},
math::{UVec2, Vec2},
};
#[derive(Clone, Copy, Debug, Event)]
pub struct TransformGridEvent<Marker: Component, const N: usize = 4> {
pub(crate) dimensions: Option<UVec2>,
pub(crate) spacing: Option<Vec2>,
pub(crate) anchor: Option<Vec2>,
marker: PhantomData<Marker>,
}
impl<Marker: Component, const N: usize> TransformGridEvent<Marker, N> {
pub fn dimensions(&self) -> Option<UVec2> {
self.dimensions
}
pub fn spacing(&self) -> Option<Vec2> {
self.spacing
}
pub fn anchor(&self) -> Option<Vec2> {
self.anchor
}
pub fn with_dimensions(mut self, value: impl Into<UVec2>) -> Self {
self.dimensions = Some(value.into());
self
}
pub fn with_spacing(mut self, value: impl Into<Vec2>) -> Self {
self.spacing = Some(value.into());
self
}
pub fn with_anchor(mut self, value: impl Into<Vec2>) -> Self {
self.anchor = Some(value.into());
self
}
}
impl<Marker: Component, const N: usize> Default for TransformGridEvent<Marker, N> {
fn default() -> Self {
Self {
dimensions: Some(UVec2::ONE),
spacing: Some(Vec2::ONE),
anchor: Some(Vec2::ZERO),
marker: PhantomData,
}
}
}
impl<Marker: Component, const N: usize> std::fmt::Display for TransformGridEvent<Marker, N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let dimensions: String = self
.dimensions
.map(|d| format!("({}, {})", d.x, d.y))
.unwrap_or("none".to_string());
let spacing: String = self
.spacing
.map(|s| format!("({}, {})", s.x, s.y))
.unwrap_or("none".to_string());
let anchor: String = self
.anchor
.map(|a| format!("({}, {})", a.x, a.y))
.unwrap_or("none".to_string());
write!(
f,
"TransformGridEvent {{ dimensions={dimensions} spacing={spacing} anchor={anchor} }}"
)
}
}