use bevy::prelude::*;
use crate::draw::{Draw, drawing};
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Properties {
pub point: Vec3,
}
pub trait SetPosition: Sized {
fn properties(&mut self) -> &mut Properties;
fn x(mut self, x: f32) -> Self {
self.properties().point.x = x;
self
}
fn y(mut self, y: f32) -> Self {
self.properties().point.y = y;
self
}
fn z(mut self, z: f32) -> Self {
self.properties().point.z = z;
self
}
fn xy(self, p: Vec2) -> Self {
self.x(p.x).y(p.y)
}
fn xyz(self, p: Vec3) -> Self {
self.x(p.x).y(p.y).z(p.z)
}
fn x_y(self, x: f32, y: f32) -> Self {
self.xy([x, y].into())
}
fn x_y_z(self, x: f32, y: f32, z: f32) -> Self {
self.xyz([x, y, z].into())
}
}
impl Properties {
pub fn transform(&self) -> Mat4 {
Mat4::from_translation(self.point.into())
}
}
impl SetPosition for Properties {
fn properties(&mut self) -> &mut Properties {
self
}
}
impl Default for Properties {
fn default() -> Self {
let point = Vec3::ZERO;
Self { point }
}
}
pub(crate) fn set_position(
draw: &Draw,
index: usize,
x: Option<f32>,
y: Option<f32>,
z: Option<f32>,
) {
drawing::with_primitive(draw, index, |prim| match prim.position_mut() {
Some(props) => {
if let Some(x) = x {
props.point.x = x;
}
if let Some(y) = y {
props.point.y = y;
}
if let Some(z) = z {
props.point.z = z;
}
}
None => bevy::log::warn_once!("drawing primitive does not support `position`"),
})
}