1use crate::icons::{self, Icon};
32use crate::shapes::{self, Fill};
33use crate::text::{TextAlign, TextStyle, draw_text_styled, text_width};
34use crate::{Color, Rect, Vec2, vec2};
35use ferravex_play::tween::{Ease, Motion};
36use std::f32::consts::TAU;
37
38pub const DEFAULT_SPARKS: usize = 384;
40pub const DEFAULT_POPUPS: usize = 24;
42pub const DEFAULT_RINGS: usize = 24;
44pub const MAX_LABEL: usize = 40;
46
47#[derive(Clone, Copy, Debug, PartialEq, Eq)]
48enum SparkKind {
49 Dot,
50 Star,
51 Confetti,
52 Puff,
53}
54
55#[derive(Clone, Copy, Debug)]
56struct Spark {
57 kind: SparkKind,
58 pos: Vec2,
59 vel: Vec2,
60 gravity: f32,
61 drag: f32,
62 size: f32,
63 color: Color,
64 angle: f32,
65 spin: f32,
66 age: f32,
67 life: f32,
68}
69
70#[derive(Clone, Copy, Debug)]
71struct Label {
72 bytes: [u8; MAX_LABEL],
73 len: u8,
74}
75
76impl Label {
77 fn new(text: &str) -> Self {
78 let mut end = text.len().min(MAX_LABEL);
79 while !text.is_char_boundary(end) {
80 end -= 1;
81 }
82 let mut bytes = [0; MAX_LABEL];
83 bytes[..end].copy_from_slice(&text.as_bytes()[..end]);
84 Self {
85 bytes,
86 len: end as u8,
87 }
88 }
89 fn as_str(&self) -> &str {
90 std::str::from_utf8(&self.bytes[..self.len as usize]).unwrap_or("")
92 }
93}
94
95#[derive(Clone, Copy, Debug)]
96struct Popup {
97 pos: Vec2,
98 label: Label,
99 icon: Option<Icon>,
100 color: Color,
101 size: f32,
102 age: f32,
103 life: f32,
104}
105
106#[derive(Clone, Copy, Debug)]
107struct Ring {
108 pos: Vec2,
109 radius: f32,
110 width: Option<f32>,
112 color: Color,
113 age: f32,
114 life: f32,
115}
116
117#[derive(Clone, Copy, Debug)]
118struct Toast {
119 label: Label,
120 icon: Option<Icon>,
121 color: Color,
122 age: f32,
123 life: f32,
124}
125
126#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
128pub struct JuiceCounts {
129 pub sparks: usize,
130 pub popups: usize,
131 pub rings: usize,
132 pub toast: bool,
133}
134
135#[derive(Clone, Debug)]
137pub struct Juice {
138 sparks: Vec<Spark>,
139 popups: Vec<Popup>,
140 rings: Vec<Ring>,
141 caps: [usize; 3],
142 victims: [usize; 3],
144 toast: Option<Toast>,
145 trauma: f32,
146 flash: Color,
147 vignette: Color,
148 time: f32,
149 seed: u32,
150 motion: Motion,
151 reduced_flash: bool,
152 words: bool,
153}
154
155impl Default for Juice {
156 fn default() -> Self {
157 Self::new()
158 }
159}
160
161impl Juice {
162 pub fn new() -> Self {
164 Self::with_capacity(DEFAULT_SPARKS, DEFAULT_POPUPS, DEFAULT_RINGS)
165 }
166 pub fn with_capacity(sparks: usize, popups: usize, rings: usize) -> Self {
168 let caps = [sparks.max(1), popups.max(1), rings.max(1)];
169 Self {
170 sparks: Vec::with_capacity(caps[0]),
171 popups: Vec::with_capacity(caps[1]),
172 rings: Vec::with_capacity(caps[2]),
173 caps,
174 victims: [0; 3],
175 toast: None,
176 trauma: 0.,
177 flash: Color::new(0., 0., 0., 0.),
178 vignette: Color::new(0., 0., 0., 0.),
179 time: 0.,
180 seed: 0x9e37_79b9,
181 motion: Motion::Full,
182 reduced_flash: false,
183 words: true,
184 }
185 }
186 pub fn set_motion(&mut self, motion: Motion) {
188 self.motion = motion;
189 if motion.is_reduced() {
190 self.trauma = 0.;
191 }
192 }
193 pub fn set_words(&mut self, enabled: bool) {
208 self.words = enabled;
209 if !enabled {
210 self.popups.clear();
211 self.toast = None;
212 }
213 }
214 pub fn set_reduced_flash(&mut self, reduced: bool) {
216 self.reduced_flash = reduced;
217 }
218 pub fn clear_words(&mut self) {
230 self.popups.clear();
231 self.toast = None;
232 self.victims[1] = 0;
233 }
234 pub fn clear(&mut self) {
236 self.sparks.clear();
237 self.popups.clear();
238 self.rings.clear();
239 self.victims = [0; 3];
240 self.toast = None;
241 self.trauma = 0.;
242 self.flash.a = 0.;
243 self.vignette.a = 0.;
244 }
245 pub fn is_idle(&self) -> bool {
247 self.sparks.is_empty()
248 && self.popups.is_empty()
249 && self.rings.is_empty()
250 && self.toast.is_none()
251 && self.trauma <= 0.
252 && self.flash.a <= 0.
253 && self.vignette.a <= 0.
254 }
255 pub fn counts(&self) -> JuiceCounts {
257 JuiceCounts {
258 sparks: self.sparks.len(),
259 popups: self.popups.len(),
260 rings: self.rings.len(),
261 toast: self.toast.is_some(),
262 }
263 }
264
265 fn random(&mut self) -> f32 {
266 let mut x = self.seed;
268 x ^= x << 13;
269 x ^= x >> 17;
270 x ^= x << 5;
271 self.seed = x;
272 (x >> 8) as f32 / (1u32 << 24) as f32
273 }
274 fn spread(&mut self, amount: f32) -> f32 {
275 (self.random() * 2. - 1.) * amount
276 }
277 fn scaled_count(&self, count: u32) -> u32 {
278 if self.motion.is_reduced() {
279 count.div_ceil(4)
280 } else {
281 count
282 }
283 }
284 fn push_spark(&mut self, spark: Spark) {
285 if !(spark.pos.is_finite() && spark.vel.is_finite() && spark.life > 0.) {
286 return;
287 }
288 push(&mut self.sparks, self.caps[0], &mut self.victims[0], spark);
289 }
290
291 pub fn burst(&mut self, at: Vec2, color: Color, count: u32) {
294 for _ in 0..self.scaled_count(count.min(256)) {
295 let angle = self.random() * TAU;
296 let speed = 70. + self.random() * 190.;
297 let life = 0.35 + self.random() * 0.45;
298 let size = 1.8 + self.random() * 2.6;
299 let tint = self.random() * 0.35;
300 let spark = Spark {
301 kind: SparkKind::Dot,
302 pos: at,
303 vel: vec2(angle.cos(), angle.sin()) * speed,
304 gravity: 220.,
305 drag: 3.2,
306 size,
307 color: shapes::mix(color, Color::new(1., 1., 1., color.a), tint),
308 angle: 0.,
309 spin: 0.,
310 age: 0.,
311 life,
312 };
313 self.push_spark(spark);
314 }
315 }
316 pub fn puff(&mut self, at: Vec2, color: Color, count: u32) {
327 for _ in 0..self.scaled_count(count.min(64)) {
328 let spark = Spark {
329 kind: SparkKind::Puff,
330 pos: at + vec2(self.spread(8.), self.spread(4.)),
331 vel: vec2(self.spread(22.), -26. - self.random() * 34.),
332 gravity: -12.,
333 drag: 1.6,
334 size: 4. + self.random() * 4.,
335 color,
336 angle: 0.,
337 spin: 0.,
338 age: -self.random() * 0.12,
339 life: 0.7 + self.random() * 0.6,
340 };
341 self.push_spark(spark);
342 }
343 }
344 pub fn sparkle(&mut self, at: Vec2, color: Color) {
346 for _ in 0..self.scaled_count(7) {
347 let offset = vec2(self.spread(22.), self.spread(18.));
348 let spark = Spark {
349 kind: SparkKind::Star,
350 pos: at + offset,
351 vel: vec2(self.spread(12.), -18. - self.random() * 30.),
352 gravity: 0.,
353 drag: 1.5,
354 size: 4. + self.random() * 5.,
355 color,
356 angle: self.random() * 0.6,
357 spin: self.spread(2.),
358 age: -self.random() * 0.18,
359 life: 0.55 + self.random() * 0.35,
360 };
361 self.push_spark(spark);
362 }
363 }
364 pub fn confetti(&mut self, at: Vec2, count: u32) {
366 const COLORS: [u32; 6] = [0xf7b553, 0xe5484d, 0x5bd5d8, 0x8fd16a, 0xb78cf2, 0xfff3d6];
367 for i in 0..self.scaled_count(count.min(256)) {
368 let angle = -std::f32::consts::FRAC_PI_2 + self.spread(1.1);
369 let speed = 180. + self.random() * 260.;
370 let spark = Spark {
371 kind: SparkKind::Confetti,
372 pos: at + vec2(self.spread(30.), self.spread(8.)),
373 vel: vec2(angle.cos(), angle.sin()) * speed,
374 gravity: 340.,
375 drag: 2.4,
376 size: 3. + self.random() * 3.,
377 color: Color::from_hex(COLORS[i as usize % COLORS.len()]),
378 angle: self.random() * TAU,
379 spin: self.spread(14.),
380 age: 0.,
381 life: 1.4 + self.random() * 1.,
382 };
383 self.push_spark(spark);
384 }
385 }
386 pub fn ring(&mut self, at: Vec2, color: Color, radius: f32) {
390 self.push_ring(at, color, radius, None);
391 }
392 pub fn ring_width(&mut self, at: Vec2, color: Color, radius: f32, width: f32) {
402 if width.is_finite() && width > 0. {
403 self.push_ring(at, color, radius, Some(width));
404 }
405 }
406 fn push_ring(&mut self, at: Vec2, color: Color, radius: f32, width: Option<f32>) {
407 if !(at.is_finite() && radius.is_finite() && radius > 0.) {
408 return;
409 }
410 let ring = Ring {
411 pos: at,
412 radius,
413 width,
414 color,
415 age: 0.,
416 life: if radius > 120. { 0.6 } else { 0.45 },
417 };
418 push(&mut self.rings, self.caps[2], &mut self.victims[2], ring);
419 }
420 pub fn popup(&mut self, at: Vec2, text: &str, color: Color) {
423 self.push_popup(at, None, text, color, 18.);
424 }
425 pub fn popup_icon(&mut self, at: Vec2, icon: Icon, text: &str, color: Color) {
428 self.push_popup(at, Some(icon), text, color, 18.);
429 }
430 pub fn popup_sized(
440 &mut self,
441 at: Vec2,
442 icon: Option<Icon>,
443 text: &str,
444 color: Color,
445 size: f32,
446 ) {
447 if size.is_finite() && size > 0. {
448 self.push_popup(at, icon, text, color, size.clamp(6., 64.));
449 }
450 }
451 pub fn callout(&mut self, at: Vec2, text: &str, color: Color) {
453 self.push_popup(at, None, text, color, 32.);
454 }
455 fn push_popup(&mut self, at: Vec2, icon: Option<Icon>, text: &str, color: Color, size: f32) {
456 if !at.is_finite() || !self.words {
457 return;
458 }
459 let mut pos = at;
461 for other in &self.popups {
462 if other.age < 0.25 && (other.pos - pos).length() < 14. {
463 pos.y -= size * 1.1;
464 }
465 }
466 let popup = Popup {
467 pos,
468 label: Label::new(text),
469 icon,
470 color,
471 size,
472 age: 0.,
473 life: if size > 24. { 1.5 } else { 1.05 },
474 };
475 push(&mut self.popups, self.caps[1], &mut self.victims[1], popup);
476 }
477 pub fn toast(&mut self, icon: Option<Icon>, text: &str, color: Color) {
480 if !self.words {
481 return;
482 }
483 self.toast = Some(Toast {
484 label: Label::new(text),
485 icon,
486 color,
487 age: 0.,
488 life: 2.6,
489 });
490 }
491 pub fn shake(&mut self, trauma: f32) {
493 if trauma.is_finite() && !self.motion.is_reduced() {
494 self.trauma = (self.trauma + trauma.max(0.)).min(1.);
495 }
496 }
497 pub fn flash(&mut self, color: Color) {
499 if color.a.is_finite() && color.a > self.flash.a {
500 self.flash = color;
501 }
502 }
503 pub fn vignette(&mut self, color: Color) {
505 if color.a.is_finite() && color.a > self.vignette.a {
506 self.vignette = color;
507 }
508 }
509
510 pub fn update(&mut self, dt: f32) {
512 let dt = if dt.is_finite() {
513 dt.clamp(0., 0.1)
514 } else {
515 0.
516 };
517 self.time += dt;
518 let still = self.motion.is_reduced();
519 self.sparks.retain_mut(|s| {
520 s.age += dt;
521 if s.age < 0. {
522 return true;
523 }
524 if !still || s.kind == SparkKind::Confetti {
525 s.vel *= (-s.drag * dt).exp();
526 s.vel.y += s.gravity * dt;
527 s.pos += s.vel * dt;
528 }
529 s.angle += s.spin * dt;
530 s.age < s.life
531 });
532 self.popups.retain_mut(|p| {
533 p.age += dt;
534 p.age < p.life
535 });
536 self.rings.retain_mut(|r| {
537 r.age += dt;
538 r.age < r.life
539 });
540 if let Some(t) = &mut self.toast {
541 t.age += dt;
542 if t.age >= t.life {
543 self.toast = None;
544 }
545 }
546 self.victims = [0; 3];
547 self.trauma = (self.trauma - dt * 1.8).max(0.);
548 self.flash.a = (self.flash.a - dt * 2.2).max(0.);
549 self.vignette.a = (self.vignette.a - dt * 1.6).max(0.);
550 }
551
552 pub fn shake_offset(&self) -> Vec2 {
555 if self.trauma <= 0. {
556 return Vec2::ZERO;
557 }
558 let amount = 7. * self.trauma * self.trauma;
559 let t = self.time;
560 vec2(
561 (t * 63.).sin() * 0.6 + (t * 97.).sin() * 0.4,
562 (t * 71.).cos() * 0.6 + (t * 109.).cos() * 0.4,
563 ) * amount
564 }
565
566 pub fn draw(&self) {
568 for r in &self.rings {
569 let t = (r.age / r.life).clamp(0., 1.);
570 let grow = Ease::CubicOut.apply(t);
571 let alpha = r.color.a * (1. - t) * (1. - t);
572 let width = (r.width.unwrap_or(r.radius * 0.18) * (1. - t)).max(1.);
573 shapes::stroke_circle(
574 r.pos,
575 r.radius * (0.25 + 0.75 * grow),
576 width,
577 with_alpha(r.color, alpha),
578 );
579 }
580 for s in &self.sparks {
581 if s.age < 0. {
582 continue;
583 }
584 let t = (s.age / s.life).clamp(0., 1.);
585 let fade = 1. - t * t;
586 match s.kind {
587 SparkKind::Dot => {
588 let radius = s.size * (1. - 0.6 * t);
589 shapes::fill_circle(s.pos, radius, with_alpha(s.color, s.color.a * fade));
590 }
591 SparkKind::Puff => {
592 let radius = s.size * (1. + 1.6 * t);
594 let a = s.color.a * (1. - t) * (1. - t);
595 shapes::fill_circle(s.pos, radius, with_alpha(s.color, a * 0.55));
596 shapes::fill_circle(s.pos, radius * 0.6, with_alpha(s.color, a * 0.45));
597 }
598 SparkKind::Star => {
599 let scale = (t * std::f32::consts::PI).sin();
601 star(
602 s.pos,
603 s.size * scale,
604 s.angle,
605 with_alpha(s.color, s.color.a * fade),
606 );
607 }
608 SparkKind::Confetti => {
609 let flutter = (s.angle * 1.7).cos().abs().max(0.2);
610 let (sin, cos) = s.angle.sin_cos();
611 let ax = vec2(cos, sin) * s.size * 1.4;
612 let ay = vec2(-sin, cos) * s.size * 0.7 * flutter;
613 let tail = if t > 0.7 { (1. - t) / 0.3 } else { 1. };
614 shapes::fill_polygon(
615 &[
616 s.pos - ax - ay,
617 s.pos + ax - ay,
618 s.pos + ax + ay,
619 s.pos - ax + ay,
620 ],
621 with_alpha(s.color, s.color.a * tail),
622 );
623 }
624 }
625 }
626 for p in &self.popups {
627 let t = (p.age / p.life).clamp(0., 1.);
628 let pop = Ease::BackOut.apply((p.age / 0.22).clamp(0., 1.));
629 let rise = if self.motion.is_reduced() {
630 0.
631 } else {
632 Ease::CubicOut.apply(t) * p.size * 1.8
633 };
634 let alpha = if t > 0.6 { 1. - (t - 0.6) / 0.4 } else { 1. } * p.color.a;
635 let size = p.size * (0.55 + 0.45 * pop);
636 let at = p.pos - vec2(0., rise);
637 let text = p.label.as_str();
638 let style = TextStyle::modern(size, with_alpha(p.color, alpha))
639 .with_shadow(Color::new(0., 0., 0., 0.55 * alpha))
640 .with_align(TextAlign::Left)
641 .with_pixel_snap(false);
642 let text_w = if text.is_empty() {
643 0.
644 } else {
645 text_width(text, style)
646 };
647 let icon_w = if p.icon.is_some() { size * 1.15 } else { 0. };
648 let gap = if p.icon.is_some() && !text.is_empty() {
649 size * 0.2
650 } else {
651 0.
652 };
653 let left = at.x - (icon_w + gap + text_w) * 0.5;
654 if let Some(icon) = p.icon {
655 icons::draw_styled(
656 icon,
657 vec2(left + icon_w * 0.5, at.y),
658 icon_w,
659 icons::IconStyle::new(with_alpha(p.color, alpha)).shadow(Color::new(
660 0.,
661 0.,
662 0.,
663 0.45 * alpha,
664 )),
665 );
666 }
667 if !text.is_empty() {
668 draw_text_styled(
669 text,
670 left + icon_w + gap,
671 style.centered_baseline(at.y),
672 style,
673 );
674 }
675 }
676 }
677
678 pub fn draw_flash(&self, area: Rect) {
680 let limit = if self.reduced_flash { 0.12 } else { 1. };
681 if self.flash.a > 0.003 {
682 shapes::fill_rounded_rect(area, 0., with_alpha(self.flash, self.flash.a.min(limit)));
683 }
684 let a = self.vignette.a.min(limit * 0.9);
685 if a > 0.003 {
686 let edge = area.w.min(area.h) * 0.2;
687 let solid = with_alpha(self.vignette, a);
688 let clear = with_alpha(self.vignette, 0.);
689 let top = Rect::new(area.x, area.y, area.w, edge);
690 let bottom = Rect::new(area.x, area.y + area.h - edge, area.w, edge);
691 let left = Rect::new(area.x, area.y, edge, area.h);
692 let right = Rect::new(area.x + area.w - edge, area.y, edge, area.h);
693 shapes::fill_rounded_rect(top, 0., Fill::vertical(top, solid, clear));
694 shapes::fill_rounded_rect(bottom, 0., Fill::vertical(bottom, clear, solid));
695 shapes::fill_rounded_rect(left, 0., Fill::horizontal(left, solid, clear));
696 shapes::fill_rounded_rect(right, 0., Fill::horizontal(right, clear, solid));
697 }
698 }
699
700 pub fn draw_toast(&self, anchor: Vec2) {
702 let Some(toast) = &self.toast else {
703 return;
704 };
705 let t = toast.age / toast.life;
706 let enter = Ease::BackOut.apply((toast.age / 0.3).clamp(0., 1.));
707 let alpha = if t > 0.8 { 1. - (t - 0.8) / 0.2 } else { 1. }.clamp(0., 1.);
708 let slide = if self.motion.is_reduced() {
709 0.
710 } else {
711 (1. - enter) * 18.
712 };
713 let text = toast.label.as_str();
714 let style = TextStyle::modern(16., with_alpha(Color::new(0.96, 0.97, 0.98, 1.), alpha));
715 let text_w = text_width(text, style);
716 let icon_w = if toast.icon.is_some() { 22. } else { 0. };
717 let w = text_w + icon_w + if toast.icon.is_some() { 30. } else { 24. };
718 let h = 34.;
719 let r = Rect::new(anchor.x - w * 0.5, anchor.y - h * 0.5 + slide, w, h);
720 shapes::drop_shadow(
721 r,
722 h * 0.5,
723 shapes::Shadow::soft(Color::new(0., 0., 0., 0.45 * alpha))
724 .offset(vec2(0., 4.))
725 .blur(14.),
726 );
727 shapes::fill_rounded_rect(r, h * 0.5, Color::new(0.06, 0.08, 0.11, 0.92 * alpha));
728 shapes::stroke_rounded_rect(r, h * 0.5, 1.5, with_alpha(toast.color, 0.8 * alpha));
729 let mut x = r.x + 12.;
730 if let Some(icon) = toast.icon {
731 icons::draw(
732 icon,
733 vec2(x + icon_w * 0.5, r.y + h * 0.5),
734 icon_w,
735 with_alpha(toast.color, alpha),
736 );
737 x += icon_w + 6.;
738 }
739 draw_text_styled(text, x, style.centered_baseline(r.y + h * 0.5), style);
740 }
741}
742
743fn push<T>(pool: &mut Vec<T>, cap: usize, victim: &mut usize, item: T) {
744 if pool.len() < cap {
745 pool.push(item);
746 } else {
747 let slot = *victim % cap;
749 pool[slot] = item;
750 *victim = slot + 1;
751 }
752}
753
754fn with_alpha(c: Color, a: f32) -> Color {
755 Color::new(c.r, c.g, c.b, a.clamp(0., 1.))
756}
757
758fn star(at: Vec2, r: f32, angle: f32, color: Color) {
760 if r <= 0.2 {
761 return;
762 }
763 let (s, c) = angle.sin_cos();
764 let ax = vec2(c, s);
765 let ay = vec2(-s, c);
766 let thin = r * 0.28;
767 shapes::fill_polygon(
768 &[at - ax * r, at - ay * thin, at + ax * r, at + ay * thin],
769 color,
770 );
771 shapes::fill_polygon(
772 &[at - ay * r, at + ax * thin, at + ay * r, at - ax * thin],
773 color,
774 );
775 shapes::fill_circle(
776 at,
777 thin * 0.9,
778 with_alpha(Color::new(1., 1., 1., 1.), color.a * 0.8),
779 );
780}
781
782pub fn pulse(time: f32, hz: f32) -> f32 {
789 0.5 + 0.5 * (time * hz * TAU - std::f32::consts::FRAC_PI_2).sin()
790}
791
792pub fn bob(time: f32, hz: f32, amplitude: f32) -> f32 {
799 (time * hz * TAU).sin() * amplitude
800}
801
802#[cfg(test)]
803mod tests {
804 use super::*;
805 use crate::shapes::tests::capture;
806
807 fn white() -> Color {
808 Color::new(1., 1., 1., 1.)
809 }
810
811 #[test]
812 fn pools_are_bounded_and_recycle_oldest() {
813 let mut fx = Juice::with_capacity(16, 3, 2);
814 for i in 0..10 {
815 fx.burst(vec2(i as f32, 0.), white(), 40);
816 fx.popup(vec2(0., i as f32 * 100.), "+1", white());
817 fx.ring(vec2(0., 0.), white(), 20.);
818 }
819 let c = fx.counts();
820 assert_eq!((c.sparks, c.popups, c.rings), (16, 3, 2));
821 assert!(fx.popups.iter().any(|p| p.pos.y == 900.));
823 }
824
825 #[test]
826 fn effects_expire_and_shake_settles() {
827 let mut fx = Juice::new();
828 fx.burst(vec2(10., 10.), white(), 30);
829 fx.sparkle(vec2(10., 10.), white());
830 fx.confetti(vec2(10., 10.), 40);
831 fx.puff(vec2(10., 10.), white(), 10);
832 fx.ring(vec2(10., 10.), white(), 40.);
833 fx.popup_icon(vec2(10., 10.), Icon::Coin, "+5", white());
834 fx.callout(vec2(10., 10.), "PERFECT", white());
835 fx.toast(Some(Icon::Check), "Order shipped", white());
836 fx.shake(2.);
837 fx.flash(Color::new(1., 1., 1., 0.5));
838 fx.vignette(Color::new(1., 0., 0., 0.6));
839 assert!(!fx.is_idle());
840 fx.update(1. / 60.);
841 assert_ne!(fx.shake_offset(), Vec2::ZERO);
842 assert!(fx.shake_offset().length() <= 7. * std::f32::consts::SQRT_2 + 0.01);
843 for _ in 0..60 * 4 {
844 fx.update(1. / 60.);
845 }
846 assert!(fx.is_idle(), "{:?}", fx.counts());
847 assert_eq!(fx.shake_offset(), Vec2::ZERO);
848 }
849
850 #[test]
851 fn reduced_motion_removes_shake_and_thins_particles() {
852 let mut fx = Juice::new();
853 fx.set_motion(Motion::Reduced);
854 fx.shake(1.);
855 assert_eq!(fx.shake_offset(), Vec2::ZERO);
856 fx.burst(vec2(0., 0.), white(), 40);
857 assert_eq!(fx.counts().sparks, 10);
858 let before = fx.sparks[0].pos;
859 fx.update(0.1);
860 assert_eq!(fx.sparks[0].pos, before, "reduced-motion sparks stay put");
861 }
862
863 #[test]
864 fn invalid_requests_are_ignored_and_labels_truncate_on_char_boundaries() {
865 let mut fx = Juice::new();
866 fx.burst(vec2(f32::NAN, 0.), white(), 10);
867 fx.ring(vec2(0., 0.), white(), -1.);
868 fx.popup(vec2(f32::INFINITY, 0.), "x", white());
869 fx.shake(f32::NAN);
870 fx.update(f32::NAN);
871 assert!(fx.is_idle());
872 let long = "é".repeat(40);
873 let label = Label::new(&long);
874 assert!(label.as_str().len() <= MAX_LABEL && label.as_str().chars().all(|c| c == 'é'));
875 assert_eq!(Label::new("+25").as_str(), "+25");
876 }
877
878 #[test]
879 fn drawing_every_effect_is_finite() {
880 let mut fx = Juice::new();
881 fx.burst(vec2(100., 100.), white(), 20);
882 fx.sparkle(vec2(100., 100.), white());
883 fx.confetti(vec2(100., 100.), 20);
884 fx.puff(vec2(100., 100.), white(), 6);
885 fx.ring(vec2(100., 100.), white(), 30.);
886 fx.ring_width(vec2(100., 100.), white(), 90., 2.);
887 fx.popup_icon(vec2(100., 100.), Icon::Star, "+10", white());
888 fx.toast(Some(Icon::Alert), "Careful", white());
889 fx.flash(Color::new(1., 1., 1., 0.4));
890 fx.vignette(Color::new(1., 0., 0., 0.4));
891 fx.update(0.05);
892 let v = capture(320, 240, || {
893 fx.draw();
894 fx.draw_flash(Rect::new(0., 0., 320., 240.));
895 fx.draw_toast(vec2(160., 200.));
896 });
897 assert!(!v.is_empty());
898 assert!(v.iter().flatten().all(|x| x.is_finite()));
899 }
900
901 #[test]
902 fn pulse_and_bob_are_periodic() {
903 assert!(pulse(0., 2.).abs() < 1e-5);
904 assert!((pulse(0.25, 2.) - 1.).abs() < 1e-5);
905 assert!((pulse(0.5, 1.) - 1.).abs() < 1e-5);
906 assert!((bob(0.25, 1., 3.) - 3.).abs() < 1e-4);
907 }
908}