rustial-engine 0.0.1

Framework-agnostic 2.5D map engine for rustial
Documentation
//! Solid background layer, similar to MapLibre's background layer.

use crate::layer::{Layer, LayerId};
use std::any::Any;

/// A solid background fill used as the map clear colour.
///
/// This is the first small step toward a broader MapLibre-style layer
/// taxonomy: renderers can derive their frame clear colour from the
/// bottom-most scene stack instead of hard-coding a sky colour.
#[derive(Debug, Clone)]
pub struct BackgroundLayer {
    id: LayerId,
    name: String,
    visible: bool,
    opacity: f32,
    color: [f32; 4],
}

impl BackgroundLayer {
    /// Create a new background layer with the given RGBA colour.
    pub fn new(name: impl Into<String>, color: [f32; 4]) -> Self {
        Self {
            id: LayerId::next(),
            name: name.into(),
            visible: true,
            opacity: 1.0,
            color,
        }
    }

    /// Return the configured RGBA colour.
    #[inline]
    pub fn color(&self) -> [f32; 4] {
        self.color
    }

    /// Replace the RGBA colour.
    pub fn set_color(&mut self, color: [f32; 4]) {
        self.color = color;
    }

    /// Return the effective clear colour after applying layer opacity.
    #[inline]
    pub fn effective_color(&self) -> [f32; 4] {
        [
            self.color[0],
            self.color[1],
            self.color[2],
            self.color[3] * self.opacity,
        ]
    }
}

impl Layer for BackgroundLayer {
    fn id(&self) -> LayerId {
        self.id
    }

    fn kind(&self) -> crate::layer::LayerKind {
        crate::layer::LayerKind::Background
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn visible(&self) -> bool {
        self.visible
    }

    fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }

    fn opacity(&self) -> f32 {
        self.opacity
    }

    fn set_opacity(&mut self, opacity: f32) {
        self.opacity = opacity.clamp(0.0, 1.0);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::layer::Layer;

    #[test]
    fn effective_color_applies_opacity() {
        let mut layer = BackgroundLayer::new("bg", [0.2, 0.3, 0.4, 1.0]);
        layer.set_opacity(0.5);
        assert_eq!(layer.effective_color(), [0.2, 0.3, 0.4, 0.5]);
    }

    #[test]
    fn kind_is_background() {
        let layer = BackgroundLayer::new("bg", [0.0, 0.0, 0.0, 1.0]);
        assert_eq!(layer.kind(), crate::layer::LayerKind::Background);
    }
}