| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use crate::project::Title; |
| 8 | use crate::transform::{KeyEase, Keyed, sample}; |
| 9 | |
| 10 | |
| 11 | pub const KEY_SCALE_RANGE: (f32, f32) = (0.05, 10.0); |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 18 | pub struct TitleKey { |
| 19 | |
| 20 | pub at: u32, |
| 21 | |
| 22 | pub x: f32, |
| 23 | |
| 24 | pub y: f32, |
| 25 | |
| 26 | pub scale: f32, |
| 27 | |
| 28 | pub opacity: f32, |
| 29 | |
| 30 | pub ease: KeyEase, |
| 31 | } |
| 32 | |
| 33 | impl Keyed for TitleKey { |
| 34 | fn at(&self) -> u32 { |
| 35 | self.at |
| 36 | } |
| 37 | fn ease(&self) -> KeyEase { |
| 38 | self.ease |
| 39 | } |
| 40 | fn placed(self, at: u32, ease: KeyEase) -> Self { |
| 41 | Self { at, ease, ..self } |
| 42 | } |
| 43 | fn mix(a: &Self, b: &Self, t: f32) -> Self { |
| 44 | let l = |x: f32, y: f32| x + (y - x) * t; |
| 45 | Self { |
| 46 | x: l(a.x, b.x), |
| 47 | y: l(a.y, b.y), |
| 48 | scale: l(a.scale, b.scale), |
| 49 | opacity: l(a.opacity, b.opacity), |
| 50 | ..*a |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 57 | pub enum TitleAnim { |
| 58 | |
| 59 | #[default] |
| 60 | None, |
| 61 | |
| 62 | Fade, |
| 63 | |
| 64 | Typewriter, |
| 65 | |
| 66 | SlideUp, |
| 67 | |
| 68 | SlideLeft, |
| 69 | |
| 70 | Pop, |
| 71 | |
| 72 | WordByWord, |
| 73 | } |
| 74 | |
| 75 | impl TitleAnim { |
| 76 | pub const ALL: [TitleAnim; 7] = [ |
| 77 | Self::None, |
| 78 | Self::Fade, |
| 79 | Self::Typewriter, |
| 80 | Self::SlideUp, |
| 81 | Self::SlideLeft, |
| 82 | Self::Pop, |
| 83 | Self::WordByWord, |
| 84 | ]; |
| 85 | |
| 86 | |
| 87 | pub fn name(self) -> &'static str { |
| 88 | match self { |
| 89 | Self::None => "none", |
| 90 | Self::Fade => "fade", |
| 91 | Self::Typewriter => "typewriter", |
| 92 | Self::SlideUp => "slide_up", |
| 93 | Self::SlideLeft => "slide_left", |
| 94 | Self::Pop => "pop", |
| 95 | Self::WordByWord => "word_by_word", |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | pub fn from_name(name: &str) -> Option<Self> { |
| 100 | Self::ALL.into_iter().find(|a| a.name() == name) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | pub const DEFAULT_SECS: f64 = 0.5; |
| 106 | |
| 107 | |
| 108 | pub const SLIDE: f32 = 0.08; |
| 109 | |
| 110 | |
| 111 | pub const POP_FROM: f32 = 0.6; |
| 112 | |
| 113 | |
| 114 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 115 | pub enum Unit { |
| 116 | |
| 117 | Letters, |
| 118 | |
| 119 | Words, |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | #[derive(Clone, Debug, PartialEq)] |
| 125 | pub struct Motion { |
| 126 | |
| 127 | pub alpha: u8, |
| 128 | |
| 129 | pub dx: f32, |
| 130 | |
| 131 | pub dy: f32, |
| 132 | |
| 133 | pub scale: f32, |
| 134 | |
| 135 | pub reveal: Option<(Unit, f32)>, |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 142 | pub struct Shown { |
| 143 | |
| 144 | pub chars: usize, |
| 145 | |
| 146 | pub ink: usize, |
| 147 | } |
| 148 | |
| 149 | impl Motion { |
| 150 | |
| 151 | |
| 152 | |
| 153 | pub fn revealed(&self, text: &str) -> Option<Shown> { |
| 154 | let (unit, share) = self.reveal?; |
| 155 | let share = share.clamp(0.0, 1.0); |
| 156 | let units = match unit { |
| 157 | Unit::Letters => text.chars().filter(|c| !c.is_whitespace()).count(), |
| 158 | Unit::Words => word_starts(text).count(), |
| 159 | }; |
| 160 | |
| 161 | let show = ((share * units as f32).ceil() as usize).min(units); |
| 162 | if show == units { |
| 163 | return None; |
| 164 | } |
| 165 | let total = text.chars().count(); |
| 166 | let stop = match unit { |
| 167 | Unit::Letters => text |
| 168 | .chars() |
| 169 | .enumerate() |
| 170 | .filter(|(_, c)| !c.is_whitespace()) |
| 171 | .nth(show) |
| 172 | .map_or(total, |(i, _)| i), |
| 173 | Unit::Words => word_starts(text).nth(show).unwrap_or(total), |
| 174 | }; |
| 175 | let ink = text.chars().take(stop).filter(|c| !c.is_whitespace()).count(); |
| 176 | Some(Shown { chars: stop, ink }) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | |
| 181 | fn word_starts(text: &str) -> impl Iterator<Item = usize> + '_ { |
| 182 | let mut prev_space = true; |
| 183 | text.chars().enumerate().filter_map(move |(i, c)| { |
| 184 | let start = prev_space && !c.is_whitespace(); |
| 185 | prev_space = c.is_whitespace(); |
| 186 | start.then_some(i) |
| 187 | }) |
| 188 | } |
| 189 | |
| 190 | |
| 191 | fn ease_out(x: f32) -> f32 { |
| 192 | let x = x.clamp(0.0, 1.0); |
| 193 | 1.0 - (1.0 - x).powi(3) |
| 194 | } |
| 195 | |
| 196 | |
| 197 | fn ease_in(x: f32) -> f32 { |
| 198 | let x = x.clamp(0.0, 1.0); |
| 199 | x * x * x |
| 200 | } |
| 201 | |
| 202 | |
| 203 | fn overshoot(p: f32) -> f32 { |
| 204 | const C1: f32 = 1.70158; |
| 205 | const C3: f32 = C1 + 1.0; |
| 206 | let q = p - 1.0; |
| 207 | 1.0 + C3 * q * q * q + C1 * q * q |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | struct Part { |
| 213 | alpha: f32, |
| 214 | dx: f32, |
| 215 | dy: f32, |
| 216 | scale: f32, |
| 217 | reveal: Option<(Unit, f32)>, |
| 218 | } |
| 219 | |
| 220 | impl Part { |
| 221 | const REST: Part = Part { alpha: 1.0, dx: 0.0, dy: 0.0, scale: 1.0, reveal: None }; |
| 222 | } |
| 223 | |
| 224 | fn effect(anim: TitleAnim, p: f32) -> Part { |
| 225 | let rest = Part::REST; |
| 226 | match anim { |
| 227 | TitleAnim::None => rest, |
| 228 | TitleAnim::Fade => Part { alpha: p, ..rest }, |
| 229 | TitleAnim::Typewriter => Part { reveal: Some((Unit::Letters, p)), ..rest }, |
| 230 | TitleAnim::WordByWord => Part { reveal: Some((Unit::Words, p)), ..rest }, |
| 231 | TitleAnim::SlideUp => Part { dy: (1.0 - p) * SLIDE, ..rest }, |
| 232 | TitleAnim::SlideLeft => Part { dx: (1.0 - p) * SLIDE, ..rest }, |
| 233 | TitleAnim::Pop => Part { scale: POP_FROM + (1.0 - POP_FROM) * overshoot(p), ..rest }, |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | impl Title { |
| 238 | |
| 239 | |
| 240 | pub fn entrance(&self) -> (TitleAnim, u32) { |
| 241 | match self.in_anim { |
| 242 | TitleAnim::None if self.fade_in > 0 => (TitleAnim::Fade, self.fade_in), |
| 243 | anim => (anim, self.anim_frames), |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | |
| 248 | pub fn exit(&self) -> (TitleAnim, u32) { |
| 249 | match self.out_anim { |
| 250 | TitleAnim::None if self.fade_out > 0 => (TitleAnim::Fade, self.fade_out), |
| 251 | anim => (anim, self.anim_frames), |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |
| 256 | |
| 257 | pub fn adopt_fades(&mut self) { |
| 258 | if self.in_anim == TitleAnim::None && self.fade_in > 0 { |
| 259 | self.in_anim = TitleAnim::Fade; |
| 260 | } |
| 261 | if self.out_anim == TitleAnim::None && self.fade_out > 0 { |
| 262 | self.out_anim = TitleAnim::Fade; |
| 263 | } |
| 264 | if (self.in_anim != TitleAnim::None && self.fade_in > 0) || (self.out_anim != TitleAnim::None && self.fade_out > 0) { |
| 265 | self.anim_frames = self.fade_in.max(self.fade_out); |
| 266 | self.fade_in = 0; |
| 267 | self.fade_out = 0; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | |
| 272 | pub fn motion_at(&self, frame: u32) -> Option<Motion> { |
| 273 | if frame < self.start || frame >= self.end() { |
| 274 | return None; |
| 275 | } |
| 276 | let k = frame - self.start; |
| 277 | let left = self.frames - k; |
| 278 | |
| 279 | let mut alpha = 1.0f32; |
| 280 | if self.fade_in > 0 && k < self.fade_in { |
| 281 | alpha = alpha.min((k + 1) as f32 / (self.fade_in + 1) as f32); |
| 282 | } |
| 283 | if self.fade_out > 0 && left <= self.fade_out { |
| 284 | alpha = alpha.min(left as f32 / (self.fade_out + 1) as f32); |
| 285 | } |
| 286 | |
| 287 | let (in_anim, in_frames) = (self.in_anim, self.anim_frames); |
| 288 | let (out_anim, out_frames) = (self.out_anim, self.anim_frames); |
| 289 | let presence_in = match in_anim { |
| 290 | TitleAnim::None => 1.0, |
| 291 | _ if k < in_frames => ease_out((k + 1) as f32 / in_frames as f32), |
| 292 | _ => 1.0, |
| 293 | }; |
| 294 | let presence_out = match out_anim { |
| 295 | TitleAnim::None => 1.0, |
| 296 | _ if left <= out_frames => ease_in(left as f32 / out_frames as f32), |
| 297 | _ => 1.0, |
| 298 | }; |
| 299 | let (a, b) = (effect(in_anim, presence_in), effect(out_anim, presence_out)); |
| 300 | let reveal = match (a.reveal, b.reveal) { |
| 301 | (Some(x), Some(y)) => Some(if x.1 <= y.1 { x } else { y }), |
| 302 | (x, y) => x.or(y), |
| 303 | }; |
| 304 | let mut motion = Motion { |
| 305 | alpha: (alpha * a.alpha * b.alpha * 255.0) as u8, |
| 306 | dx: a.dx + b.dx, |
| 307 | dy: a.dy + b.dy, |
| 308 | scale: a.scale * b.scale, |
| 309 | reveal, |
| 310 | }; |
| 311 | |
| 312 | if let Some(key) = sample(&self.keys, f64::from(k)) { |
| 313 | motion.dx += key.x; |
| 314 | motion.dy += key.y; |
| 315 | motion.scale *= key.scale; |
| 316 | motion.alpha = (f32::from(motion.alpha) * key.opacity).round() as u8; |
| 317 | } |
| 318 | Some(motion) |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | #[cfg(test)] |
| 323 | mod tests { |
| 324 | use super::*; |
| 325 | use crate::project::Title; |
| 326 | use crate::text::TextStyle; |
| 327 | |
| 328 | fn title(anim: TitleAnim, frames: u32) -> Title { |
| 329 | Title { |
| 330 | start: 0, |
| 331 | frames, |
| 332 | text: "Hello big world".into(), |
| 333 | style: TextStyle::default(), |
| 334 | fade_in: 0, |
| 335 | fade_out: 0, |
| 336 | in_anim: anim, |
| 337 | out_anim: TitleAnim::None, |
| 338 | anim_frames: 12, |
| 339 | keys: Vec::new(), |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | #[test] |
| 344 | fn two_keys_fade_and_shift_a_title_halfway_between_them() { |
| 345 | |
| 346 | |
| 347 | let key = |at, x, opacity| crate::titleanim::TitleKey { at, x, y: 0.0, scale: 1.0, opacity, ease: crate::transform::KeyEase::Linear }; |
| 348 | let t = Title { keys: vec![key(0, 0.0, 0.0), key(10, 0.2, 1.0)], ..title(TitleAnim::None, 60) }; |
| 349 | let m = t.motion_at(5).expect("up"); |
| 350 | assert!(m.alpha.abs_diff(128) <= 1, "alpha {}", m.alpha); |
| 351 | assert!((m.dx - 0.1).abs() < 1e-6, "dx {}", m.dx); |
| 352 | } |
| 353 | |
| 354 | #[test] |
| 355 | fn a_typewriter_title_shows_more_letters_from_its_start_to_its_middle_to_its_end() { |
| 356 | |
| 357 | let t = Title { anim_frames: 26, ..title(TitleAnim::Typewriter, 60) }; |
| 358 | let letters = |f: u32| { |
| 359 | let m = t.motion_at(f).expect("up"); |
| 360 | m.revealed(&t.text).map_or(13, |s| s.ink) |
| 361 | }; |
| 362 | let (start, middle, end) = (letters(0), letters(6), letters(25)); |
| 363 | assert!(start >= 1 && start < middle && middle < end, "{start} {middle} {end}"); |
| 364 | assert_eq!(end, 13, "all 13 letters are up at the end of the animation"); |
| 365 | |
| 366 | let style = TextStyle::default(); |
| 367 | let lines = vec![t.text.clone()]; |
| 368 | let full = crate::text::overlay(&style, 640, 360, &lines); |
| 369 | let part = crate::text::overlay_revealed(&style, 640, 360, &lines, 5); |
| 370 | let (f, p) = (full.fill(), part.fill()); |
| 371 | assert!(p.iter().zip(f).all(|(&p, &f)| p == 0 || p == f), "the typed part sits where the whole line does"); |
| 372 | let drawn = |v: &[u8]| v.iter().filter(|&&x| x > 0).count(); |
| 373 | assert!(0 < drawn(p) && drawn(p) < drawn(f), "{} {}", drawn(p), drawn(f)); |
| 374 | } |
| 375 | } |