diff --git a/src/avatar.rs b/src/avatar.rs index f77bf51..730e871 100644 --- a/src/avatar.rs +++ b/src/avatar.rs @@ -4,8 +4,9 @@ use eframe::egui::{self, pos2, vec2, Color32, Pos2, Rect, Shape, Stroke}; use std::f32::consts::PI; -pub const SHAPES: [&str; 12] = [ - "squircle", "blob", "capsule", "egg", "hex", "gem", "cloud", "arch", "star", "heart", "shield", "pebble", +pub const SHAPES: [&str; 14] = [ + "squircle", "blob", "capsule", "egg", "hex", "gem", "cloud", "arch", "star", "heart", "shield", "pebble", "moon", + "pumpkin", ]; pub fn shape_label(s: &str) -> &'static str { @@ -22,10 +23,41 @@ pub fn shape_label(s: &str) -> &'static str { "heart" => "愛心", "shield" => "盾牌", "pebble" => "石頭", + "moon" => "月牙", + "pumpkin" => "南瓜", _ => "圓球", } } +/// Where the face sits, in units of the shape's half-size: (inset, dx, dy, eye-separation). +/// +/// The eyes are placed from the outline, so a concave shape (a star's rays, a moon's +/// wedge) has less real room than its bounding box looks, and the eyes cross the +/// outline at the resting size. Each shape declares how much of its half-size the face +/// may use, the offset to its solid part, and how far apart the eyes sit; the eyes +/// scale and move by it. `paint` and `paint_posed` both read this, so a thinking +/// face's eyes do not jump. +fn face_fit(shape: &str) -> (f32, f32, f32, f32) { + match shape { + // The star's solid core is the pentagon under the rays (inradius ~0.48s): + // small eyes, drawn closer together so they keep a face's spacing. + "star" => (0.52, 0.0, 0.08, 0.20), + // The moon keeps its wedge (mouth) on the right; the face rides left of it. + "moon" => (0.55, -0.3, 0.0, 0.27), + // The pumpkin's face rides on the middle row of its ribs, a touch low. + "pumpkin" => (0.70, 0.0, -0.02, 0.27), + _ => (1.0, 0.0, 0.0, 0.27), + } +} + +/// Where the eyes rest, in half-size units of the shape (centre at the origin): +/// each eye's (centre, half-width, half-height). The paint paths draw from this, and +/// the tests below check the eyes stay inside every shape's body. +pub fn eye_rects(shape: &str) -> [(Pos2, f32, f32); 2] { + let (k, fx, fy, sep) = face_fit(shape); + [-sep, sep].map(|dx| (pos2(dx * k + fx, 0.02 + fy), 0.11 * k, 0.22 * k)) +} + /// Bot colours, from the original's bubble fills (index CSS --sand-fill-*-bubble-user). pub const COLORS: [(&str, &str, [u8; 3]); 10] = [ ("black", "黑色", [0x1c, 0x1c, 0x1c]), @@ -191,21 +223,23 @@ pub fn paint_posed(painter: &egui::Painter, rect: Rect, shape: &str, col: Color3 let bs = body.width().min(body.height()) / 2.0; let eye = eye_colour(col); let open = (1.0 - pose.blink).clamp(0.06, 1.0); + let (k, fx, fy, sep) = face_fit(shape); let look = vec2(pose.gaze.x * 0.16 * s * turn, -pose.gaze.y * 0.12 * s + pose.pitch * 0.01 * s); - for dx in [-0.27, 0.27] { - let center = spin_about(pos2(c.x + dx * body.width() / 2.0, c.y + 0.02 * bs) + look, c, spin); + for dx in [-sep, sep] { + let center = + spin_about(pos2(c.x + dx * k * body.width() / 2.0 + fx * s * sx, c.y + (0.02 + fy) * bs) + look, c, spin); match pose.eyes { Eyes::Open => { - painter.add(Shape::convex_polygon(ellipse_points(center, s * 0.11 * sx, s * 0.22 * open * sy, -0.35 + spin, 20), eye, Stroke::NONE)); + painter.add(Shape::convex_polygon(ellipse_points(center, s * 0.11 * k * sx, s * 0.22 * k * open * sy, -0.35 + spin, 20), eye, Stroke::NONE)); } // An arc, open downward (a smiling eye). Eyes::Smile => { - let (w, h) = (s * 0.17 * sx, s * 0.17 * sy); + let (w, h) = (s * 0.17 * k * sx, s * 0.17 * k * sy); let pts: Vec = (0..=12).map(|k| { let a = PI + k as f32 / 12.0 * PI; spin_about(pos2(center.x + w * a.cos(), center.y + h * 0.5 + h * a.sin()), c, spin) }).collect(); painter.add(Shape::line(pts, Stroke::new((s * 0.1).max(1.5), eye))); } Eyes::Flat => { - let w = s * 0.17 * sx; + let w = s * 0.17 * k * sx; let (a, b) = (spin_about(pos2(center.x - w, center.y), c, spin), spin_about(pos2(center.x + w, center.y), c, spin)); painter.add(Shape::line(vec![a, b], Stroke::new((s * 0.1).max(1.5), eye))); } @@ -222,8 +256,8 @@ pub fn paint(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32) { let c = rect.center(); let s = rect.width().min(rect.height()) / 2.0; let eye = eye_colour(col); - for dx in [-0.27, 0.27] { - painter.add(Shape::convex_polygon(ellipse_points(pos2(c.x + dx * s, c.y + 0.02 * s), s * 0.11, s * 0.22, -0.35, 20), eye, Stroke::NONE)); + for (p, rx, ry) in eye_rects(shape) { + painter.add(Shape::convex_polygon(ellipse_points(pos2(c.x + p.x * s, c.y + p.y * s), s * rx, s * ry, -0.35, 20), eye, Stroke::NONE)); } } @@ -281,6 +315,47 @@ fn paint_body(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32, sp } painter.add(body((0..10).filter(|i| i % 2 == 1).map(|i| pt(i, s * 0.5)).collect())) } + "moon" => { + // A circle with a wedge open to the right, split at a diameter so every + // part is convex (a crescent as one outline is not, and egui shapes are + // convex polygons). The mouth reads at 24 px; a thin crescent does not. + let sector = |a0: f32, a1: f32| { + let pts = std::iter::once(pos2(c.x, c.y)) + .chain((0..=20).map(move |i| { + let a = a0 + (a1 - a0) * i as f32 / 20.0; + pos2(c.x + 0.95 * s * a.cos(), c.y + 0.95 * s * a.sin()) + })) + .map(cc) + .collect(); + body(pts) + }; + painter.add(sector(0.6981, 4.0143)); // 40°..230° + painter.add(sector(4.0143, 5.5851)) // 230°..320° + } + "pumpkin" => { + // Rows of overlapping ellipses, a stem on top, two ribs drawn in. + painter.add(Shape::convex_polygon( + rounded_poly(Rect::from_center_size(cc(pos2(c.x + 0.02 * s, c.y - 0.8 * s)), vec2(0.14 * s, 0.36 * s)), 0.05 * s), + col.linear_multiply(0.72), + Stroke::NONE, + )); + for (dx, dy, rx, ry) in [(-0.52, 0.05, 0.46, 0.72), (0.52, 0.05, 0.46, 0.72), (0.0, 0.12, 0.55, 0.8), (0.0, 0.3, 0.5, 0.62)] { + painter.add(Shape::convex_polygon(ellipse_points(cc(pos2(c.x + dx * s, c.y + dy * s)), rx * s, ry * s, 0.0, 32), col, Stroke::NONE)); + } + let rib = Stroke::new(s * 0.045, col.linear_multiply(0.62)); + for dx in [-0.26f32, 0.26] { + let pts: Vec = (0..=10) + .map(|i| { + let t = i as f32 / 10.0; + let y = -0.5 + 1.2 * t; + pos2(c.x + s * (dx + 0.1 * dx * (1.0 - (y / 0.62).powi(2))), c.y + s * (y + 0.1)) + }) + .map(cc) + .collect(); + painter.add(Shape::line(pts, rib)); + } + painter.add(Shape::Noop) + } _ => painter.add(Shape::convex_polygon(ellipse_points(c, rect.width() / 2.0 * 0.95, rect.height() / 2.0 * 0.95, spin, 48), col, Stroke::NONE)), }; } @@ -308,4 +383,103 @@ mod tests { // Presets answer too, so "make it custom" starts from the colour you had. assert_eq!(color(&hex_of_hsv(hsv_of("blue"))), color("blue")); } + + /// Is `p` inside the closed polygon `pts` (even-odd ray test). + fn in_poly(p: Pos2, pts: &[Pos2]) -> bool { + let mut inside = false; + for i in 0..pts.len() { + let (a, b) = (pts[i], pts[(i + 1) % pts.len()]); + if (a.y > p.y) != (b.y > p.y) && p.x < (b.x - a.x) * (p.y - a.y) / (b.y - a.y) + a.x { + inside = !inside; + } + } + inside + } + + fn in_ellipse(p: Pos2, c: Pos2, rx: f32, ry: f32, rot: f32) -> bool { + let (s, co) = rot.sin_cos(); + let (dx, dy) = (p.x - c.x, p.y - c.y); + let (u, v) = (dx * co + dy * s, -dx * s + dy * co); + (u / rx).powi(2) + (v / ry).powi(2) <= 1.0 + } + + /// The body's real area in half-size units (centre at the origin), for the shapes + /// whose paint_body draws more than one convex part (cloud, heart, star, moon, pumpkin). + fn body_regions(shape: &str) -> Vec<(Pos2, f32, f32)> { + match shape { + "cloud" => [(-0.45f32, 0.15f32, 0.55f32), (0.45, 0.15, 0.55), (0.0, -0.2, 0.65), (0.0, 0.35, 0.55)] + .into_iter() + .map(|(x, y, r)| (pos2(x, y), r, r)) + .collect(), + "heart" => vec![(pos2(-0.45, -0.25), 0.55, 0.55), (pos2(0.45, -0.25), 0.55, 0.55)], + _ => vec![], + } + } + + fn star_pt(i: usize, r: f32) -> Pos2 { + let a = -PI / 2.0 + i as f32 * PI / 5.0; + pos2(r * a.cos(), 0.08 + r * a.sin()) + } + + /// The moon's body: a 0.95 disc with its wedge (mouth, ±40° around the +x axis) open. + fn in_moon(p: Pos2) -> bool { + let a = p.y.atan2(p.x); + p.to_vec2().length() <= 0.95 && (a - 0.6981).rem_euclid(2.0 * PI) <= 4.887 + } + + /// Is a point on the body (not the background)? + fn on_body(shape: &str, p: Pos2) -> bool { + if body_regions(shape).iter().any(|&(c, rx, ry)| in_ellipse(p, c, rx, ry, 0.0)) { + return true; + } + match shape { + "squircle" | "capsule" => { + let (hw, hh, r) = match shape { + "squircle" => (0.92, 0.92, 0.42), + _ => (0.675, 1.0, 0.66), + }; + let inner = Rect::from_center_size(Pos2::ZERO, vec2(2.0 * (hw - r), 2.0 * (hh - r))); + let cx = p.x.clamp(inner.min.x, inner.max.x); + let cy = p.y.clamp(inner.min.y, inner.max.y); + (p.x - cx).powi(2) + (p.y - cy).powi(2) <= r * r + } + "egg" => in_ellipse(p, pos2(0.0, 0.08), 0.8, if p.y < 0.08 { 1.0 } else { 0.86 }, 0.0), + "hex" => in_poly(p, &(0..6).map(|i| { let a = i as f32 / 6.0 * 2.0 * PI + PI / 6.0; pos2(a.cos(), a.sin()) }).collect::>()), + "gem" => in_poly(p, &[pos2(-0.55, -0.8), pos2(0.55, -0.8), pos2(1.0, -0.2), pos2(0.0, 1.0), pos2(-1.0, -0.2)]), + "arch" => { + if in_ellipse(p, pos2(0.0, -0.15), 0.85, 0.85, 0.0) && p.y <= -0.15 { + return true; + } + in_poly(p, &[pos2(-0.85, -0.15), pos2(0.85, -0.15), pos2(0.85, 1.0), pos2(-0.85, 1.0)]) + } + "shield" => in_poly(p, &[pos2(-0.85, -0.9), pos2(0.85, -0.9), pos2(0.85, 0.05), pos2(0.5, 0.65), pos2(0.0, 1.0), pos2(-0.5, 0.65), pos2(-0.85, 0.05)]), + "pebble" => in_ellipse(p, Pos2::ZERO, 1.0, 0.78, -0.25), + "star" => { + let core: Vec = (0..10).filter(|i| i % 2 == 1).map(|i| star_pt(i, 0.5)).collect(); + in_poly(p, &core) || (0..5).any(|i| in_poly(p, &[star_pt(2 * i + 9, 0.48), star_pt(2 * i, 1.05), star_pt(2 * i + 1, 0.48)])) + } + "moon" => in_moon(p), + "pumpkin" => [(-0.52f32, 0.05f32, 0.46f32, 0.72f32), (0.52, 0.05, 0.46, 0.72), (0.0, 0.12, 0.55, 0.8), (0.0, 0.3, 0.5, 0.62)] + .into_iter() + .any(|(x, y, rx, ry)| in_ellipse(p, pos2(x, y), rx, ry, 0.0)), + // The default is the blob's plain ellipse, sized from the rect (square here). + _ => in_ellipse(p, Pos2::ZERO, 0.95, 0.95, 0.0), + } + } + + /// The eyes are placed from the outline, so a new concave shape without its + /// `face_fit` entry puts eyes on the background — the star did, quietly. + #[test] + fn eyes_stay_inside_every_shape() { + for shape in SHAPES { + for (c, rx, ry) in eye_rects(shape) { + // The eye is a rotated ellipse; its four corners bound it. + let (co, si) = 0.35f32.sin_cos(); + for (ux, uy) in [(1.0, 1.0), (1.0, -1.0), (-1.0, 1.0), (-1.0, -1.0)] { + let p = pos2(c.x + (rx * ux * co - ry * uy * si), c.y + (rx * ux * si + ry * uy * co)); + assert!(on_body(shape, p), "{shape}: eye corner ({:.3}, {:.3}) is off the body", p.x, p.y); + } + } + } + } }