1use crate::avatar::{Eye, Face, Shape};
6use gpui::{
7 AnyElement, Bounds, Hsla, IntoElement, Pixels, Styled, Window, canvas, fill, point, px, size,
8};
9use theme::Theme;
10
11pub const GRID: usize = 8;
13
14const SQUEEZE: f32 = 0.92;
17
18fn cells(shape: &Shape, eyes: &[Eye; 2], lid: f32) -> [u8; GRID] {
20 let span = shape
23 .outline()
24 .iter()
25 .fold(0.0f32, |far, (x, y)| far.max(x.hypot(*y)));
26
27 let mut rows = [0u8; GRID];
28 for (y, row) in rows.iter_mut().enumerate() {
29 let uy = unit(y, span);
30 for x in 0..GRID {
31 let ux = unit(x, span);
32 if ux.hypot(uy) <= shape.reach(ux, uy) {
36 *row |= 1 << x;
37 }
38 }
39 }
40 for eye in eyes {
41 punch(&mut rows, eye, lid, span);
42 }
43 rows
44}
45
46fn unit(i: usize, span: f32) -> f32 {
48 ((i as f32 + 0.5) / GRID as f32 * 2.0 - 1.0) * span
49}
50
51fn punch(rows: &mut [u8; GRID], eye: &Eye, lid: f32, span: f32) {
52 let ry = (eye.ry * (1.0 - lid * SQUEEZE)).max(f32::EPSILON);
53 let (sin, cos) = eye.rot.to_radians().sin_cos();
54 let mut hit = false;
55 for (y, row) in rows.iter_mut().enumerate() {
56 for x in 0..GRID {
57 let (dx, dy) = (unit(x, span) - eye.cx, unit(y, span) - eye.cy);
58 let (a, b) = (dx * cos + dy * sin, dy * cos - dx * sin);
59 if (a / eye.rx).abs().powf(eye.n) + (b / ry).abs().powf(eye.n) <= 1.0 {
60 *row &= !(1 << x);
61 hit = true;
62 }
63 }
64 }
65 if hit || lid >= 0.5 {
66 return;
67 }
68 for step in 0..=GRID {
72 let k = 1.0 - step as f32 / GRID as f32;
73 let (x, y) = (cell(eye.cx * k, span), cell(eye.cy * k, span));
74 if rows[y] >> x & 1 == 1 {
75 rows[y] &= !(1 << x);
76 return;
77 }
78 }
79}
80
81fn cell(u: f32, span: f32) -> usize {
83 (((u / span + 1.0) / 2.0 * GRID as f32) as usize).min(GRID - 1)
84}
85
86pub fn mascot(face: &Face, t: f32) -> AnyElement {
88 let rows = cells(
92 &face.shape,
93 &face.eyes.place(&face.shape, 0.0),
94 face.motion.beat(t).lid,
95 );
96 let tint = face.color;
97 canvas(
98 move |_: Bounds<Pixels>, _, _| (),
99 move |bounds, (), window, cx| {
100 let color = tint.unwrap_or(Theme::of(cx).accent);
101 paint(window, bounds, &rows, color);
102 },
103 )
104 .size_full()
105 .into_any_element()
106}
107
108fn paint(window: &mut Window, bounds: Bounds<Pixels>, rows: &[u8; GRID], color: Hsla) {
109 let scale = window.scale_factor();
110 let snap = |v: f32| (v * scale).round() / scale;
111 let box_side = bounds.size.width.min(bounds.size.height).to_f64() as f32;
112 let side = ((box_side * scale / GRID as f32).floor() / scale).max(1.0 / scale);
114 let sprite = side * GRID as f32;
115 let left = snap(bounds.center().x.to_f64() as f32 - sprite / 2.0);
116 let top = snap(bounds.center().y.to_f64() as f32 - sprite / 2.0);
117
118 for (y, row) in rows.iter().enumerate() {
119 let mut x = 0;
120 while x < GRID {
121 if row >> x & 1 == 0 {
122 x += 1;
123 continue;
124 }
125 let mut run = 1;
127 while x + run < GRID && row >> (x + run) & 1 == 1 {
128 run += 1;
129 }
130 window.paint_quad(fill(
131 Bounds::new(
132 point(px(left + side * x as f32), px(top + side * y as f32)),
133 size(px(side * run as f32), px(side)),
134 ),
135 color,
136 ));
137 x += run;
138 }
139 }
140}