1use crate::Error;
4
5pub const MAX_TRANSLATION_COORDINATE: f64 = 1_000_000.;
6
7#[derive(Clone, Copy, Debug, Default, PartialEq)]
8pub struct TranslationOffset {
9 pub x: f64,
10 pub y: f64,
11}
12impl TranslationOffset {
13 pub const ZERO: Self = Self { x: 0., y: 0. };
14 pub const fn new(x: f64, y: f64) -> Self {
15 Self { x, y }
16 }
17 fn validate(self) -> Result<(), Error> {
18 if [self.x, self.y]
19 .into_iter()
20 .any(|value| !value.is_finite() || value.abs() > MAX_TRANSLATION_COORDINATE)
21 {
22 return Err(Error::InvalidProps(
23 "translation coordinates must be finite and within +/-1000000",
24 ));
25 }
26 Ok(())
27 }
28}
29#[derive(Clone, Copy, Debug, PartialEq)]
30pub enum TranslationKind {
31 Loop {
33 from: TranslationOffset,
34 to: TranslationOffset,
35 period_millis: u32,
36 },
37 To {
39 target: TranslationOffset,
40 duration_millis: u16,
41 },
42}
43#[derive(Clone, Copy, Debug, PartialEq)]
44pub struct TranslationMotion {
45 pub kind: TranslationKind,
46 pub paused: bool,
48}
49impl TranslationMotion {
50 pub fn validate(self) -> Result<(), Error> {
51 match self.kind {
52 TranslationKind::Loop {
53 from,
54 to,
55 period_millis,
56 } => {
57 from.validate()?;
58 to.validate()?;
59 if !(16..=60_000).contains(&period_millis) {
60 return Err(Error::InvalidProps(
61 "translation period must be in 16..=60000 milliseconds",
62 ));
63 }
64 }
65 TranslationKind::To {
66 target,
67 duration_millis,
68 } => {
69 target.validate()?;
70 if duration_millis > 2000 {
71 return Err(Error::InvalidProps(
72 "translation duration must be in 0..=2000 milliseconds",
73 ));
74 }
75 }
76 }
77 Ok(())
78 }
79 pub fn resting_offset(self) -> TranslationOffset {
81 match self.kind {
82 TranslationKind::Loop { from, .. } => from,
83 TranslationKind::To { target, .. } => target,
84 }
85 }
86}
87
88#[derive(Clone, Debug)]
91pub struct TranslationPhase {
92 config: TranslationMotion,
93 from: TranslationOffset,
94 elapsed: f64,
95 started: f64,
96 allowed: bool,
97 settled: bool,
98}
99impl TranslationPhase {
100 pub fn new(config: TranslationMotion, seconds: f64, allowed: bool) -> Result<Self, Error> {
101 config.validate()?;
102 clock(seconds, 0.)?;
103 Ok(Self {
104 config,
105 from: config.resting_offset(),
106 elapsed: 0.,
107 started: seconds,
108 allowed,
109 settled: !matches!(config.kind, TranslationKind::Loop { .. }),
110 })
111 }
112 fn elapsed(&self, seconds: f64) -> f64 {
113 let elapsed = self.elapsed
114 + if self.allowed && !self.config.paused && !self.settled {
115 seconds - self.started
116 } else {
117 0.
118 };
119 match self.config.kind {
120 TranslationKind::Loop { period_millis, .. } => {
121 elapsed.rem_euclid(f64::from(period_millis) / 1000.)
122 }
123 TranslationKind::To {
124 duration_millis, ..
125 } => elapsed.min(f64::from(duration_millis) / 1000.),
126 }
127 }
128 pub fn config(&self) -> TranslationMotion {
129 self.config
130 }
131 pub fn origin(&self) -> TranslationOffset {
133 self.from
134 }
135 pub fn admitted(&self) -> bool {
138 self.allowed && !self.settled
139 }
140 pub fn offset(&self, seconds: f64) -> Result<TranslationOffset, Error> {
141 clock(seconds, self.started)?;
142 if !self.allowed {
143 return Ok(self.config.resting_offset());
144 }
145 let (from, to, fraction) = match self.config.kind {
146 TranslationKind::Loop {
147 from,
148 to,
149 period_millis,
150 } => (
151 from,
152 to,
153 self.elapsed(seconds) / (f64::from(period_millis) / 1000.),
154 ),
155 TranslationKind::To {
156 target,
157 duration_millis,
158 } => {
159 if self.settled || duration_millis == 0 {
160 return Ok(target);
161 }
162 (
163 self.from,
164 target,
165 self.elapsed(seconds) / (f64::from(duration_millis) / 1000.),
166 )
167 }
168 };
169 Ok(TranslationOffset {
170 x: from.x + (to.x - from.x) * fraction,
171 y: from.y + (to.y - from.y) * fraction,
172 })
173 }
174 pub fn running(&self, seconds: f64) -> Result<bool, Error> {
175 clock(seconds, self.started)?;
176 if !self.allowed || self.config.paused {
177 return Ok(false);
178 }
179 Ok(match self.config.kind {
180 TranslationKind::Loop { from, to, .. } => from != to,
181 TranslationKind::To {
182 target,
183 duration_millis,
184 } => {
185 !self.settled
186 && self.from != target
187 && self.elapsed(seconds) < f64::from(duration_millis) / 1000.
188 }
189 })
190 }
191 pub fn configure(
194 &mut self,
195 config: TranslationMotion,
196 seconds: f64,
197 allowed: bool,
198 ) -> Result<(), Error> {
199 config.validate()?;
200 clock(seconds, self.started)?;
201 if config == self.config && allowed == self.allowed {
202 return Ok(());
203 }
204 let previous = self.offset(seconds)?;
205 let previous_allowed = self.allowed;
206 if config.kind == self.config.kind && allowed == previous_allowed {
207 self.elapsed = self.elapsed(seconds);
208 } else {
209 self.from = previous;
210 self.elapsed = 0.;
211 self.settled = !previous_allowed || !allowed;
212 }
213 if allowed != previous_allowed {
214 self.elapsed = 0.;
215 self.from = config.resting_offset();
216 self.settled = !matches!(config.kind, TranslationKind::Loop { .. });
217 }
218 self.config = config;
219 self.started = seconds;
220 self.allowed = allowed;
221 Ok(())
222 }
223}
224fn clock(seconds: f64, earliest: f64) -> Result<(), Error> {
225 if !seconds.is_finite() || seconds < earliest {
226 return Err(Error::InvalidProps(
227 "translation clock must be finite, nonnegative and monotonic",
228 ));
229 }
230 Ok(())
231}
232
233#[cfg(test)]
234mod tests {
235 use super::*;
236 fn to(x: f64) -> TranslationMotion {
237 TranslationMotion {
238 kind: TranslationKind::To {
239 target: TranslationOffset::new(x, 0.),
240 duration_millis: 1000,
241 },
242 paused: false,
243 }
244 }
245 fn looping() -> TranslationMotion {
246 TranslationMotion {
247 kind: TranslationKind::Loop {
248 from: TranslationOffset::ZERO,
249 to: TranslationOffset::new(100., 0.),
250 period_millis: 1000,
251 },
252 paused: false,
253 }
254 }
255 #[test]
256 fn finite_coordinates_durations_and_clock_are_independently_checked() {
257 for value in [
258 f64::NAN,
259 f64::INFINITY,
260 f64::NEG_INFINITY,
261 -1_000_001.,
262 1_000_001.,
263 ] {
264 for point in [
265 TranslationOffset::new(value, 0.),
266 TranslationOffset::new(0., value),
267 ] {
268 assert!(
269 TranslationMotion {
270 kind: TranslationKind::To {
271 target: point,
272 duration_millis: 0
273 },
274 paused: false
275 }
276 .validate()
277 .is_err()
278 );
279 for (from, to) in [
280 (point, TranslationOffset::ZERO),
281 (TranslationOffset::ZERO, point),
282 ] {
283 assert!(
284 TranslationMotion {
285 kind: TranslationKind::Loop {
286 from,
287 to,
288 period_millis: 1000
289 },
290 paused: false
291 }
292 .validate()
293 .is_err()
294 );
295 }
296 }
297 }
298 for millis in [0, 15, 60_001, u32::MAX] {
299 assert!(
300 TranslationMotion {
301 kind: TranslationKind::Loop {
302 from: TranslationOffset::ZERO,
303 to: TranslationOffset::ZERO,
304 period_millis: millis
305 },
306 paused: false
307 }
308 .validate()
309 .is_err()
310 );
311 }
312 assert!(
313 TranslationMotion {
314 kind: TranslationKind::To {
315 target: TranslationOffset::ZERO,
316 duration_millis: 2001
317 },
318 paused: false
319 }
320 .validate()
321 .is_err()
322 );
323 for seconds in [-1., f64::NAN, f64::INFINITY] {
324 assert!(TranslationPhase::new(to(0.), seconds, true).is_err());
325 }
326 let mut phase = TranslationPhase::new(to(0.), 1., true).unwrap();
327 let before = phase.clone();
328 assert!(phase.configure(to(f64::NAN), 2., true).is_err());
329 assert_eq!(phase.offset(2.).unwrap(), before.offset(2.).unwrap());
330 assert!(phase.configure(to(20.), 0.5, true).is_err());
331 }
332 #[test]
333 fn once_first_static_interrupt_pause_clear_and_reduce_have_no_history() {
334 let mut phase = TranslationPhase::new(to(10.), 0., true).unwrap();
335 assert_eq!(phase.offset(0.5).unwrap().x, 10.);
336 phase.configure(to(110.), 1., true).unwrap();
337 assert_eq!(phase.offset(1.25).unwrap().x, 35.);
338 phase
339 .configure(
340 TranslationMotion {
341 paused: true,
342 ..to(110.)
343 },
344 1.25,
345 true,
346 )
347 .unwrap();
348 assert_eq!(phase.offset(50.).unwrap().x, 35.);
349 phase.configure(to(110.), 50., true).unwrap();
350 assert_eq!(phase.offset(50.25).unwrap().x, 60.);
351 phase.configure(to(-40.), 50.25, true).unwrap();
352 assert_eq!(phase.offset(50.5).unwrap().x, 35.);
353 phase.configure(to(-40.), 50.5, false).unwrap();
354 assert_eq!(phase.offset(51.).unwrap().x, -40.);
355 phase.configure(to(200.), 52., true).unwrap();
356 assert_eq!(phase.offset(52.).unwrap().x, 200.);
357 assert!(!phase.running(52.).unwrap());
358 phase = TranslationPhase::new(to(0.), 53., true).unwrap();
359 assert_eq!(phase.offset(53.).unwrap(), TranslationOffset::ZERO);
360 }
361 #[test]
362 fn repeat_retains_paused_phase_and_restarts_only_for_explicit_new_endpoints() {
363 let mut phase = TranslationPhase::new(looping(), 0., true).unwrap();
364 assert_eq!(phase.offset(0.25).unwrap().x, 25.);
365 phase.configure(looping(), 0.25, true).unwrap();
366 assert_eq!(phase.offset(0.5).unwrap().x, 50.);
367 phase
368 .configure(
369 TranslationMotion {
370 paused: true,
371 ..looping()
372 },
373 0.5,
374 true,
375 )
376 .unwrap();
377 assert_eq!(phase.offset(100.).unwrap().x, 50.);
378 phase.configure(looping(), 100., true).unwrap();
379 assert_eq!(phase.offset(100.25).unwrap().x, 75.);
380 assert!(phase.offset(f64::MAX).unwrap().x.is_finite());
381 phase.configure(looping(), 101., false).unwrap();
382 assert_eq!(phase.offset(102.).unwrap(), TranslationOffset::ZERO);
383 phase.configure(looping(), 103., true).unwrap();
384 assert_eq!(phase.offset(103.25).unwrap().x, 25.);
385 phase
386 .configure(
387 TranslationMotion {
388 kind: TranslationKind::Loop {
389 from: TranslationOffset::new(100., 0.),
390 to: TranslationOffset::ZERO,
391 period_millis: 1000,
392 },
393 paused: false,
394 },
395 104.,
396 true,
397 )
398 .unwrap();
399 assert_eq!(phase.offset(104.25).unwrap().x, 75.);
400 }
401}