mln_style/property/
evaluation.rs1use std::sync::Arc;
5use std::time::Duration;
6
7use crate::collections::VecSet;
8use crate::expression::{CameraContext, GlobalStateMap};
9use crate::tile_id::CanonicalTileID;
10use crate::time::Instant;
11
12#[derive(Clone, Copy, Debug, Default, PartialEq, mln_wire::Wire)]
13pub struct CrossfadeParameters {
14 pub from_scale: f32,
15 pub to_scale: f32,
16 pub t: f32,
17}
18
19#[derive(Clone, Debug)]
20pub struct PropertyEvaluationParameters {
21 pub(crate) z: f64,
22 pub now: Option<Instant>,
24 pub(crate) zoom_history: ZoomHistory,
25 pub(crate) default_fade_duration: Duration,
26
27 pub global_state: Option<Arc<GlobalStateMap>>,
29
30 pub changed_global_state_keys: Option<Arc<VecSet<String>>>,
33
34 pub camera: Option<CameraContext>,
36
37 pub canonical: Option<CanonicalTileID>,
39
40 pub invalidated_host_functions: Option<Arc<VecSet<String>>>,
43
44 pub zoom_changed: bool,
45 pub camera_changed: bool,
46 pub host_changed: bool,
47 pub layer_changed: bool,
48 pub has_crossfade: bool,
49 pub global_state_changed: bool,
50}
51
52impl PropertyEvaluationParameters {
53 #[must_use]
54 pub fn new(z: f64) -> Self {
55 PropertyEvaluationParameters {
56 z,
57 now: None,
58 zoom_history: ZoomHistory::default(),
59 default_fade_duration: Duration::ZERO,
60 global_state: None,
61 changed_global_state_keys: None,
62 camera: None,
63 canonical: None,
64 invalidated_host_functions: None,
65 zoom_changed: true,
66 camera_changed: true,
67 host_changed: false,
68 layer_changed: false,
69 has_crossfade: true,
70 global_state_changed: false,
71 }
72 }
73
74 #[must_use]
75 pub fn with_zoom_history(zoom_history: ZoomHistory, now: Option<Instant>, default_fade_duration: Duration) -> Self {
76 PropertyEvaluationParameters {
77 z: zoom_history.last_zoom,
78 now,
79 zoom_history,
80 default_fade_duration,
81 ..PropertyEvaluationParameters::new(0.0)
82 }
83 }
84
85 #[must_use]
86 pub fn crossfade_parameters(&self) -> CrossfadeParameters {
87 let fraction = (self.z - self.z.floor()) as f32;
88 let t = match (self.now, self.zoom_history.last_integer_zoom_time) {
90 _ if self.default_fade_duration.is_zero() => 1.0,
91 (Some(now), Some(last)) => {
92 now.saturating_duration_since(last).div_duration_f32(self.default_fade_duration).min(1.0)
93 }
94 _ => 1.0,
95 };
96
97 if self.z > self.zoom_history.last_integer_zoom {
98 CrossfadeParameters { from_scale: 2.0, to_scale: 1.0, t: fraction + (1.0 - fraction) * t }
99 } else {
100 CrossfadeParameters { from_scale: 0.5, to_scale: 1.0, t: 1.0 - (1.0 - t) * fraction }
101 }
102 }
103}
104
105#[derive(Clone, Copy, Debug)]
106pub struct ZoomHistory {
107 pub last_zoom: f64,
108 pub(crate) last_floor_zoom: f64,
109 pub(crate) last_integer_zoom: f64,
110 pub(crate) last_integer_zoom_time: Option<Instant>,
112 pub first: bool,
113}
114
115impl Default for ZoomHistory {
116 fn default() -> Self {
117 ZoomHistory {
118 last_zoom: 0.0,
119 last_floor_zoom: 0.0,
120 last_integer_zoom: 0.0,
121 last_integer_zoom_time: None,
122 first: true,
123 }
124 }
125}
126
127impl ZoomHistory {
128 #[must_use]
131 pub fn cross_fading(&self, now: Option<Instant>, duration: Duration) -> bool {
132 match (now, self.last_integer_zoom_time) {
133 (Some(now), Some(crossed)) => now.saturating_duration_since(crossed) < duration,
134 _ => false,
135 }
136 }
137
138 pub fn update(&mut self, z: f64, now: Option<Instant>) -> bool {
140 let floor_z = z.floor();
141
142 if self.first {
143 self.first = false;
144 self.last_integer_zoom = floor_z;
145 self.last_integer_zoom_time = None;
146 self.last_zoom = z;
147 self.last_floor_zoom = floor_z;
148 return true;
149 }
150
151 if self.last_floor_zoom > floor_z {
152 self.last_integer_zoom = floor_z + 1.0;
153 self.last_integer_zoom_time = now;
154 } else if self.last_floor_zoom < floor_z {
155 self.last_integer_zoom = floor_z;
156 self.last_integer_zoom_time = now;
157 }
158
159 if z != self.last_zoom {
160 self.last_zoom = z;
161 self.last_floor_zoom = floor_z;
162 return true;
163 }
164
165 false
166 }
167}