use crate::optimization::jitter::JitterReducer;
use crate::optimization::pacing::AdaptiveFramePacer;
use crate::optimization::polling::HighFrequencyInputPoller;
use crate::optimization::prediction::InputPredictor;
pub struct UniversalLatencyOptimizer {
pub poller: HighFrequencyInputPoller,
pub pacer: AdaptiveFramePacer,
pub predictor: InputPredictor,
pub jitter_reducer: JitterReducer,
pub prediction_enabled: bool,
pub jitter_reduction_enabled: bool,
}
impl UniversalLatencyOptimizer {
pub fn new(target_hz: u32, target_fps: u32) -> Self {
Self {
poller: HighFrequencyInputPoller::new(target_hz),
pacer: AdaptiveFramePacer::new(target_fps),
predictor: InputPredictor::new(),
jitter_reducer: JitterReducer::new(0.5), prediction_enabled: false,
jitter_reduction_enabled: false,
}
}
pub fn on_raw_input(&mut self, timestamp: f64, mouse_x: f32, mouse_y: f32) {
self.predictor.add_sample(mouse_x, mouse_y, timestamp);
}
pub fn get_optimized_mouse_pos(
&mut self,
current_time: f64,
raw_x: f32,
raw_y: f32,
) -> (f32, f32) {
let mut x = raw_x;
let mut y = raw_y;
if self.prediction_enabled {
let (px, py) = self.predictor.predict_at(current_time);
x = px;
y = py;
}
if self.jitter_reduction_enabled {
let (jx, jy) = self.jitter_reducer.process(x, y);
x = jx;
y = jy;
}
(x, y)
}
}