use std::sync::Arc; use anyhow::Result; use rfactor_sm_reader::{rF2VehicleTelemetry, VehicleScoringInfoV01}; use ui::prelude::*; use crate::overlay::{ rfactor_data::{DataReceiver, GamePhase}, UiOverlay, }; pub struct Pedals { gui: Arc, brake: Arc, throttle: Arc, history: Arc, throttle_samples: Vec, brake_samples: Vec, } impl Pedals { pub fn new(gui_handler: &Arc) -> Result { const DESC: &str = include_str!("pedals.xml"); let gui = GuiBuilder::from_str(gui_handler, DESC)?; let brake = gui.element("brake")?; let throttle = gui.element("throttle")?; let history = gui.element("history")?; Ok(Self { gui, brake, throttle, history, throttle_samples: Vec::new(), brake_samples: Vec::new(), }) } } impl UiOverlay for Pedals {} impl DataReceiver for Pedals { fn scoring_update( &mut self, _phase: GamePhase, _vehicle_scoring: &[VehicleScoringInfoV01], ) -> Result<()> { Ok(()) } fn telemetry_update( &mut self, player_id: Option, telemetries: &[rF2VehicleTelemetry], ) -> Result<()> { match player_id { Some(id) => { self.gui.enable()?; if let Some(telemetry) = telemetries.iter().find(|telemetry| telemetry.id == id) { let brake = 1.0 - telemetry.unfiltered_brake as f32; let throttle = 1.0 - telemetry.unfiltered_throttle as f32; self.throttle.set_progress(throttle)?; self.brake.set_progress(brake)?; self.throttle_samples.push(throttle); self.brake_samples.push(brake); } } None => { self.gui.disable()?; } } Ok(()) } }