Skip to main content

mittens_engine/engine/ecs/component/
input_xr.rs

1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3
4/// Marker/config for an XR headset pose driver.
5///
6/// Semantics:
7/// - Attach a `TransformComponent` as a child of this component.
8/// - the active XR runtime will drive that transform child from the headset/root pose.
9#[derive(Debug, Clone)]
10pub struct InputXRComponent {
11    pub enabled: bool,
12    /// Runtime-only: true after a valid headset pose was applied this frame.
13    pub pose_valid: bool,
14    pub component_id: Option<ComponentId>,
15}
16
17impl InputXRComponent {
18    pub fn new(enabled: bool) -> Self {
19        Self {
20            enabled,
21            pose_valid: false,
22            component_id: None,
23        }
24    }
25
26    pub fn on() -> Self {
27        Self::new(true)
28    }
29
30    pub fn off() -> Self {
31        Self::new(false)
32    }
33}
34
35impl Default for InputXRComponent {
36    fn default() -> Self {
37        Self::on()
38    }
39}
40
41impl Component for InputXRComponent {
42    fn name(&self) -> &'static str {
43        "input_xr"
44    }
45
46    fn as_any(&self) -> &dyn std::any::Any {
47        self
48    }
49
50    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
51        self
52    }
53
54    fn set_id(&mut self, component: ComponentId) {
55        self.component_id = Some(component);
56    }
57
58    fn init(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter, component: ComponentId) {
59        self.component_id = Some(component);
60        emit.push_intent_now(
61            component,
62            crate::engine::ecs::IntentValue::RegisterInputXr {
63                component_ids: vec![component],
64            },
65        );
66    }
67
68    fn cleanup(
69        &mut self,
70        emit: &mut dyn crate::engine::ecs::SignalEmitter,
71        component: ComponentId,
72    ) {
73        emit.push_intent_now(
74            component,
75            crate::engine::ecs::IntentValue::RemoveInputXr {
76                component_ids: vec![component],
77            },
78        );
79    }
80
81    fn to_mms_ast(
82        &self,
83        _world: &crate::engine::ecs::World,
84    ) -> crate::scripting::ast::ComponentExpression {
85        use crate::engine::ecs::component::ce_helpers::*;
86        let ctor = if self.enabled { "on" } else { "off" };
87        ce_call("InputXR", ctor, vec![])
88    }
89}