Skip to main content

velox_engine/motion/
tween.rs

1//! 基于绝对时间戳的声明式补间器(`Tween`)与显隐挂载生命周期状态机(`Presence`)。
2
3use crate::style::Easing;
4
5/// 基于绝对时间戳(`cx.now_ms()`)的单值补间器(`no_std`、零分配、`Copy`)。
6///
7/// 存放在 `View` 状态中驱动任意自定义数值动画(角度、进度、偏移、颜色混合比等)。
8/// 渲染期以 `value(cx.now_ms())` 采样,若 `is_running(...)` 则调用 `cx.request_frame()` 请求下一帧。
9#[derive(Copy, Clone, PartialEq, Eq, Debug)]
10pub struct Tween {
11    from: i32,
12    to: i32,
13    start_ms: u32,
14    duration_ms: u16,
15    delay_ms: u16,
16    easing: Easing,
17}
18
19impl Tween {
20    /// 创建一个静止于 `value` 的补间器(默认 `200ms`、`EaseOut`)。
21    #[inline(always)]
22    pub const fn new(value: i32) -> Self {
23        Self {
24            from: value,
25            to: value,
26            start_ms: 0,
27            duration_ms: 200,
28            delay_ms: 0,
29            easing: Easing::EaseOut,
30        }
31    }
32
33    /// 设置缓动曲线。
34    #[inline(always)]
35    pub const fn with_easing(mut self, easing: Easing) -> Self {
36        self.easing = easing;
37        self
38    }
39
40    /// 设置补间时长(毫秒)。
41    #[inline(always)]
42    pub const fn with_duration(mut self, duration_ms: u16) -> Self {
43        self.duration_ms = duration_ms;
44        self
45    }
46
47    /// 设置启动延迟(毫秒)。
48    #[inline(always)]
49    pub const fn with_delay(mut self, delay_ms: u16) -> Self {
50        self.delay_ms = delay_ms;
51        self
52    }
53
54    /// 最终目标值。
55    #[inline(always)]
56    pub const fn target(&self) -> i32 {
57        self.to
58    }
59
60    /// 从当前采样值平滑过渡到新目标 `to`(打断进行中的补间时保持连续)。
61    #[inline]
62    pub fn animate_to(&mut self, now_ms: u32, to: i32) {
63        if to == self.to {
64            return;
65        }
66        self.from = self.value(now_ms);
67        self.to = to;
68        self.start_ms = now_ms;
69    }
70
71    /// 以显式起止值重新开始补间。
72    #[inline]
73    pub fn start(&mut self, now_ms: u32, from: i32, to: i32) {
74        self.from = from;
75        self.to = to;
76        self.start_ms = now_ms;
77    }
78
79    /// 立即跳转到 `value` 并停止补间。
80    #[inline]
81    pub fn jump_to(&mut self, value: i32) {
82        self.from = value;
83        self.to = value;
84    }
85
86    /// 已缓动的归一化进度(`0..=256`,过冲曲线可短暂超出)。
87    #[inline]
88    pub fn progress_256(&self, now_ms: u32) -> i32 {
89        let elapsed = now_ms.wrapping_sub(self.start_ms);
90        if elapsed < self.delay_ms as u32 {
91            return 0;
92        }
93        self.easing
94            .sample_256(elapsed - self.delay_ms as u32, self.duration_ms as u32)
95    }
96
97    /// 在时间戳 `now_ms` 处采样当前值。
98    #[inline]
99    pub fn value(&self, now_ms: u32) -> i32 {
100        if self.from == self.to {
101            return self.to;
102        }
103        let t = self.progress_256(now_ms) as i64;
104        (self.from as i64 + (((self.to as i64 - self.from as i64) * t) >> 8)) as i32
105    }
106
107    /// 补间是否仍在进行中(含延迟段)。
108    #[inline]
109    pub fn is_running(&self, now_ms: u32) -> bool {
110        self.from != self.to
111            && now_ms.wrapping_sub(self.start_ms) < self.delay_ms as u32 + self.duration_ms as u32
112    }
113}
114
115/// 显隐过渡状态机(实现元素的“退场动画”:隐藏后仍保持挂载直至淡出/滑出完成)。
116///
117/// ```ignore
118/// // 渲染期:
119/// let now = cx.now_ms();
120/// if self.popup.is_mounted(now) {
121///     root = root.child(cx, div().opacity(self.popup.opacity(now)).mt(self.popup.offset(now, 16)));
122/// }
123/// if self.popup.is_animating(now) { cx.request_frame(); }
124/// // 事件中:self.popup.hide(acx.now_ms());
125/// ```
126#[derive(Copy, Clone, PartialEq, Eq, Debug)]
127pub struct Presence {
128    tween: Tween,
129}
130
131impl Presence {
132    /// 创建初始显隐状态为 `visible`、过渡时长为 `duration_ms` 的状态机。
133    #[inline(always)]
134    pub const fn new(visible: bool, duration_ms: u16) -> Self {
135        Self {
136            tween: Tween::new(if visible { 256 } else { 0 }).with_duration(duration_ms),
137        }
138    }
139
140    /// 设置显隐过渡缓动曲线。
141    #[inline(always)]
142    pub const fn with_easing(mut self, easing: Easing) -> Self {
143        self.tween = self.tween.with_easing(easing);
144        self
145    }
146
147    /// 目标状态是否为可见。
148    #[inline(always)]
149    pub const fn is_visible(&self) -> bool {
150        self.tween.target() == 256
151    }
152
153    /// 开始入场(显示)过渡。
154    #[inline]
155    pub fn show(&mut self, now_ms: u32) {
156        self.tween.animate_to(now_ms, 256);
157    }
158
159    /// 开始退场(隐藏)过渡;过渡完成前 `is_mounted` 仍返回 `true`。
160    #[inline]
161    pub fn hide(&mut self, now_ms: u32) {
162        self.tween.animate_to(now_ms, 0);
163    }
164
165    /// 切换显隐状态。
166    #[inline]
167    pub fn toggle(&mut self, now_ms: u32) {
168        if self.is_visible() {
169            self.hide(now_ms);
170        } else {
171            self.show(now_ms);
172        }
173    }
174
175    /// 显式设置显隐状态。
176    #[inline]
177    pub fn set_visible(&mut self, now_ms: u32, visible: bool) {
178        if visible {
179            self.show(now_ms);
180        } else {
181            self.hide(now_ms);
182        }
183    }
184
185    /// 元素是否仍需挂载渲染(可见,或正在执行退场过渡)。
186    #[inline]
187    pub fn is_mounted(&self, now_ms: u32) -> bool {
188        self.is_visible() || self.tween.value(now_ms) > 0
189    }
190
191    /// 显隐过渡是否正在进行(渲染期据此调用 `cx.request_frame()`)。
192    #[inline]
193    pub fn is_animating(&self, now_ms: u32) -> bool {
194        self.tween.is_running(now_ms)
195    }
196
197    /// 当前显现进度(`0` 完全隐藏 ~ `256` 完全显示)。
198    #[inline]
199    pub fn progress_256(&self, now_ms: u32) -> i32 {
200        self.tween.value(now_ms)
201    }
202
203    /// 当前显现进度映射的不透明度(`0..=255`)。
204    #[inline]
205    pub fn opacity(&self, now_ms: u32) -> u8 {
206        ((self.progress_256(now_ms).clamp(0, 256) * 255) >> 8) as u8
207    }
208
209    /// 当前显现进度映射的位移:完全隐藏时为 `distance`,完全显示时为 `0`(用于滑入/滑出)。
210    #[inline]
211    pub fn offset(&self, now_ms: u32, distance: i16) -> i16 {
212        let rem = 256 - self.progress_256(now_ms);
213        ((distance as i32 * rem) >> 8) as i16
214    }
215}