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