Skip to main content

mittens_engine/engine/ecs/component/
camera_3d.rs

1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3use crate::engine::graphics::CameraTarget;
4
5/// 3D camera component.
6///
7/// Contract:
8/// - On init, registers a camera with `CameraSystem`.
9/// - The most recently registered camera becomes active.
10/// - Call `make_active_camera()` to explicitly set this camera active.
11#[derive(Debug, Clone)]
12pub struct Camera3DComponent {
13    pub enabled: bool,
14
15    // Handle owned by CameraSystem. Filled in during init.
16    pub handle: Option<crate::engine::ecs::system::camera_system::CameraHandle>,
17
18    // Cached ECS id (runtime-only). Filled in during init.
19    pub component_id: Option<ComponentId>,
20
21    /// Which output this camera targets for activation.
22    ///
23    /// Notes:
24    /// - Today, the 3D camera drives the Window camera matrices.
25    /// - This field exists so `make_active_camera()` can be target-aware.
26    pub target: CameraTarget,
27
28    /// Vertical field of view (degrees).
29    pub fov_y_degrees: f32,
30    pub z_near: f32,
31    pub z_far: f32,
32}
33
34impl Camera3DComponent {
35    pub const DEFAULT_FOV_Y_DEGREES: f32 = 60.0;
36    pub const DEFAULT_Z_NEAR: f32 = 0.1;
37    pub const DEFAULT_Z_FAR: f32 = 150.0;
38
39    pub fn new() -> Self {
40        Self {
41            enabled: true,
42            handle: None,
43            component_id: None,
44            target: CameraTarget::Window,
45            fov_y_degrees: Self::DEFAULT_FOV_Y_DEGREES,
46            z_near: Self::DEFAULT_Z_NEAR,
47            z_far: Self::DEFAULT_Z_FAR,
48        }
49    }
50
51    pub fn with_fov(mut self, fov_y_degrees: f32) -> Self {
52        self.fov_y_degrees = fov_y_degrees;
53        self
54    }
55
56    pub fn with_near(mut self, z_near: f32) -> Self {
57        self.z_near = z_near;
58        self
59    }
60
61    pub fn with_far(mut self, z_far: f32) -> Self {
62        self.z_far = z_far;
63        self
64    }
65
66    pub fn with_enabled(mut self, enabled: bool) -> Self {
67        self.enabled = enabled;
68        self
69    }
70
71    /// Ask the CameraSystem to make this the active camera.
72    pub fn make_active_camera(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter) {
73        if self.handle.is_some() {
74            if let Some(component) = self.component_id {
75                emit.push_intent_now(
76                    component,
77                    crate::engine::ecs::IntentValue::MakeActiveCamera {
78                        component_ids: vec![component],
79                    },
80                );
81            }
82        }
83    }
84}
85
86impl Default for Camera3DComponent {
87    fn default() -> Self {
88        Self::new()
89    }
90}
91
92impl Component for Camera3DComponent {
93    fn name(&self) -> &'static str {
94        "camera3d"
95    }
96
97    fn as_any(&self) -> &dyn std::any::Any {
98        self
99    }
100
101    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
102        self
103    }
104
105    fn init(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter, component: ComponentId) {
106        self.component_id = Some(component);
107        emit.push_intent_now(
108            component,
109            crate::engine::ecs::IntentValue::RegisterCamera3d {
110                component_ids: vec![component],
111            },
112        );
113    }
114
115    fn to_mms_ast(
116        &self,
117        _world: &crate::engine::ecs::World,
118    ) -> crate::scripting::ast::ComponentExpression {
119        use crate::engine::ecs::component::ce_helpers::*;
120        let target = match self.target {
121            CameraTarget::Window => "window",
122            CameraTarget::Xr => "xr",
123        };
124        ce("Camera3D")
125            .with_call("enabled", vec![b(self.enabled)])
126            .with_call("target", vec![s(target)])
127            .with_call("fov", vec![num(self.fov_y_degrees as f64)])
128            .with_call("near", vec![num(self.z_near as f64)])
129            .with_call("far", vec![num(self.z_far as f64)])
130    }
131}