ranim_core/traits/transform/
shift.rs1use glam::DVec3;
2
3use crate::anchor::{Aabb, AabbPoint, Locate};
4
5pub trait ShiftTransform {
10 fn shift(&mut self, offset: DVec3) -> &mut Self;
12}
13
14pub trait ShiftTransformExt: ShiftTransform {
18 fn with_origin(&mut self, p: impl Locate<Self>, f: impl FnOnce(&mut Self)) -> &mut Self {
22 let p = p.locate(self);
23 self.shift(-p);
24 f(self);
25 self.shift(p)
26 }
27 fn move_anchor_to<A>(&mut self, anchor: A, point: DVec3) -> &mut Self
31 where
32 A: Locate<Self>,
33 {
34 self.shift(point - anchor.locate(self));
35 self
36 }
37 fn move_to(&mut self, point: DVec3) -> &mut Self
39 where
40 AabbPoint: Locate<Self>,
41 {
42 self.move_anchor_to(AabbPoint::CENTER, point)
43 }
44 fn move_next_to<T: Aabb + ?Sized>(&mut self, target: &T, anchor: AabbPoint) -> &mut Self
46 where
47 AabbPoint: Locate<Self>,
48 {
49 self.move_next_to_padded(target, anchor, 0.0)
50 }
51 fn move_next_to_padded<T: Aabb + ?Sized>(
53 &mut self,
54 target: &T,
55 anchor: AabbPoint,
56 padding: f64,
57 ) -> &mut Self
58 where
59 AabbPoint: Locate<Self>,
60 {
61 let neg_anchor = AabbPoint(-anchor.0);
62 self.move_anchor_to(
63 neg_anchor,
64 Locate::<T>::locate(&anchor, target) + anchor.0.normalize() * padding,
65 )
66 }
67}
68
69impl<T: ShiftTransform + ?Sized> ShiftTransformExt for T {}