dolly/drivers/
lock_position.rs

1use std::marker::PhantomData;
2
3use crate::{
4    driver::RigDriver, handedness::Handedness, rig::RigUpdateParams, transform::Transform,
5};
6
7/// Locks/constrains the position of the camera to one or more axes
8#[derive(Debug)]
9pub struct LockPosition {
10    x: Option<f32>,
11    y: Option<f32>,
12    z: Option<f32>,
13}
14
15impl LockPosition {
16    pub fn new() -> Self {
17        Self {
18            x: None,
19            y: None,
20            z: None,
21        }
22    }
23    pub fn from(x: Option<f32>, y: Option<f32>, z: Option<f32>) -> Self {
24        Self { x, y, z }
25    }
26    pub fn x(&self, x: f32) -> Self {
27        Self {
28            x: Some(x),
29            y: self.y,
30            z: self.z,
31        }
32    }
33    pub fn y(&self, y: f32) -> Self {
34        Self {
35            x: self.x,
36            y: Some(y),
37            z: self.z,
38        }
39    }
40    pub fn z(&self, z: f32) -> Self {
41        Self {
42            x: self.x,
43            y: self.y,
44            z: Some(z),
45        }
46    }
47}
48
49impl Default for LockPosition {
50    fn default() -> Self {
51        Self::new()
52    }
53}
54
55impl<H: Handedness> RigDriver<H> for LockPosition {
56    fn update(&mut self, params: RigUpdateParams<H>) -> Transform<H> {
57        let mut delta_pos = params.parent.position;
58        delta_pos.x = self.x.unwrap_or(delta_pos.x);
59        delta_pos.y = self.y.unwrap_or(delta_pos.y);
60        delta_pos.z = self.z.unwrap_or(delta_pos.z);
61        Transform {
62            position: delta_pos,
63            rotation: params.parent.rotation,
64            phantom: PhantomData,
65        }
66    }
67}