nannou/draw/properties/spatial/
position.rs

1//! Items related to describing positioning along each axis as
2
3use crate::geom::{Point2, Point3};
4use crate::glam::Mat4;
5
6/// Position properties for **Drawing** a **Primitive**.
7#[derive(Copy, Clone, Debug, PartialEq)]
8pub struct Properties {
9    pub point: Point3,
10}
11
12/// An API for setting the **position::Properties**.
13pub trait SetPosition: Sized {
14    /// Provide a mutable reference to the **position::Properties** for updating.
15    fn properties(&mut self) -> &mut Properties;
16
17    /// Build with the given **Absolute** **Position** along the *x* axis.
18    fn x(mut self, x: f32) -> Self {
19        self.properties().point.x = x;
20        self
21    }
22
23    /// Build with the given **Absolute** **Position** along the *y* axis.
24    fn y(mut self, y: f32) -> Self {
25        self.properties().point.y = y;
26        self
27    }
28
29    /// Build with the given **Absolute** **Position** along the *z* axis.
30    fn z(mut self, z: f32) -> Self {
31        self.properties().point.z = z;
32        self
33    }
34
35    /// Set the **Position** with some two-dimensional point.
36    fn xy(self, p: Point2) -> Self {
37        self.x(p.x).y(p.y)
38    }
39
40    /// Set the **Position** with some three-dimensional point.
41    fn xyz(self, p: Point3) -> Self {
42        self.x(p.x).y(p.y).z(p.z)
43    }
44
45    /// Set the **Position** with *x* *y* coordinates.
46    fn x_y(self, x: f32, y: f32) -> Self {
47        self.xy([x, y].into())
48    }
49
50    /// Set the **Position** with *x* *y* *z* coordinates.
51    fn x_y_z(self, x: f32, y: f32, z: f32) -> Self {
52        self.xyz([x, y, z].into())
53    }
54}
55
56impl Properties {
57    pub fn transform(&self) -> Mat4 {
58        Mat4::from_translation(self.point.into())
59    }
60}
61
62impl SetPosition for Properties {
63    fn properties(&mut self) -> &mut Properties {
64        self
65    }
66}
67
68impl Default for Properties {
69    fn default() -> Self {
70        let point = Point3::ZERO;
71        Self { point }
72    }
73}