Skip to main content

mittens_engine/engine/ecs/component/
camera_xr.rs

1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3use crate::engine::graphics::CameraTarget;
4
5#[derive(Debug, Clone, Copy)]
6pub struct CameraXRComponent {
7    pub enabled: bool,
8
9    // Cached ECS id (runtime-only). Filled in during init.
10    pub component_id: Option<ComponentId>,
11
12    /// Which output this camera targets for activation.
13    ///
14    /// For XR, this is typically `CameraTarget::Xr` and represents the XR rig selection.
15    pub target: CameraTarget,
16}
17
18impl CameraXRComponent {
19    pub fn new(enabled: bool) -> Self {
20        Self {
21            enabled,
22            component_id: None,
23            target: CameraTarget::Xr,
24        }
25    }
26
27    pub fn on() -> Self {
28        Self::new(true)
29    }
30
31    pub fn off() -> Self {
32        Self::new(false)
33    }
34
35    /// Ask the CameraSystem to make this the active XR camera rig.
36    pub fn make_active_camera(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter) {
37        if let Some(component) = self.component_id {
38            emit.push_intent_now(
39                component,
40                crate::engine::ecs::IntentValue::MakeActiveCamera {
41                    component_ids: vec![component],
42                },
43            );
44        }
45    }
46}
47
48impl Default for CameraXRComponent {
49    fn default() -> Self {
50        Self::on()
51    }
52}
53
54impl Component for CameraXRComponent {
55    fn as_any(&self) -> &dyn std::any::Any {
56        self
57    }
58
59    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
60        self
61    }
62
63    fn name(&self) -> &'static str {
64        "camera_xr"
65    }
66
67    fn init(&mut self, _emit: &mut dyn crate::engine::ecs::SignalEmitter, component: ComponentId) {
68        self.component_id = Some(component);
69    }
70
71    fn to_mms_ast(
72        &self,
73        _world: &crate::engine::ecs::World,
74    ) -> crate::scripting::ast::ComponentExpression {
75        use crate::engine::ecs::component::ce_helpers::*;
76        let ctor = if self.enabled { "on" } else { "off" };
77        let target_str = match self.target {
78            CameraTarget::Window => "window",
79            CameraTarget::Xr => "xr",
80        };
81        let mut ce = ce_call("CameraXR", ctor, vec![]);
82        if !matches!(self.target, CameraTarget::Xr) {
83            ce = ce.with_call("target", vec![s(target_str)]);
84        }
85        ce
86    }
87}