velox_engine/motion/
tween.rs1use crate::style::Easing;
4
5#[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 #[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 #[inline(always)]
35 pub const fn with_easing(mut self, easing: Easing) -> Self {
36 self.easing = easing;
37 self
38 }
39
40 #[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 #[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 #[inline(always)]
56 pub const fn target(&self) -> i32 {
57 self.to
58 }
59
60 #[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 #[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 #[inline]
81 pub fn jump_to(&mut self, value: i32) {
82 self.from = value;
83 self.to = value;
84 }
85
86 #[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 #[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 #[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#[derive(Copy, Clone, PartialEq, Eq, Debug)]
127pub struct Presence {
128 tween: Tween,
129}
130
131impl Presence {
132 #[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 #[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 #[inline(always)]
149 pub const fn is_visible(&self) -> bool {
150 self.tween.target() == 256
151 }
152
153 #[inline]
155 pub fn show(&mut self, now_ms: u32) {
156 self.tween.animate_to(now_ms, 256);
157 }
158
159 #[inline]
161 pub fn hide(&mut self, now_ms: u32) {
162 self.tween.animate_to(now_ms, 0);
163 }
164
165 #[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 #[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 #[inline]
187 pub fn is_mounted(&self, now_ms: u32) -> bool {
188 self.is_visible() || self.tween.value(now_ms) > 0
189 }
190
191 #[inline]
193 pub fn is_animating(&self, now_ms: u32) -> bool {
194 self.tween.is_running(now_ms)
195 }
196
197 #[inline]
199 pub fn progress_256(&self, now_ms: u32) -> i32 {
200 self.tween.value(now_ms)
201 }
202
203 #[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 #[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}