Skip to main content

mittens_engine/engine/ecs/component/
fit_bounds.rs

1use super::Component;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum FitBoundsMode {
5    RenderableOnly,
6    LayoutAware,
7}
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum FitBoundsTarget {
11    ExplicitBounds,
12    ParentPaddingBox,
13}
14
15#[derive(Debug, Clone, Copy)]
16pub struct FitBoundsComponent {
17    pub mode: FitBoundsMode,
18    pub target: FitBoundsTarget,
19    pub target_bounds: [f32; 6],
20}
21
22impl FitBoundsComponent {
23    pub fn new() -> Self {
24        Self {
25            mode: FitBoundsMode::RenderableOnly,
26            target: FitBoundsTarget::ExplicitBounds,
27            target_bounds: [-0.5, -0.5, -0.5, 0.5, 0.5, 0.5],
28        }
29    }
30}
31
32impl Default for FitBoundsComponent {
33    fn default() -> Self {
34        Self::new()
35    }
36}
37
38impl Component for FitBoundsComponent {
39    fn name(&self) -> &'static str {
40        "fit_bounds"
41    }
42
43    fn as_any(&self) -> &dyn std::any::Any {
44        self
45    }
46
47    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
48        self
49    }
50
51    fn to_mms_ast(
52        &self,
53        _world: &crate::engine::ecs::World,
54    ) -> crate::scripting::ast::ComponentExpression {
55        use crate::engine::ecs::component::ce_helpers::{CeBuilder, array, ce_call, num};
56
57        let mut expr = match self.mode {
58            FitBoundsMode::RenderableOnly => ce_call("FitBounds", "renderable_only", vec![]),
59            FitBoundsMode::LayoutAware => ce_call("FitBounds", "layout_aware", vec![]),
60        };
61
62        expr = match self.target {
63            FitBoundsTarget::ExplicitBounds => expr.with_call(
64                "to",
65                vec![array(
66                    self.target_bounds
67                        .iter()
68                        .map(|&value| num(value as f64))
69                        .collect(),
70                )],
71            ),
72            FitBoundsTarget::ParentPaddingBox => expr.with_call("to_container", vec![]),
73        };
74
75        expr
76    }
77}