Skip to main content

mln_style/
style.rs

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