202 lines
8.6 KiB
Rust
202 lines
8.6 KiB
Rust
|
|
// Bot avatars: a simple shape in the bot's colour with two slanted oval eyes,
|
||
|
|
// drawn in the style of the original's bot faces (not copied from its pictures).
|
||
|
|
|
||
|
|
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 fn shape_label(s: &str) -> &'static str {
|
||
|
|
match s {
|
||
|
|
"squircle" => "圓角方",
|
||
|
|
"blob" => "圓球",
|
||
|
|
"capsule" => "膠囊",
|
||
|
|
"egg" => "蛋",
|
||
|
|
"hex" => "六角",
|
||
|
|
"gem" => "寶石",
|
||
|
|
"cloud" => "雲",
|
||
|
|
"arch" => "拱門",
|
||
|
|
"star" => "星星",
|
||
|
|
"heart" => "愛心",
|
||
|
|
"shield" => "盾牌",
|
||
|
|
"pebble" => "石頭",
|
||
|
|
_ => "圓球",
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// 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]),
|
||
|
|
("red", "紅", [0xc8, 0x25, 0x32]),
|
||
|
|
("orange", "橘", [0xd2, 0x55, 0x0a]),
|
||
|
|
("yellow", "黃", [0xc2, 0x73, 0x0b]),
|
||
|
|
("green", "綠", [0x0d, 0x96, 0x55]),
|
||
|
|
("cyan", "青", [0x0d, 0x93, 0x81]),
|
||
|
|
("blue", "藍", [0x12, 0x6f, 0xd4]),
|
||
|
|
("purple", "紫", [0x74, 0x42, 0xd1]),
|
||
|
|
("magenta", "洋紅", [0xc8, 0x27, 0x79]),
|
||
|
|
("brown", "棕", [0x86, 0x59, 0x2e]),
|
||
|
|
];
|
||
|
|
|
||
|
|
pub fn color(name: &str) -> Color32 {
|
||
|
|
let c = COLORS.iter().find(|c| c.0 == name).map(|c| c.2).unwrap_or(COLORS[0].2);
|
||
|
|
Color32::from_rgb(c[0], c[1], c[2])
|
||
|
|
}
|
||
|
|
|
||
|
|
fn ellipse_points(c: Pos2, rx: f32, ry: f32, rot: f32, n: usize) -> Vec<Pos2> {
|
||
|
|
let (s, co) = rot.sin_cos();
|
||
|
|
(0..n)
|
||
|
|
.map(|i| {
|
||
|
|
let t = i as f32 / n as f32 * 2.0 * PI;
|
||
|
|
let (x, y) = (rx * t.cos(), ry * t.sin());
|
||
|
|
pos2(c.x + x * co - y * s, c.y + x * s + y * co)
|
||
|
|
})
|
||
|
|
.collect()
|
||
|
|
}
|
||
|
|
|
||
|
|
fn rounded_poly(rect: Rect, r: f32) -> Vec<Pos2> {
|
||
|
|
let mut pts = vec![];
|
||
|
|
let corners = [
|
||
|
|
(rect.right_bottom() - vec2(r, r), 0.0),
|
||
|
|
(rect.left_bottom() + vec2(r, -r), 0.5 * PI),
|
||
|
|
(rect.left_top() + vec2(r, r), PI),
|
||
|
|
(rect.right_top() + vec2(-r, r), 1.5 * PI),
|
||
|
|
];
|
||
|
|
for (c, a0) in corners {
|
||
|
|
for k in 0..=6 {
|
||
|
|
let a = a0 + k as f32 / 6.0 * 0.5 * PI;
|
||
|
|
pts.push(pos2(c.x + r * a.cos(), c.y + r * a.sin()));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
pts
|
||
|
|
}
|
||
|
|
|
||
|
|
/// How a face is held at this moment (the original's mark: gaze, blink, squash, lift, spin).
|
||
|
|
#[derive(Clone, Copy, Debug)]
|
||
|
|
pub struct Pose {
|
||
|
|
/// Where it looks, -1…1 on each axis (y up).
|
||
|
|
pub gaze: egui::Vec2,
|
||
|
|
/// 0 = open, 1 = shut.
|
||
|
|
pub blink: f32,
|
||
|
|
/// Horizontal / vertical scale (a bounce squashes and stretches).
|
||
|
|
pub squash: egui::Vec2,
|
||
|
|
/// Upward hop, as a share of the size.
|
||
|
|
pub lift: f32,
|
||
|
|
/// Turn around the vertical axis in degrees (a spin).
|
||
|
|
pub yaw: f32,
|
||
|
|
/// Nod: eyes drop a little.
|
||
|
|
pub pitch: f32,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Default for Pose {
|
||
|
|
fn default() -> Self {
|
||
|
|
Pose { gaze: egui::Vec2::ZERO, blink: 0.0, squash: vec2(1.0, 1.0), lift: 0.0, yaw: 0.0, pitch: 0.0 }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// A face with a pose: the body leans toward the gaze (the original turns it up to 26° sideways
|
||
|
|
/// and 19° up/down; drawn flat here as a shift), the eyes move further, blink and spin.
|
||
|
|
pub fn paint_posed(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32, pose: Pose) {
|
||
|
|
// Not edge to edge: the face sits inside its cell with a little room around it.
|
||
|
|
let rect = Rect::from_center_size(rect.center(), rect.size() * 0.84);
|
||
|
|
let s = rect.width().min(rect.height()) / 2.0;
|
||
|
|
let turn = pose.yaw.to_radians().cos();
|
||
|
|
let sx = pose.squash.x * turn.abs().max(0.06);
|
||
|
|
let sy = pose.squash.y;
|
||
|
|
let shift = vec2(pose.gaze.x * 0.05 * s, -pose.gaze.y * 0.04 * s - pose.lift * s * 0.55);
|
||
|
|
let body = Rect::from_center_size(rect.center() + shift, vec2(rect.width() * sx, rect.height() * sy));
|
||
|
|
// Squash from the feet: keep the bottom where it was when it flattens.
|
||
|
|
let body = body.translate(vec2(0.0, (1.0 - sy) * rect.height() / 2.0));
|
||
|
|
paint_body(painter, body, shape, col);
|
||
|
|
if turn <= 0.0 {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
let c = body.center();
|
||
|
|
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 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 = pos2(c.x + dx * body.width() / 2.0, c.y + 0.02 * bs) + look;
|
||
|
|
painter.add(Shape::convex_polygon(ellipse_points(center, s * 0.11 * sx, s * 0.22 * open * sy, -0.35, 20), eye, Stroke::NONE));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn eye_colour(col: Color32) -> Color32 {
|
||
|
|
if col.r() as u32 + col.g() as u32 + col.b() as u32 > 600 { Color32::from_rgb(0x1c, 0x1c, 0x1c) } else { Color32::from_rgb(0xfc, 0xfc, 0xfc) }
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn paint(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32) {
|
||
|
|
paint_body(painter, rect, shape, col);
|
||
|
|
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));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn paint_body(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32) {
|
||
|
|
let c = rect.center();
|
||
|
|
let s = rect.width().min(rect.height()) / 2.0;
|
||
|
|
let body = |pts: Vec<Pos2>| Shape::convex_polygon(pts, col, Stroke::NONE);
|
||
|
|
match shape {
|
||
|
|
"squircle" => painter.add(body(rounded_poly(Rect::from_center_size(c, vec2(2.0 * s, 2.0 * s) * 0.92), s * 0.42))),
|
||
|
|
"capsule" => painter.add(body(rounded_poly(Rect::from_center_size(c, vec2(1.35 * s, 2.0 * s)), s * 0.66))),
|
||
|
|
"egg" => painter.add(body(
|
||
|
|
(0..48)
|
||
|
|
.map(|i| {
|
||
|
|
let t = i as f32 / 48.0 * 2.0 * PI;
|
||
|
|
let ry = if t.sin() < 0.0 { s * 1.0 } else { s * 0.86 };
|
||
|
|
pos2(c.x + s * 0.8 * t.cos(), c.y + 0.08 * s + ry * t.sin())
|
||
|
|
})
|
||
|
|
.collect(),
|
||
|
|
)),
|
||
|
|
"hex" => painter.add(body((0..6).map(|i| { let a = i as f32 / 6.0 * 2.0 * PI + PI / 6.0; pos2(c.x + s * a.cos(), c.y + s * a.sin()) }).collect())),
|
||
|
|
"gem" => painter.add(body(vec![pos2(c.x - s * 0.55, c.y - s * 0.8), pos2(c.x + s * 0.55, c.y - s * 0.8), pos2(c.x + s, c.y - s * 0.2), pos2(c.x, c.y + s), pos2(c.x - s, c.y - s * 0.2)])),
|
||
|
|
"arch" => painter.add(body(
|
||
|
|
(0..=24)
|
||
|
|
.map(|i| { let a = PI + i as f32 / 24.0 * PI; pos2(c.x + s * 0.85 * a.cos(), c.y - s * 0.15 + s * 0.85 * a.sin()) })
|
||
|
|
.chain([pos2(c.x + s * 0.85, c.y + s), pos2(c.x - s * 0.85, c.y + s)])
|
||
|
|
.collect(),
|
||
|
|
)),
|
||
|
|
"shield" => painter.add(body(vec![
|
||
|
|
pos2(c.x - s * 0.85, c.y - s * 0.9),
|
||
|
|
pos2(c.x + s * 0.85, c.y - s * 0.9),
|
||
|
|
pos2(c.x + s * 0.85, c.y + s * 0.05),
|
||
|
|
pos2(c.x + s * 0.5, c.y + s * 0.65),
|
||
|
|
pos2(c.x, c.y + s),
|
||
|
|
pos2(c.x - s * 0.5, c.y + s * 0.65),
|
||
|
|
pos2(c.x - s * 0.85, c.y + s * 0.05),
|
||
|
|
])),
|
||
|
|
"pebble" => painter.add(body(ellipse_points(c, s, s * 0.78, -0.25, 48))),
|
||
|
|
"cloud" => {
|
||
|
|
for (dx, dy, r) in [(-0.45, 0.15, 0.55), (0.45, 0.15, 0.55), (0.0, -0.2, 0.65), (0.0, 0.35, 0.55)] {
|
||
|
|
painter.circle_filled(pos2(c.x + dx * s, c.y + dy * s), r * s, col);
|
||
|
|
}
|
||
|
|
painter.add(Shape::Noop)
|
||
|
|
}
|
||
|
|
"heart" => {
|
||
|
|
painter.circle_filled(pos2(c.x - s * 0.45, c.y - s * 0.25), s * 0.55, col);
|
||
|
|
painter.circle_filled(pos2(c.x + s * 0.45, c.y - s * 0.25), s * 0.55, col);
|
||
|
|
painter.add(body(vec![pos2(c.x - s * 0.97, c.y - s * 0.1), pos2(c.x + s * 0.97, c.y - s * 0.1), pos2(c.x, c.y + s)]))
|
||
|
|
}
|
||
|
|
"star" => {
|
||
|
|
let pt = |i: usize, r: f32| { let a = -PI / 2.0 + i as f32 * PI / 5.0; pos2(c.x + r * a.cos(), c.y + 0.08 * s + r * a.sin()) };
|
||
|
|
for i in 0..5 {
|
||
|
|
painter.add(body(vec![pt(2 * i + 9, s * 0.48), pt(2 * i, s * 1.05), pt(2 * i + 1, s * 0.48)]));
|
||
|
|
}
|
||
|
|
painter.add(body((0..10).filter(|i| i % 2 == 1).map(|i| pt(i, s * 0.5)).collect()))
|
||
|
|
}
|
||
|
|
_ => painter.add(Shape::convex_polygon(ellipse_points(c, rect.width() / 2.0 * 0.95, rect.height() / 2.0 * 0.95, 0.0, 48), col, Stroke::NONE)),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn avatar(ui: &mut egui::Ui, size: f32, shape: &str, col: Color32) -> egui::Response {
|
||
|
|
let (rect, resp) = ui.allocate_exact_size(vec2(size, size), egui::Sense::click());
|
||
|
|
paint(ui.painter(), rect, shape, col);
|
||
|
|
resp
|
||
|
|
}
|