use eframe::egui;
use serde_json::Value;
const ACCENT: egui::Color32 = egui::Color32::from_rgb(255, 176, 0);
const BAND: egui::Color32 = egui::Color32::from_rgb(12, 14, 18);
const CAPTION_INK: egui::Color32 = egui::Color32::from_rgb(238, 240, 244);
const TITLE_INK: egui::Color32 = ACCENT;
const PAD: f32 = 14.0;
const TITLE_SIZE: f32 = 15.0;
const CAPTION_SIZE: f32 = 21.0;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Annotation {
pub title: Option<String>,
pub caption: Option<String>,
pub highlight: Vec<String>,
pub points: Vec<[f32; 2]>,
pub step: Option<(u32, u32)>,
}
impl Annotation {
pub fn is_empty(&self) -> bool {
self.title.is_none()
&& self.caption.is_none()
&& self.highlight.is_empty()
&& self.points.is_empty()
&& self.step.is_none()
}
pub fn parse(spec: &Value) -> Result<Self, String> {
let Some(obj) = spec.as_object() else {
return Err(format!("annotate: expected an object, got {spec}"));
};
let mut out = Annotation::default();
for (key, value) in obj {
match key.as_str() {
"title" | "caption" => {
let text = match value {
Value::Null => None,
Value::String(s) if s.is_empty() => None,
Value::String(s) => Some(s.clone()),
other => return Err(format!("annotate: `{key}` must be a string, got {other}")),
};
if key == "title" {
out.title = text;
} else {
out.caption = text;
}
}
"highlight" => match value {
Value::Null => {}
Value::String(s) => out.highlight.push(s.clone()),
Value::Array(a) => {
for item in a {
let s = item.as_str().ok_or_else(|| format!("annotate: `highlight` entries must be strings, got {item}"))?;
out.highlight.push(s.to_string());
}
}
other => return Err(format!("annotate: `highlight` must be a string or an array of strings, got {other}")),
},
"point" => match value {
Value::Null => {}
Value::Array(a) if a.len() == 2 && a.iter().all(|v| v.is_number()) => {
out.points.push([a[0].as_f64().unwrap_or(0.0) as f32, a[1].as_f64().unwrap_or(0.0) as f32]);
}
Value::Array(a) => {
for item in a {
let pair = item
.as_array()
.filter(|p| p.len() == 2 && p.iter().all(|v| v.is_number()))
.ok_or_else(|| format!("annotate: `point` entries must be `[x, y]`, got {item}"))?;
out.points.push([pair[0].as_f64().unwrap_or(0.0) as f32, pair[1].as_f64().unwrap_or(0.0) as f32]);
}
}
other => return Err(format!("annotate: `point` must be `[x, y]` or an array of them, got {other}")),
},
"step" => match value {
Value::Null => {}
Value::Array(a) if a.len() == 2 => {
let n = a[0].as_u64().ok_or("annotate: `step` entries must be whole numbers")?;
let total = a[1].as_u64().ok_or("annotate: `step` entries must be whole numbers")?;
out.step = Some((n as u32, total as u32));
}
other => return Err(format!("annotate: `step` must be `[n, total]`, got {other}")),
},
other => return Err(format!("annotate: unknown field `{other}` (title | caption | highlight | point | step)")),
}
}
Ok(out)
}
}
pub fn published_rects() -> std::collections::BTreeMap<String, [f32; 4]> {
brep_app::automation::registry::lock().hit_rects(None)
}
pub fn unknown_keys(wanted: &[String]) -> Vec<String> {
let published = published_rects();
wanted
.iter()
.filter(|k| !published.contains_key(k.as_str()))
.map(|k| {
let prefix = k.split('/').next().unwrap_or("");
let near: Vec<&str> = published
.keys()
.filter(|p| p.starts_with(prefix))
.map(String::as_str)
.take(6)
.collect();
if near.is_empty() {
format!("`{k}` is not a published widget")
} else {
format!("`{k}` is not a published widget (nearby: {})", near.join(", "))
}
})
.collect()
}
fn ring_point(painter: &egui::Painter, centre: egui::Pos2) {
painter.circle_stroke(centre, 20.0, egui::Stroke::new(2.0, egui::Color32::from_black_alpha(150)));
painter.circle_stroke(centre, 18.0, egui::Stroke::new(3.0, ACCENT));
}
pub fn paint(ctx: &egui::Context, note: &Annotation) {
if note.is_empty() {
return;
}
let screen = ctx.content_rect();
let painter = ctx.layer_painter(egui::LayerId::new(egui::Order::Debug, egui::Id::new("brep-mcp-annotation")));
if !note.highlight.is_empty() {
let rects = published_rects();
for key in ¬e.highlight {
let Some(r) = rects.get(key.as_str()) else { continue };
if r[2] < 1.0 && r[3] < 1.0 {
ring_point(&painter, egui::pos2(r[0], r[1]));
continue;
}
let rect = egui::Rect::from_min_size(egui::pos2(r[0], r[1]), egui::vec2(r[2], r[3])).expand(3.0);
painter.rect_stroke(
rect.expand(2.0),
6.0,
egui::Stroke::new(2.0, egui::Color32::from_black_alpha(150)),
egui::StrokeKind::Inside,
);
painter.rect_stroke(rect, 5.0, egui::Stroke::new(3.0, ACCENT), egui::StrokeKind::Outside);
}
}
for p in ¬e.points {
ring_point(&painter, egui::pos2(p[0], p[1]));
}
if note.title.is_none() && note.caption.is_none() && note.step.is_none() {
return;
}
let counter = note.step.map(|(n, total)| format!("{n} / {total}"));
let counter_galley = counter
.as_ref()
.map(|c| painter.layout_no_wrap(c.clone(), egui::FontId::proportional(TITLE_SIZE), ACCENT));
let counter_w = counter_galley.as_ref().map(|g| g.size().x + PAD).unwrap_or(0.0);
let text_w = (screen.width() - 2.0 * PAD - counter_w).max(80.0);
let title_galley = note
.title
.as_ref()
.map(|t| painter.layout(t.clone(), egui::FontId::proportional(TITLE_SIZE), TITLE_INK, text_w));
let caption_galley = note
.caption
.as_ref()
.map(|c| painter.layout(c.clone(), egui::FontId::proportional(CAPTION_SIZE), CAPTION_INK, text_w));
let title_h = title_galley.as_ref().map(|g| g.size().y + 4.0).unwrap_or(0.0);
let caption_h = caption_galley.as_ref().map(|g| g.size().y).unwrap_or(0.0);
let band_h = (PAD + title_h + caption_h + PAD).max(48.0);
let band = egui::Rect::from_min_max(
egui::pos2(screen.min.x, screen.max.y - band_h),
egui::pos2(screen.max.x, screen.max.y),
);
painter.rect_filled(band, 0.0, BAND);
painter.line_segment(
[band.left_top(), band.right_top()],
egui::Stroke::new(2.0, ACCENT),
);
let mut y = band.min.y + PAD;
if let Some(g) = title_galley {
let h = g.size().y;
painter.galley(egui::pos2(band.min.x + PAD, y), g, TITLE_INK);
y += h + 4.0;
}
if let Some(g) = caption_galley {
painter.galley(egui::pos2(band.min.x + PAD, y), g, CAPTION_INK);
}
if let Some(g) = counter_galley {
let size = g.size();
painter.galley(
egui::pos2(band.max.x - PAD - size.x, band.min.y + PAD),
g,
ACCENT,
);
}
}
pub struct Annotated {
app: brep_app::app::BrepApp,
pub note: Annotation,
}
impl Annotated {
pub fn new(app: brep_app::app::BrepApp) -> Self {
Self { app, note: Annotation::default() }
}
pub fn app(&self) -> &brep_app::app::BrepApp {
&self.app
}
}
impl eframe::App for Annotated {
fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) {
eframe::App::raw_input_hook(&mut self.app, ctx, raw_input);
}
fn logic(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
eframe::App::logic(&mut self.app, ctx, frame);
}
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
eframe::App::ui(&mut self.app, ui, frame);
paint(ui.ctx(), &self.note);
}
}