use dais_core::state::InkStroke;
use egui::{Color32, Rect, Stroke, Ui, epaint::PathShape};
pub fn draw_ink_strokes(ui: &mut Ui, image_rect: Rect, strokes: &[InkStroke]) {
let painter = ui.painter_at(image_rect);
for stroke in strokes {
if stroke.points.len() < 2 {
if let Some(&(x, y)) = stroke.points.first() {
let pos = denormalize(image_rect, x, y);
let color = rgba_to_color32(stroke.color);
painter.circle_filled(pos, stroke.width * 0.5, color);
}
continue;
}
let color = rgba_to_color32(stroke.color);
let egui_stroke = Stroke::new(stroke.width, color);
let radius = stroke.width * 0.5;
let points: Vec<egui::Pos2> =
stroke.points.iter().map(|&(x, y)| denormalize(image_rect, x, y)).collect();
painter.add(PathShape::line(points.clone(), egui_stroke));
if let Some(&first) = points.first() {
painter.circle_filled(first, radius, color);
}
if let Some(&last) = points.last() {
painter.circle_filled(last, radius, color);
}
}
}
fn denormalize(rect: Rect, nx: f32, ny: f32) -> egui::Pos2 {
egui::pos2(rect.min.x + nx * rect.width(), rect.min.y + ny * rect.height())
}
fn rgba_to_color32(rgba: [u8; 4]) -> Color32 {
Color32::from_rgba_unmultiplied(rgba[0], rgba[1], rgba[2], rgba[3])
}