diff --git a/src/main.rs b/src/main.rs index 8d89814..9bf848c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ use crate::tts::Tts; use analysis::save_frames_from; use config::Config; use eframe::{ - egui::{self, FontTweak, Ui, Visuals}, + egui::{self, plot::{LinkedAxisGroup}, FontTweak, Ui, Visuals}, emath::Vec2, epaint::Color32, }; @@ -78,12 +78,7 @@ fn main() -> anyhow::Result<()> { ); } -fn show_optional_u8_with_diff( - ui: &mut Ui, - color: Color32, - old: &Option, - v: &Option, -) { +fn show_optional_u8_with_diff(ui: &mut Ui, color: Color32, old: &Option, v: &Option) { let diff_string = match (old, v) { (Some(old), Some(v)) => { let diff = *v as isize - *old as isize; @@ -298,6 +293,75 @@ fn show_race_state( prev_lap = Some(lap); } }); + + let axis_group = LinkedAxisGroup::x(); + let plot = egui::plot::Plot::new("health-gas-tyres") + .width(600.) + .height(200.) + .link_axis(axis_group.clone()); + fn push_all_values>(values: &mut Vec, data: &[T], lap_number: usize) { + for (i, d) in data.iter().enumerate() { + let x = (lap_number as f64) + (i as f64 / data.len() as f64); + values.push(egui::plot::Value { x, y: (*d).into() }) + } + } + plot.show(ui, |plot| { + let mut health_values = Vec::new(); + let mut gas_values = Vec::new(); + let mut tyre_values = Vec::new(); + for (i, lap) in race.laps.iter().enumerate() { + push_all_values(&mut health_values, &lap.metrics.health, i); + push_all_values(&mut gas_values, &lap.metrics.gas, i); + push_all_values(&mut tyre_values, &lap.metrics.tyres, i); + } + plot.line( + egui::plot::Line::new(egui::plot::Values::from_values(health_values)) + .name("health") + .color(Color32::RED), + ); + plot.line( + egui::plot::Line::new(egui::plot::Values::from_values(gas_values)) + .name("gas") + .color(Color32::LIGHT_BLUE), + ); + plot.line( + egui::plot::Line::new(egui::plot::Values::from_values(tyre_values)) + .name("tyres") + .color(Color32::GREEN), + ); + }); + let plot = egui::plot::Plot::new("position") + .width(600.) + .height(200.) + .link_axis(axis_group.clone()); + plot.show(ui, |plot| { + let mut pos_values = Vec::new(); + for (i, lap) in race.laps.iter().enumerate() { + push_all_values(&mut pos_values, &lap.metrics.position, i); + } + plot.line( + egui::plot::Line::new(egui::plot::Values::from_values(pos_values)) + .name("position") + .color(Color32::LIGHT_GRAY), + ); + }); + let plot = egui::plot::Plot::new("pace") + .width(600.) + .height(200.) + .link_axis(axis_group); + plot.show(ui, |plot| { + let mut lap_values = Vec::new(); + for (i, lap) in race.laps.iter().enumerate() { + if let Some(lap_time) = lap.lap_time { + lap_values.push(egui::plot::Value { x: (i + 1) as f64, y: lap_time.as_secs_f64() }) + } + } + plot.line( + egui::plot::Line::new(egui::plot::Values::from_values(lap_values)) + .name("pace") + .color(Color32::LIGHT_GREEN), + ); + }); } fn show_debug_frames(