Skip to main content

rustial_engine/layers/
model_layer.rs

1//! Model layer for placing 3D objects on the map.
2
3use crate::layer::{Layer, LayerId};
4use crate::models::ModelInstance;
5use std::any::Any;
6
7/// A layer containing placed 3D model instances.
8pub struct ModelLayer {
9    id: LayerId,
10    name: String,
11    visible: bool,
12    opacity: f32,
13    /// Optional originating style layer id for query APIs.
14    pub query_layer_id: Option<String>,
15    /// Optional originating style source id for query APIs.
16    pub query_source_id: Option<String>,
17    /// The model instances in this layer.
18    pub instances: Vec<ModelInstance>,
19}
20
21impl ModelLayer {
22    /// Create a new empty model layer.
23    pub fn new(name: impl Into<String>) -> Self {
24        Self {
25            id: LayerId::next(),
26            name: name.into(),
27            visible: true,
28            opacity: 1.0,
29            query_layer_id: None,
30            query_source_id: None,
31            instances: Vec::new(),
32        }
33    }
34
35    /// Attach style/runtime query metadata to this layer.
36    pub fn with_query_metadata(
37        mut self,
38        layer_id: impl Into<Option<String>>,
39        source_id: impl Into<Option<String>>,
40    ) -> Self {
41        self.query_layer_id = layer_id.into();
42        self.query_source_id = source_id.into();
43        self
44    }
45
46    /// Attach style/runtime query metadata to this layer in place.
47    pub fn set_query_metadata(&mut self, layer_id: Option<String>, source_id: Option<String>) {
48        self.query_layer_id = layer_id;
49        self.query_source_id = source_id;
50    }
51
52    /// Add a model instance to the layer.
53    pub fn add(&mut self, instance: ModelInstance) {
54        self.instances.push(instance);
55    }
56}
57
58impl Layer for ModelLayer {
59    fn id(&self) -> LayerId {
60        self.id
61    }
62
63    fn kind(&self) -> crate::layer::LayerKind {
64        crate::layer::LayerKind::Model
65    }
66
67    fn name(&self) -> &str {
68        &self.name
69    }
70
71    fn visible(&self) -> bool {
72        self.visible
73    }
74
75    fn set_visible(&mut self, visible: bool) {
76        self.visible = visible;
77    }
78
79    fn opacity(&self) -> f32 {
80        self.opacity
81    }
82
83    fn set_opacity(&mut self, opacity: f32) {
84        self.opacity = opacity.clamp(0.0, 1.0);
85    }
86
87    fn as_any(&self) -> &dyn Any {
88        self
89    }
90
91    fn as_any_mut(&mut self) -> &mut dyn Any {
92        self
93    }
94}