Skip to main content

mittens_engine/engine/ecs/component/
color.rs

1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3
4/// Per-instance color for a renderable.
5///
6/// Intended to be attached as a descendant of a `RenderableComponent`.
7#[derive(Debug, Clone, Copy)]
8pub struct ColorComponent {
9    pub rgba: [f32; 4],
10}
11
12impl ColorComponent {
13    pub fn new() -> Self {
14        Self {
15            rgba: [1.0, 1.0, 1.0, 1.0],
16        }
17    }
18
19    pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
20        Self { rgba: [r, g, b, a] }
21    }
22
23    pub fn with_rgba(mut self, r: f32, g: f32, b: f32, a: f32) -> Self {
24        self.rgba = [r, g, b, a];
25        self
26    }
27}
28
29impl Default for ColorComponent {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl Component for ColorComponent {
36    fn name(&self) -> &'static str {
37        "color"
38    }
39
40    fn as_any(&self) -> &dyn std::any::Any {
41        self
42    }
43
44    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
45        self
46    }
47
48    fn init(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter, component: ComponentId) {
49        emit.push_intent_now(
50            component,
51            crate::engine::ecs::IntentValue::RegisterColor {
52                component_ids: vec![component],
53            },
54        );
55    }
56
57    fn to_mms_ast(
58        &self,
59        _world: &crate::engine::ecs::World,
60    ) -> crate::scripting::ast::ComponentExpression {
61        use crate::engine::ecs::component::ce_helpers::*;
62        ce_call("Color", "rgba", nums(self.rgba.iter().map(|&v| v as f64)))
63    }
64}