Skip to main content

rscad_core/transformations/
offset.rs

1use crate::prelude_::*;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
6pub struct Offset<T: Object> {
7    pub object: T,
8    pub offset: f64,
9    pub offset_type: OffsetType,
10}
11
12#[derive(Clone, Copy, Debug, Default, PartialEq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
15pub enum OffsetType {
16    #[default]
17    Normal,
18    Rounded,
19}
20
21impl<T: Object> Offset<T> {
22    pub fn new<O: Number>(object: T, offset: O, offset_type: OffsetType) -> Self {
23        Self {
24            object,
25            offset: offset.to_f64(),
26            offset_type,
27        }
28    }
29}
30
31pub trait OffsetObject: Object {
32    fn offset<O: Number>(self, offset: O) -> Offset<Self>
33    where
34        Self: Sized,
35    {
36        Offset::new(self, offset, OffsetType::Normal)
37    }
38
39    fn offset_rounded<O: Number>(self, offset: O) -> Offset<Self>
40    where
41        Self: Sized,
42    {
43        Offset::new(self, offset, OffsetType::Rounded)
44    }
45}
46
47impl<T: TwoDimensional> OffsetObject for T {}
48
49impl<T: Object> Object for Offset<T> {}
50
51impl<T: Object> TwoDimensional for Offset<T> where T: TwoDimensional {}