i_slint_core/animations/simulations.rs
1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4//! This module contains various physics simulations which can be used as animation (internally only yet).
5//! Currently it is used in the flickable to animate the content position of the Flickable
6//!
7//! Currently it contains two simulations:
8//! - `ConstantDeceleration`
9//! - `ConstantDecelerationSpringDamper` with spring damper simulation when reaching the limit
10
11pub mod constant_deceleration;
12pub mod constant_deceleration_spring_damper;
13pub mod spring;
14
15use crate::animations::Instant;
16
17/// The direction the simulation is running
18#[derive(Debug)]
19enum Direction {
20 /// The start value is smaller than the limit value
21 Increasing,
22 /// The start value is larger than the limit value
23 Decreasing,
24}
25
26/// Common simulation trait
27/// All simulations must implement this trait
28pub trait Simulation {
29 fn step(&mut self, current: &mut f32, new_tick: Instant) -> bool;
30}
31
32/// Trait to convert parameter objects into a simulation
33/// All parameter objects must implement this trait!
34pub trait Parameter {
35 type Output;
36 fn simulation(
37 self,
38 start_value: f32,
39 limit_value: core::pin::Pin<alloc::boxed::Box<crate::Property<f32>>>,
40 ) -> Self::Output;
41}
42
43#[cfg(test)]
44macro_rules! assert_approx_eq {
45 ($a:expr, $b:expr) => {
46 assert!(($a - $b).abs() < 1e-4, "{} != {}", $a, $b);
47 };
48}
49#[cfg(test)]
50pub(crate) use assert_approx_eq;
51
52#[cfg(test)]
53pub(crate) fn test_limit_property(
54 value: f32,
55) -> core::pin::Pin<alloc::boxed::Box<crate::Property<f32>>> {
56 alloc::boxed::Box::pin(crate::Property::new(value))
57}