rustial-engine 0.0.1

Framework-agnostic 2.5D map engine for rustial
Documentation
//! Model layer for placing 3D objects on the map.

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

/// A layer containing placed 3D model instances.
pub struct ModelLayer {
    id: LayerId,
    name: String,
    visible: bool,
    opacity: f32,
    /// Optional originating style layer id for query APIs.
    pub query_layer_id: Option<String>,
    /// Optional originating style source id for query APIs.
    pub query_source_id: Option<String>,
    /// The model instances in this layer.
    pub instances: Vec<ModelInstance>,
}

impl ModelLayer {
    /// Create a new empty model layer.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            id: LayerId::next(),
            name: name.into(),
            visible: true,
            opacity: 1.0,
            query_layer_id: None,
            query_source_id: None,
            instances: Vec::new(),
        }
    }

    /// Attach style/runtime query metadata to this layer.
    pub fn with_query_metadata(
        mut self,
        layer_id: impl Into<Option<String>>,
        source_id: impl Into<Option<String>>,
    ) -> Self {
        self.query_layer_id = layer_id.into();
        self.query_source_id = source_id.into();
        self
    }

    /// Attach style/runtime query metadata to this layer in place.
    pub fn set_query_metadata(&mut self, layer_id: Option<String>, source_id: Option<String>) {
        self.query_layer_id = layer_id;
        self.query_source_id = source_id;
    }

    /// Add a model instance to the layer.
    pub fn add(&mut self, instance: ModelInstance) {
        self.instances.push(instance);
    }
}

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

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

    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
    }
}