Skip to main content

cellix_render/
spinner.rs

1//! The one mark that says something is under way: a faint ring with a quarter of it in full
2//! colour, turning, where there are pictures, and turning braille on cells. Anything places it
3//! on the grid (`Grid::spinner`), in the colour of what it means. Every spinner on a grid
4//! stands at the same step, which the grid is given (`Grid::turn`), so one clock turns them
5//! all and a turn is a cycle of ten pictures: once a row's ten tiles are in the terminal,
6//! turning sends nothing but placements.
7//!
8//! With motion off a spinner is a still dot in its colour, and asks for no frames.
9
10use std::sync::OnceLock;
11use std::time::{Duration, Instant};
12
13/// Steps in one turn: the braille frames, and as many places for the ring's arc.
14pub const STEPS: u8 = 10;
15/// How long a step lasts: a turn in 0.9 s.
16pub const STEP: Duration = Duration::from_millis(90);
17/// The cell tier's frames, one a step.
18pub const FRAMES: [&str; STEPS as usize] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
19/// What stands for a spinner on cells with motion off, and for a state's dot: a still dot.
20pub const STILL: &str = "●";
21
22/// The clock every spinner turns by: time since it was first read. A test gives its grid a
23/// step of its own instead.
24pub fn clock() -> Duration {
25    static START: OnceLock<Instant> = OnceLock::new();
26    START.get_or_init(Instant::now).elapsed()
27}
28
29/// The step a spinner stands at, at a time on that clock.
30pub fn step_at(time: Duration) -> u8 {
31    ((time.as_millis() / STEP.as_millis()) % u128::from(STEPS)) as u8
32}
33
34/// How long from a time on that clock until the next step begins: when a grid with something
35/// turning on it wants drawing again.
36pub fn until_next(time: Duration) -> Duration {
37    let step = STEP.as_millis();
38    let into = time.as_millis() % step;
39    Duration::from_millis((step - into) as u64)
40}
41
42/// How much of a pixel at (u, v) in the spinner's square box is inked, as (the ring, the arc),
43/// each 0 to 1: the ring is drawn at a quarter of the colour's strength and the arc over it at
44/// full. The box is 24 units across, as an icon's, the ring 9 from the middle with a stroke
45/// of 3.2, and the arc a quarter of it with round ends, starting at the top and going round
46/// a tenth of a turn a step. `None` is the still dot: a disc half the box across.
47pub fn inked(step: Option<u8>, u: f32, v: f32) -> (bool, bool) {
48    let (x, y) = (u * 24.0 - 12.0, v * 24.0 - 12.0);
49    let distance = (x * x + y * y).sqrt();
50    let Some(step) = step else {
51        return (false, distance < 6.0);
52    };
53    const RADIUS: f32 = 9.0;
54    const HALF: f32 = 1.6;
55    let ring = (distance - RADIUS).abs() < HALF;
56    // Angles clockwise from the top, as a turn's share: 0 at the top, 0.25 at the right.
57    let at = |x: f32, y: f32| (x.atan2(-y) / std::f32::consts::TAU).rem_euclid(1.0);
58    let start = f32::from(step) / f32::from(STEPS);
59    let into = (at(x, y) - start).rem_euclid(1.0);
60    let arc = if into <= 0.25 {
61        ring
62    } else {
63        // Past either end, the round cap: within a stroke's half of the end's middle.
64        let end = |turn: f32| {
65            let angle = turn * std::f32::consts::TAU;
66            let (ex, ey) = (RADIUS * angle.sin(), -RADIUS * angle.cos());
67            ((x - ex).powi(2) + (y - ey).powi(2)).sqrt() < HALF
68        };
69        end(start) || end(start + 0.25)
70    };
71    (ring, arc)
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn a_turn_is_ten_steps_in_under_a_second() {
80        assert_eq!(STEP * u32::from(STEPS), Duration::from_millis(900));
81        assert_eq!(step_at(Duration::ZERO), 0);
82        assert_eq!(step_at(Duration::from_millis(89)), 0);
83        assert_eq!(step_at(Duration::from_millis(90)), 1);
84        assert_eq!(step_at(Duration::from_millis(899)), 9);
85        assert_eq!(step_at(Duration::from_millis(900)), 0, "round again");
86        assert_eq!(until_next(Duration::from_millis(0)), STEP);
87        assert_eq!(
88            until_next(Duration::from_millis(100)),
89            Duration::from_millis(80)
90        );
91        assert_eq!(
92            until_next(Duration::from_millis(179)),
93            Duration::from_millis(1)
94        );
95    }
96
97    /// Where the arc is, as the share of a turn its ink's middle stands at from the top.
98    fn arc_middle(step: u8) -> f32 {
99        let (mut sx, mut sy, mut count) = (0.0, 0.0, 0);
100        for j in 0..96 {
101            for i in 0..96 {
102                let (u, v) = ((i as f32 + 0.5) / 96.0, (j as f32 + 0.5) / 96.0);
103                if inked(Some(step), u, v).1 {
104                    sx += u - 0.5;
105                    sy += v - 0.5;
106                    count += 1;
107                }
108            }
109        }
110        assert!(count > 0);
111        (f32::atan2(sx, -sy) / std::f32::consts::TAU).rem_euclid(1.0)
112    }
113
114    #[test]
115    fn the_arc_is_a_quarter_of_the_ring_and_goes_round_a_tenth_a_step() {
116        // At the first step it's the top right quarter: its middle an eighth of a turn round.
117        assert!((arc_middle(0) - 0.125).abs() < 0.01, "{}", arc_middle(0));
118        for step in 1..STEPS {
119            let moved = (arc_middle(step) - arc_middle(step - 1)).rem_euclid(1.0);
120            assert!((moved - 0.1).abs() < 0.01, "step {step} moved {moved}");
121        }
122        // A quarter of the ring, and its round ends past that: each a half stroke, 1.6 of
123        // the ring's 9, about 0.028 of a turn. Nothing of it is off the ring.
124        let (mut ring, mut arc, mut off) = (0, 0, 0);
125        for j in 0..96 {
126            for i in 0..96 {
127                let (r, a) = inked(Some(3), (i as f32 + 0.5) / 96.0, (j as f32 + 0.5) / 96.0);
128                ring += usize::from(r);
129                arc += usize::from(a && r);
130                off += usize::from(a && !r);
131            }
132        }
133        let share = arc as f32 / ring as f32;
134        let ends = 2.0 * 1.6 / 9.0 / std::f32::consts::TAU;
135        assert!(share > 0.25 && share < 0.25 + ends + 0.01, "{share}");
136        assert!(off < arc / 50, "{off} of {arc} off the ring");
137    }
138
139    #[test]
140    fn with_motion_off_it_is_a_still_dot() {
141        assert_eq!(inked(None, 0.5, 0.5), (false, true));
142        assert_eq!(inked(None, 0.05, 0.5), (false, false));
143        assert_eq!(inked(None, 0.2, 0.5), (false, false), "no ring round it");
144    }
145}