Skip to main content

ascending_camera/
camera.rs

1use glam::Mat4;
2
3use super::projection::Projection;
4
5#[derive(Clone, Debug)]
6pub struct Camera<Controls>
7where
8    Controls: super::controls::Controls,
9{
10    projection: Projection,
11    controls: Controls,
12    changed: bool,
13}
14
15impl<Controls> Camera<Controls>
16where
17    Controls: super::controls::Controls,
18{
19    pub fn controls(&self) -> &Controls {
20        &self.controls
21    }
22
23    pub fn controls_mut(&mut self) -> &mut Controls {
24        &mut self.controls
25    }
26
27    pub fn eye(&self) -> [f32; 3] {
28        self.controls.eye()
29    }
30
31    pub fn new(projection: Projection, controls: Controls) -> Self {
32        Self {
33            projection,
34            controls,
35            changed: true,
36        }
37    }
38
39    pub fn projection(&self) -> Mat4 {
40        self.projection.into()
41    }
42
43    pub fn set_controls(&mut self, controls: Controls) -> Controls {
44        let controls = std::mem::replace(&mut self.controls, controls);
45        self.changed = true;
46        controls
47    }
48
49    pub fn set_projection(&mut self, projection: Projection) {
50        self.projection = projection;
51        self.changed = true;
52    }
53
54    pub fn update(&mut self, delta: f32) -> bool {
55        let mut changed = self.changed;
56
57        changed |= self.controls.update(delta);
58
59        self.changed = false;
60        changed
61    }
62
63    pub fn view(&self) -> Mat4 {
64        self.controls.view()
65    }
66
67    pub fn scale(&self) -> f32 {
68        self.controls.scale()
69    }
70}