Skip to main content

mln_style/
style.rs

1// Ports include/mln/style/style.hpp + src/mln/style/style.cpp.
2
3use crate::expression::GlobalStateMap;
4use crate::image;
5use crate::layer::Layer;
6use crate::light::Light;
7use crate::source::Source;
8use crate::transition_options::TransitionOptions;
9use maplibre_native_base::Value;
10use crate::map::camera::CameraOptions;
11use mln_util::exception::{ExceptionPtr, MisuseException};
12use mln_util::immutable::Immutable;
13
14pub use crate::style_impl::{Impl, LayerRefMut, LightRefMut};
15
16#[derive(Debug)]
17pub struct Style {
18    // Private implementation
19    pub impl_: Box<Impl>,
20}
21
22impl Style {
23    /// A style without a file source (see `Impl::new`).
24    pub fn new(pixel_ratio: f32) -> Self {
25        Style {
26            impl_: Box::new(Impl::new(pixel_ratio)),
27        }
28    }
29
30    /// `Style(std::shared_ptr<FileSource>, float pixelRatio, const TaggedScheduler&)`.
31    pub fn with_file_source(
32        file_source: Option<std::sync::Arc<dyn mln_storage::FileSource>>,
33        pixel_ratio: f32,
34        thread_pool: mln_actor::TaggedScheduler,
35    ) -> Self {
36        Style {
37            impl_: Box::new(Impl::with_file_source(file_source, pixel_ratio, thread_pool)),
38        }
39    }
40
41    pub fn load_json(&mut self, json: &str) {
42        self.impl_.load_json(json);
43    }
44
45    pub fn load_url(&mut self, url: &str) {
46        self.impl_.load_url(url);
47    }
48
49    pub fn get_json(&self) -> String {
50        self.impl_.get_json()
51    }
52
53    pub fn get_url(&self) -> String {
54        self.impl_.get_url()
55    }
56
57    /// False while a style passed to load_json/load_url is still being fetched or parsed.
58    pub fn is_loaded(&self) -> bool {
59        self.impl_.loaded
60    }
61
62    // Defaults
63    pub fn get_name(&self) -> String {
64        self.impl_.get_name()
65    }
66
67    pub fn get_default_camera(&self) -> CameraOptions {
68        self.impl_.get_default_camera()
69    }
70
71    // TransitionOptions
72    pub fn get_transition_options(&self) -> TransitionOptions {
73        self.impl_.get_transition_options()
74    }
75
76    pub fn set_transition_options(&mut self, options: &TransitionOptions) {
77        self.impl_.mutated = true;
78        self.impl_.set_transition_options(options);
79    }
80
81    // Light
82    pub fn get_light_mut(&mut self) -> LightRefMut<'_> {
83        self.impl_.mutated = true;
84        self.impl_.get_light_mut()
85    }
86
87    pub fn get_light(&self) -> &Light {
88        self.impl_.get_light()
89    }
90
91    pub fn set_light(&mut self, light: Light) {
92        self.impl_.set_light(light);
93    }
94
95    // Global state, used by the "global-state" expression.
96    pub fn get_global_state(&self) -> GlobalStateMap {
97        self.impl_.get_global_state()
98    }
99
100    /// C++ throws `util::MisuseException` if the style has not finished loading.
101    pub fn set_global_state_property(&mut self, name: &str, value: &Value) -> Result<(), ExceptionPtr> {
102        if !self.impl_.loaded {
103            return Err(MisuseException::ptr("Style is not done loading."));
104        }
105
106        self.impl_.mutated = true;
107        self.impl_.set_global_state_property(name, value);
108        Ok(())
109    }
110
111    // Images
112    pub fn get_image(&self, name: &str) -> Option<Immutable<image::Impl>> {
113        self.impl_.get_image(name)
114    }
115
116    pub fn add_image(&mut self, image: Immutable<image::Impl>) {
117        self.impl_.mutated = true;
118        self.impl_.add_image(image);
119    }
120
121    pub fn remove_image(&mut self, name: &str) {
122        self.impl_.mutated = true;
123        self.impl_.remove_image(name);
124    }
125
126    // Sources
127    pub fn get_sources(&self) -> Vec<&dyn Source> {
128        self.impl_.get_sources()
129    }
130
131    pub fn get_source(&self, id: &str) -> Option<&dyn Source> {
132        self.impl_.get_source(id)
133    }
134
135    pub fn add_source(&mut self, source: Box<dyn Source>) -> Result<(), ExceptionPtr> {
136        self.impl_.mutated = true;
137        self.impl_.add_source(source)
138    }
139
140    pub fn remove_source(&mut self, source_id: &str) -> Option<Box<dyn Source>> {
141        self.impl_.mutated = true;
142        self.impl_.remove_source(source_id)
143    }
144
145    // Layers
146    pub fn get_layers(&self) -> Vec<&dyn Layer> {
147        self.impl_.get_layers()
148    }
149
150    pub fn get_layer(&self, layer_id: &str) -> Option<&dyn Layer> {
151        self.impl_.get_layer(layer_id)
152    }
153
154    /// The non-const `getLayer` (marks the style mutated). The guard notifies
155    /// the style of changes when dropped.
156    pub fn get_layer_mut(&mut self, layer_id: &str) -> Option<LayerRefMut<'_>> {
157        self.impl_.mutated = true;
158        self.impl_.get_layer_mut(layer_id)
159    }
160
161    pub fn add_layer(&mut self, layer: Box<dyn Layer>, before_layer_id: Option<&str>) -> Result<(), ExceptionPtr> {
162        self.impl_.mutated = true;
163        self.impl_.add_layer(layer, before_layer_id).map(|_| ())
164    }
165
166    pub fn remove_layer(&mut self, id: &str) -> Option<Box<dyn Layer>> {
167        self.impl_.mutated = true;
168        self.impl_.remove_layer(id)
169    }
170}