use eframe::egui::{self, Color32, CornerRadius, FontId, Pos2, Stroke, StrokeKind, Vec2};
use embedded_gui::motion::timing::{EasingCurve, evaluate_easing};
pub fn render_curve_graph(ui: &mut egui::Ui, curve: EasingCurve, norm_t: f32, size: Vec2) {
let (response, painter) = ui.allocate_painter(size, egui::Sense::hover());
let rect = response.rect;
painter.rect_filled(rect, CornerRadius::same(4), Color32::from_rgb(18, 20, 26));
painter.rect_stroke(
rect,
CornerRadius::same(4),
Stroke::new(1.0f32, Color32::from_rgb(45, 52, 65)),
StrokeKind::Inside,
);
let graph_rect = rect.shrink2(Vec2::new(12.0, 10.0));
painter.line_segment(
[
Pos2::new(graph_rect.min.x, graph_rect.max.y),
Pos2::new(graph_rect.max.x, graph_rect.min.y),
],
Stroke::new(1.0f32, Color32::from_rgba_unmultiplied(80, 90, 110, 80)),
);
let steps = 60;
let mut pts = Vec::with_capacity(steps);
for i in 0..=steps {
let x_norm = i as f32 / steps as f32;
let y_val = evaluate_easing(curve, x_norm);
let px = graph_rect.min.x + x_norm * graph_rect.width();
let py = graph_rect.max.y - y_val * graph_rect.height();
pts.push(Pos2::new(px, py));
}
for w in pts.windows(2) {
painter.line_segment(
[w[0], w[1]],
Stroke::new(2.0f32, Color32::from_rgb(60, 180, 255)),
);
}
let cur_y_val = evaluate_easing(curve, norm_t.clamp(0.0, 1.0));
let bead_x = graph_rect.min.x + norm_t.clamp(0.0, 1.0) * graph_rect.width();
let bead_y = graph_rect.max.y - cur_y_val * graph_rect.height();
let bead_pos = Pos2::new(bead_x, bead_y);
painter.line_segment(
[
Pos2::new(bead_x, graph_rect.min.y),
Pos2::new(bead_x, graph_rect.max.y),
],
Stroke::new(1.0f32, Color32::from_rgba_unmultiplied(255, 255, 255, 60)),
);
painter.circle_filled(bead_pos, 5.5, Color32::from_rgb(255, 200, 60));
painter.circle_stroke(bead_pos, 5.5, Stroke::new(1.5f32, Color32::WHITE));
painter.text(
Pos2::new(graph_rect.min.x + 4.0, graph_rect.min.y + 2.0),
egui::Align2::LEFT_TOP,
format!("{:?} ({:.0}%)", curve, cur_y_val * 100.0),
FontId::proportional(9.5),
Color32::from_rgb(170, 200, 240),
);
}