sector-f-linux

Sector F Linux
git clone https://git.sectorflabs.com/sector-f-linux.git
Log | Files | Refs | README | LICENSE

flask.rs (4607B)


      1 //! The Sector F flask, the animation of the boot splash (linux/branding/plymouth-theme.py draws
      2 //! the same frames for Plymouth): the liquid rises and falls, bubbles rise through it, and the two
      3 //! circles at the end of the pipe pulse. Drawn as an SVG per frame, in the skin's colours, and
      4 //! rasterised by egui_extras; a loop is `FRAMES` frames, so the images are cached by their URI.
      5 
      6 use std::fmt::Write;
      7 
      8 use egui::{Color32, Ui, vec2};
      9 
     10 /// Frames a second: the loop is drawn at this rate and no faster, since a computer with no GPU
     11 /// (software rendering) would spend the install's CPU on drawing.
     12 pub const FPS: usize = 24;
     13 /// Seconds for one loop.
     14 pub const LOOP: f64 = 3.0;
     15 pub const FRAMES: usize = FPS * LOOP as usize;
     16 
     17 const OUTER: &str = "M388,385 V515 L252,697 Q222,738 222,785 Q230,900 510,905 Q790,900 798,785 \
     18                      Q798,738 768,697 L632,515 V385 Z";
     19 const INNER: &str = "M428,385 V528 L287,720 Q262,758 262,790 Q270,865 510,865 Q750,865 758,790 \
     20                      Q758,758 733,720 L592,528 V385 Z";
     21 
     22 fn hex(c: Color32) -> String {
     23     format!("#{:02X}{:02X}{:02X}", c.r(), c.g(), c.b())
     24 }
     25 
     26 /// Frame `i` of the loop, as SVG text.
     27 pub fn frame_svg(i: usize, glass: Color32, liquid: Color32, background: Color32) -> String {
     28     let t = i as f64 / FRAMES as f64;
     29     let phase = 2.0 * std::f64::consts::PI * t;
     30     let level = 700.0 - 95.0 * phase.sin();
     31     let mut wave = String::new();
     32     let mut first = 0.0;
     33     let mut x = 200;
     34     while x <= 820 {
     35         let y = level + 9.0 * (phase * 2.0 + x as f64 / 55.0).sin();
     36         if x == 200 {
     37             first = y;
     38         }
     39         let _ = write!(wave, " L{x},{y:.1}");
     40         x += 20;
     41     }
     42     let liquid_path = format!("M200,1000 L200,{first:.1}{wave} L820,1000 Z");
     43     let mut bubbles = String::new();
     44     for (k, (bx, speed, r)) in [
     45         (420.0, 1.0, 17),
     46         (520.0, 2.0, 12),
     47         (600.0, 1.0, 20),
     48         (470.0, 3.0, 9),
     49     ]
     50     .into_iter()
     51     .enumerate()
     52     {
     53         let u = (t * speed + k as f64 * 0.23) % 1.0;
     54         let y = 870.0 - u * (870.0 - level);
     55         if y > level + 12.0 {
     56             let _ = write!(
     57                 bubbles,
     58                 r#"<circle cx="{:.1}" cy="{y:.1}" r="{r}" fill="none" stroke="{}" stroke-opacity=".55" stroke-width="7"/>"#,
     59                 bx + 12.0 * (u * 9.0 + k as f64).sin(),
     60                 hex(background)
     61             );
     62         }
     63     }
     64     let r1 = 40.0 + 3.0 * phase.sin();
     65     let r2 = 40.0 + 3.0 * (phase + std::f64::consts::PI).sin();
     66     let (glass, liquid) = (hex(glass), hex(liquid));
     67     format!(
     68         r#"<svg xmlns="http://www.w3.org/2000/svg" width="700" height="835" viewBox="150 105 700 835">
     69   <defs><clipPath id="belly"><path d="{INNER}"/></clipPath></defs>
     70   <g fill="none" stroke="{glass}" stroke-linejoin="round" stroke-linecap="round">
     71     <path d="M181,297 H664 M285,297 L398,172 H548" stroke-width="32"/>
     72     <circle cx="591" cy="178" r="{r1:.1}" stroke-width="37"/>
     73     <circle cx="710" cy="293" r="{r2:.1}" stroke-width="37"/>
     74   </g>
     75   <g clip-path="url(#belly)"><path d="{liquid_path}" fill="{liquid}"/>{bubbles}</g>
     76   <path fill="{glass}" fill-rule="evenodd" d="{OUTER} {INNER}"/>
     77   <rect x="350" y="352" width="318" height="40" rx="20" fill="{glass}"/>
     78 </svg>"#
     79     )
     80 }
     81 
     82 /// Which frame of the loop it is `seconds` in.
     83 pub fn frame_at(seconds: f64) -> usize {
     84     ((seconds % LOOP) / LOOP * FRAMES as f64) as usize % FRAMES
     85 }
     86 
     87 /// Draws the flask `height` px tall, at the frame for `seconds`, and, if `animate`, asks for the
     88 /// next repaint.
     89 pub fn show(ui: &mut Ui, seconds: f64, height: f32, animate: bool) {
     90     let v = ui.visuals();
     91     let (glass, liquid, bg) = (v.text_color(), v.selection.bg_fill, v.panel_fill);
     92     let i = frame_at(seconds);
     93     let svg = frame_svg(i, glass, liquid, bg);
     94     let uri = format!(
     95         "bytes://flask-{i}-{}{}{}.svg",
     96         hex(glass),
     97         hex(liquid),
     98         hex(bg)
     99     );
    100     ui.add(
    101         egui::Image::from_bytes(uri, svg.into_bytes())
    102             .fit_to_exact_size(vec2(height * 700.0 / 835.0, height)),
    103     );
    104     if animate {
    105         ui.ctx()
    106             .request_repaint_after(std::time::Duration::from_millis(1000 / FPS as u64));
    107     }
    108 }
    109 
    110 #[cfg(test)]
    111 mod tests {
    112     use super::*;
    113 
    114     #[test]
    115     fn frames_are_svg_and_loop() {
    116         let c = Color32::from_rgb(1, 2, 3);
    117         let a = frame_svg(0, c, c, c);
    118         assert!(a.starts_with("<svg") && a.ends_with("</svg>"));
    119         assert_ne!(a, frame_svg(15, c, c, c));
    120         assert_eq!(frame_at(0.0), frame_at(LOOP));
    121         assert_eq!(frame_at(LOOP - 0.001), FRAMES - 1);
    122     }
    123 }